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.Customers; import com.unilever.audit.entities.Merchandisers; import com.unilever.audit.entities.Visit; import com.unilever.audit.services.VisitFacadeREST; 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.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPInputStream; import javax.annotation.Resource; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.HeuristicMixedException; import javax.transaction.HeuristicRollbackException; import javax.transaction.RollbackException; 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("Sync_Up") @RequestScoped public class Sync_Up { @PersistenceContext(unitName = "com.unilever_audit_war_1.0-SNAPSHOTPU") private EntityManager em; @Resource private UserTransaction utx; @Inject private VisitFacadeREST visitFacadeREST; /** * Creates a new instance of SyncUpResource */ public Sync_Up() { } /** * Retrieves representation of an instance of * com.unilever.audit.services2.SyncUpResource * * @return an instance of java.lang.String */ @POST @Produces("application/json") @Consumes("text/plain") public String postSyncUP(byte[] jsonByte) throws IOException { //TODO return proper representation object JSONObject syncUPJsonObj; byte[] buffer; try { /** * ***************** Decompress JsonObject *********************** */ ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPInputStream in; in = new GZIPInputStream(new ByteArrayInputStream(jsonByte)); IOUtils.copy(in, out); buffer = out.toByteArray(); syncUPJsonObj = new JSONObject(new String(buffer)); /** * *********** Call Methods To Save Sync UP objects ************** */ persistVisit(syncUPJsonObj); } catch (IOException ex) { ex.printStackTrace(); } catch (JSONException ex) { ex.printStackTrace(); } return null; } private void persistVisit(JSONObject obj) { try { JSONArray visitsArr = obj.getJSONArray("visits"); for (int i = 0; i < visitsArr.length(); i++) { 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()); visitFacadeREST.create(v); } } catch (JSONException ex) { ex.printStackTrace(); } } }