Example usage for com.google.gson JsonArray JsonArray

List of usage examples for com.google.gson JsonArray JsonArray

Introduction

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

Prototype

public JsonArray() 

Source Link

Document

Creates an empty JsonArray.

Usage

From source file:com.adobe.acs.commons.wcm.impl.TagWidgetConfigurationServlet.java

License:Apache License

private void writeConfigResource(Resource resource, String propertyName, SlingHttpServletRequest request,
        SlingHttpServletResponse response) throws IOException, ServletException {
    JsonObject widget = createEmptyWidget(propertyName);

    RequestParameterMap map = request.getRequestParameterMap();
    for (Map.Entry<String, RequestParameter[]> entry : map.entrySet()) {
        String key = entry.getKey();
        RequestParameter[] params = entry.getValue();
        if (params != null) {
            if (params.length > 1) {
                JsonArray arr = new JsonArray();
                for (int i = 0; i < params.length; i++) {
                    arr.add(new JsonPrimitive(params[i].getString()));
                }//  w  w  w .j a  va 2  s .  co m
                widget.add(key, arr);
            } else if (params.length == 1) {
                widget.addProperty(key, params[0].getString());
            }
        }
    }

    widget = underlay(widget, resource);

    JsonObject parent = new JsonObject();
    parent.addProperty("xtype", "dialogfieldset");
    parent.addProperty("border", false);
    parent.addProperty("padding", 0);
    parent.addProperty("style", "padding: 0px");
    parent.add("items", widget);
    Gson gson = new Gson();
    gson.toJson(parent, response.getWriter());
}

From source file:com.adobe.acs.commons.wcm.views.impl.WCMViewsServlet.java

License:Apache License

@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {

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

    if (WCMMode.DISABLED.equals(WCMMode.fromRequest(request))) {
        response.setStatus(SlingHttpServletResponse.SC_NOT_FOUND);
        response.getWriter().write("");
        return;//from w w  w.ja  va 2s .c om
    }

    /* Valid WCMMode */

    final PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
    final Page page = pageManager.getContainingPage(request.getResource());

    final WCMViewsResourceVisitor visitor = new WCMViewsResourceVisitor();
    visitor.accept(page.getContentResource());

    final Set<String> viewSet = new HashSet<String>(visitor.getWCMViews());

    // Get the Views provided by the Servlet
    for (final Map.Entry<String, String[]> entry : this.defaultViews.entrySet()) {
        if (StringUtils.startsWith(page.getPath(), entry.getKey())) {
            viewSet.addAll(Arrays.asList(entry.getValue()));
        }
    }

    final List<String> views = new ArrayList<String>(viewSet);

    Collections.sort(views);

    log.debug("Collected WCM Views {} for Page [ {} ]", views, page.getPath());

    final JsonArray jsonArray = new JsonArray();

    for (final String view : views) {
        final JsonObject json = new JsonObject();
        json.addProperty("title", StringUtils.capitalize(view) + " View");
        json.addProperty("value", view);

        jsonArray.add(json);
    }

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

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

License:Apache License

private JsonObject accumulate(JsonObject obj, String key, JsonElement value) {
    if (obj.has(key)) {
        JsonElement existingValue = obj.get(key);
        if (existingValue instanceof JsonArray) {
            ((JsonArray) existingValue).add(value);
        } else {// w  w w. j  a  v  a2  s  . c  o  m
            JsonArray array = new JsonArray();
            array.add(existingValue);
            obj.add(key, array);
        }
    } else {
        JsonArray array = new JsonArray();
        array.add(value);
        obj.add(key, array);
    }
    return obj;
}

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());
    }//  ww w  .  j a 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.removal.impl.servlets.InitServlet.java

License:Apache License

/**
 * Get the JSON data to populate the Workflow Removal form.
 *
 * @param resourceResolver//  w w  w.ja va  2  s .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.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);// w w w .ja v  a  2  s .c o m
    }
    return jsonArray.toString();
}

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

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

    Market market = marketFacade.find(Integer.valueOf(marketId));

    JsonObject obj = new JsonObject();
    obj.addProperty("id", market.getId());
    obj.addProperty("name", market.getName());
    obj.addProperty("description", market.getDescription());
    jsonArray.add(obj);//  ww  w  .  ja v  a 2 s  .  c o m

    return jsonArray.toString();
}

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

@Override
public String createMarket(String obj) {
    JsonObject jsonObject = (new JsonParser()).parse(obj).getAsJsonObject();
    Market market = new Market();
    market.setName(jsonObject.get("name").getAsString());
    market.setDescription(jsonObject.get("description").getAsString());

    marketFacade.create(market);/* w w  w. j  a va 2 s  .  c  om*/

    JsonArray jsonArray = new JsonArray();
    JsonObject jsonObj = new JsonObject();
    jsonObj.addProperty("id", market.getId());
    jsonObj.addProperty("name", market.getName());
    jsonObj.addProperty("description", market.getDescription());
    jsonArray.add(jsonObj);
    return jsonArray.toString();
}

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

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

    //        Feed feed = feedFacade.find(marketId);
    Market market = marketFacade.find(Integer.valueOf(marketId));

    List<Feed> feeds = em.createNamedQuery("Feed.findByMarketId").setParameter("marketId", market)
            .getResultList();//from   w  ww.j a va 2  s  .c om

    for (Feed feed : feeds) {
        JsonObject obj = new JsonObject();
        obj.addProperty("id", feed.getId());
        obj.addProperty("marketId", feed.getIdmarket().getId());
        obj.addProperty("json", feed.getJson());
        jsonArray.add(obj);
    }

    return jsonArray.toString();
}

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

@Override
public String createFeed(String obj) {
    JsonObject jsonObject = (new JsonParser()).parse(obj).getAsJsonObject();

    Market marketId = marketFacade.find(jsonObject.get("marketId").getAsInt());

    //        Feed feedCheck = feedFacade.find(marketId);
    List<Feed> feeds = em.createNamedQuery("Feed.findByMarketId").setParameter("marketId", marketId)
            .getResultList();//from w w  w.  j  a va 2 s.com
    Integer feedCheck = null;

    if (feeds.size() > 0) {
        feedCheck = feeds.get(0).getId();
    }

    Feed feed = new Feed();
    feed.setIdmarket(marketId);
    feed.setJson(jsonObject.get("json").getAsJsonArray().toString());

    JsonArray jsonArray = new JsonArray();
    JsonObject jsonObj = new JsonObject();

    if (feedCheck == null) {
        feedFacade.create(feed);
        jsonObj.addProperty("id", feed.getId());
        jsonObj.addProperty("marketId", feed.getIdmarket().getId());
        jsonObj.addProperty("json", feed.getJson());
        jsonArray.add(jsonObj);
        return jsonArray.toString();
    } else {
        feed.setId(feedCheck);
        feedFacade.edit(feed);
        jsonObj.addProperty("id", feed.getId());
        jsonObj.addProperty("marketId", feed.getIdmarket().getId());
        jsonObj.addProperty("json", feed.getJson());
        jsonArray.add(jsonObj);
        return jsonArray.toString();
    }

}