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.NpdResult; import com.unilever.audit.entities.Npdkpis; import com.unilever.audit.entities.PosmaterialResult; import com.unilever.audit.entities.Posmaterialkpi; import com.unilever.audit.entities.PriceResult; import com.unilever.audit.entities.Pricekpis; import com.unilever.audit.entities.ProductResult; import com.unilever.audit.entities.Productkpis; import com.unilever.audit.entities.PromotionResult; import com.unilever.audit.entities.Promotionkpis; import com.unilever.audit.entities.QuestionResult; import com.unilever.audit.entities.Questionkpis; import com.unilever.audit.entities.ShareoffolderKpiResult; import com.unilever.audit.entities.Shareoffolderkpis; import com.unilever.audit.entities.ShareofshelfKpiResult; import com.unilever.audit.entities.Shareofshelfkpi; import com.unilever.audit.entities.StockResult; import com.unilever.audit.entities.Stockkpis; import com.unilever.audit.entities.Visit; import com.unilever.audit.entities.Visitsjson; import com.unilever.audit.pojo.SyncUpReply; import com.unilever.audit.services.MerchandisersFacadeREST; import com.unilever.audit.services.NpdResultFacadeREST; import com.unilever.audit.services.PosmaterialResultFacadeREST; import com.unilever.audit.services.PriceResultFacadeREST; import com.unilever.audit.services.ProductResultFacadeREST; import com.unilever.audit.services.PromotionResultFacadeREST; import com.unilever.audit.services.QuestionResultFacadeREST; import com.unilever.audit.services.ShareoffolderKpiResultFacadeREST; import com.unilever.audit.services.ShareofshelfKpiResultFacadeREST; import com.unilever.audit.services.StockResultFacadeREST; import com.unilever.audit.services.VisitFacadeREST; import com.unilever.audit.services.VisitsjsonFacadeREST; import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.UserTransaction; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * REST Web Service * * @author Trust */ @Path("SyncUp") @Stateless public class SyncUp { @PersistenceContext(unitName = "com.unilever_audit_war_1.0-SNAPSHOTPU") private EntityManager em; @Resource private SessionContext context; @Inject ProductResultFacadeREST productResultFacadeREST; @Inject PriceResultFacadeREST priceResultFacadeREST; @Inject QuestionResultFacadeREST questionResultFacadeREST; @Inject PromotionResultFacadeREST promotionResultFacadeREST; @Inject ShareoffolderKpiResultFacadeREST shareoffolderKpiResultFacadeREST; @Inject StockResultFacadeREST stockResultFacadeREST; @Inject NpdResultFacadeREST npdResultFacadeREST; @Inject ShareofshelfKpiResultFacadeREST shareofshelfKpiResultFacadeREST; @Inject PosmaterialResultFacadeREST posmaterialResultFacadeREST; @Inject private VisitFacadeREST visitFacadeREST; @Inject private VisitsjsonFacadeREST visitsjsonFacadeREST; @Inject private MerchandisersFacadeREST merchandisersFacadeREST; LogSync syncLog; public SyncUp() { syncLog = new LogSync(); } @POST @Path("Data") @Produces("application/json") @Consumes("text/plain") public SyncUpReply SyncUp(String json) { SyncUpReply syncReply = new SyncUpReply(); String id = null; try { JSONObject result = (JSONObject) new JSONParser().parse(json); JSONArray visitArray = (JSONArray) result.get("visit"); id = ((JSONObject) visitArray.get(0)).get("merchandiserId").toString(); System.out.println("--------------" + visitArray); /*****************save Json***********************/ Visitsjson visitsjson = new Visitsjson(); visitsjson.setSyncdate(new Date()); visitsjson.setMerchandiserid(new BigInteger(id)); visitsjson.setJson(json); visitsjsonFacadeREST.create(visitsjson); for (Iterator<JSONObject> it = visitArray.iterator(); it.hasNext();) { try { // save every visit with its results by calling one method that works in a single transaction JSONObject visitData = it.next(); context.getBusinessObject(SyncUp.class).saveVisit(visitData); syncReply.getVisits().add(visitData.get("visitId").toString()); syncReply.setMsg("successfully added"); } catch (Exception e) // if any error/exception occured this will not affect the other visits { e.printStackTrace(); syncLog.setAuditLog(0, Integer.parseInt(id), new Date(), Utils.getRootCause(e).getMessage(), "syncUp"); // log the exception } } if (syncReply.getVisits().size() > 0) { Merchandisers m = em.getReference(Merchandisers.class, new BigDecimal(id)); m.setLast_sync_up(new Date()); merchandisersFacadeREST.edit(new BigDecimal(id), m); } } catch (Exception ex) { // TODO: log this in DB Logger.getLogger(SyncUp.class.getName()).log(Level.SEVERE, null, ex); syncLog.setAuditLog(0, Integer.parseInt(id), new Date(), Utils.getRootCause(ex).getMessage(), "syncUp"); } return syncReply; } // suspend any open transaction and open new specific transaction for this function, then the container will resume the caller transaction @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void saveVisit(JSONObject visitData) throws Exception { Map<String, Object> hm = new HashMap<String, Object>(); /***************Visit***********************/ Visit saveVisit = new Visit(); hm.put("id", visitData.get("visitId").toString()); if (visitFacadeREST.findOneByQuery("Visit.findById", hm) == null) { saveVisit.setId(visitData.get("visitId").toString()); Customers c = em.getReference(Customers.class, (Integer.parseInt(visitData.get("customerId").toString()))); saveVisit.setCustomerid(c); Merchandisers m = em.getReference(Merchandisers.class, new BigDecimal(visitData.get("merchandiserId").toString())); saveVisit.setMerchandiserid(m); try { //Date date = new SimpleDateFormat("MM/dd/yyyy").parse(visitData.get("visitDate").toString()); Date date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") .parse(visitData.get("visitDate").toString()); saveVisit.setVisitdate(date); } catch (java.text.ParseException ex) { ex.printStackTrace(); } saveVisit.setSubmitteddate(new Date()); visitFacadeREST.create(saveVisit); /****************Product Result*********************/ JSONArray Productarray = (JSONArray) visitData.get("productResult"); for (Iterator<JSONObject> it1 = Productarray.iterator(); it1.hasNext();) { JSONObject ProductJson = it1.next(); ProductResult productResult = new ProductResult(); productResult.setQuantity(new BigInteger(ProductJson.get("quantity").toString())); productResult.setExsit(new BigInteger(ProductJson.get("exist").toString())); productResult.setVisitid(saveVisit); Productkpis ProductKPI = em.getReference(Productkpis.class, new BigDecimal(ProductJson.get("productKpiId").toString())); productResult.setProductid(ProductKPI); productResultFacadeREST.create(productResult); } /*********************Price Result*******************/ JSONArray Pricearray = (JSONArray) visitData.get("priceResult"); if (Pricearray.size() > 0) { for (Iterator<JSONObject> it2 = Pricearray.iterator(); it2.hasNext();) { JSONObject PriceJson = it2.next(); PriceResult priceResult = new PriceResult(); priceResult.setVisitid(saveVisit); priceResult.setPrice(new BigDecimal(PriceJson.get("pricevalue").toString())); priceResult.setPricetagexist(new BigInteger(PriceJson.get("pricetagcheck").toString())); Pricekpis PriceKPI = em.getReference(Pricekpis.class, new BigDecimal(PriceJson.get("priceKPIid").toString())); priceResult.setPriceid(PriceKPI); priceResultFacadeREST.create(priceResult); } } /*********************Question Result*****************/ JSONArray Questionarray = (JSONArray) visitData.get("questionResult"); for (Iterator<JSONObject> it3 = Questionarray.iterator(); it3.hasNext();) { JSONObject QuestionJson = it3.next(); QuestionResult questionResult = new QuestionResult(); questionResult.setVisitid(saveVisit); questionResult.setAnswertext(QuestionJson.get("answerText").toString()); Questionkpis questionKPI = em.getReference(Questionkpis.class, new BigDecimal(QuestionJson.get("questionId").toString())); questionResult.setQuestionid(questionKPI); questionResultFacadeREST.create(questionResult); } /*********************Promotion Result*********************/ JSONArray promotionarray = (JSONArray) visitData.get("promotionResult"); for (Iterator<JSONObject> it4 = promotionarray.iterator(); it4.hasNext();) { JSONObject PromotionJson = it4.next(); PromotionResult promotionResult = new PromotionResult(); promotionResult.setVisitid(saveVisit); promotionResult.setExist(new BigInteger(PromotionJson.get("exist").toString())); Promotionkpis promotionKPI = em.getReference(Promotionkpis.class, new BigDecimal(PromotionJson.get("promotionKPIid").toString())); promotionResult.setPromotionid(promotionKPI); promotionResultFacadeREST.create(promotionResult); } /***********************ShareOfFolder Result*********************/ JSONArray ShareOfFolderarray = (JSONArray) visitData.get("sofResult"); for (Iterator<JSONObject> it5 = ShareOfFolderarray.iterator(); it5.hasNext();) { JSONObject SofJson = it5.next(); ShareoffolderKpiResult SofResult = new ShareoffolderKpiResult(); SofResult.setVisitid(saveVisit); SofResult.setNoofpicture(new BigInteger(SofJson.get("noOfPicture").toString())); SofResult.setTotalofpicture(new BigInteger(SofJson.get("totalNoOfPicture").toString())); Shareoffolderkpis shareoffolderkpis = em.getReference(Shareoffolderkpis.class, new BigDecimal(SofJson.get("sofID").toString())); SofResult.setSofid(shareoffolderkpis); SofResult.setSofexist(new BigInteger(SofJson.get("sofExist").toString())); shareoffolderKpiResultFacadeREST.create(SofResult); } /***************************STOCK Result***********************/ JSONArray Stockarray = (JSONArray) visitData.get("stockResult"); for (Iterator<JSONObject> it8 = Stockarray.iterator(); it8.hasNext();) { JSONObject StockJson = it8.next(); StockResult stockResult = new StockResult(); stockResult.setQuantity(new BigInteger(StockJson.get("quantity").toString())); stockResult.setVisitid(saveVisit); stockResult.setStockquantity(new BigInteger(StockJson.get("stockQuantity").toString())); Stockkpis stockKPI = em.getReference(Stockkpis.class, new BigDecimal(StockJson.get("stockKpiId").toString())); stockResult.setStockid(stockKPI); stockResultFacadeREST.create(stockResult); } /********************NPD Result**************************/ JSONArray Npdarray = (JSONArray) visitData.get("npdResult"); for (Iterator<JSONObject> it6 = Npdarray.iterator(); it6.hasNext();) { JSONObject NpdJson = it6.next(); NpdResult npdResult = new NpdResult(); npdResult.setVisitid(saveVisit); npdResult.setExist(new BigInteger(NpdJson.get("exist").toString())); Npdkpis npdKPI = em.getReference(Npdkpis.class, new BigDecimal(NpdJson.get("npdid").toString())); npdResult.setNpdid(npdKPI); npdResultFacadeREST.create(npdResult); } /***********************share of shelf Result**********************/ JSONArray shareOfShelfarray = (JSONArray) visitData.get("shareOfShelfResult"); for (Iterator<JSONObject> it7 = shareOfShelfarray.iterator(); it7.hasNext();) { JSONObject ShareOfShelfJson = it7.next(); ShareofshelfKpiResult shareofshelfKpiResult = new ShareofshelfKpiResult(); shareofshelfKpiResult.setVisitid(saveVisit); shareofshelfKpiResult.setSosbrand(new BigInteger(ShareOfShelfJson.get("sosBrand").toString())); shareofshelfKpiResult.setSossegment(new BigInteger(ShareOfShelfJson.get("sosSegment").toString())); Shareofshelfkpi ShareOfShelfKPI = em.getReference(Shareofshelfkpi.class, new BigDecimal(ShareOfShelfJson.get("shareofShelfID").toString())); shareofshelfKpiResult.setShareofshelfid(ShareOfShelfKPI); shareofshelfKpiResultFacadeREST.create(shareofshelfKpiResult); } /***********************POS Result************************/ JSONArray Posarray = (JSONArray) visitData.get("posResult"); for (Iterator<JSONObject> it9 = Posarray.iterator(); it9.hasNext();) { JSONObject PosJson = it9.next(); PosmaterialResult posResult = new PosmaterialResult(); posResult.setVisitid(saveVisit); posResult.setExist(new BigInteger(PosJson.get("exist").toString())); Posmaterialkpi posmaterialKPI = em.getReference(Posmaterialkpi.class, new BigDecimal(PosJson.get("posid").toString())); posResult.setPosmaterialid(posmaterialKPI); posmaterialResultFacadeREST.create(posResult); } } } }