Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created October 25, 2013 19:53
Show Gist options
  • Save cfalzone/7160864 to your computer and use it in GitHub Desktop.
Save cfalzone/7160864 to your computer and use it in GitHub Desktop.
// Deal with relationships
Map<Relationship, List<Contentlet>> cRels = new HashMap<Relationship, List<Contentlet>>();
if(rels.size() > 0) {
for(Relationship rel : rels) {
String relName = rel.getParentRelationName()+"-"+rel.getChildRelationName();
String relQuery = "+structureName:"+rel.getParentStructure().getVelocityVarName()+
" +"+relName+":"+oldIdentifier+" +live:true +deleted:false";
String relurl = OLDHOST+"/JSONContent/?type=json&limit=1000&offset=0&q=";
Logger.debug(this, "the query is: "+relQuery);
try {
relurl = relurl + URLEncoder.encode(relQuery, "UTF-8");
} catch (Exception e) {
failed.put(stVarName, "Unable to encode the url to fetch the relationships: "+e.getMessage());
throw new Exception(e);
}
Logger.debug(this, "The relurl is "+relurl);
HttpGet relmethod = new HttpGet(relurl);
HttpResponse relresponse;
try {
relresponse = client.execute(relmethod);
} catch (Exception e) {
failed.put(stVarName, "Unable to fetch the relationships data from ("+relName+") from ("+relurl+"): "+e.getMessage());
throw new Exception(e);
}
InputStream relinStream = relresponse.getEntity().getContent();
StringWriter relwriter = new StringWriter();
IOUtils.copy(relinStream, relwriter, "UTF-8");
String reldataString = relwriter.toString();
JSONObject relData;
JSONArray relCons;
try {
relData = new JSONObject(reldataString);
relCons = relData.getJSONArray("contentlets");
} catch (Exception e) {
failed.put(stVarName, "Unable to get the relationships for ("+relName+") from the json: "+e.getMessage());
throw new Exception(e);
}
Logger.debug(this, "The Related Cons: "+relCons.toString(2));
ArrayList<Contentlet> parents = new ArrayList<Contentlet>();
for(int j=0; j<relCons.length(); j++) {
try {
JSONObject relCon = relCons.getJSONObject(j);
String parentId = identifierMap.get(relCon.getString("identifier"));
Contentlet parentCon = conAPI.findContentletByIdentifier(parentId, true, languageId, userAPI.getSystemUser(), false);
parents.add(parentCon);
} catch (Exception e) {
failed.put(stVarName, "Unable to get a parent for the relationship ("+relName+") from the json ("+j+"): "+e.getMessage());
throw new Exception(e);
}
}
cRels.put(rel, parents);
}
}
// later on
contentlet = conAPI.checkin(contentlet, cRels, cats, new ArrayList<Permission>(), userAPI.getSystemUser(), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment