org.dbunit.database.AmbiguousTableNameException - java

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<employee name = "abc" empId = "100" location = "goa" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
<employee name = "XYZ" empId = "101" location = "mombai" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
<employee name = "PQA" empId = "102" location = "delhi" dept = "IT" />
<facilities transport="Y" mealcard = "" driver = "Y"/>
<reportees manager = "XXX" lead = "XXX" VP = "XXX"/>
above is my XML. I've to validate the same. I'm using DBUnit to compare the same XML with other XML (other XML has the same content but generated from different source). While creating dataset I'm getting "org.dbunit.database.AmbiguousTableNameException".
I've SQL query for every XML tag. E.g. For employee tag I'm querying on empID. for facilities I'm querying on mealCard etc etc. I'm looping through the XML tags. first iteration goes fine without error. I'm using QueryDataSet.addTable(table_name ,query) to add tables i.e xml tags. But when next iteration comes and I'm trying to add employee tag again I'm getting the above stated error.

Configure the schema name so dbUnit knows which to use. See Why am I getting an "AmbiguousTableNameException"?

I resolved this issue. I'll tell you how. In dataset for repeating tags I just added random numbers e.g. in my above XML employee is repeating tag hence I added employee_123 for the first instance in dataset for next instance I added employee_098 this is how I manage to get rid of AmbigiousTableNameException. Later I used RegEx to remove the "_[randomNumber]" appended to employee tag. The code is running successful.

#DBUnit(qualifiedTableNames = true)
this setting helped me

Related

Conditionals in String - Java Properties File

Have a java property like below
sample.properties
query={name} in {address:-}
Currently using StrSubstitutor to replace the values
valuesMap.put("name", "Har");
valuesMap.put("address", "Park Street");
String queryString= properties.get("query");
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(queryString);
resolvedString = Har in Park Street
What I need is that if the "address" isn't available, the resolved string should be as :
resolvedString = Har instead of resolvedString = Har in
Is it possible to achieve this using StrSubstitutor or by anyother means like using template engine?
Do not want any Java code dependency as the query pattern can change.
This could be a way not to loose generality:
query1={name} in {address}
query2={name}
...
String queryString;
if (valueMap.get("address") == "") {
queryString= properties.get("query2");
} else {
queryString= properties.get("query1");
}
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(queryString);
in this way even if in the future queries have to change, you'll be able to manage the difference between the two cases.
EDIT: if in the future you want to add new properties such in your comment:
{name} as {alias} in {address}
I think a way to proceed could be to put even "as" and "in" into the query:
query = {name} {as} {alias} {in} {address}
and the populate the database or the valueMap (don't know where the data come from in your application)) in the right way (so {as}="" when no "alias" and {in}="" when no address). Would that be feasible for you?

Java appendChild (CSV to XML conversion) doesn't work for ONE node

So I need to convert a CSV file to seperate XML files (one XML file per line in the CSV file). And this all works fine, except for the fact that it refuses to add one value. It adds the other without a problem, but for some mysterious reason refuses to create a tag for one of my nodes.
Document newDoc = documentBuilder.newDocument();
Element rootElement = newDoc.createElement("XMLoutput");
newDoc.appendChild(rootElement);
String header = headers.get(col);
String value = null;
String value2 = null;
if (col < rowValues.length) {
if(header.equals("delay")) {
value = rowValues[col];
Thread.sleep(Long.parseLong(value));
}
Element shipidElement = newDoc.createElement("shipID");
shipidElement.appendChild(newDoc.createTextNode(FilenameUtils.getBaseName(csvFileName)));
rootElement.appendChild(shipidElement);
if(header.equals("centraleID")) {
value = rowValues[col];
System.out.println(value); //to check if the if condition works, it does
Element centralElement = newDoc.createElement(header);
Text child = newDoc.createTextNode(value);
centralElement.appendChild(child);
rootElement.appendChild(centralElement);
}
else if(header.equals("afstandTotKade")) {
value2 = rowValues[col];
Element curElement = newDoc.createElement(header);
curElement.appendChild(newDoc.createTextNode(value2));
rootElement.appendChild(curElement);
}
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
Element timeElement = newDoc.createElement("Timestamp");
timeElement.appendChild(newDoc.createTextNode(timeStamp));
rootElement.appendChild(timeElement);
}
So in the above code, the if loop checking for CentraleID actually works, because it prints out the values, however the XML file does not add a tag, not even if I just insert a string instead of the header value. It does, however, insert the "afstandTotKade" node and timestamp node. I am dumbfounded.
PS: this is only part of the code of course, but the problem is so minute, that it seemed superfluous to add all of it.
PPS: the code was originally the same as the others, I've just been playing around.
This is the resulting XML File btw, so the other if does add the nodes, and I know the spelling is correct in checking the header because it does print out the values (the sout code) when I run it:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<XMLoutput>
<shipID>1546312</shipID>
<afstandTotKade>1000</afstandTotKade>
<Timestamp>22:06:33</Timestamp>
</XMLoutput>

How do I load up configuration params defined in a XML file?

I have a project A that I want to use from a project B, and I would like to pass some configuration parameters into the project A using a XML configuration file.
In other words, suppose I am implementing something like my own spring-data-mongo (for instance) and I want to configure my Mongo instance from a configuration file in which I have defined as follows:
<mongo: mongo host = "$ {mongo.hostname}" port = "$ {mongo.port}">
<mongo: options connections-per-host = "8"
threads-allowed-to-block-for-connection-multiplier = "4"
connect-timeout = "15000"
max-wait-time = "1500"
auto-connect-retry = "true"
socket-keep-alive = "true"
socket-timeout = "60000"
slave-ok = "true"
write-number = "1"
write-timeout = "0"
write-fsync = "false" />
How do I achieve something like this?

Converting JSON Object to table rows Dynamically

I'm now working with API calls currectly, so most the requests are done through ajax call. As you can expect, both the data sent to server and data received from server will be xml data. I've successfully able to dynamically construct the xml data during the sign up process and during post requests. By utilising JaxB, I've done the necessary steps in server.
Now I've to get the data as xml from server if a user needs to view a ceertain resource. Like before, I'm using jaxb to convert Java object into xml data and I'm getting the xml data in success function of Javascript. There are lot of examples and questions present on converting this xml data into user viewable form.
By my main goal is to make every function Dynamic, Consider now I'm going to show the list of users to admin, I can use these examples to convert my xml data into tables.
Display JSON Data in HTML Table
populate jquery data table with returned json data
If I'm doing like that, I've to manually write the same process with some modification based on table fields for every list view. This is a bad practise if I'm going to copy paste the same code with some modifications for 10 list views. I would like to make the xml to table conversion as a common function for any number of tables no matter how many fields are present in it.
I'm getting the xml data from server as String. So I've converted it to recognizable xmlData using following code.
var xmlData = jQuery.parseXML(data); //data is the xml String which I'm getting from server
//Converting xmlData into JSON Objects
var containerTag = myTag //I can get this from - document.getElementById("tableId").name
var output = new Array( );
var rawData = xmlData.getElementsByTagName(containerTag)[0];
var i, j, oneRecord, oneObject;
for (i = 0; i < rawData.childNodes.length; i++) {
if (rawData.childNodes[i].nodeType == 1) {
oneRecord = rawData.childNodes[i];
oneObject = output[output.length] = new Object( );
for (j = 0; j < oneRecord.childNodes.length; j++) {
if (oneRecord.childNodes[j].nodeType == 1) {
oneObject[oneRecord.childNodes[j].tagName] = oneRecord.childNodes[j].firstChild.nodeValue;
}
}
}
}
By displaying the data as console.log(output[0]); , I'm getting my real data. But I searched to use this information to populate it in table, most of them suggests to do it like
.fieldname1
.fieldname2
and so on which is not I'm expecting to. I've been learning Javascript now a days, but I dont know how to make the process common for all tables irrespective of no of fileds.
Note: I'm using jquery datatables.
Just a thought comes up in my head. Is it possible to just give the Json object to jquery datatables and it'll do the remaining process..?
For reference, this is my xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<user>
<id>1</id>
<username>user1</username>
<email>email1</email>
<status>active</status>
</user>
<user>
<id>2</id>
<username>user2</username>
<email>email2</email>
<status>active</status>
</user>
<user>
<id>3</id>
<username>user3</username>
<email>email3</email>
<status>inactive</status>
</user>
</Users>
This is the Json object
Object { id: "1", username: "user1", email: "email1", status: "active" } //output[0]
Object { id: "2", username: "user2", email: "email2", status: "active" } //output[1]
Object { id: "3", username: "user3", email: "email3", status: "inactive" } //output[2]
As #epascarello said only a cleaner way is to create tr's dinamically based on the output array index and td's based on the keys from the output[i] -json. Using jquery an example should look like this.
var table = $('<table></table>');
for(var i=0; i < output.length; i++) {
var tr = $('<tr></tr>');
for(var key in output[i]) {
var td = $('<td></td>');
td.attr('class', key);
td.text(output[i][key]);
tr.append(td);
}
table.append(tr);
}
$('body').append(table);
Example here: http://jsfiddle.net/atrifan/4zfs57m8/3
you can even try this
var output = [{id: 1, name: 'Alex', phone: '000'}, {id: 2, name: 'Paul', phone: '010'}];
var stBldr='';
stBldr+='<table border=1>';
$.each(output,function(i,v){
stBldr+='<tr>';
stBldr+='<td>'+$(v)[0].id+'</td><td>'+$(v)[0].name+'</td><td>'+$(v)[0].phone+'</td>';
stBldr+='</tr>';
});
stBldr+='</table>';
var str=$(stBldr);
$('body').append(str);
DEMO

How to Import & Export Custom Fields For Customer List in QuickBooks

In Quickbooks Pro 2009 I'm Adding and Importing list of Customers through the C# windows application. In quick book itself Import and Export options are there for customers list, but we have defined our own business validations logics and we are pushing the data to Quick book DB for customers.
In Quick Book there is a option to define custom fields under Additional Tab.
While programmatically adding Quick Book List of Customers, I'm able to add the values for Built-In Field values Like Customer Name Company Name and Etc. If I try to push the data for custom fields. Its giving me the error...
QuickBooks found an error when parsing the provided XML text stream.
If I skip the custom field data pushing operation then defined fields data passing is working fine.
My Code:
XmlDocument inputXMLDoc = new XmlDocument();
inputXMLDoc.AppendChild(inputXMLDoc.CreateXmlDeclaration("1.0", null, null));
inputXMLDoc.AppendChild(inputXMLDoc.CreateProcessingInstruction("qbxml", "version=\"2.0\""));
XmlElement qbXML = inputXMLDoc.CreateElement("QBXML");
inputXMLDoc.AppendChild(qbXML);
XmlElement qbXMLMsgsRq = inputXMLDoc.CreateElement("QBXMLMsgsRq");
qbXML.AppendChild(qbXMLMsgsRq);
qbXMLMsgsRq.SetAttribute("onError", "stopOnError");
XmlElement custAddRq = inputXMLDoc.CreateElement("CustomerAddRq");
qbXMLMsgsRq.AppendChild(custAddRq);
custAddRq.SetAttribute("requestID", "1");
XmlElement custAdd = inputXMLDoc.CreateElement("CustomerAdd");
custAddRq.AppendChild(custAdd);
//In-Built Columns
custAdd.AppendChild(inputXMLDoc.CreateElement("Name")).InnerText = Name;
custAdd.AppendChild(inputXMLDoc.CreateElement("AccountNumber")).InnerText = AccountNumber;
//Defined Custom Columns
custAdd.AppendChild(inputXMLDoc.CreateElement("CUSTFLD1")).InnerText = JRNL_NO;
String name = CustName.Text.Trim();
string input = inputXMLDoc.OuterXml;
//Push the Data to do the qbXMLRP request
RequestProcessor2 rp = null;
string ticket = null;
string response = null;
try
{
rp = new RequestProcessor2();
rp.OpenConnection("", "IDN CustomerAdd C# sample");
ticket = rp.BeginSession("", QBFileMode.qbFileOpenDoNotCare);
response = rp.ProcessRequest(ticket, input);
}
catch (System.Runtime.InteropServices.COMException ex)
{
MessageBox.Show("COM Error Description = " + ex.Message, "COM error");
}
What am I doing wrong here.. any helps?? or any suggestions / Ideas ????
QuickBooks has a very specific syntax you have to follow (defined by an XML .XSD) when creating requests for it to process. You can't just make up new XML nodes and expect them to work.
Thus, this is going to break things:
//Defined Custom Columns
custAdd.AppendChild(inputXMLDoc.CreateElement("CUSTFLD1")).InnerText = JRNL_NO;
Custom fields are added/modified by sending DataExt requests to QuickBooks. They are entirely separate from the main CustomerAdd or CustomerMod request. To add the custom fields, you can either send an entirely separate request:
<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="7.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<DataExtModRq>
<DataExtMod>
<OwnerID>0</OwnerID>
<DataExtName>Your Custom Field Name</DataExtName>
<ListDataExtType>Customer</ListDataExtType>
<ListObjRef>
<FullName>Your Customer Name</FullName>
</ListObjRef>
<DataExtValue>Custom field value</DataExtValue>
</DataExtMod>
</DataExtModRq>
</QBXMLMsgsRq>
</QBXML>
Or, you can chain the two requests together:
<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="7.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq requestID="Q3VzdG9tZXJBZGR8MTExMTIxMjE=">
<CustomerAdd>
<Name>Keith Palmer Jr.</Name>
<FirstName>Keith</FirstName>
<MiddleName></MiddleName>
<LastName>Palmer Jr.</LastName>
<BillAddress>
<Addr1>134 Stonemill Road</Addr1>
<City>Mansfield</City>
<State>CT</State>
<PostalCode>06268</PostalCode>
<Country>USA</Country>
</BillAddress>
<Phone>999-99-9999</Phone>
<Email>test#example.com</Email>
<Contact>Keith Palmer Jr.</Contact>
</CustomerAdd>
</CustomerAddRq>
<DataExtModRq>
<DataExtMod>
<OwnerID>0</OwnerID>
<DataExtName>Your Custom Field Name</DataExtName>
<ListDataExtType>Customer</ListDataExtType>
<ListObjRef>
<FullName>Keith Palmer Jr.</FullName>
</ListObjRef>
<DataExtValue>Custom field value</DataExtValue>
</DataExtMod>
</DataExtModRq>
</QBXMLMsgsRq>
</QBXML>
Additional examples can be found on our QuickBooks development wiki.

Categories

Resources