use of org.collectionspace.chain.csp.schema.Field in project application by collectionspace.
the class GenericStorage method initializeGlean.
/**
* initialise the info for the glean array
*
* view_good keys are prefixed by the values found in the mini tag in the config file
* to help simplify deciding what to find and reduce irrelevant fan out,
* @param r
* @param view_good map of servicenames of fields to descriptors
* @param view_map map of csid to service name of field
* @param xxx_view_deurn
* @param view_search_optional
* @param view_merge map of fields needed if one field to the UI is really multiple fields in the services
* @param view_useCsid
*/
// CSPACE-5988: Allow maps to be passed as parameters, instead of using instance variables.
protected void initializeGlean(Record r, Map<String, String> view_good, Map<String, String> view_map, Set<String> xxx_view_deurn, Set<String> view_search_optional, Map<String, List<String>> view_merge, Map<String, List<String>> view_useCsid) {
// Number
if (r.getMiniNumber() != null) {
view_good.put("number", r.getMiniNumber().getID());
view_map.put(r.getMiniNumber().getServicesTag(), r.getMiniNumber().getID());
if (r.getMiniNumber().hasMergeData()) {
view_merge.put("number", r.getMiniNumber().getAllMerge());
}
if (r.getMiniNumber().hasAutocompleteInstance() || // values that do not start with urn:
r.isType("searchall"))
xxx_view_deurn.add(r.getMiniNumber().getID());
}
// Summary
if (r.getMiniSummary() != null) {
view_good.put("summary", r.getMiniSummary().getID());
view_map.put(r.getMiniSummary().getServicesTag(), r.getMiniSummary().getID());
if (r.getMiniSummary().hasMergeData()) {
view_merge.put("summary", r.getMiniSummary().getAllMerge());
}
if (r.getMiniSummary().hasAutocompleteInstance() || // values that do not start with urn:
r.isType("searchall"))
xxx_view_deurn.add(r.getMiniSummary().getID());
}
// complex summary list
if (r.getAllMiniDataSets().length > 0) {
for (String setName : r.getAllMiniDataSets()) {
String prefix = setName;
if (r.getMiniDataSetByName(prefix).length > 0 && !prefix.equals("")) {
for (FieldSet fs : r.getMiniDataSetByName(prefix)) {
view_map.put(fs.getServicesTag(), fs.getID());
view_good.put(prefix + "_" + fs.getID(), fs.getID());
if (fs instanceof Field) {
Field f = (Field) fs;
String fsID = f.getID();
// If the field is actually based upon another, marked use-csid,
// then put the name of that field in the view map instead.
// We need to glean that from the list results, so we can build
// the field from it.
String use_csid = f.useCsid();
if (use_csid != null && f.useCsidField() != null) {
String useID = f.useCsidField();
List<String> useStrings = new ArrayList<String>();
useStrings.add(useID);
useStrings.add(use_csid);
view_useCsid.put(prefix + "_" + fsID, useStrings);
// Need to ensure that we glean the useId value as well
view_good.put(prefix + "_" + useID, useID);
// Strictly speaking we should get the services tag for
// the field, but we'll cheat for simplicity, since it is
// almost always the ID, and we'll just insist that for
// useCsid that it must be. Sigh.
view_map.put(useID, useID);
}
// Single field
if (f.hasMergeData()) {
view_merge.put(prefix + "_" + fsID, f.getAllMerge());
for (String fm : f.getAllMerge()) {
if (fm != null) {
if (r.getFieldFullList(fm).hasAutocompleteInstance()) {
xxx_view_deurn.add(fsID);
}
}
}
}
if (f.hasAutocompleteInstance()) {
xxx_view_deurn.add(fsID);
}
}
}
}
}
}
// Special cases not currently supported in config. This is a hack, and once we figure out
// what a pattern looks like, we can consider configuring this somehow.
// The current support is for a return field that may or may not be present in list
// results. When it is, we want to pass it along to the UI
view_search_optional.add("related");
view_good.put("search_related", "related");
view_map.put("related", "related");
}
use of org.collectionspace.chain.csp.schema.Field 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.chain.csp.schema.Field 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.chain.csp.schema.Field 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.chain.csp.schema.Field in project application by collectionspace.
the class DataGenerator method getAuthdata.
/**
* Return appropriate authority for the field in question
* @param f
* @param context
* @return
* @throws JSONException
*/
private JSONObject getAuthdata(FieldSet fs, UISpecRunContext context) throws JSONException {
JSONObject dataitem = new JSONObject();
JSONObject allnames = new JSONObject();
Integer i = 0;
if (fs instanceof Field) {
for (Instance type : ((Field) fs).getAllAutocompleteInstances()) {
String iid = type.getTitleRef();
Record ir = type.getRecord();
JSONArray thesenames = ctl.get(this.storage, iid, ir, authoritylimit);
log.info("getting authority: " + type.getID() + ":" + type.getRecord());
try {
tty.line("getting authority: " + type.getID() + ":" + type.getRecord());
} catch (UIException e) {
}
allnames.put(i.toString(), thesenames);
i++;
}
}
Random objrandom = new Random();
Integer pickobj = objrandom.nextInt(allnames.length());
JSONArray getallnames = allnames.getJSONArray(pickobj.toString());
// use random number to choose which item to use
Random arrayrandom = new Random();
if (getallnames.length() > 0) {
int pick = arrayrandom.nextInt(getallnames.length());
dataitem = getallnames.getJSONObject(pick);
}
return dataitem;
}
Aggregations