Search in sources :

Example 1 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class ServicesRelationStorage method findNameUnderNode.

private void findNameUnderNode(JSONObject out, String nameKey, String refNameKey, Node itemNode) throws JSONException {
    // Look for something to put into the subjectname. Start with refName,
    // then name, then number
    Node itemRefName = itemNode.selectSingleNode("refName");
    String nameValue = null;
    if (itemRefName != null) {
        String refNameValue = itemRefName.getText();
        out.put(refNameKey, refNameValue);
        RefName.AuthorityItem item = RefName.AuthorityItem.parse(refNameValue);
        if (item != null) {
            nameValue = item.displayName;
        } else {
            RefName.Authority authority = RefName.Authority.parse(refNameValue);
            if (authority != null) {
                nameValue = authority.displayName;
            }
        }
    }
    // If no displayName from refName, then try name element
    if (nameValue == null) {
        Node itemNameNode = itemNode.selectSingleNode("name");
        if (itemNameNode != null) {
            nameValue = itemNameNode.getText();
        }
    }
    // Still nothing? try number element
    if (nameValue == null) {
        Node itemNumberNode = itemNode.selectSingleNode("number");
        if (itemNumberNode != null) {
            nameValue = itemNumberNode.getText();
        }
    }
    if (nameValue == null) {
        nameValue = "MISSING DATA";
    }
    out.put(nameKey, nameValue);
}
Also used : RefName(org.collectionspace.services.common.api.RefName) Node(org.dom4j.Node)

Example 2 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class ConfiguredVocabStorage method get.

private JSONObject get(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, String url, String ims_url) throws ConnectionException, ExistException, UnderlyingStorageException, JSONException {
    // int status=0;
    String csid = "";
    JSONObject out = new JSONObject();
    // XXX pagination support
    String softurl = url;
    if (r.hasSoftDeleteMethod()) {
        softurl = softpath(url);
    }
    if (r.hasHierarchyUsed("screen")) {
        softurl = hierarchicalpath(softurl);
    }
    ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, softurl, null, creds, cache);
    if (doc.getStatus() == 404)
        throw new ExistException("Does not exist " + softurl);
    if (doc.getStatus() == 403) {
        // permission error - keep calm and carry on with what we can glean
        out.put("displayName", getDisplayNameKey());
        out.put("csid", csid);
        out.put("recordtype", r.getWebURL());
        return out;
    }
    if (doc.getStatus() > 299)
        throw new UnderlyingStorageException("Could not retrieve vocabulary status=" + doc.getStatus(), doc.getStatus(), softurl);
    String name = null;
    String refid = null;
    String termStatus = null;
    String parentcsid = null;
    String shortIdentifier = "";
    for (String section : r.getServicesRecordPathKeys()) {
        String path = r.getServicesRecordPath(section);
        String[] record_path = path.split(":", 2);
        String[] tag_path = record_path[1].split(",", 2);
        Document result = doc.getDocument(record_path[0].trim());
        if (result != null) {
            if ("common".equals(section)) {
                // XXX hardwired :(
                String dnXPath = getDisplayNameXPath();
                name = result.selectSingleNode(tag_path[1] + "/" + dnXPath).getText();
                if (result.selectSingleNode(tag_path[1] + "/shortIdentifier") != null) {
                    shortIdentifier = result.selectSingleNode(tag_path[1] + "/shortIdentifier").getText();
                }
                refid = result.selectSingleNode(tag_path[1] + "/refName").getText();
                // We need to replace this with the same model as for displayName
                if (result.selectSingleNode(tag_path[1] + "/termStatus") != null) {
                    termStatus = result.selectSingleNode(tag_path[1] + "/termStatus").getText();
                } else {
                    termStatus = "";
                }
                csid = result.selectSingleNode(tag_path[1] + "/csid").getText();
                parentcsid = result.selectSingleNode(tag_path[1] + "/inAuthority").getText();
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            } else {
                XmlJsonConversion.convertToJson(out, r, result, "GET", section, csid, ims_url);
            }
        } else {
            log.warn(String.format("XML Payload for '%s' was missing part '%s'.", url, record_path[0]));
        }
    }
    // If this record has hierarchy, will pull out the relations section and map it to the hierarchy
    // fields (special case handling of XML-JSON
    handleHierarchyPayloadRetrieve(r, doc, out, csid);
    // get related sub records?
    for (FieldSet fs : r.getAllSubRecords("GET")) {
        Record sr = fs.usesRecordId();
        // sr.getID()
        if (sr.isType("authority")) {
            String getPath = url + "/" + sr.getServicesURL();
            JSONArray subout = get(storage, creds, cache, url, getPath, sr);
            if (fs instanceof Field) {
                JSONObject fielddata = subout.getJSONObject(0);
                Iterator<String> rit = fielddata.keys();
                while (rit.hasNext()) {
                    String key = rit.next();
                    out.put(key, fielddata.get(key));
                }
            } else if (fs instanceof Group) {
                if (subout.length() > 0) {
                    out.put(fs.getID(), subout.getJSONObject(0));
                }
            } else {
                out.put(fs.getID(), subout);
            }
        }
    }
    // csid = urn_processor.deconstructURN(refid,false)[4];
    out.put(getDisplayNameKey(), name);
    out.put("csid", csid);
    out.put("refid", refid);
    RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
    out.put("namespace", item.getParentShortIdentifier());
    out.put("shortIdentifier", shortIdentifier);
    out.put("termStatus", termStatus);
    out.put("authorityid", parentcsid);
    out.put("recordtype", r.getWebURL());
    return out;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) RefName(org.collectionspace.services.common.api.RefName) JSONArray(org.json.JSONArray) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record)

Example 3 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class ConfiguredVocabStorage method miniViewAbstract.

@Override
protected JSONObject miniViewAbstract(ContextualisedStorage storage, CSPRequestCredentials creds, CSPRequestCache cache, JSONObject out, String servicepath, String filePath) throws UnderlyingStorageException {
    try {
        // actually use cache
        String cachelistitem = "/" + servicepath;
        if (filePath != null) {
            cachelistitem = cachelistitem + "/" + filePath;
        }
        if (!cachelistitem.startsWith("/")) {
            cachelistitem = "/" + cachelistitem;
        }
        String dnName = getDisplayNameKey();
        String g1 = getGleanedValue(cache, cachelistitem, "refName");
        String g2 = getGleanedValue(cache, cachelistitem, "shortIdentifier");
        String g3 = getGleanedValue(cache, cachelistitem, dnName);
        String g4 = getGleanedValue(cache, cachelistitem, "csid");
        String g5 = getGleanedValue(cache, cachelistitem, "termStatus");
        String g6 = getGleanedValue(cache, cachelistitem, "workflow");
        if (g1 == null || g2 == null || g3 == null || g4 == null || g5 == null) {
            if (log.isWarnEnabled()) {
                StringBuilder sb = new StringBuilder();
                sb.append("ConfiguredVocabStorage fanning out ");
                if (g2 != null) {
                    sb.append("(shId:");
                    sb.append(g2);
                    sb.append(")");
                }
                if (g4 != null) {
                    sb.append("(csid:");
                    sb.append(g4);
                    sb.append(")");
                }
                sb.append(", as could not get: ");
                if (g1 == null)
                    sb.append("refName,");
                if (g2 == null)
                    sb.append("shortIdentifier,");
                if (g3 == null)
                    sb.append("dnName,");
                if (g4 == null)
                    sb.append("csid,");
                if (g5 == null)
                    sb.append("termStatus,");
                log.warn(sb.toString());
            }
            JSONObject cached = get(storage, creds, cache, servicepath, filePath);
            g1 = cached.getString("refid");
            g2 = cached.getString("shortIdentifier");
            g3 = cached.getString(dnName);
            g4 = cached.getString("csid");
            g5 = cached.getString("termStatus");
        }
        out.put(dnName, g3);
        out.put("refid", g1);
        out.put("csid", g4);
        out.put("termStatus", g5);
        out.put("workflow", g6);
        // out.put("authorityid", cached.get("authorityid"));
        out.put("shortIdentifier", g2);
        out.put("recordtype", r.getWebURL());
        RefName.AuthorityItem item = RefName.AuthorityItem.parse(g1);
        out.put("namespace", item.getParentShortIdentifier());
        return out;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Connection exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (ExistException e) {
        throw new UnderlyingStorageException("ExistException exception" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("JSONException exception" + e.getLocalizedMessage(), e);
    }
}
Also used : JSONObject(org.json.JSONObject) RefName(org.collectionspace.services.common.api.RefName) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 4 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class RecordSearchList method generateMiniRecord.

/**
 * Retrieve the mini summary information e.g. summary and number and append
 * the csid and recordType to it
 *
 * @param {Storage} storage Type of storage (e.g. AuthorizationStorage,
 * RecordStorage,...)
 * @param {String} type The type of record requested (e.g. permission)
 * @param {String} csid The csid of the record
 * @return {JSONObject} The JSON string containing the mini record
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 * @throws JSONException
 */
private JSONObject generateMiniRecord(Storage storage, String type, String csid) throws JSONException {
    String postfix = "list";
    if (this.mode == MODE_SEARCH) {
        postfix = "search";
    }
    JSONObject restrictions = new JSONObject();
    JSONObject out = new JSONObject();
    try {
        if (csid == null || csid.equals("")) {
            return out;
        }
        out = storage.retrieveJSON(type + "/" + csid + "/view/" + postfix, restrictions);
        out.put("csid", csid);
        String recordtype = null;
        if (!r.isType("searchall")) {
            recordtype = type_to_url.get(type);
        } else {
            JSONObject summarylist = out.getJSONObject("summarylist");
            String uri = summarylist.getString("uri");
            if (uri != null && uri.startsWith("/")) {
                uri = uri.substring(1);
            }
            String[] parts = uri.split("/");
            String recordurl = parts[0];
            Record itemr = r.getSpec().getRecordByServicesUrl(recordurl);
            if (itemr == null) {
                String docType = summarylist.getString("docType");
                itemr = r.getSpec().getRecordByServicesDocType(docType);
            }
            if (itemr == null) {
                recordtype = UNKNOWN_RECORD_TYPE;
                log.warn("Could not get record type for record with services URI " + uri);
            } else {
                recordtype = type_to_url.get(itemr.getID());
                String refName = null;
                if (summarylist.has("refName")) {
                    refName = summarylist.getString("refName");
                }
                // For an authority item (i.e. an item in a vocabulary),
                // include the name of its parent vocabulary in a
                // "namespace" value within the mini summary.
                RefName.AuthorityItem item = null;
                if (refName != null) {
                    item = RefName.AuthorityItem.parse(refName);
                }
                // authority item refName, then include the "namespace" value.
                if (item != null) {
                    String namespace = item.getParentShortIdentifier();
                    if (namespace != null) {
                        out.put("namespace", namespace);
                    } else {
                        log.warn("Could not get vocabulary namespace for record with services URI " + uri);
                    }
                }
            }
        }
        out.put("recordtype", recordtype);
        // CSPACE-2894
        if (this.r.getID().equals("permission")) {
            String summary = out.getString("summary");
            String name = Generic.ResourceNameUI(this.r.getSpec(), summary);
            if (name.contains(WORKFLOW_SUB_RESOURCE)) {
                return null;
            }
            out.put("summary", name);
            out.put("display", Generic.getPermissionView(this.r.getSpec(), summary));
        }
    } 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", "Exist 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", "Exist Exception:" + e.getMessage());
        JSONArray msgs = new JSONArray();
        msgs.put(msg);
        out.put("messages", msgs);
    }
    return out;
}
Also used : JSONObject(org.json.JSONObject) RefName(org.collectionspace.services.common.api.RefName) JSONArray(org.json.JSONArray) Record(org.collectionspace.chain.csp.schema.Record) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 5 with RefName

use of org.collectionspace.services.common.api.RefName in project application by collectionspace.

the class VocabulariesRead method getRefObjs.

/**
 * Returns all the objects that are linked to a vocabulary item
 * @param storage
 * @param path
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 * @throws JSONException
 */
@SuppressWarnings("unchecked")
private void getRefObjs(Storage storage, String path, JSONObject out, String itemsKey, boolean addPagination, JSONObject restrictions) throws JSONException {
    JSONArray items = new JSONArray();
    try {
        JSONObject refObjs = storage.retrieveJSON(path + "/refObjs", restrictions);
        if (refObjs != null) {
            if (refObjs.has("items")) {
                JSONArray ritems = refObjs.getJSONArray("items");
                for (int i = 0; i < ritems.length(); i++) {
                    JSONObject in = ritems.getJSONObject(i);
                    // JSONObject in=ritems.getJSONObject(field);
                    String rt = in.getString("sourceFieldType");
                    Record rec = this.spec.getRecordByServicesDocType(rt);
                    // as the key.
                    if (rec == null) {
                        if (in.has("summarylist")) {
                            JSONObject summaryList = in.getJSONObject("summarylist");
                            if (summaryList.has("uri")) {
                                String uri = summaryList.getString("uri");
                                if (Tools.notBlank(uri)) {
                                    String recordtypekey = uri.split("/")[0];
                                    if (Tools.notBlank(recordtypekey)) {
                                        rec = this.spec.getRecordByServicesUrl(recordtypekey);
                                    }
                                }
                            }
                        }
                    }
                    String uiname;
                    if (rec != null) {
                        uiname = rec.getWebURL();
                        if (rec.isType("authority")) {
                            // Need to add namespace for authorities
                            String refName = null;
                            if (in.has("refName")) {
                                refName = in.getString("refName");
                            } else if (in.has("summarylist")) {
                                JSONObject summList = in.getJSONObject("summarylist");
                                if (summList.has("refName")) {
                                    refName = summList.getString("refName");
                                }
                            }
                            if (refName != null) {
                                RefName.AuthorityItem item = RefName.AuthorityItem.parse(refName);
                                in.put("namespace", item.getParentShortIdentifier());
                            }
                        }
                    } else {
                        uiname = rt;
                    }
                    in.put("recordtype", uiname);
                    /*
						JSONObject entry=new JSONObject();
						entry.put("csid",in.getString("csid"));
						entry.put("recordtype",in.getString("sourceFieldType"));
						entry.put("sourceFieldName",field);
						entry.put("number",in.getString("sourceFieldName"));
						*/
                    items.put(in);
                }
            }
            out.put(itemsKey, items);
            if (addPagination && refObjs.has("pagination")) {
                out.put("pagination", refObjs.get("pagination"));
            }
        }
    } catch (JSONException ex) {
        log.debug("JSONException" + ex.getLocalizedMessage());
    // wordlessly eat the errors at the moment as they might be permission errors
    } catch (ExistException e) {
        log.debug("ExistException" + e.getLocalizedMessage());
    // wordlessly eat the errors at the moment as they might be permission errors
    } catch (UnimplementedException e) {
        log.debug("UnimplementedException" + e.getLocalizedMessage());
    // wordlessly eat the errors at the moment as they might be permission errors
    } catch (UnderlyingStorageException e) {
        log.debug("UnderlyingStorageException" + e.getLocalizedMessage());
    // wordlessly eat the errors at the moment as they might be permission errors
    }
}
Also used : JSONObject(org.json.JSONObject) RefName(org.collectionspace.services.common.api.RefName) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Record(org.collectionspace.chain.csp.schema.Record) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Aggregations

RefName (org.collectionspace.services.common.api.RefName)6 Record (org.collectionspace.chain.csp.schema.Record)4 ExistException (org.collectionspace.csp.api.persistence.ExistException)4 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)4 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)2 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)2 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)2 Document (org.dom4j.Document)2 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)1 Field (org.collectionspace.chain.csp.schema.Field)1 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)1 Group (org.collectionspace.chain.csp.schema.Group)1 Element (org.dom4j.Element)1 Node (org.dom4j.Node)1