Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.unilever.audit.services2; import com.unilever.audit.entities.Visit; import com.unilever.audit.entities.Customers; import com.unilever.audit.entities.Merchandisers; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.zip.GZIPInputStream; import javax.annotation.Resource; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.enterprise.context.RequestScoped; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.SystemException; import javax.transaction.UserTransaction; import javax.ws.rs.POST; import org.apache.commons.io.IOUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * REST Web Service * * @author ESR */ @Path("visitsync") @RequestScoped public class VisitSync { @Context private UriInfo context; @Resource private UserTransaction utx; @PersistenceContext(unitName = "com.unilever_audit_war_1.0-SNAPSHOTPU") private EntityManager em; /** * Creates a new instance of VisitSync */ public VisitSync() { } /** * Retrieves representation of an instance of com.unilever.audit.services2.VisitSync * @return an instance of java.lang.String */ @GET @Produces("application/json") public String getJson() { //TODO return proper representation object throw new UnsupportedOperationException(); } @POST @Produces("application/json") @Consumes("text/plain") public String postJson(byte[] jsonObj) throws IOException { JSONObject result = new JSONObject(); JSONArray failedIds = new JSONArray(); boolean status = true; byte[] buffer; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(jsonObj)); IOUtils.copy(in, out); buffer = out.toByteArray(); System.out.println("======================siiiize" + buffer.length); JSONObject obj = new JSONObject(new String(buffer)); JSONArray visitsArr = obj.getJSONArray("visits"); for (int i = 0; i < visitsArr.length(); i++) { try { utx.begin(); } catch (javax.transaction.NotSupportedException ex) { ex.printStackTrace(); } catch (SystemException ex) { ex.printStackTrace(); } JSONObject visitObj = (JSONObject) visitsArr.get(i); Visit v = new Visit(); v.setId(visitObj.getString("id")); Customers c = em.getReference(Customers.class, new BigDecimal(visitObj.getInt("customerid"))); v.setCustomerid(c); Merchandisers m = em.getReference(Merchandisers.class, new BigDecimal(visitObj.getInt("merchid"))); v.setMerchandiserid(m); try { Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy") .parse(visitObj.getString("finishdate")); v.setVisitdate(date); } catch (java.text.ParseException ex) { ex.printStackTrace(); } v.setSubmitteddate(new Date()); try { em.persist(v); System.out.println("Timeeeeeeeeeeeeee ============= " + (v.getSubmitteddate().getTime() - v.getVisitdate().getTime())); utx.commit(); } catch (Exception ex) { status = false; failedIds.put(v.getId()); } } result.put("status", status); result.put("failedIds", failedIds); } catch (JSONException ex) { ex.printStackTrace(); } return result.toString(); } }