use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.
the class XmlJsonConversion method addGroupToXml.
// XXX could refactor this and addRepeatToXML as this is what happens in the middle of addRepeatToXML
private static void addGroupToXml(Element root, Group group, JSONObject in, String section, String permlevel) throws JSONException, UnderlyingStorageException {
if (group.isServicesReadOnly()) {
// Omit fields that are read-only in the services layer.
log.debug("Omitting services-readonly group: " + group.getID());
return;
}
Element element = root;
if (group.hasServicesParent()) {
for (String path : group.getServicesParent()) {
if (path != null) {
element = element.addElement(path);
}
}
}
Object value = null;
value = in.opt(group.getID());
if (value == null || ((value instanceof String) && StringUtils.isBlank((String) value)))
return;
if (value instanceof String) {
// And sometimes the services ahead of the UI
JSONObject next = new JSONObject();
next.put(group.getID(), value);
value = next;
}
if (!(value instanceof JSONObject))
throw new UnderlyingStorageException("Bad JSON in repeated field: must be string or object for group field not an array - that would a repeat field");
JSONObject object = (JSONObject) value;
Element groupelement = element;
groupelement = element.addElement(group.getServicesTag());
Object one_value = object;
if (one_value == null || ((one_value instanceof String) && StringUtils.isBlank((String) one_value))) {
// do nothing
} else if (one_value instanceof String) {
// Assume it's just the first entry (useful if there's only one)
FieldSet[] fs = group.getChildren(permlevel);
if (fs.length < 1) {
// do nothing
} else {
JSONObject d1 = new JSONObject();
d1.put(fs[0].getID(), one_value);
addFieldSetToXml(groupelement, fs[0], d1, section, permlevel);
}
} else if (one_value instanceof JSONObject) {
List<FieldSet> children = getChildrenWithGroupFields(group, permlevel);
for (FieldSet fs : children) addFieldSetToXml(groupelement, fs, (JSONObject) one_value, section, permlevel);
}
element = groupelement;
}
use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.
the class XmlJsonConversion method addFieldToJson.
private static void addFieldToJson(JSONObject out, Element root, Field field, String operation, JSONObject tempSon, String csid, String ims_url) throws JSONException {
String use_csid = field.useCsid();
if (use_csid != null) {
if (field.useCsidField() != null) {
csid = tempSon.getString(field.useCsidField());
}
out.put(field.getID(), csid_value(csid, field.useCsid(), ims_url));
} else {
Element el = root;
if (field.getUIType().startsWith("groupfield") && field.getUIType().contains("selfrenderer")) {
String[] parts = field.getUIType().split("/");
Record subitems = field.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
addFieldSetToJson(out, el, fs, operation, tempSon, csid, ims_url);
}
return;
}
el = getFieldNodeEl(root, field);
if (el == null) {
return;
}
addExtraToJson(out, el, field, tempSon);
Object val = el.getText();
if (field.getUIType().startsWith("groupfield")) {
String[] parts = field.getUIType().split("/");
Record subitems = field.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
JSONObject temp = new JSONObject();
for (FieldSet fs : subitems.getAllServiceFieldTopLevel(operation, "common")) {
addFieldSetToJson(temp, el, fs, operation, tempSon, csid, ims_url);
}
out.put(field.getID(), temp);
} else if (field.getDataType().equals("boolean")) {
out.put(field.getID(), (Boolean.parseBoolean((String) val) ? true : false));
} else {
out.put(field.getID(), val);
}
tempSon = addtemp(tempSon, field.getID(), val);
}
}
use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.
the class XmlJsonConversion method convertToJson.
public static JSONObject convertToJson(JSONObject out, Record r, Document doc, String operation, String section, String csid, String ims_url) throws JSONException {
Element root = doc.getRootElement();
JSONObject tempSon = new JSONObject();
for (FieldSet f : r.getAllServiceFieldTopLevel(operation, section)) {
addFieldSetToJson(out, root, f, operation, tempSon, csid, ims_url);
}
if (r.hasMerged()) {
for (FieldSet f : r.getAllMergedFields()) {
for (String fm : f.getAllMerge()) {
if (fm != null) {
if (r.hasFieldByOperation(fm, operation)) {
if (tempSon.has(fm)) {
String data = tempSon.getString(fm);
if (data != null && !data.equals("") && !out.has(f.getID())) {
out.put(f.getID(), data);
}
}
}
}
}
}
}
return tempSon;
}
use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.
the class VocabulariesCreateUpdate method store_set.
private void store_set(Storage storage, UIRequest request, String path) throws UIException {
try {
JSONObject data = request.getJSONBody();
String redirectpath = "";
// is this an instance or an item?
if (this.r == null && this.n != null) {
FieldSet displayNameFS = n.getRecord().getDisplayNameField();
String displayNameFieldName = (displayNameFS != null) ? displayNameFS.getID() : null;
boolean quickie = false;
String quickieDisplayName = null;
if (create) {
quickie = (data.has("_view") && data.getString("_view").equals("autocomplete"));
// Check to see if displayName field needs remapping from UI
if (quickie && !"displayName".equals(displayNameFieldName)) {
// Need to map the field for displayName, and put it into a proper structure
JSONObject fields = data.getJSONObject("fields");
quickieDisplayName = fields.getString("displayName");
if (quickieDisplayName != null) {
// displayNames are nested now, so must have a field parent
FieldSet parentTermGroup = (FieldSet) displayNameFS.getParent();
JSONArray parentTermInfoArray = new JSONArray();
JSONObject termInfo = new JSONObject();
termInfo.put(displayNameFieldName, quickieDisplayName);
parentTermInfoArray.put(termInfo);
fields.put(parentTermGroup.getID(), parentTermInfoArray);
fields.remove("displayName");
}
}
}
path = createItem(storage, request, path, data);
data = reader.getJSON(storage, path);
String refid = data.getJSONObject("fields").getString("refid");
data.put("urn", refid);
data.getJSONObject("fields").put("urn", refid);
data.put("csid", data.getJSONObject("fields").getString("csid"));
if (quickie) {
JSONObject newdata = new JSONObject();
newdata.put("urn", refid);
newdata.put("displayName", quickieDisplayName);
data = newdata;
}
redirectpath = n.getWebURL();
}
if (this.r != null && this.n == null) {
path = createInstance(storage, request, path, data);
redirectpath = data.getJSONObject("fields").getString("shortIdentifier");
}
request.sendJSONResponse(data);
request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
if (create)
request.setSecondaryRedirectPath(new String[] { redirectpath, path });
// request.setSecondaryRedirectPath(new String[]{n.getWebURL(),path});
} catch (JSONException x) {
throw new UIException("Failed to parse json: ", x);
} catch (ExistException x) {
throw new UIException("Existence exception: ", x);
} catch (UnimplementedException x) {
throw new UIException("Unimplemented exception: ", x);
} catch (UnderlyingStorageException x) {
UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
request.sendJSONResponse(uiexception.getJSON());
}
}
use of org.collectionspace.chain.csp.schema.FieldSet in project application by collectionspace.
the class UserStorage method deleteJSON.
@Override
public void deleteJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath) throws ExistException, UnimplementedException, UnderlyingStorageException {
String[] parts = filePath.split("/");
if (parts.length > 2) {
for (FieldSet allfs : r.getAllSubRecords("DELETE")) {
Record allr = allfs.usesRecordId();
if (allr.getID().equals(parts[2])) {
filePath = parts[0] + "/" + allr.getServicesURL() + "/" + parts[2];
}
}
}
// String serviceurl = r.getServicesURL() + "/";
deleteJSON(root, creds, cache, filePath, r);
}
Aggregations