Replace XML path with a string in groovy file - java

I'm trying to replace a xml rootnode with a string, but it doesn't allow me.
I was trying to give it as
String str = "SOAP-ENV:Body'.'ns1:creditCardResponse";
I should not repeat SOAP-ENV:Body'.'ns1:creditCardResponse in all these lines.
def rootnode = new XmlParser().parseText(responseXml);
status = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text();
errorCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorcode.text();
errorInfo = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorinfo.text();
referenceCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.ReferenceCode.text();
requestIp = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.RequestIP.text()
Any ideas would be greatly appreciated.
Thanks.

Remember that these "paths" are just a series of normal groovy property accesses, so you can store any intermediate point in the path as a variable and continue navigating from there:
deg rtn = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return
status = rtn.Status.text()
errorCode = rtn.Errorcode.text()
// etc.

Related

Solr Nextcursor mark returning error Unable to parse 'cursorMark' after totem

On my code I am trying to search for page and getting result I have included cursor mark as * when I get the return next cursor mark as "AoE/SkhJVkVfRFNDNjAwNDlfVklTVEFfVklTVEFQX1RaX0RCOi8vSGl2ZSBNZXRhc3RvcmUvZHNjNjAwNDlfdmlzdGFfdmlzdGFwX3R6X2RiL2Fucl9sb2dvbl9sb2cvbG9nX3RpbWVzdGFtcA== where it's throwing error:
Unable to parse 'cursorMark' after totem: value must either be '*' or the 'nextCursorMark' returned by a previous search
I need to encode the nextCursorMark here is my code below:
do {
String response = RestClient.doGet(url, userName, password, offSetTemp, pageSize, cursorMark);
System.out.println("res:----"+response);
jsonObject = ProfileDataExportUtil.getJson(response);
JsonArray profilingInfo = null;
if (jsonObject.has("items")) {
profilingInfo = jsonObject.get("items").getAsJsonArray();
}
if (profilingInfo == null) continue;
moreResultsExists = profilingInfo.size() >= 1;
ProfileDataExporter.writeProfileInformation(bWriter, profilingInfo);
if (!jsonObject.has("nextCursorMark")) continue;
cursorMark = jsonObject.get("nextCursorMark").toString();
} while (moreResultsExists);
I have tried using tostring which throws an error.
My URL is encoded and when I use
cursorMark = jsonObject.get("nextCursorMark").getAsString();
I get "string out of bound exception" error.
Can anyone tell me how encode the nextCursorMark?
I have faced similar issue. It was due to nextCursor String encoding. You should encode this string also before appending in the final URL. A sample example is given below
from urllib.parse import quote_plus
...
cursorMark = quote_plus(response['nextCursorMark'])
...

Parsing Properties file

I am trying to parse a Properties file that has the following format:
CarModel=Prius
CarMake=Toyota
Option1=Transmission
OptionValue1a=Manual
OptionValue1b=Automatic
Option2=Brakes
OptionValue2a=Regular
OptionValue2b=ABS
My question is, what if there are various forms of the Properties file? For instance, what if a Properties file has 3 options for Option 1, and another Properties file has 2 options for Option 1? Right now my code looks like this:
Properties props = new Properties();
FileInputStream x = new FileInputStream(filename);
props.load(x);
String carModel = props.getProperty("CarModel");
if(!carModel.equals(null)){
String carMake = props.getProperty("CarMake");
String option1 = props.getProperty("Option1");
String option1a = props.getProperty("OptionValue1a");
String option1b = props.getProperty("OptionValue1b");
etc. I'm thinking I need a lot of 'if' statements, but I'm unsure how to implement them. Any ideas?
Are you sure you want to use a properties file? I suggest using YAML.
I am trying to parse a Properties file that has the following format:
CarModel: Prius
CarMake: Toyota
Transmission:
- Manual
- Automatic
Brakes:
- Regular
- ABS
Using SnakeYAML you can do
Map<String, Object> car = (Map) new Yaml().load(new FileReader(filename));
Note the lines starting with - are turned into a list.
If you must stick with Properties, I suggest putting the list in a property.
CarModel=Prius
CarMake=Toyota
Options=Transmission Manual|Automatic,\
Brakes Regular|ABS
This way you can read the options like
String options = prop.getProperty("Options");
for(String option : options.split("\\s*,\\s*")) {
String[] parts = option.split("\\s+");
String optionType = parts[0];
String[] optionChoices = parts[1].split("[|]");
}
This way you can have any number of options with any number of choices.

What is the error in the following HL7 encoding?

I am trying to encode an HL7 message of the type ORU_R01 using the HAPI 2.0 library for an OpenMRS module. I have followed the tutorials given in the HAPI documentation and according to that, I have populated the required fields of the ORU_R01 message. Now, I want to post this message using the following link:
http://localhost:8080/openmrs/remotecommunication/postHl7.form
I am using the following message for testing:
MSH|^~\&|||||20140713154042||ORU^R01|20140713154042|P|2.5|1
PID|||1
OBR|1||1234^SensorReading|88304
OBX|0|NM|1||45
OBX|1|NM|2||34
OBX|2|NM|3||23
I have properly ensured that all the parameters are correct. Once I have posted the HL7 message, I start the HL7 task from the scheduler. Then I go to the admin page and click on "Manage HL7 errors" in order to see if the message arrives there. I get the following stack trace:
ca.uhn.hl7v2.HL7Exception: HL7 encoding not supported
...
Caused by: ca.uhn.hl7v2.parser.EncodingNotSupportedException: Can't parse message beginning MSH|^~\
at ca.uhn.hl7v2.parser.Parser.parse(Parser.java:140)
The full stack trace is here: http://pastebin.com/ZnbFqfWC.
I have written the following code to encode the HL7 message (using the HAPI library):
public String createHL7Message(int p_id, int concept_id[], String val[])
throws HL7Exception {
ORU_R01 message = new ORU_R01();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",
Locale.ENGLISH);
MSH msh = message.getMSH();
msh.getFieldSeparator().setValue("|");
msh.getEncodingCharacters().setValue("^~\\&");
msh.getProcessingID().getProcessingID().setValue("P");
msh.getSequenceNumber().setValue("1");
msh.getMessageType().getTriggerEvent().setValue("R01");
msh.getMessageType().getMessageCode().setValue("ORU");
msh.getVersionID().getVersionID().setValue("2.5");
msh.getMessageControlID().setValue(
sdf.format(Calendar.getInstance().getTime()));
msh.getDateTimeOfMessage().getTime()
.setValue(sdf.format(Calendar.getInstance().getTime()));
ORU_R01_ORDER_OBSERVATION orderObservation = message
.getPATIENT_RESULT().getORDER_OBSERVATION();
ca.uhn.hl7v2.model.v25.segment.PID pid = message.getPATIENT_RESULT()
.getPATIENT().getPID();
Patient patient = (Patient) Context.getPatientService()
.getPatient(p_id);
System.out.println(String.valueOf(p_id) + " " + patient.getGivenName()
+ " " + patient.getFamilyName());
pid.getPatientName(0).getFamilyName().getSurname()
.setValue(patient.getFamilyName());
pid.getPatientName(0).getGivenName().setValue(patient.getGivenName());
pid.getPatientIdentifierList(0).getIDNumber()
.setValue(String.valueOf(p_id));
System.out.println();
// Parser parser = new PipeParser();
// String encodedMessage = null;
// encodedMessage = parser.encode(message);
// System.out.println(encodedMessage);
// Populate the OBR
OBR obr = orderObservation.getOBR();
obr.getSetIDOBR().setValue("1");
obr.getFillerOrderNumber().getEntityIdentifier().setValue("1234");
obr.getFillerOrderNumber().getNamespaceID().setValue("SensorReading");
obr.getUniversalServiceIdentifier().getIdentifier().setValue("88304");
Varies value = null;
// Varies value[] = new Varies[4];
for (int i = 0; i < concept_id.length; i++) {
ORU_R01_OBSERVATION observation = orderObservation
.getOBSERVATION(i);
OBX obx2 = observation.getOBX();
obx2.getSetIDOBX().setValue(String.valueOf(i));
obx2.getObservationIdentifier().getIdentifier()
.setValue(String.valueOf(concept_id[i]));
obx2.getValueType().setValue("NM");
NM nm = new NM(message);
nm.setValue(val[i]);
value = obx2.getObservationValue(0);
value.setData(nm);
}
Parser parser = new PipeParser();
String encodedMessage = null;
encodedMessage = parser.encode(message);
return encodedMessage;
}
In all likelihood, something is wrong with the MSH segment of the message, but I cannot seem to figure out what it is. What can I do to correct this error?
Why do you declare the Encoding Characters using double backslashes?
msh.getEncodingCharacters().setValue("^~\\&");
Shouldn't it be:
msh.getEncodingCharacters().setValue("^~\&");
...and because your message is using the default encoding characters maybe you don't even need to declare them at all? Extract from HAPI MSH Class reference
getENCODINGCHARACTERS
public ST getENCODINGCHARACTERS()
Returns MSH-2: "ENCODING CHARACTERS" - creates it if necessary
Update
I have no previous experience with HAPI. A quick google found an ORU example. Could you try initializing your MSH with initQuickstart("ORU", "R01", "P");
According to the comments in the example-code the initQuickstart method populates all of the mandatory fields in the MSH segment of the message, including the message type, the timestamp, and the control ID. (...and hopefully the default encoding chars as well :-)

Null display when parsing json from php to android

here is my php code
$titikPetaInti = array();
while($row = mysql_fetch_assoc($hasil2))
{
$titikPetaInti[] = $row['koordinat'];
}
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
echo $data;
?>
then here is my android code
xResultTitikPeta is result request to php
jObject = new JSONObject(xResultTitikPeta);
JSONArray myArray1 = (JSONArray) jObject.getJSONArray("titikPeta");
String[]titikPeta = new String[myArray1.length()];
for(int a = 0; a < myArray1.length(); a++)
{
titikPeta[a] = myArray1.getJSONObject(a).toString();
}
teks1 = (TextView) findViewById(R.id.textView1);
teks1.setText(Arrays.toString(titikPeta));
it displaying null at emulator like no value
--EDIT--
i think there something mistake in parsing code, cus when i display the xResultTitikPeta in android, it give me string result
here is result of xResultTitikPeta
{titikPeta:["-8.705378,115.225189","-8.56056700000000,115.42395100000","-8.57659700000000,115.40065300000","-8.55596300000000,115.41085700000","-8.51855200000000,115.491908000000","-8.54743200000000,115.41036800000","-8.56551100000000,115.45173900000","-8.44321000000000,115.616019000000"]}
this is malformed JSON! no double quotes on key.
$data = "{titikPeta:".json_encode($titikPetaInti)."}";
instead do:
$data = '{"titikPeta":'.json_encode($titikPetaInti).'}';
EDITED:
Ok, remove that hand made approach:
$data = json_encode(array("titikPeta"=>$titikPetaInti));
OK, I've found your bug! As well as fixing the $data = json_encode(array("titikPeta" => $titikPetaInti)); issue, the problem is here:
titikPeta[a] = myArray1.getJSONObject(a).toString();
The elements of myArray1 are actually of type string and cause an exception to be thrown, so you need instead:
titikPeta[a] = myArray1.getString(a);
This produces the output of:
[-8.705378,115.225189, -8.56056700000000,115.42395100000, -8.57659700000000,115.40065300000, -8.55596300000000,115.41085700000, -8.51855200000000,115.491908000000, -8.54743200000000,115.41036800000, -8.56551100000000,115.45173900000, -8.44321000000000,115.616019000000]
As each element in your array is of the form "-8.705378,115.225189", the JSON parser assumes they are strings. If you change the elements to "-8.705378","115.225189" you can also use:
titikPeta[a] = Double.toString(myArray1.getDouble(a));
However, the first version will work too.
Note: my personal preference is that I would declare each array element as:
{"x":-8.705378,"y":115.225189}
try
$data = "{\"titikPeta\":".json_encode($titikPetaInti)."}";

how to run the same step again without datasource in soapui

I don't have soap ui pro. I am testing the web service. The actual implementation is i need pass one error code on the request, and the corresponding error description should be displayed on the response. I need to add this assertion. Every time the description in the response varies.
Here is the thing i want exactly...
Every time i need to run the same request but the error code (which is input) only should be changed on each time and the description varies on the response. How to validate this? Is there any way to do this without data source.
Regards,
Chandra
This is the way i have created.. is there any way to improve the code to do better way;
import java.io.File;
File file = new File('c:/customers.csv');
InputStream inputFile = new FileInputStream(file);
String[] lines = inputFile.text.split('\n');
List<String[]> rows = lines.collect {it.split(',')}
log.info('There are ' + rows.size() + ' customers to be inserted');
for(int i = 0; i < rows.size(); i++) {
String[] row = rows.get(i);
String errorcode = row[0];
// log.info(errorcode)
String errorDescription = row[1];
//log.info(errorDescription)
testRunner.testCase.testSuite.project.setPropertyValue('errorcode', errorcode);
testRunner.testCase.testSuite.project.setPropertyValue('errorDescription', errorDescription);
testRunner.runTestStepByName("createCard-1");
log.info(errorcode +"Finsihed")
}

Categories