Example usage for com.google.gson JsonObject JsonObject

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

Introduction

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

Prototype

JsonObject

Source Link

Usage

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private <E extends FCAAbstract> JsonElement abstracts2JSon(Class<E> type) {
    BlockingDeque<E> objects = Database.getInstance().getAll(type);
    JsonObject block = new JsonObject();
    for (E o : objects) {
        JsonObject jso = prepare(o);//from  www.  j a  v a  2 s . c  om
        jso.addProperty(CID, o.getCreationId());
        LinkedList<Long> loIds = new LinkedList<Long>();
        LinkedList<Long> loByLearnerIds = new LinkedList<Long>();
        for (LearningObject lo : o.getLearningObjects()) {
            loIds.add(lo.getId());
        }
        for (LearningObject lo : o.getLearningObjectsByLearners()) {
            loByLearnerIds.add(lo.getId());
        }
        jso.add(SECTION_LO, gson.toJsonTree(loIds, new TypeToken<List<Long>>() {
        }.getType()));
        jso.add(SECTION_LO_L, gson.toJsonTree(loByLearnerIds, new TypeToken<List<Long>>() {
        }.getType()));
        block.add(Long.toString(o.getId()), jso);
    }
    return block;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private JsonElement metadata2JSON() {
    BlockingDeque<FCAItemMetadata> lobjects = Database.getInstance().getAll(FCAItemMetadata.class);
    JsonObject block = new JsonObject();
    for (FCAItemMetadata o : lobjects) {
        JsonObject jso = prepare(o);/*from w w  w .j a  v a  2 s  .  c om*/
        jso.addProperty(O_ID, o.getItemID());
        LinkedList<Long> loIds = new LinkedList<Long>();
        for (LearningObject lo : o.getLearningObjects()) {
            if (lo != null)
                loIds.add(lo.getId());
        }
        LinkedList<Long> loByLernerIds = new LinkedList<Long>();
        for (LearningObject lo : o.getLearningObjectByLearner()) {
            if (lo != null)
                loByLernerIds.add(lo.getId());
        }
        jso.add(SECTION_LO, gson.toJsonTree(loIds, new TypeToken<List<Long>>() {
        }.getType()));
        jso.add(SECTION_LO_L, gson.toJsonTree(loByLernerIds, new TypeToken<List<Long>>() {
        }.getType()));
        block.add(Long.toString(o.getId()), jso);
    }
    return block;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private JsonElement domains2JSON() {
    BlockingDeque<Domain> domains = Database.getInstance().getAll(Domain.class);
    JsonArray jsD = new JsonArray();
    for (Domain d : domains) {
        IncidenceMatrix mat = d.getMapping();
        Map<FCAObject, Set<FCAAttribute>> mapping = mat.getObjects();
        JsonObject jso = prepare(d);/*from   w w w.j  av  a 2 s  .  c o  m*/
        JsonObject jsM = new JsonObject();
        for (long mId : mat.getItemMetadata().keySet()) {
            jsM.addProperty(Long.toString(mId), mat.getItemMetadata().get(mId).getId());
        }
        jso.add(SECTION_M, jsM);
        List<Long> ownerIDs = new LinkedList<Long>();
        for (User o : d.getOwners()) {
            ownerIDs.add(o.getId());
        }
        jso.add(OWNER, gson.toJsonTree(ownerIDs, new TypeToken<List<Long>>() {
        }.getType()));
        jso.addProperty(GLOBAL, d.isGlobal());
        jso.addProperty(APPROVED, d.isApproved());
        JsonObject jsMapping = new JsonObject();
        for (FCAObject o : mapping.keySet()) {
            LinkedList<Long> aIds = new LinkedList<Long>();
            for (FCAAttribute a : mapping.get(o)) {
                aIds.add(a.getId());
            }
            jsMapping.add(Long.toString(o.getId()), gson.toJsonTree(aIds, new TypeToken<List<Long>>() {
            }.getType()));
        }
        jso.add(MAPPING, jsMapping);
        jsD.add(jso);
    }
    return jsD;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private String export() {
    JsonObject output = new JsonObject();

    output.add(SECTION_U, users2JSON());

    output.add(SECTION_LO, learningObjects2JSON());

    output.add(SECTION_O, objects2JSON());

    output.add(SECTION_A, attributes2JSON());

    output.add(SECTION_M, metadata2JSON());

    output.add(SECTION_D, domains2JSON());

    output.add(SECTION_C, courses2JSON());

    return output.toString();
}

From source file:at.yawk.votifier.VotifierKeyPair.java

License:Mozilla Public License

public JsonObject toJson() {
    return toJson(new JsonObject());
}

From source file:au.edu.apsr.pids.to.Handle.java

License:Apache License

/** 
 * Create a Handle object object using the provided Identifier and
 * handle values/*from w  ww.  jav a2 s . c o  m*/
 * 
 * @return Handle
 *           the Handle object
 * @param identifier
 *           the agent Identifier object
 * @param hv
 *           an array of HandleValue objects to be added to the handle
 * @throws DAOException
 * @throws HandleException
 * @throws IOException 
 */
public static Handle create(Identifier identifier, HandleValue[] hv)
        throws DAOException, HandleException, IOException {
    Handle handleObject = new Handle();

    HandleConfig hc = HandleConfig.getHandleConfig();
    handleObject.setHandle(hc.getPrefix() + '/' + Handle.getNextSuffix());

    AdminRecord admin = handleObject.createAdminRecord(Constants.NA_HANDLE_PREFIX + hc.getPrefix(),
            Constants.ADMIN_GROUP_IDX);

    HandleValue values[] = new HandleValue[hv.length + 2];

    // load the passed values into the value array
    int i;

    for (i = 0; i < hv.length; i++) {
        values[i] = hv[i];
    }

    // add the admin values
    values[i] = new HandleValue();
    values[i].setIndex(Constants.ADMIN_IDX);
    values[i].setType(Common.STD_TYPE_HSADMIN);
    values[i].setData(Encoder.encodeAdminRecord(admin));
    values[i].setTTL(Constants.DEFAULT_TTL);

    values[i + 1] = new HandleValue();
    values[i + 1].setIndex(Constants.AGENT_IDX);
    values[i + 1].setType(Constants.XT_AGENTID);
    values[i + 1].setData(Util.encodeString(identifier.getHandle()));
    values[i + 1].setTTL(Constants.DEFAULT_TTL);

    AbstractResponse response = handleObject.createHandle(values);

    if (response.responseCode == AbstractMessage.RC_SUCCESS) {
        JsonObject obj = new JsonObject();
        obj.addProperty("status", "success");
        obj.addProperty("handle", handleObject.getHandle());
        obj.addProperty("identifier", identifier.getIdentifier());
        obj.addProperty("authDomain", identifier.getAuthDomain());
        obj.addProperty("appId", identifier.getAppid());
        String jsonStr = new Gson().toJson(obj);
        log.info(jsonStr);
        return handleObject;
    } else {
        log.info("Failed to create handle: " + response);
        return null;
    }
}

From source file:au.edu.apsr.pids.to.Handle.java

License:Apache License

/** 
 * Create an agent admin Handle object object using the provided identifier
 * string and authentication domain string
 * /*from w ww . java  2  s.  c  o m*/
 * @return Handle
 *           the Handle object
 * @param identifier
 *           the agent identifier string
 * @param authDomain
 *           the agent authentication domain string
 * @throws DAOException
 * @throws HandleException
 */
public static Handle createAdmin(String identifier, String authDomain, String appId)
        throws DAOException, HandleException {
    Handle handleObject = new Handle();

    HandleConfig hc = HandleConfig.getHandleConfig();

    handleObject.setHandle(hc.getPrefix() + '/' + Handle.getNextSuffix());

    AdminRecord admin = handleObject.createAdminRecord(Constants.NA_HANDLE_PREFIX + hc.getPrefix(),
            Constants.ADMIN_GROUP_IDX);

    HandleValue values[] = new HandleValue[4];

    values[0] = new HandleValue();
    values[0].setIndex(Constants.AGENT_DESC_IDX);
    values[0].setType(Constants.XT_TYPE_DESC);
    values[0].setAnyoneCanRead(false);
    values[0].setData(Util.encodeString(identifier + Identifier.separator + authDomain));
    values[0].setTTL(Constants.DEFAULT_TTL);

    values[1] = new HandleValue();
    values[1].setIndex(Constants.ADMIN_IDX);
    values[1].setType(Common.STD_TYPE_HSADMIN);
    values[1].setData(Encoder.encodeAdminRecord(admin));
    values[1].setTTL(Constants.DEFAULT_TTL);

    values[2] = new HandleValue();
    values[2].setIndex(Constants.AGENT_IDX);
    values[2].setType(Constants.XT_AGENTID);
    values[2].setData(Util.encodeString(handleObject.getHandle()));
    values[2].setTTL(Constants.DEFAULT_TTL);

    values[3] = new HandleValue();
    values[3].setIndex(Constants.AGENT_DESC_APPIDX);
    values[3].setType(Constants.XT_APPID);
    values[3].setAnyoneCanRead(false);
    values[3].setData(Util.encodeString(appId));
    values[3].setTTL(Constants.DEFAULT_TTL);

    AbstractResponse response = handleObject.createHandle(values);

    if (response.responseCode == AbstractMessage.RC_SUCCESS) {
        JsonObject obj = new JsonObject();
        obj.addProperty("status", "success");
        obj.addProperty("handle", handleObject.getHandle());
        obj.addProperty("identifier", identifier);
        obj.addProperty("authDomain", authDomain);
        obj.addProperty("appId", appId);
        String jsonStr = new Gson().toJson(obj);
        log.info(jsonStr);

        log.info("Successfully created admin handle: " + handleObject.getHandle());
        return handleObject;
    } else {
        log.info("Failed to create admin handle: " + response);
        return null;
    }
}

From source file:au.edu.unsw.cse.soc.federatedcloud.CloudResourceBaseDeploymentEngine.java

License:Open Source License

@Path("/deploy")
@POST//w ww . j  a  va  2s. c o  m
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response deployCloudResourceDescription(@QueryParam("description_id") String description_id,
        @QueryParam("description_json") @DefaultValue("{}") String description) {
    log.info("Deployment Request Received for id: " + description_id);
    try {
        log.info("Input Resource description: " + URLDecoder.decode(description, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    }

    try {
        CloudResourceDescription desc = DataModelUtil.buildCouldResourceDescriptionFromJSON(description_id);
        CloudResourceBaseDeploymentEngine engine = new CloudResourceBaseDeploymentEngine();
        String response = engine.deployCloudResourceDescription(desc);
        log.info("Deployed Resource:" + desc.toString());

        JsonObject json = new JsonObject();
        json.addProperty("result", response);
        return Response.status(Response.Status.OK).entity(json.toString()).build();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.OK).entity(e.getMessage()).build();
    }
}

From source file:au.edu.unsw.cse.soc.federatedcloud.cloudRessourceBase.RecommenderAPI.java

License:Open Source License

private JsonObject returnDummyObject() {
    JsonObject json = new JsonObject();
    JsonArray descriptionsJsonArray = new JsonArray();

    JsonObject desciption1 = new JsonObject();
    desciption1.addProperty("Id", "1");
    desciption1.addProperty("Name", "SE-Bucket");
    desciption1.addProperty("TargetEnv", "IaaS");
    desciption1.addProperty("Deployer", "au.edu.unsw.cse.soc.federatedcloud.deployers.aws.AWSS3Deployer");
    descriptionsJsonArray.add(desciption1);

    JsonObject desciption2 = new JsonObject();
    desciption2.addProperty("Id", "2");
    desciption2.addProperty("Name", "Rackspace-VM");
    desciption2.addProperty("TargetEnv", "IaaS");
    desciption2.addProperty("Deployer",
            "au.edu.unsw.cse.soc.federatedcloud.deployers.rackspace.RackspaceDeployer");
    descriptionsJsonArray.add(desciption2);

    JsonObject desciption3 = new JsonObject();
    desciption3.addProperty("Id", "3");
    desciption3.addProperty("Name", "GoogleDrive");
    desciption3.addProperty("TargetEnv", "SaaS");
    desciption3.addProperty("Deployer", "au.edu.unsw.cse.soc.federatedcloud.deployers.GoogleDriveDeployer");
    descriptionsJsonArray.add(desciption3);

    json.add("CloudResourceDescriptions", descriptionsJsonArray);

    return json;/*from   www  .  jav a  2s. com*/
}

From source file:au.edu.unsw.cse.soc.federatedcloud.curator.CuratorAPI.java

License:Open Source License

@Path("/putResource")
@POST/*w w  w.  j  a  v  a 2  s.co  m*/
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response putResource(CloudResourceDescription description) {
    persistResource(description);
    log.warn("Composite Resource handler is not yet implemented.");

    JsonObject json = new JsonObject();
    try {
        json.addProperty("cloudResourceDescriptionId", description.getID());
        return Response.status(Response.Status.OK).entity(json.toString()).build();
    } catch (Exception e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
}