Example usage for com.google.gson GsonBuilder setDateFormat

List of usage examples for com.google.gson GsonBuilder setDateFormat

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setDateFormat.

Prototype

public GsonBuilder setDateFormat(int style) 

Source Link

Document

Configures Gson to to serialize Date objects according to the style value provided.

Usage

From source file:com.quix.aia.cn.imo.rest.TrainingRest.java

License:Open Source License

/**
 * <p>/*from w w  w. ja  v  a  2 s  .c  o  m*/
 * This method gets Candidate Training Result Results.
 * </p>
 */
@GET
@Path("/getABCTrainingResult")
@Produces(MediaType.APPLICATION_JSON)
public Response getABCTrainingResult(@Context HttpServletRequest request, @Context ServletContext context) {

    log.log(Level.INFO, "Training Result --> get ABC Training Results.");

    MsgBeans beans = new MsgBeans();
    Gson googleJson = null;
    GsonBuilder builder = new GsonBuilder();
    AuditTrailMaintenance auditTrailMaint = new AuditTrailMaintenance();
    CandidateTrainingResultMaintenance candidateTrainingResultMaint = new CandidateTrainingResultMaintenance();
    CandidateTrainingResult candidateTrainingResult = null;
    String jsonString = "";

    try {

        String nric = request.getParameter("perAgentId");
        String recruiterAgentCode = request.getParameter("recruiterAgentCode");

        log.log(Level.INFO, "Training Result --> fetching Information ... ");

        candidateTrainingResult = candidateTrainingResultMaint.getCandidateTrainingResult(recruiterAgentCode,
                nric);

        if (null != candidateTrainingResult) {

            candidateTrainingResult.setBranchCode(null);
            candidateTrainingResult.setCourseType(null);
            candidateTrainingResult.setCreationDate(null);
            candidateTrainingResult.setPerAgentId(null);
            candidateTrainingResult.setPerAgentName(null);
            candidateTrainingResult.setRecruiterAgentCode(null);
            candidateTrainingResult.setResultCode(null);
            candidateTrainingResult.setTrainingResult("PASS");
        } else {
            candidateTrainingResult = new CandidateTrainingResult();
        }

        // Convert the object to a JSON string
        log.log(Level.INFO, "Training Result --> Information fetched successfully... ");

        return Response.status(200)
                .entity(builder.setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(candidateTrainingResult))
                .build();
    } catch (Exception e) {
        log.log(Level.SEVERE, "Training --> Error in fetching Record.");
        log.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
        StringWriter errors = new StringWriter();
        e.printStackTrace(new PrintWriter(errors));
        LogsMaintenance logsMain = new LogsMaintenance();
        logsMain.insertLogs("TrainingRest", Level.SEVERE + "", errors.toString());
        beans.setCode("500");
        beans.setMassage(
                "Something wrong happens, please contact administrator. Error Message : " + e.getMessage());
        auditTrailMaint.insertAuditTrail(
                new AuditTrail("Rest", AuditTrail.MODULE_TRAINING, AuditTrail.FUNCTION_FAIL, "FAILED"));

        return Response.status(500).entity(new Gson().toJson(beans)).build();
    } finally {
        auditTrailMaint = null;
        jsonString = null;
        beans = null;
        System.gc();
    }
}

From source file:com.reclabs.recomendar.esdriver.helper.JestResultHelper.java

License:Open Source License

/**
 * @param source/*from   ww  w .ja v a  2 s.  co  m*/
 * @param type
 * @param <T>
 * @return
 */
private static <T> T createSourceObject(Object source, Class<?> type) {
    Object obj = null;
    try {
        if (source instanceof Map) {
            GsonBuilder builder = new GsonBuilder();
            builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            builder.registerTypeAdapter(RecMoney.class, new RecMoneyDeserializer());
            Gson gson = builder.create();
            String json = gson.toJson(source, Map.class);
            obj = gson.fromJson(json, type);
        } else {
            obj = type.cast(source);
        }

        //Fijamos la id
        Field[] fields = type.getDeclaredFields();
        for (Field field : fields) {
            if (StringHelper.equals(field.getName(), "id")) {
                try {
                    field.setAccessible(true);
                    Object value = field.get(obj);
                    if (value == null) {
                        field.set(obj, ((Map) source).get(ES_METADATA_ID));
                    }
                } catch (IllegalAccessException e) {
                    LOGGER.error("Unhandled exception occurred while getting annotated id from source");
                }
                break;
            }
        }

    } catch (Exception e) {
        LOGGER.error("Unhandled exception occurred while converting source to the object ."
                + type.getCanonicalName(), e);
    }
    return (T) obj;
}

From source file:com.singularity.ee.agent.systemagent.monitors.KeynoteMonitor.java

License:Apache License

public KeynoteMonitor() {
    System.out.println(logVersion());

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setDateFormat("yyyy-MM-dd hh:mm:ss");
    gson = gsonBuilder.create();//w w w . ja  v a2 s  .  co  m
    client = new DefaultHttpClient();
}

From source file:com.sire.web.BancosBean.java

private void obtenerBancos() {
    BanBancosFacadeREST banBancosFacadeREST = new BanBancosFacadeREST();
    String banBancosString = banBancosFacadeREST.findAll_JSON(String.class);
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
    bancos = gson.fromJson(banBancosString, new TypeToken<List<BanBancos>>() {
    }.getType());//from   ww  w .  j a va 2  s  . c  o  m
}

From source file:com.sire.web.BodegasBean.java

public List<InvBodegaArt> getInvBodegaArts() {
    InvMovimientoDtll invMovimientoDtll = articulosBean.getInvMovimientoDtllSeleccionado();
    Logger.getLogger(BodegasBean.class.getName()).info("Invocando getInvBodegaArts");
    if (invMovimientoDtll != null) {
        int codArticulo = invMovimientoDtll.getInvBodegaArt().getInvBodegaArtPK().getCodArticulo();
        Logger.getLogger(BodegasBean.class.getName()).log(Level.INFO, "codArticulo: {0}",
                String.valueOf(codArticulo));
        InvBodegaArtFacadeREST invBodegaArtFacadeREST = new InvBodegaArtFacadeREST();
        String result = invBodegaArtFacadeREST.findByCodArticulo_JSON(String.class,
                String.valueOf(codArticulo));
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.setDateFormat("yyyy-MM-dd").create();
        invBodegaArts = gson.fromJson(result, new TypeToken<List<InvBodegaArt>>() {
        }.getType());//from   ww  w  .  j a v  a  2s. c o m
    }
    return invBodegaArts;
}

From source file:com.sire.web.CajFacturaEnviadaBean.java

public CajFacturaEnviadaBean() {
    GsonBuilder builder = new GsonBuilder();
    gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
    pryProyectoFacadeREST = new PryProyectoFacadeREST();
    cajRubroFacadeREST = new CajRubroFacadeREST();
    cajFacturaEnviadaFacadeREST = new CajFacturaEnviadaFacadeREST();
    prySupervisorUsuarioFacadeREST = new PrySupervisorUsuarioFacadeREST();
    CajFacturaEnviadaPK cajFacturaEnviadaPK = new CajFacturaEnviadaPK();
    this.cajFacturaEnviada = new CajFacturaEnviada();
    this.cajFacturaEnviada.setCajFacturaEnviadaPK(cajFacturaEnviadaPK);
}

From source file:com.sire.web.CxcDocCobrarBean.java

public CxcDocCobrarBean() {
    facParametrosFacadeREST = new FacParametrosFacadeREST();
    GsonBuilder builder = new GsonBuilder();
    gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();

    cxcCheques = new ArrayList<>();
    loadBanCtaCtes();//from www  .  j  av a  2s  .c  o  m
}

From source file:com.sire.web.EmpresasBean.java

public EmpresasBean() {
    this.gnrEmpresaFacadeREST = new GnrEmpresaFacadeREST();
    GsonBuilder builder = new GsonBuilder();
    gson = builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
}

From source file:com.sire.web.InventariosBean.java

public List<InvInventario> getInvInventarios() {
    InvMovimientoDtll invMovimientoDtll = articulosBean.getInvMovimientoDtllSeleccionado();

    if (invMovimientoDtll != null) {
        String codBod = invMovimientoDtll.getInvBodegaArt().getInvBodegaArtPK().getCodBodega();
        if (codBod != null) {
            int codBodega = Integer.valueOf(codBod);

            InvInventarioFacadeREST invInventarioFacadeREST = new InvInventarioFacadeREST();
            String result = invInventarioFacadeREST.findByCodBodega(String.class, String.valueOf(codBodega));
            GsonBuilder builder = new GsonBuilder();
            Gson gson = builder.setDateFormat("yyyy-MM-dd").create();
            invInventarios = gson.fromJson(result, new TypeToken<List<InvInventario>>() {
            }.getType());/*from  w ww. ja  va 2s . c o  m*/

            LOGGER.info(
                    "codBodega: " + String.valueOf(codBodega) + ", # Inventarios: " + invInventarios.size());
        }
    }
    return invInventarios;
}

From source file:com.sire.web.InventariosBean.java

public List<InvUnidadAlternativa> getInvUnidadAlternativas() {
    InvMovimientoDtll invMovimientoDtll = articulosBean.getInvMovimientoDtllSeleccionado();

    if (invMovimientoDtll != null) {
        int codArticulo = invMovimientoDtll.getInvBodegaArt().getInvBodegaArtPK().getCodArticulo();
        InvUnidadAlternativaFacadeREST invUnidadAlternativaFacadeREST = new InvUnidadAlternativaFacadeREST();
        String result = invUnidadAlternativaFacadeREST.findByCodArticulo(String.class,
                String.valueOf(codArticulo));
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.setDateFormat("yyyy-MM-dd").create();
        invUnidadAlternativas = gson.fromJson(result, new TypeToken<List<InvUnidadAlternativa>>() {
        }.getType());/*from ww w. j  ava  2s  .c om*/

        LOGGER.info(
                "codArticulo: " + codArticulo + ", # InvUnidadAlternativas: " + invUnidadAlternativas.size());
    }
    return invUnidadAlternativas;
}