com.unilever.audit.services2.Sync_Down.java Source code

Java tutorial

Introduction

Here is the source code for com.unilever.audit.services2.Sync_Down.java

Source

/*
 * 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.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.unilever.audit.entities.Answers;
import com.unilever.audit.entities.Answertypes;
import com.unilever.audit.entities.Categories;
import com.unilever.audit.entities.Competition;
import com.unilever.audit.entities.CompetitionKpi;
import com.unilever.audit.entities.CompetitionKpiSurvey;
import com.unilever.audit.entities.CustomerComptition;
import com.unilever.audit.entities.Customers;
import com.unilever.audit.entities.Items;
import com.unilever.audit.entities.Kpi;
import com.unilever.audit.entities.MerchandiserChannel;
import com.unilever.audit.entities.MerchidisersCustomers;
import com.unilever.audit.entities.Npdkpichannel;
import com.unilever.audit.entities.Npdkpis;
import com.unilever.audit.entities.Photo;
import com.unilever.audit.entities.Posmaterial;
import com.unilever.audit.entities.Posmaterialchannel;
import com.unilever.audit.entities.Posmaterialkpi;
import com.unilever.audit.entities.Posmaterialsurveycategory;
import com.unilever.audit.entities.Pricekpichannel;
import com.unilever.audit.entities.Pricekpis;
import com.unilever.audit.entities.Productkpichannel;
import com.unilever.audit.entities.Productkpis;
import com.unilever.audit.entities.Promotionchannel;
import com.unilever.audit.entities.Promotionkpis;
import com.unilever.audit.entities.Questionkpichannel;
import com.unilever.audit.entities.Questionkpis;
import com.unilever.audit.entities.Questions;
import com.unilever.audit.entities.Questionsanswers;
import com.unilever.audit.entities.Salesectors;
import com.unilever.audit.entities.Shareoffolderkpis;
import com.unilever.audit.entities.Shareofshelfkpi;
import com.unilever.audit.entities.Shelfcheckchannel;
import com.unilever.audit.entities.Sofkpichannel;
import com.unilever.audit.entities.Stockkpichannel;
import com.unilever.audit.entities.Stockkpis;
import com.unilever.audit.entities.Surveycategory;
import com.unilever.audit.pojo.SyncDownObjects;
import com.unilever.audit.services.AnswersFacadeREST;
import com.unilever.audit.services.AnswertypesFacadeREST;
import com.unilever.audit.services.CategoriesFacadeREST;
import com.unilever.audit.services.CompetitionFacadeREST;
import com.unilever.audit.services.CompetitionKpiFacadeREST;
import com.unilever.audit.services.CompetitionKpiSurveyFacadeREST;
import com.unilever.audit.services.CustomerComptitionFacadeREST;
import com.unilever.audit.services.CustomersFacadeREST;
import com.unilever.audit.services.ItemsFacadeREST;
import com.unilever.audit.services.KpiFacadeREST;
import com.unilever.audit.services.MerchandiserChannelFacade;
import com.unilever.audit.services.MerchidisersCustomersFacadeREST;
import com.unilever.audit.services.NpdkpichannelFacadeREST;
import com.unilever.audit.services.NpdkpisFacadeREST;
import com.unilever.audit.services.PhotoFacadeREST;
import com.unilever.audit.services.PosmaterialFacadeREST;
import com.unilever.audit.services.PosmaterialchannelFacadeREST;
import com.unilever.audit.services.PosmaterialkpiFacadeREST;
import com.unilever.audit.services.PosmaterialsurveycategoryFacadeREST;
import com.unilever.audit.services.PricekpichannelFacadeREST;
import com.unilever.audit.services.PricekpisFacadeREST;
import com.unilever.audit.services.ProductkpichannelFacadeREST;
import com.unilever.audit.services.ProductkpisFacadeREST;
import com.unilever.audit.services.PromotionchannelFacadeREST;
import com.unilever.audit.services.PromotionkpisFacadeREST;
import com.unilever.audit.services.QuestionkpichannelFacadeREST;
import com.unilever.audit.services.QuestionkpisFacadeREST;
import com.unilever.audit.services.QuestionsFacadeREST;
import com.unilever.audit.services.QuestionsanswersFacadeREST;
import com.unilever.audit.services.SalesectorsFacadeREST;
import com.unilever.audit.services.ShareoffolderkpisFacadeREST;
import com.unilever.audit.services.ShareofshelfkpiFacadeREST;
import com.unilever.audit.services.ShelfcheckchannelFacadeREST;
import com.unilever.audit.services.SofkpichannelFacadeREST;
import com.unilever.audit.services.StockkpichannelFacadeREST;
import com.unilever.audit.services.StockkpisFacadeREST;
import com.unilever.audit.services.SurveycategoryFacadeREST;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.inject.Inject;
import javax.inject.Named;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.eclipse.persistence.internal.jpa.rs.metadata.model.Query;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * REST Web Service
 *
 * @author ESR
 */
@Stateless
@Path("SyncDown")
@Interceptors(TXEnforcer.class)
@TransactionManagement(TransactionManagementType.BEAN)
public class Sync_Down {

    @PersistenceContext(unitName = "com.unilever_audit_war_1.0-SNAPSHOTPU")
    private EntityManager em;
    static int count;

    @Inject
    private CompetitionFacadeREST competitionFacadeREST;

    @Inject
    private CompetitionKpiFacadeREST competitionKpiFacadeREST;

    @Inject
    private CustomerComptitionFacadeREST customercomptitionFacadeREST;

    @Inject
    private MerchidisersCustomersFacadeREST merchidisersCustomersFacadeREST;

    @Inject
    private CompetitionKpiSurveyFacadeREST competitionkpisurveyFacadeREST;

    @Inject
    private CustomersFacadeREST customersFacadeREST;

    @Inject
    private KpiFacadeREST kpiFacadeREST;

    @Inject
    private QuestionsanswersFacadeREST questionsanswersFasadeREST;

    @Inject
    private AnswersFacadeREST answersFacadeREST;

    @Inject
    private QuestionkpisFacadeREST questionkpisFacadeREST;

    @Inject
    private PricekpisFacadeREST pricekpisFacadeREST;

    @Inject
    private PricekpichannelFacadeREST pricekpichannelFacadeREST;

    @Inject
    private PromotionkpisFacadeREST promotionkpisFacadeREST;

    @Inject
    private PromotionchannelFacadeREST promotionchannelFacadeREST;

    @Inject
    private ShareoffolderkpisFacadeREST shareoffolderkpisFacadeREST;

    @Inject
    private SofkpichannelFacadeREST sofkpichannelFacadeREST;

    @Inject
    private ProductkpisFacadeREST productkpisFacadeREST;

    @Inject
    private ProductkpichannelFacadeREST productkpichannelFacadeREST;

    @Inject
    private QuestionsFacadeREST questionsFacadeREST;

    @Inject
    private StockkpisFacadeREST stockkpisFacadeREST;

    @Inject
    private StockkpichannelFacadeREST stockkpichannelFacadeREST;

    @Inject
    private NpdkpisFacadeREST npdkpisFacadeREST;

    @Inject
    private NpdkpichannelFacadeREST npdkpichannelFacadeREST;

    @Inject
    private ShareofshelfkpiFacadeREST shareofshelfkpiFacadeREST;

    @Inject
    private ShelfcheckchannelFacadeREST shelfcheckchannelFacadeREST;

    @Inject
    private ItemsFacadeREST itemsFacadeREST;

    @Inject
    private SurveycategoryFacadeREST surveycategoryFacadeREST;

    @Inject
    private PosmaterialkpiFacadeREST posmaterialkpiFacadeREST;

    @Inject
    private PosmaterialFacadeREST posmaterialFacadeREST;

    @Inject
    private PosmaterialchannelFacadeREST posmaterialchannelFacadeREST;

    @Inject
    private PosmaterialsurveycategoryFacadeREST posmaterialsurveycategoryFacadeREST;

    @Inject
    private CategoriesFacadeREST categoriesFacadeREST;

    @Inject
    private SalesectorsFacadeREST salesectorsFacadeREST;

    @Inject
    private PhotoFacadeREST photoFacadeREST;

    @Inject
    private QuestionkpichannelFacadeREST questionkpichannelFacadeREST;

    @Inject
    private AnswertypesFacadeREST answertypesFacadeREST;

    @Inject
    private MerchandiserChannelFacade merchandiserChannelFacade;

    /**
     * Creates a new instance of AuditResource
     */

    public Sync_Down() {
    }

    /**
     * Retrieves representation of an instance of
     * com.unilever.audit.services2.AuditResource
     *
     * @param id
     * @param dataType
     * @return an instance of java.lang.String
     */
    @GET
    @Path("getSyncObject/{id}/{dataType}/{compress}")
    @Produces("application/json")
    public byte[] getSyncObject(@PathParam("id") int id, @PathParam("dataType") String dataType,
            @PathParam("compress") int compress) {

        GZIPOutputStream gzip = null;
        count++;
        ByteArrayOutputStream out = null;
        SyncDownObjects syncDownObjects = getObject(dataType, id);

        try {
            out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);

            ObjectMapper mapper = new ObjectMapper();
            AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
            AnnotationIntrospector introspector1 = new JacksonAnnotationIntrospector();
            mapper.setAnnotationIntrospectors(introspector, introspector1);
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

            //String jsonString = mapper.writeValueAsString(syncDownObjects);
            //JSONObject jsonobject = (JSONObject) new JSONParser().parse(jsonString);
            //gzip.write(jsonobject.toString().getBytes("8859_1"));
            //gzip.write(jsonobject.toString().getBytes("UTF-8"));
            gzip.write(mapper.writeValueAsBytes(syncDownObjects));
            gzip.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        } //catch (ParseException ex) {
          // ex.printStackTrace();
          // }
        System.out.println("======================= count : " + count);
        return out.toByteArray();
    }

    @GET
    @Path("getSyncObject/{id}/{dataType}")
    @Produces("application/json")
    public SyncDownObjects getSyncObject(@PathParam("id") int id, @PathParam("dataType") String dataType)
            throws JsonProcessingException {
        //  ObjectMapper mapper = new ObjectMapper();
        //  final DateFormat df = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");                                      
        //  mapper.setDateFormat(df);
        //   String  jsonString  = mapper.writeValueAsString(getObject(dataType,id)) ;
        //  System.out.println("JsonString = " + jsonString) ;
        //  return jsonString  ;

        return getObject(dataType, id);
    }

    private SyncDownObjects getObject(String dataType, int id) {
        SyncDownObjects syncDownObjects = new SyncDownObjects();
        Map<String, Object> hm = new HashMap<String, Object>();
        hm.put("id", id);

        // get the salessectors and categories allowed for this 
        //List<MerchandiserChannel>  merchandiserChannelList  =  merchandiserChannelFacade.findAllByQuery("MerchandiserChannel.findByMerchandiserId" , hm ) ;

        if (dataType.equals("f")) {
            javax.persistence.Query q = em.createNamedQuery("Items.getFullSyncDownList");
            q.setParameter(1, id);
            q.setParameter(2, id);
            q.setParameter(3, id);
            q.setParameter(4, id);
            q.setParameter(5, id);
            q.setParameter(6, id);
            q.setParameter(7, id);
            q.setParameter(8, id);
            q.setParameter(9, id);
            q.setParameter(10, id);
            List<Items> items = q.getResultList();
            syncDownObjects.setItems(items);
        } //else if (dataType.equals("p")) {
          //List<Items> itemsList = itemsFacadeREST.findAllByQuery("Items.getSyncDownList", hm);
          //syncDownObjects.setItems(itemsList);

        //}

        List<Customers> CustomersList = customersFacadeREST.findAllByQuery("Customers.getSyncDownList", hm);
        syncDownObjects.setCustomers(CustomersList);

        List<MerchidisersCustomers> merchidisersCustomersList = merchidisersCustomersFacadeREST
                .findAllByQuery("MerchidisersCustomers.getSyncDownList", hm);
        syncDownObjects.setMerchidisersCustomersList(merchidisersCustomersList);

        List<Categories> categoriesList = categoriesFacadeREST.findAllByQuery("Categories.getSyncDownList", hm);
        syncDownObjects.setCategoriesList(categoriesList);

        List<Salesectors> salesectorsList = salesectorsFacadeREST.findAllByQuery("Salesectors.getSyncDownList", hm);
        syncDownObjects.setSalesectorsList(salesectorsList);

        List<Surveycategory> surveycategory = surveycategoryFacadeREST.findAllByQuery("Surveycategory.findAll",
                null);
        syncDownObjects.setSurveycategory(surveycategory);

        List<Kpi> kpi = kpiFacadeREST.findAllByQuery("Kpi.findAll", null);
        syncDownObjects.setKpi(kpi);

        /*******************Price KPI*********************/
        List<Pricekpis> priceKPIsList = pricekpisFacadeREST.findAllByQuery("Pricekpis.getSyncDownList", hm);
        syncDownObjects.setPriceKPI(priceKPIsList);

        List<Pricekpichannel> pricekpichannelList = pricekpichannelFacadeREST
                .findAllByQuery("Pricekpichannel.getSyncDownList", hm);
        syncDownObjects.setPricekpichannelList(pricekpichannelList);

        /*******************NPD KPI*********************/
        List<Npdkpis> npdKpisList = npdkpisFacadeREST.findAllByQuery("Npdkpis.getSyncDownList", hm);
        syncDownObjects.setNpdKPI(npdKpisList);

        List<Npdkpichannel> npdKpisChannelList = npdkpichannelFacadeREST
                .findAllByQuery("Npdkpichannel.getSyncDownList", hm);
        syncDownObjects.setNpdKpisChannelList(npdKpisChannelList);

        /*******************Promotions KPI*********************/
        List<Promotionkpis> promotionsList = promotionkpisFacadeREST.findAllByQuery("Promotionkpis.getSyncDownList",
                hm);
        syncDownObjects.setPromotionkpis(promotionsList);

        List<Promotionchannel> promotionkpischannelList = promotionchannelFacadeREST
                .findAllByQuery("Promotionchannel.getSyncDownList", hm);
        syncDownObjects.setPromotionkpischannelList(promotionkpischannelList);

        /*******************Product KPI*********************/
        List<Productkpis> productKPIsList = productkpisFacadeREST.findAllByQuery("Productkpis.getSyncDownList", hm);
        syncDownObjects.setProductKPI(productKPIsList);

        List<Productkpichannel> productkpichannelList = productkpichannelFacadeREST
                .findAllByQuery("Productkpichannel.getSyncDownList", hm);
        syncDownObjects.setProductkpichannelList(productkpichannelList);

        /*******************Questions KPI*********************/
        List<Questionkpichannel> questionskpischannelList = questionkpichannelFacadeREST
                .findAllByQuery("Questionkpichannel.getSyncDownList", hm);
        syncDownObjects.setQuestionkpichannel(questionskpischannelList);

        List<Questionkpis> questionkpisList = questionkpisFacadeREST.findAllByQuery("Questionkpis.getSyncDownList",
                hm);
        syncDownObjects.setQuestionkpis(questionkpisList);

        List<Questions> questionsList = questionsFacadeREST.findAllByQuery("Questions.getSyncDownList", hm);
        syncDownObjects.setQuestions(questionsList);

        List<Questionsanswers> questionsAnswersList = questionsanswersFasadeREST
                .findAllByQuery("Questionsanswers.getSyncDownList", hm);
        syncDownObjects.setQuestionsAnswers(questionsAnswersList);

        List<Answers> answersList = answersFacadeREST.findAllByQuery("Answers.getSyncDownList", hm);
        syncDownObjects.setAnswers(answersList);

        List<Answertypes> answertypesList = answertypesFacadeREST.findAll();
        syncDownObjects.setAnswertypes(answertypesList);
        /*******************Share of folder KPI*********************/
        List<Shareoffolderkpis> shareoffolderkpisList = shareoffolderkpisFacadeREST
                .findAllByQuery("Shareoffolderkpis.getSyncDownList", hm);
        syncDownObjects.setShareOfFolderKPI(shareoffolderkpisList);

        List<Sofkpichannel> sofkpichannelList = sofkpichannelFacadeREST
                .findAllByQuery("Sofkpichannel.getSyncDownList", hm);
        syncDownObjects.setSofkpichannelList(sofkpichannelList);

        /*******************Share of Self KPI*********************/
        List<Shareofshelfkpi> shelfKpisList = shareofshelfkpiFacadeREST
                .findAllByQuery("Shareofshelfkpi.getSyncDownList", hm);
        syncDownObjects.setShelfKpisList(shelfKpisList);

        List<Shelfcheckchannel> shelfcheckchannelList = shelfcheckchannelFacadeREST
                .findAllByQuery("Shelfcheckchannel.getSyncDownList", hm);
        syncDownObjects.setShelfcheckchannelList(shelfcheckchannelList);

        /*******************Stock KPI*********************/
        List<Stockkpis> StockKPIsList = stockkpisFacadeREST.findAllByQuery("Stockkpis.getSyncDownList", hm);
        syncDownObjects.setStockKPI(StockKPIsList);

        List<Stockkpichannel> StockkpichannelList = stockkpichannelFacadeREST
                .findAllByQuery("Stockkpichannel.getSyncDownList", hm);
        syncDownObjects.setStockkpichannelList(StockkpichannelList);

        /*******************Competition KPI*********************/
        List<Competition> competition = competitionFacadeREST.findAllByQuery("Competition.getSyncDownList", hm);
        syncDownObjects.setCompetition(competition);

        List<CustomerComptition> customerComptition = customercomptitionFacadeREST
                .findAllByQuery("CustomerComptition.getSyncDownList", hm);
        syncDownObjects.setCustomerComptition(customerComptition);

        List<CompetitionKpi> competitionKpi = competitionKpiFacadeREST
                .findAllByQuery("CompetitionKpi.getSyncDownList", hm);
        syncDownObjects.setCompetitionKpi(competitionKpi);

        List<CompetitionKpiSurvey> competitionKpiSurvey = competitionkpisurveyFacadeREST
                .findAllByQuery("CompetitionKpiSurvey.getSyncDownList", hm);
        syncDownObjects.setCompetitionKpiSurveyList(competitionKpiSurvey);
        /*******************POS KPI*********************/
        List<Posmaterial> posmaterial = posmaterialFacadeREST.findAllByQuery("Posmaterial.getSyncDownList", hm);
        syncDownObjects.setPosmaterial(posmaterial);

        List<Posmaterialkpi> posmaterialkpi = posmaterialkpiFacadeREST
                .findAllByQuery("Posmaterialkpi.getSyncDownList", hm);
        syncDownObjects.setPosmaterialkpi(posmaterialkpi);

        List<Posmaterialchannel> Posmaterialchannel = posmaterialchannelFacadeREST
                .findAllByQuery("Posmaterialchannel.getSyncDownList", hm);
        syncDownObjects.setPosmaterialchannel(Posmaterialchannel);

        List<Posmaterialsurveycategory> posmaterialsurveycategory = posmaterialsurveycategoryFacadeREST
                .findAllByQuery("Posmaterialsurveycategory.getSyncDownList", hm);
        syncDownObjects.setPosmaterialsurveycategory(posmaterialsurveycategory);

        /********************************photo table*************/
        javax.persistence.Query q = null;
        if (dataType.equals("f")) {
            q = em.createNamedQuery("Photo.getSyncDownList");
        } else if (dataType.equals("p")) {
            q = em.createNamedQuery("Photo.getSyncDownList.partial");
        }
        q.setParameter(1, id);
        q.setParameter(2, id);
        q.setParameter(3, id);
        q.setParameter(4, id);
        q.setParameter(5, id);
        q.setParameter(6, id);
        List<Photo> photos = q.getResultList();
        //List<Photo> photos=photoFacadeREST.findAllByQuery("Photo.getSyncDownList", hm);
        System.out.println("=========" + photos.size());
        syncDownObjects.setPhotos(photos);
        return syncDownObjects;
    }

}