Example usage for com.google.gson JsonObject addProperty

List of usage examples for com.google.gson JsonObject addProperty

Introduction

In this page you can find the example usage for com.google.gson JsonObject addProperty.

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:com.adobe.acs.commons.workflow.bulk.execution.impl.servlets.JSONErrorUtil.java

License:Apache License

public static void sendJSONError(SlingHttpServletResponse response, int statusCode, String title,
        String message) throws IOException {

    response.setStatus(statusCode);/*from  w w w  . j  a  va2 s.  c  om*/
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    JsonObject json = new JsonObject();
    json.addProperty("title", title);
    json.addProperty("message", message);
    Gson gson = new Gson();
    gson.toJson(json, response.getWriter());
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.impl.servlets.StatusServlet.java

License:Apache License

@Override
@SuppressWarnings({ "squid:S3776", "squid:S1192" })
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    Config config = request.getResource().adaptTo(Config.class);
    Workspace workspace = config.getWorkspace();

    final JsonObject json = new JsonObject();

    json.addProperty("initialized", workspace.isInitialized());
    json.addProperty("status", workspace.getStatus().name());

    if (workspace.getSubStatus() != null) {
        json.addProperty("subStatus", workspace.getSubStatus().name());
    }/*  www .  ja v  a 2s  .c o  m*/

    json.addProperty("runnerType", config.getRunnerType());
    json.addProperty("queryType", config.getQueryType());
    json.addProperty("queryStatement", config.getQueryStatement());
    json.addProperty("workflowModel", StringUtils.removeEnd(config.getWorkflowModelId(), "/jcr:content/model"));
    json.addProperty("batchSize", config.getBatchSize());
    json.addProperty("autoThrottle", config.isAutoThrottle());

    json.addProperty("purgeWorkflow", config.isPurgeWorkflow());
    json.addProperty("interval", config.getInterval());
    json.addProperty("retryCount", config.getRetryCount());
    json.addProperty("timeout", config.getTimeout());
    json.addProperty("throttle", config.getThrottle());
    json.addProperty("message", workspace.getMessage());

    if (config.isUserEventData()) {
        json.addProperty("userEventData", config.getUserEventData());
    }

    ActionManager actionManager = actionManagerFactory.getActionManager(workspace.getActionManagerName());
    if (actionManager != null && !Status.COMPLETED.equals(workspace.getStatus())) {
        JsonArray failures = new JsonArray();
        json.add("failures", failures);
        // If Complete, then look to JCR for final accounts as ActionManager may be gone
        addActionManagerTrackedCounts(workspace.getActionManagerName(), json);
        for (com.adobe.acs.commons.fam.Failure failure : actionManager.getFailureList()) {
            JsonObject failureJSON = new JsonObject();
            failureJSON.addProperty(Failure.PN_PAYLOAD_PATH, failure.getNodePath());
            failureJSON.addProperty(Failure.PN_FAILED_AT, sdf.format(failure.getTime().getTime()));
            failures.add(failureJSON);
        }
    } else {
        addWorkspaceTrackedCounts(workspace, json);
        JsonArray failures = new JsonArray();
        json.add("failures", failures);
        // Failures
        for (Failure failure : workspace.getFailures()) {
            failures.add(failure.toJSON());
        }
    }

    // Times
    if (workspace.getStartedAt() != null) {
        json.addProperty("startedAt", sdf.format(workspace.getStartedAt().getTime()));
        json.addProperty("timeTakenInMillis",
                (Calendar.getInstance().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
    }

    if (workspace.getStoppedAt() != null) {
        json.addProperty("stoppedAt", sdf.format(workspace.getStoppedAt().getTime()));
        json.addProperty("timeTakenInMillis",
                (workspace.getStoppedAt().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
    }

    if (workspace.getCompletedAt() != null) {
        json.addProperty("completedAt", sdf.format(workspace.getCompletedAt().getTime()));
        json.addProperty("timeTakenInMillis", (workspace.getCompletedAt().getTime().getTime()
                - workspace.getStartedAt().getTime().getTime()));
    }

    if (AEMWorkflowRunnerImpl.class.getName().equals(config.getRunnerType())) {
        JsonArray activePayloads = new JsonArray();
        json.add("activePayloads", activePayloads);
        for (Payload payload : config.getWorkspace().getActivePayloads()) {
            activePayloads.add(payload.toJSON());
        }
    }

    json.add("systemStats", getSystemStats());

    Gson gson = new Gson();
    gson.toJson(json, response.getWriter());
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.impl.servlets.StatusServlet.java

License:Apache License

private void addActionManagerTrackedCounts(String name, JsonObject json) {
    final ActionManager actionManager = actionManagerFactory.getActionManager(name);

    int failureCount = actionManager.getErrorCount();
    int completeCount = actionManager.getSuccessCount();
    int totalCount = actionManager.getAddedCount();
    int remainingCount = actionManager.getRemainingCount();

    json.addProperty("totalCount", totalCount);
    json.addProperty("completeCount", completeCount);
    json.addProperty("remainingCount", remainingCount);
    json.addProperty("failCount", failureCount);
    json.addProperty("percentComplete",
            Math.round(((totalCount - remainingCount) / (totalCount * 1F)) * DECIMAL_TO_PERCENT));
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.impl.servlets.StatusServlet.java

License:Apache License

private void addWorkspaceTrackedCounts(Workspace workspace, JsonObject json) {
    // Counts/*from   w  w w .j  av a  2  s  .  c  o  m*/
    int remainingCount = workspace.getTotalCount() - (workspace.getCompleteCount() + workspace.getFailCount());
    json.addProperty("totalCount", workspace.getTotalCount());
    json.addProperty("completeCount", workspace.getCompleteCount());
    json.addProperty("remainingCount", remainingCount);
    json.addProperty("failCount", workspace.getFailCount());
    json.addProperty("percentComplete",
            Math.round(((workspace.getTotalCount() - remainingCount) / (workspace.getTotalCount() * 1F))
                    * DECIMAL_TO_PERCENT));
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.impl.servlets.StatusServlet.java

License:Apache License

@SuppressWarnings("squid:S1192")
private JsonObject getSystemStats() {
    JsonObject json = new JsonObject();
    try {/* w ww. j a  v a 2s  .c o  m*/
        json.addProperty("cpu", MessageFormat.format("{0,number,#%}", ttrs.getCpuLevel()));
    } catch (InstanceNotFoundException e) {
        log.error("Could not collect CPU stats", e);
        json.addProperty("cpu", -1);
    } catch (ReflectionException e) {
        log.error("Could not collect CPU stats", e);
        json.addProperty("cpu", -1);
    }
    json.addProperty("mem", MessageFormat.format("{0,number,#%}", ttrs.getMemoryUsage()));
    json.addProperty("maxCpu", MessageFormat.format("{0,number,#%}", ttrs.getMaxCpu()));
    json.addProperty("maxMem", MessageFormat.format("{0,number,#%}", ttrs.getMaxHeap()));
    return json;
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.model.Failure.java

License:Apache License

public JsonObject toJSON() {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");

    JsonObject json = new JsonObject();
    json.addProperty(PN_PATH, getPath());
    json.addProperty(PN_PAYLOAD_PATH, getPayloadPath());
    json.addProperty(PN_FAILED_AT, sdf.format(getFailedAt().getTime()));

    return json;// w  w  w . j a v  a 2  s  .  c o  m
}

From source file:com.adobe.acs.commons.workflow.bulk.execution.model.Payload.java

License:Apache License

/** Renditions **/

public JsonObject toJSON() {
    JsonObject json = new JsonObject();
    json.addProperty(PN_STATUS, getStatus().toString());
    json.addProperty(PN_PATH, getPayloadPath());
    return json;/*w  w  w  . j a  va2s .  c  o  m*/
}

From source file:com.adobe.acs.commons.workflow.bulk.removal.impl.servlets.InitServlet.java

License:Apache License

/**
 * Get the JSON data to populate the Workflow Removal form.
 *
 * @param resourceResolver/*  ww  w . j  a v  a  2s .  c o  m*/
 * @return
 * @throws WorkflowException
 */
private JsonObject getFormJSONObject(final ResourceResolver resourceResolver) throws WorkflowException {

    final JsonObject json = new JsonObject();

    final WorkflowSession workflowSession = workflowService
            .getWorkflowSession(resourceResolver.adaptTo(Session.class));

    final WorkflowModel[] workflowModels = workflowSession.getModels();

    JsonArray models = new JsonArray();
    json.add("workflowModels", models);
    for (final WorkflowModel workflowModel : workflowModels) {
        final JsonObject jsonWorkflow = new JsonObject();
        jsonWorkflow.addProperty("title", workflowModel.getTitle());
        jsonWorkflow.addProperty("id", workflowModel.getId());
        models.add(jsonWorkflow);
    }

    Gson gson = new Gson();
    json.add("statuses", gson.toJsonTree(Arrays.asList(WORKFLOW_STATUSES)));

    return json;
}

From source file:com.adobe.acs.commons.workflow.bulk.removal.WorkflowRemovalStatus.java

License:Apache License

public JsonObject getJSON() {
    final JsonObject json = new JsonObject();

    json.addProperty(KEY_RUNNING, this.isRunning());
    json.addProperty(KEY_INITIATED_BY, this.getInitiatedBy());
    json.addProperty(KEY_CHECKED_COUNT, this.getChecked());
    json.addProperty(KEY_REMOVED_COUNT, this.getRemoved());

    if (this.getStartedAt() != null) {
        json.addProperty(KEY_STARTED_AT, this.getStartedAt());
    }//from w  ww .j av  a2  s  .  c o  m

    if (this.getErredAt() != null) {
        json.addProperty(KEY_ERRED_AT, this.getErredAt());
        json.addProperty(KEY_DURATION, getDuration(this.startedAt, this.erredAt));
    } else if (this.getForceQuitAt() != null) {
        json.addProperty(KEY_FORCE_QUIT_AT, this.getForceQuitAt());
        json.addProperty(KEY_DURATION, getDuration(this.startedAt, this.forceQuitAt));
    } else if (this.getCompletedAt() != null) {
        json.addProperty(KEY_COMPLETED_AT, this.getCompletedAt());
        json.addProperty(KEY_DURATION, getDuration(this.startedAt, this.completedAt));
    } else {
        json.addProperty(KEY_DURATION, getDuration(this.startedAt, Calendar.getInstance()));
    }

    return json;
}

From source file:com.adssets.ejb.DataAccess.java

@Override
public String getMarkets() {
    JsonArray jsonArray = new JsonArray();

    List<Market> markets = marketFacade.findAll();
    for (Market market : markets) {
        JsonObject obj = new JsonObject();
        obj.addProperty("id", market.getId());
        obj.addProperty("name", market.getName());
        obj.addProperty("description", market.getDescription());
        jsonArray.add(obj);/*from ww  w .  j  av a2s  . c  o  m*/
    }
    return jsonArray.toString();
}