////////////////////////////////////////////////
//CANONICAL EXAMPLE:
Set<ObjectId> communities = new HashSet<ObjectId>();
communities.add(new ObjectId("a0000000000000000000000a"));
rp1.setData(sp, new SourcePojoApiMap(null, communities, communities));
String sRPSingleObject = rp1.toApi();
System.out.println("RPa=" + sRPSingleObject); // ("chris" removed, toApi handles RepsonsePojo specially)
////////////////////////////////////////////////
System.out.println("RPb=" + ResponsePojo.toApi(rp1, rp1.getMapper())); // ("chris" removed because of mapper)
System.out.println("RPc=" + ResponsePojo.toApi(rp1)); // ("chris" removed, toApi handles RepsonsePojo specially)
//API: Get an non-API object
String sJson = "{ 'url':'http://test2', 'isApproved': false, 'harvestBadSource': true, 'created': 'Feb 14, 2013 9:24:34 PM' } ";
//sp = BaseApiPojo.mapFromApi(sJson, SourcePojo.class, null);
// Equivalent to:
SourcePojo sp2 = ApiManager.mapFromApi(sJson, SourcePojo.class, new SourcePojoApiMap(null, new HashSet<ObjectId>(), new HashSet<ObjectId>()));
System.out.println("RPd="+new Gson().toJson(sp2)); // "alex" and "chris" both removed
//API: add a list to the response Pojo
List<SourcePojo> list = Arrays.asList(sp, sp2);
//CHECK THIS DOESN'T COMPILE
//rp1.setData(list); // (Not allowed SourcePojo isn't a BaseApiPojo)
sp2.addToCommunityIds(new ObjectId("a0000000000000000000000a")); // (alex will be allowed again)
rp1.setData(list, new SourcePojoApiMap(null, communities, communities));
String sRPList = rp1.toApi();
sp2.setCommunityIds(null);
//API: And get as a list
String listJson = BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData());
System.out.println("RP=" + listJson); // include "alex" and "chris" - no mapping applied
////////////////////////////////////////////////
//CANONICAL EXAMPLE:
list = ApiManager.mapListFromApi(listJson, SourcePojo.listType(), null);
////////////////////////////////////////////////
System.out.println("SPL=" + BaseApiPojo.getDefaultBuilder().create().toJson(list)); // both "alex" and "chris", no mapping "from API"
Set<SourcePojo> set = ApiManager.mapListFromApi(BaseApiPojo.getDefaultBuilder().create().toJson(rp1.getData()), new TypeToken<Set<SourcePojo>>(){}, null);
System.out.println("SPS=" + BaseApiPojo.getDefaultBuilder().create().toJson(set)); // both "alex" and "chris", no mapping "from API"
// API: finally transform to a JSON list (applies mapping)
try {
System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, null, communities)));
// should fail because one of the communities does not have
System.out.println("**********FAILED SHOULD HAVE THROWN SECURITY EXCEPTION");
}
catch (RuntimeException e) {
// Add "communities" to object with missing val
for (SourcePojo spSet: set) {
if (null == spSet.getCommunityIds()) {
spSet.setCommunityIds(communities);
}
}
// Try again:
System.out.println("SPJ=" + ApiManager.mapListToApi(set, new TypeToken<Set<SourcePojo>>(){}, new SourcePojoApiMap(null, communities, communities)));
// (just has "alex")
}
// And now in the other direction, ie deserializing....
ResponsePojo rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class, SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED RP_SRC=" + rpRecreated.toApi());
System.out.println("RECREATED SRC(RP_SRC)=" + ((BasicDBObject)((SourcePojo)rpRecreated.getData()).toDb()).toString());
rpRecreated = ResponsePojo.listFromApi(sRPList, ResponsePojo.class, SourcePojo.listType(), new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED RP_LSRC=" + rpRecreated.toApi());
rpRecreated = ResponsePojo.fromApi(sRPSingleObject, ResponsePojo.class);
System.out.println("RECREATED RAW(RP_SRC)=" + ((JsonElement)rpRecreated.getData()));
sp = ApiManager.mapFromApi((JsonElement)rpRecreated.getData(), SourcePojo.class, new SourcePojoApiMap(null, communities, communities));
System.out.println("RECREATED SRC(RAW(RP_SRC))=" + ((JsonElement)rpRecreated.getData()));
// Real-life source pojo testing:
// No longer needed - if commented in will fail because communities not assigned in mapTo/FromApi
// BasicDBObject srcQuery = new BasicDBObject("useExtractor", "ModusOperandi");