use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class GenericStorage method refObjViewRetrieveJSON.
/**
* get data needed for list of objects related to a termUsed
* @param storage
* @param creds
* @param cache
* @param path
* @return
* @throws ExistException
* @throws UnderlyingStorageException
* @throws JSONException
* @throws UnimplementedException
*/
public JSONObject refObjViewRetrieveJSON(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String path, JSONObject restrictions, Record vr) throws ExistException, UnderlyingStorageException, JSONException, UnimplementedException {
JSONObject out = new JSONObject();
try {
// map of servicenames of fields to descriptors
Map<String, String> refObj_view_good = new HashMap<String, String>();
// map of csid to service name of field
Map<String, String> refObj_view_map = new HashMap<String, String>();
if (vr.hasRefObjUsed()) {
path = getRestrictedPath(path, restrictions, "kw", "", false, "");
// XXX need a way to append the data needed from the field,
// which we don't know until after we have got the information...
refObj_view_map.put("docType", "docType");
refObj_view_map.put("docId", "docId");
refObj_view_map.put("docName", "docName");
refObj_view_map.put("docNumber", "docNumber");
refObj_view_map.put("sourceField", "sourceField");
refObj_view_map.put("uri", "uri");
refObj_view_map.put("refName", "refName");
refObj_view_good.put("terms_docType", "docType");
refObj_view_good.put("terms_docId", "docId");
refObj_view_good.put("terms_docName", "docName");
refObj_view_good.put("terms_docNumber", "docNumber");
refObj_view_good.put("terms_sourceField", "sourceField");
refObj_view_good.put("terms_refName", "refName");
// XXX this might be the wrong record to pass to checkf or hard/soft delet listing
JSONObject data = getRepeatableListView(storage, creds, cache, path, "authority-ref-doc-list/authority-ref-doc-item", "uri", true, vr, refObj_view_map);
JSONArray recs = data.getJSONArray("listItems");
if (data.has("pagination")) {
out.put("pagination", data.getJSONObject("pagination"));
}
JSONArray items = new JSONArray();
// String[] filepaths = (String[]) data.get("listItems");
for (int i = 0; i < recs.length(); ++i) {
String uri = recs.getJSONObject(i).getString("csid");
// recs.getJSONObject(i).getString("csid");
String filePath = uri;
if (filePath != null && filePath.startsWith("/"))
filePath = filePath.substring(1);
String[] parts = filePath.split("/");
String recordurl = parts[0];
Record thisr = vr.getSpec().getRecordByServicesUrl(recordurl);
// Set up the glean maps required for this record. We need to reset these each time
// through the loop, because every record could be a different type.
Map<String, String> thisr_view_good = new HashMap<String, String>(refObj_view_good);
Map<String, String> thisr_view_map = new HashMap<String, String>(refObj_view_map);
Set<String> thisr_xxx_view_deurn = new HashSet<String>();
Set<String> thisr_view_search_optional = new HashSet<String>();
Map<String, List<String>> thisr_view_merge = new HashMap<String, List<String>>();
Map<String, List<String>> thisr_view_useCsid = new HashMap<String, List<String>>();
initializeGlean(thisr, thisr_view_good, thisr_view_map, thisr_xxx_view_deurn, thisr_view_search_optional, thisr_view_merge, thisr_view_useCsid);
String csid = parts[parts.length - 1];
JSONObject dataitem = miniViewRetrieveJSON(cache, creds, csid, "terms", uri, thisr, thisr_view_good, thisr_xxx_view_deurn, thisr_view_search_optional, thisr_view_merge, thisr_view_useCsid);
dataitem.getJSONObject("summarylist").put("uri", filePath);
String key = recs.getJSONObject(i).getString("sourceField");
dataitem.getJSONObject("summarylist").put("sourceField", key);
String fieldName = "unknown";
String fieldSelector = "unknown";
if (key.contains(":")) {
fieldName = key.split(":")[1];
// FIXME: We might remove the following if CSPACE-2909's fix makes this moot - ADR 2012-07-19
while (thisr.getFieldFullList(fieldName) instanceof Repeat || thisr.getFieldFullList(fieldName) instanceof Group) {
fieldName = ((Repeat) thisr.getFieldFullList(fieldName)).getChildren("GET")[0].getID();
}
Field fieldinstance = (Field) thisr.getFieldFullList(fieldName);
fieldSelector = fieldinstance.getSelector();
}
dataitem.put("csid", csid);
dataitem.put("sourceFieldselector", fieldSelector);
dataitem.put("sourceFieldName", fieldName);
dataitem.put("sourceFieldType", dataitem.getJSONObject("summarylist").getString("docType"));
dataitem.put("sourceFieldType", dataitem.getJSONObject("summarylist").getString("docType"));
// items.put(csid+":"+key,dataitem);
items.put(dataitem);
}
out.put("items", items);
}
return out;
} catch (ConnectionException e) {
log.error("failed to retrieve refObjs for " + path);
JSONObject dataitem = new JSONObject();
dataitem.put("csid", "");
dataitem.put("sourceFieldselector", "Functionality Failed");
dataitem.put("sourceFieldName", "Functionality Failed");
dataitem.put("sourceFieldType", "Functionality Failed");
dataitem.put("message", e.getMessage());
out.put("Functionality Failed", dataitem);
// return out;
throw new UnderlyingStorageException("Connection problem" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
} catch (UnsupportedEncodingException uae) {
log.error("failed to retrieve refObjs for " + path);
JSONObject dataitem = new JSONObject();
dataitem.put("message", uae.getMessage());
out.put("Functionality Failed", dataitem);
throw new UnderlyingStorageException("Problem building query" + uae.getLocalizedMessage(), uae);
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class AuthoritiesVocabulariesInitialize method list_vocab.
private JSONObject list_vocab(JSONObject shortIdentifiers, Instance n, Storage storage, String param, Integer pageSize, Integer pageNum, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
JSONObject restriction = new JSONObject();
if (param != null) {
restriction.put(n.getRecord().getDisplayNameField().getID(), param);
}
if (pageNum != null) {
restriction.put("pageNum", pageNum);
}
if (pageSize != null) {
restriction.put("pageSize", pageSize);
}
// CSPACE-6371: When fetching existing vocabulary terms, include soft-deleted ones, so that terms
// deleted through the UI are not re-added.
restriction.put("deleted", true);
String url = thisr.getID() + "/" + n.getTitleRef();
JSONObject data = null;
try {
data = storage.getPathsJSON(url, restriction);
} catch (UnderlyingStorageException x) {
JSONObject fields = new JSONObject("{'displayName':'" + n.getTitle() + "','shortIdentifier':'" + n.getWebURL() + "'}");
if (thisr.getFieldFullList("termStatus") instanceof Field) {
fields.put("termStatus", ((Field) thisr.getFieldFullList("termStatus")).getOptionDefault());
}
String base = thisr.getID();
storage.autocreateJSON(base, fields, null);
data = storage.getPathsJSON(url, restriction);
}
String[] results = (String[]) data.get("listItems");
/* Get a view of each */
for (String result : results) {
// change csid into shortIdentifier
JSONObject termData = getTermData(storage, thisr.getID(), n.getTitleRef(), result);
shortIdentifiers.put(termData.getString("shortIdentifier"), result);
}
JSONObject alldata = new JSONObject();
alldata.put("shortIdentifiers", shortIdentifiers);
alldata.put("pagination", data.getJSONObject("pagination"));
return alldata;
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class AuthoritiesVocabulariesInitialize method fillVocab.
public void fillVocab(Storage storage, Record thisr, Instance instance, StringBuffer tty, Option[] allOpts, Boolean appendit) throws UIException, ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
//
if (allOpts != null && allOpts.length > 0) {
// get list from Service layer
JSONObject results = new JSONObject();
Integer pageNum = 0;
// Setting the pageSize to 0 means the Services layer will return ALL the terms in one request.
Integer pageSize = 0;
// The "paging" code here is broken. It's made some false assumptions about the order in which terms are returned from the Services layer.
// In short, setting the pageSize to 0 is a workaround to the bugs in the paging code.
JSONObject fulldata = list_vocab(results, instance, storage, null, pageSize, pageNum, thisr);
while (!fulldata.isNull("pagination")) {
Integer total = fulldata.getJSONObject("pagination").getInt("totalItems");
pageSize = fulldata.getJSONObject("pagination").getInt("pageSize");
Integer itemsInPage = fulldata.getJSONObject("pagination").getInt("itemsInPage");
pageNum = fulldata.getJSONObject("pagination").getInt("pageNum");
results = fulldata.getJSONObject("shortIdentifiers");
pageNum++;
// are there more results
if (total > (pageSize * (pageNum))) {
fulldata = list_vocab(results, instance, storage, null, pageSize, pageNum, thisr);
} else {
break;
}
}
// compare
results = fulldata.getJSONObject("shortIdentifiers");
for (Option opt : allOpts) {
String name = opt.getName();
String shortIdentifier = opt.getID();
if (shortIdentifier == null || shortIdentifier.equals("")) {
shortIdentifier = name.trim().replaceAll("\\W", "").toLowerCase();
}
if (!results.has(shortIdentifier)) {
// create it if term is not already present
JSONObject data = new JSONObject();
data.put("displayName", name);
data.put("description", opt.getDesc());
data.put("shortIdentifier", shortIdentifier);
if (thisr.getFieldFullList("termStatus") instanceof Field) {
data.put("termStatus", ((Field) thisr.getFieldFullList("termStatus")).getOptionDefault());
}
String url = thisr.getID() + "/" + instance.getTitleRef();
// assume we're going to fail and an exception will be thrown
failedPosts++;
try {
storage.autocreateJSON(url, data, null);
// we succeeded so we can remove our assumed failure
failedPosts--;
if (tty != null) {
String msg = String.format("Added term %s:'%s' with short ID='%s'.", instance.getTitleRef(), name, shortIdentifier);
tty.append(msg + '\n');
log.info(msg);
}
} catch (UnderlyingStorageException e) {
if (e.getStatus() == HttpStatus.SC_CONFLICT) {
log.error("Terms must have unique short identifiers. The CollectionSpace administrator needs to change the non-unique short identifier and restart CollectionSpace.");
String msg = String.format("CollectionSpace attempted to create the term %s:'%s' using a non-unique short identifier of '%s'.", instance.getTitleRef(), name, shortIdentifier);
log.error(msg);
log.error(String.format("Failed JSON payload:%s", data.toString()));
} else {
throw e;
}
}
} else {
// remove from results so can delete everything else if
// necessary in next stage
// though has issues with duplicates
results.remove(shortIdentifier);
String msg = String.format("Tried to create term %s:'%s' with short ID='%s', but a term with that short ID already exists.", instance.getTitleRef(), name, shortIdentifier);
log.debug(msg);
}
}
if (!appendit) {
// delete everything that is not in options
Iterator<String> rit = results.keys();
while (rit.hasNext()) {
String key = rit.next();
String csid = results.getString(key);
// assume we're going to fail and an exception will be thrown
failedPosts++;
storage.deleteJSON(thisr.getID() + "/" + instance.getTitleRef() + "/" + csid);
// we succeeded so remove our assumed failure
failedPosts--;
if (tty != null) {
log.info("Deleting term " + key);
tty.append("Deleting term " + key + '\n');
}
}
}
}
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class AuthoritiesVocabulariesSearchList method generateMiniRecord.
private JSONObject generateMiniRecord(Storage storage, String auth_type, String inst_type, String csid) throws JSONException {
JSONObject out = new JSONObject();
try {
StringBuilder sb = new StringBuilder();
sb.append(auth_type);
sb.append("/");
sb.append(inst_type);
sb.append("/");
sb.append(csid);
sb.append((this.search) ? "/view/search" : "/view/list");
String path = sb.toString();
out = storage.retrieveJSON(path, new JSONObject());
out.put("csid", csid);
// Record type should be set properly from list results.
// out.put("recordtype",inst_type);
} catch (ExistException e) {
out.put("csid", csid);
out.put("isError", true);
JSONObject msg = new JSONObject();
msg.put("severity", "error");
msg.put("message", "Exist Exception:" + e.getMessage());
JSONArray msgs = new JSONArray();
msgs.put(msg);
out.put("messages", msgs);
} catch (UnimplementedException e) {
out.put("csid", csid);
out.put("isError", true);
JSONObject msg = new JSONObject();
msg.put("severity", "error");
msg.put("message", "Unimplemented Exception:" + e.getMessage());
JSONArray msgs = new JSONArray();
msgs.put(msg);
out.put("messages", msgs);
} catch (UnderlyingStorageException e) {
out.put("csid", csid);
out.put("isError", true);
JSONObject msg = new JSONObject();
msg.put("severity", "error");
msg.put("message", "UnderlyingStorage Exception:" + e.getMessage());
JSONArray msgs = new JSONArray();
msgs.put(msg);
out.put("messages", msgs);
}
return out;
}
use of org.collectionspace.csp.api.persistence.UnderlyingStorageException in project application by collectionspace.
the class VocabulariesRead method createRelations.
private JSONObject createRelations(Storage storage, String csid) throws ExistException, UnimplementedException, UnderlyingStorageException, JSONException {
JSONObject recordtypes = new JSONObject();
JSONObject restrictions = new JSONObject();
restrictions.put("src", base + "/" + csid);
// XXX needs pagination support CSPACE-1819
JSONObject data = storage.getPathsJSON("relations/main", restrictions);
String[] relations = (String[]) data.get("listItems");
for (String r : relations) {
try {
JSONObject relateitem = generateRelationEntry(storage, r);
String type = relateitem.getString("recordtype");
if (!recordtypes.has(type)) {
recordtypes.put(type, new JSONArray());
}
recordtypes.getJSONArray(type).put(relateitem);
} catch (Exception e) {
// Never mind.
}
}
return recordtypes;
}
Aggregations