Wednesday, February 17, 2016

How to send xml message through servlet http post?

I want to send an xml file to a service through servlet http post. After I did some research and I found a way to easily do this.

1. I chose to use Apache HttpComponents.

2. Download the latest HttpClient jars from https://hc.apache.org/downloads.cgi

3. In my servlet, I import the following classes.

 import org.apache.http.HttpResponse;  
 import org.apache.http.client.HttpClient;  
 import org.apache.http.client.methods.HttpPost;  
 import org.apache.http.entity.StringEntity;  
 import org.apache.http.impl.client.HttpClientBuilder;  
 import org.apache.http.util.EntityUtils;  

4. In the doPost method, I add the following code.

  // HTTP post xml document to outside service  
           HttpPost post = new HttpPost("https://testserviceurl");  
           post.setHeader("Content-Type", "application/xml");  
           post.setEntity(new StringEntity(xmlString));  
           HttpClient client = HttpClientBuilder.create().build();  
           HttpResponse resp = client.execute(post);  
           String result = EntityUtils.toString(resp.getEntity());  
           System.out.println("post result:" + result);  

HttpClient really provides a simply way to do the job.


Thursday, August 6, 2015

How to split big XML file into small files with certain number of items?

I have a huge xml file with over 2000 PO receipts. To make the data processing fast, I would like to split this big file into small xml files.

This is the structure of original xml file.


This is the split file I would like to have. This file is with same header information as the original one.



This is the code I wrote in Java.

 public class XmlSplit {  
   public static void main(String [] args) throws Exception {  
     File input = new File("input.xml");  
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
     Document doc = dbf.newDocumentBuilder().parse(input);  
     XPath xpath = XPathFactory.newInstance().newXPath();  
     NodeList headernodes = (NodeList) xpath.evaluate("//PurchaseReceiptMessage/Header", doc, XPathConstants.NODESET);  
     NodeList nodes = (NodeList) xpath.evaluate("//PurchaseReceiptMessage/PurchaseReceipt", doc, XPathConstants.NODESET);  
     int itemsPerFile = 5;  
     int fileNumber = 0;  
     Document currentDoc = dbf.newDocumentBuilder().newDocument();  
     Node rootNode = currentDoc.createElement("PurchaseReceiptMessage");  
     for (int i=1; i <= headernodes.getLength(); i++) {  
     Node headerNode = currentDoc.importNode(headernodes.item(i-1), true);  
     rootNode.appendChild(headerNode);  
     }  
     File currentFile = new File(fileNumber+".xml");  
     for (int i=1; i <= nodes.getLength(); i++) {  
       Node imported = currentDoc.importNode(nodes.item(i-1), true);  
       rootNode.appendChild(imported);  
       if (i % itemsPerFile == 0) {  
         writeToFile(rootNode, currentFile);  
         rootNode = currentDoc.createElement("PurchaseReceiptMessage");  
         currentFile = new File((++fileNumber)+".xml");  
       }  
     }  
     writeToFile(rootNode, currentFile);  
   }  
   private static void writeToFile(Node node, File file) throws Exception {  
     Transformer transformer = TransformerFactory.newInstance().newTransformer();  
     transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));  
   }  
 }  






Monday, July 20, 2015

How to create a function in Jquery and added into an element? (jquery delegation)

I have a json data, and want to process it in Javascript and meanwhile generate a grid to append in  <form id="supplierFilter">.

The code is as following.

Javascript code:

 $(document).ready(function() {  
      var DTclass = ${class};  
      $.fn.classfunction = function() {  
            console.log("classes json:"+DTclass);  
          var columndiv;  
          var classes;  
          for (var i = 0; i < DTclass.length; i++) {  
               columndiv = $("<div class='col-sm-3'>");  
               classes = $("<div class='checkbox'>");  
               classes.append("<label> <input name='filter' value='"+DTclass[i].IMG_FILE_ID+"' type='checkbox'/>     <img src='${pageContext.request.contextPath}/download/"+DTclass[i].IMG_FILE_ID+"' alt='"+DTclass[i].DISPLAY_NAME+"' title='"+DTclass[i].DISPLAY_NAME+"'' /> "+DTclass[i].DISPLAY_NAME);  
               columndiv.append(classes);  
            $('#supplierFilter').append(columndiv);   
          }  
        };   
             $('#supplierFilter').classfunction();            
      )}    


HTML code:

 <form id="supplierFilter"></form>  


At beginning, I just use a regular javascript function, it did generate the html code, but when I clicked on the checkbox function, the function won't be triggered. This is caused by jquery delegation. There are more detail information from :
http://learn.jquery.com/events/event-delegation/

Wednesday, July 15, 2015

How to get last inserted id in DB2?

I have two tables in db2, one child, one parent. I would like to insert data into one table, and get the id (which auto generated in the db2,) then insert the id into another table as a foreign key by using ETL tool, Talend open studio.

After a long time research I found out a solution.
Within the same connection and same transaction use tDB2Input with this select:

 SELECT Identity_val_Local() as id FROM sysibm.sysdummy1  

The Talend job is like this:





Tuesday, July 14, 2015

How to create a dynamic bootstrap grid by using json data from a URL?


I want to display the above dynamic data by using bootstrap grid . The data is in Json format from a  server side URL. Json format data is as below.

 [{"DISPLAY_NAME":"Agreement Supplier","IMG_FILE_ID":138},  
  {"DISPLAY_NAME":"Subrecipient","IMG_FILE_ID" :102},  
  {"DISPLAY_NAME":"California Supplier","IMG_FILE_ID":103},  
  {"DISPLAY_NAME":"Certified Small Business Concern","IMG_FILE_ID":162},  
  {"DISPLAY_NAME":"HUBZone Small Business","IMG_FILE_ID":163},  
  {"DISPLAY_NAME":"Minority-Owned Business","IMG_FILE_ID":164},  
  {"DISPLAY_NAME":"Service-Disabled Veteran-Owned","IMG_FILE_ID" :165},  
  {"DISPLAY_NAME":"Small Business","IMG_FILE_ID":166},  
  {"DISPLAY_NAME":"Small Disadvantaged Business","IMG_FILE_ID":167},  
  {"DISPLAY_NAME":"Veteran-Owned Small Business","IMG_FILE_ID":168},  
  {"DISPLAY_NAME":"Women-Owned Small Business","IMG_FILE_ID":169},  
  {"DISPLAY_NAME":"AbilityOne","IMG_FILE_ID":139},  
  {"DISPLAY_NAME":"8(a)","IMG_FILE_ID":170},  
  {"DISPLAY_NAME":"Catering Supplier","IMG_FILE_ID":171},  
  {"DISPLAY_NAME":"Individual","IMG_FILE_ID":116}]   



I used jQuery to read data from URL and generate the HTML code. The code is as following.

 $(document).ready(function() {  
 // top classes selection  
      $.getJSON("${pageContext.request.contextPath}/download/${classesfileId}",  
             function (json) {  
               console.log(json);  
               var columndiv;  
               var classes;  
               for (var i = 0; i < json.length; i++) {  
                    columndiv = $("<div class='col-sm-3'>");  
                    classes = $("<div class='checkbox'>");  
                    classes.append("<label> <input name='filter' value='"+json[i].IMG_FILE_ID+"' type='checkbox'/><img src='${pageContext.request.contextPath}/download/"+json[i].IMG_FILE_ID+"' alt='"+json[i].DISPLAY_NAME+"' title='"+json[i].DISPLAY_NAME+"'' /> "+json[i].DISPLAY_NAME);  
                    columndiv.append(classes);  
                $('#supplierFilter').append(columndiv);   
               }  
             });  
 });  

In the HTML, I have code as this:

 <form id="supplierFilter">  
 </form>  


When it display in browser, the HTML is generated as this:

 <form id="supplierFilter">  
      <div class="col-sm-3">  
           <div class="checkbox">  
           <label>  
                <input type="checkbox" value="138" name="filter">  
                <img title="Agreement Supplier" alt="Agreement Supplier" src="/supsearch/download/138">  
                Agreement Supplier  
           </label>  
           </div>  
      </div>  
      <div class="col-sm-3">  
           <div class="checkbox">  
           <label>  
                <input type="checkbox" value="102" name="filter">  
                <img title="Subrecipient" alt="Subrecipient" src="/supsearch/download/102">  
                Subrecipient  
           </label>  
           </div>  
      </div>  
      <div class="col-sm-3">  
           <div class="checkbox">  
                <label>  
                <input type="checkbox" value="103" name="filter">  
                <img title="California Supplier" alt="California Supplier" src="/supsearch/download/103">  
                California Supplier  
                </label>  
           </div>  
      </div>  
      <div class="col-sm-3">  
           <div class="checkbox">  
                <label>  
                <input type="checkbox" value="162" name="filter">  
                <img title="Certified Small Business Concern" alt="Certified Small Business Concern" src="/supsearch/download/162">  
                Certified Small Business Concern  
                </label>  
           </div>  
      </div>  
      ......  
 </form>  




Wednesday, June 3, 2015

How to add leading zeroes in front of a number by using Java?

I have some integers. I would like to add leading zeroes if they are less than 100. For example, If the number is 1,  it would be converted to 001. If the number is 10, it would be as 010.

By using Java 7 and above, we can simply to do this by using the following line.

 int i =0;  
 int n=10;  
 System.out.println(String.format("%03d", i));  
 System.out.println(String.format("%03d", n));  




Wednesday, May 27, 2015

Select one unique record from DB table with the latest date

I have a file_store table like below.



I would like to select the latest records for each filetype_id.



The SQL to do this can be like this.

 SELECT f.file_id,f.FILETYPE_ID, f.file_obj_l,max_date  
 FROM FILE_STORE f inner JOIN   
 (SELECT FILETYPE_ID, max(f1.UPDATETS) as max_date FROM FILE_STORE f1  
 where FILETYPE_ID in (1, 2, 3)   
  group by FILETYPE_ID  
 ) a  
 on a.FILETYPE_ID = f.FILETYPE_ID and a.max_date = f.UPDATETS