Last active
December 23, 2015 06:39
-
-
Save cfalzone/6595248 to your computer and use it in GitHub Desktop.
Creating a contentlet in 2.5 from json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSONObject data = new JSONObject(dataString); | |
JSONArray contentlets = data.getJSONArray("contentlets"); | |
for(int i = 0; i<contentlets.length(); i++) { | |
JSONObject con = contentlets.getJSONObject(i); | |
Contentlet contentlet=new Contentlet(); | |
contentlet.setStructureInode(stInode); | |
contentlet.setHost(stHost.getIdentifier()); | |
long languageId = con.getLong("languageId"); | |
contentlet.setLanguageId(languageId); | |
JSONArray keys = con.names(); | |
for(int j = 0; j<keys.length(); j++) { | |
String key = keys.getString(j); | |
String value = con.getString(key); | |
Field f = fieldMap.get(key); | |
if(f == null) continue; | |
conAPI.setContentletProperty(contentlet, f, value); | |
} | |
try { | |
contentlet = conAPI.checkin(contentlet, userAPI.getSystemUser(), false); | |
conAPI.publish(contentlet, userAPI.getSystemUser(), false); | |
} catch (Exception e) { | |
failed.put(stVarName, "Unable to checkin and publish Contentlet: "+e.getMessage()); | |
throw new Exception(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that if you are actually going to use this you need to do some exception handling on the JSONObject methods. It is using org.json.JSONObject.