Search in sources :

Example 21 with Record

use of org.collectionspace.chain.csp.schema.Record in project application by collectionspace.

the class ServicesStorageGenerator method real_init.

private void real_init(CSPManager cspManager, Spec spec, boolean forXsdGeneration) throws CSPDependencyException {
    try {
        ServicesConnection conn = new ServicesConnection(base_url, ims_url);
        for (Record r : spec.getAllRecords()) {
            if (r.isType("blob") || r.isType("report") || r.isType("batch"))
                addChild(r.getID(), new BlobStorage(spec.getRecord(r.getID()), conn));
            else if (r.isType("userdata"))
                addChild(r.getID(), new UserStorage(spec.getRecord(r.getID()), conn));
            else if (r.isType("record") || r.isType("searchall"))
                addChild(r.getID(), new RecordStorage(spec.getRecord(r.getID()), conn));
            else if (r.isType("authority"))
                addChild(r.getID(), new ConfiguredVocabStorage(spec.getRecord(r.getID()), conn));
            else if (r.isType("authorizationdata"))
                addChild(r.getID(), new AuthorizationStorage(spec.getRecord(r.getID()), conn));
        }
        addChild("direct", new DirectRedirector(spec));
        addChild("id", new ServicesIDGenerator(conn, spec));
        addChild("relations", new ServicesRelationStorage(conn, spec));
        // 
        if (forXsdGeneration == false) {
            initializeAuthorities(cspManager, spec);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // XXX wrong type
        throw new CSPDependencyException("Could not set target", e);
    }
}
Also used : AuthorizationStorage(org.collectionspace.chain.csp.persistence.services.authorization.AuthorizationStorage) CSPDependencyException(org.collectionspace.csp.api.core.CSPDependencyException) ServicesConnection(org.collectionspace.chain.csp.persistence.services.connection.ServicesConnection) Record(org.collectionspace.chain.csp.schema.Record) ConfiguredVocabStorage(org.collectionspace.chain.csp.persistence.services.vocab.ConfiguredVocabStorage) UserStorage(org.collectionspace.chain.csp.persistence.services.user.UserStorage) ServicesRelationStorage(org.collectionspace.chain.csp.persistence.services.relation.ServicesRelationStorage) UIException(org.collectionspace.csp.api.ui.UIException) CSPDependencyException(org.collectionspace.csp.api.core.CSPDependencyException)

Example 22 with Record

use of org.collectionspace.chain.csp.schema.Record in project application by collectionspace.

the class XmlJsonConversion method getChildrenWithGroupFields.

// merges in the pseudo sub records 'groupfields' with the normal fields unless they need to be nested
private static List<FieldSet> getChildrenWithGroupFields(Repeat parent, String operation) {
    List<FieldSet> children = new ArrayList<FieldSet>();
    if (parent.getUIType().startsWith("groupfield")) {
        String[] parts = parent.getUIType().split("/");
        Record subitems = parent.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
        for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
            children.add(fd);
        }
    }
    for (FieldSet fs : parent.getChildren(operation)) {
        if (fs.getUIType().startsWith("groupfield")) {
            String[] parts = fs.getUIType().split("/");
            Record subitems = fs.getRecord().getSpec().getRecordByServicesUrl(parts[1]);
            if (fs instanceof Group) {
                if (((Group) fs).getXxxServicesNoRepeat()) {
                    for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
                        // non-nested groupfields?
                        children.add(fd);
                    }
                    // groups with xxx-services-no-repeat set to true.
                    for (FieldSet fd : ((Group) fs).getChildren(operation)) {
                        children.add(fd);
                    }
                } else {
                    // this one should be nested
                    children.add(fs);
                }
            } else {
                for (FieldSet fd : subitems.getAllFieldTopLevel(operation)) {
                    // what about nested groupfields?
                    children.add(fd);
                }
            }
        } else {
            children.add(fs);
        }
    }
    return children;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) ArrayList(java.util.ArrayList) Record(org.collectionspace.chain.csp.schema.Record)

Example 23 with Record

use of org.collectionspace.chain.csp.schema.Record in project application by collectionspace.

the class GenericStorage method simpleRetrieveJSON.

/**
 * return data just as the service layer gives it to the App layer
 * no extra columns required
 * @param creds
 * @param cache
 * @param filePath
 * @param servicesurl
 * @param thisr
 * @return
 * @throws ExistException
 * @throws UnimplementedException
 * @throws UnderlyingStorageException
 */
public JSONObject simpleRetrieveJSON(CSPRequestCredentials creds, CSPRequestCache cache, String filePath, String servicesurl, Record thisr) throws ExistException, UnimplementedException, UnderlyingStorageException {
    String csid = "";
    if (filePath == null) {
        filePath = "";
    }
    String[] path_parts = filePath.split("/");
    if (path_parts.length > 1)
        csid = path_parts[1];
    else
        csid = filePath;
    JSONObject out = new JSONObject();
    try {
        String softpath = filePath;
        if (thisr.hasSoftDeleteMethod()) {
            softpath = softpath(filePath);
        }
        if (thisr.hasHierarchyUsed("screen")) {
            softpath = hierarchicalpath(softpath);
        }
        if (thisr.isMultipart()) {
            ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
            for (String section : thisr.getServicesRecordPathKeys()) {
                String path = thisr.getServicesRecordPath(section);
                String[] parts = path.split(":", 2);
                if (doc.getDocument(parts[0]) != null) {
                    convertToJson(out, doc.getDocument(parts[0]), thisr, "GET", section, csid);
                }
            }
            // 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(thisr, doc, out, csid);
        } else {
            ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, servicesurl + softpath, null, creds, cache);
            if ((doc.getStatus() < 200 || doc.getStatus() >= 300))
                throw new UnderlyingStorageException("Does not exist ", doc.getStatus(), softpath);
            convertToJson(out, doc.getDocument(), thisr, "GET", "common", csid);
        }
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    }
    /*
		 * Get data for any sub records that are part of this record e.g. contact in person, blob in media
		 */
    try {
        for (FieldSet fs : thisr.getAllSubRecords("GET")) {
            Boolean validator = true;
            Record sr = fs.usesRecordId();
            if (fs.usesRecordValidator() != null) {
                validator = false;
                if (out.has(fs.usesRecordValidator())) {
                    String test = out.getString(fs.usesRecordValidator());
                    if (test != null && !test.equals("")) {
                        validator = true;
                    }
                }
            }
            if (validator) {
                String getPath = servicesurl + filePath + "/" + sr.getServicesURL();
                if (null != fs.getServicesUrl()) {
                    getPath = fs.getServicesUrl();
                }
                if (fs.getWithCSID() != null) {
                    getPath = getPath + "/" + out.getString(fs.getWithCSID());
                }
                // need to get update and delete working? tho not going to be used for media as handling blob seperately
                if (fs instanceof Group) {
                    JSONObject outer = simpleRetrieveJSON(creds, cache, getPath, "", sr);
                    JSONArray group = new JSONArray();
                    group.put(outer);
                    out.put(fs.getID(), group);
                }
                if (fs instanceof Repeat) {
                    // NEED TO GET A LIST OF ALL THE THINGS
                    JSONArray repeat = new JSONArray();
                    String path = getPath;
                    while (!path.equals("")) {
                        JSONObject data = getListView(creds, cache, path, sr.getServicesListPath(), "csid", false, r);
                        if (data.has("listItems")) {
                            String[] results = (String[]) data.get("listItems");
                            for (String result : results) {
                                JSONObject rout = simpleRetrieveJSON(creds, cache, getPath + "/" + result, "", sr);
                                // add in csid so I can do update with a modicum of confidence
                                rout.put("_subrecordcsid", result);
                                repeat.put(rout);
                            }
                        }
                        if (data.has("pagination")) {
                            Integer ps = Integer.valueOf(data.getJSONObject("pagination").getString("pageSize"));
                            Integer pn = Integer.valueOf(data.getJSONObject("pagination").getString("pageNum"));
                            Integer ti = Integer.valueOf(data.getJSONObject("pagination").getString("totalItems"));
                            if (ti > (ps * (pn + 1))) {
                                JSONObject restrictions = new JSONObject();
                                restrictions.put("pageSize", Integer.toString(ps));
                                restrictions.put("pageNum", Integer.toString(pn + 1));
                                path = getRestrictedPath(getPath, restrictions, sr.getServicesSearchKeyword(), "", false, "");
                            // need more values
                            } else {
                                path = "";
                            }
                        }
                    }
                    // group.put(outer);
                    out.put(fs.getID(), repeat);
                }
            }
        }
    } catch (Exception e) {
    // ignore exceptions for sub records at the moment - make it more intelligent later
    // throw new UnderlyingStorageException("Service layer exception",e);
    }
    return out;
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Repeat(org.collectionspace.chain.csp.schema.Repeat) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) DocumentException(org.dom4j.DocumentException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) IOException(java.io.IOException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 24 with Record

use of org.collectionspace.chain.csp.schema.Record in project application by collectionspace.

the class GenericStorage method autocreateJSON.

/**
 * Convert the JSON from the UI Layer into XML for the Service layer while using the XML structure from cspace-config.xml
 * Send the XML through to the Service Layer to store it in the database
 * The Service Layer returns a url to the object we just stored.
 * @param {ContextualisedStorage} root
 * @param {CSPRequestCredentials} creds
 * @param {CSPRequestCache} cache
 * @param {String} filePath part of the path to the Service URL (containing the type of object)
 * @param {JSONObject} jsonObject The JSON string coming in from the UI Layer, containing the object to be stored
 * @return {String} csid The id of the object in the database
 */
@Override
public String autocreateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        ReturnedURL url = null;
        Document doc = null;
        // used by userroles and permroles as they have complex urls
        if (r.hasPrimaryField()) {
            for (String section : r.getServicesRecordPathKeys()) {
                doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "POST");
                String path = r.getServicesURL();
                path = path.replace("*", getSubCsid(jsonObject, r.getPrimaryField()));
                String restrictedPath = getRestrictedPath(path, restrictions, null);
                deleteJSON(root, creds, cache, restrictedPath);
                url = conn.getURL(RequestMethod.POST, restrictedPath, doc, creds, cache);
            }
        } else {
            String restrictedPath = getRestrictedPath(r.getServicesURL(), restrictions, null);
            // REM - We need a way to send query params (restrictions) on POST and UPDATE
            url = autoCreateSub(creds, cache, jsonObject, doc, restrictedPath, r);
        }
        // I am developing this.. it might not work...
        for (FieldSet fs : r.getAllSubRecords("POST")) {
            Record sr = fs.usesRecordId();
            // sr.getID()
            if (sr.isType("authority")) {
            // need to use code from configuredVocabStorage
            } else {
                String savePath = url.getURL() + "/" + sr.getServicesURL();
                if (fs instanceof Field) {
                    // get the fields form inline XXX untested - might not work...
                    JSONObject subdata = new JSONObject();
                    // loop thr jsonObject and find the fields I need
                    for (FieldSet subfs : sr.getAllFieldTopLevel("POST")) {
                        String key = subfs.getID();
                        if (jsonObject.has(key)) {
                            subdata.put(key, jsonObject.get(key));
                        }
                    }
                    subautocreateJSON(root, creds, cache, sr, subdata, savePath);
                } else if (fs instanceof Group) {
                    // JSONObject
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONObject) {
                            JSONObject subrecord = (JSONObject) subdata;
                            subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                        }
                    }
                } else {
                    // JSONArray
                    if (jsonObject.has(fs.getID())) {
                        Object subdata = jsonObject.get(fs.getID());
                        if (subdata instanceof JSONArray) {
                            JSONArray subarray = (JSONArray) subdata;
                            for (int i = 0; i < subarray.length(); i++) {
                                JSONObject subrecord = subarray.getJSONObject(i);
                                subautocreateJSON(root, creds, cache, sr, subrecord, savePath);
                            }
                        }
                    }
                }
            }
        }
        return url.getURLTail();
    } catch (ConnectionException e) {
        String msg = e.getMessage();
        if (e.getStatus() == 403) {
            // permissions error
            msg += " permissions error";
        }
        throw new UnderlyingStorageException(msg, e.getStatus(), e.getUrl(), e);
    } catch (UnderlyingStorageException e) {
        // REM - CSPACE-5632: Need to catch and rethrow this exception type to prevent throwing an "UnimplementedException" exception below.
        throw e;
    } catch (Exception e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) Group(org.collectionspace.chain.csp.schema.Group) JSONArray(org.json.JSONArray) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) DocumentException(org.dom4j.DocumentException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) IOException(java.io.IOException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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) JSONObject(org.json.JSONObject) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 25 with Record

use of org.collectionspace.chain.csp.schema.Record 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);
    }
}
Also used : Group(org.collectionspace.chain.csp.schema.Group) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Repeat(org.collectionspace.chain.csp.schema.Repeat) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) Record(org.collectionspace.chain.csp.schema.Record) ArrayList(java.util.ArrayList) List(java.util.List) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException) HashSet(java.util.HashSet)

Aggregations

Record (org.collectionspace.chain.csp.schema.Record)59 JSONObject (org.json.JSONObject)33 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)23 JSONArray (org.json.JSONArray)19 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)14 JSONException (org.json.JSONException)14 Field (org.collectionspace.chain.csp.schema.Field)12 Group (org.collectionspace.chain.csp.schema.Group)12 Instance (org.collectionspace.chain.csp.schema.Instance)11 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)11 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)10 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)10 Document (org.dom4j.Document)10 HashMap (java.util.HashMap)9 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)9 ExistException (org.collectionspace.csp.api.persistence.ExistException)9 Element (org.dom4j.Element)9 UIException (org.collectionspace.csp.api.ui.UIException)8 IOException (java.io.IOException)7 Repeat (org.collectionspace.chain.csp.schema.Repeat)7