Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Created April 19, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfalzone/738c1b50eee0a41cff1d08b435f40209 to your computer and use it in GitHub Desktop.
Save cfalzone/738c1b50eee0a41cff1d08b435f40209 to your computer and use it in GitHub Desktop.
Old 2.0 Code to create a structure in dotCMS
private void createStructure(String stVarName, Host stHost, JSONObject json) throws Exception {
Structure st = new Structure();
st.setDefaultStructure(false);
st.setFixed(false);
st.setStructureType(Structure.STRUCTURE_TYPE_CONTENT);
st.setName(json.getString("name"));
st.setVelocityVarName(stVarName);
st.setHost(stHost.getIdentifier());
try {
StructureFactory.saveStructure(st);
} catch(Exception e) {
failed.put(stVarName, "Unable to save the structure: "+e.getMessage());
throw new Exception(e);
}
StructureCache.removeStructure(st);
StructureCache.addStructure(st);
String stInode = st.getInode();
JSONArray fs = json.getJSONArray("fields");
for(int i = 0; i<fs.length(); i++) {
JSONObject fjson = fs.getJSONObject(i);
String fVarName = fjson.getString("varname");
Field field = new Field();
field.setVelocityVarName(fVarName);
field.setStructureInode(stInode);
field.setFixed(false);
field.setReadOnly(false);
field.setFieldName(fjson.getString("name"));
field.setFieldContentlet(fjson.getString("indexName"));
String type = fjson.getString("type");
field.setFieldType(type);
if(type.equals("radio") || type.equals("checkbox") || type.equals("select")) {
field.setValues(fjson.getString("values").replaceAll(",", "\r\n").replaceAll("~#~", ","));
}
if(type.equals("custom_field")) {
field.setValues(fjson.getString("values"));
}
if(type.equals("category")) {
String catId = catAPI.findByKey(fjson.getString("values"), userAPI.getSystemUser(), true).getCategoryId();
field.setValues(catId);
}
field.setSortOrder(fjson.getInt("sortOrder"));
field.setRequired(fjson.getBoolean("required"));
field.setListed(fjson.getBoolean("listed"));
boolean isIndexed = fjson.getBoolean("indexed");
if(isIndexed) {
field.setSearchable(true);
field.setIndexed(true);
} else {
field.setSearchable(false);
field.setIndexed(false);
}
try {
FieldFactory.saveField(field);
} catch (Exception e) {
failed.put(stVarName, "Unable to save this field ("+fVarName+"): "+e.getMessage());
throw new Exception(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment