Search in sources :

Example 11 with ReturnedMultipartDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument 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 12 with ReturnedMultipartDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.

the class TestService method testCRUD.

// TODO merge this method with testPostGetDelete - this is Chris's temporary
// fork to help testing repeatable fields
/**
 * Test Create, Update, Read and Delete for a record type
 *
 * @param serviceurl
 * @param partname
 * @param Createfilename
 * @param Updatefilename
 * @param xpath
 * @param expected
 */
private void testCRUD(String serviceurl, String partname, String Createfilename, String Updatefilename, String xpath, String expected) throws Exception {
    ReturnedURL url;
    log.info("Testing " + serviceurl + " with " + Createfilename + " and partname=" + partname);
    // POST (Create)
    if (partname != null) {
        Map<String, Document> parts = new HashMap<String, Document>();
        parts.put(partname, getDocument(Createfilename));
        url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
    } else {
        url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(Createfilename), creds, cache);
    }
    assertEquals(201, url.getStatus());
    // ensures e.g.
    assertTrue(url.getURL().startsWith("/" + serviceurl));
    // CSPACE-305
    // hasn't
    // regressed
    // GET (Read)
    int getStatus;
    Document doc;
    if (partname != null) {
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    assertEquals(200, getStatus);
    assertNotNull(doc);
    Node n = doc.selectSingleNode(xpath);
    assertNotNull(n);
    String text = n.getText();
    assertEquals(expected, text);
    // Update
    if (partname != null) {
        Map<String, Document> parts = new HashMap<String, Document>();
        parts.put(partname, getDocument(Updatefilename));
        conn.getMultipartXMLDocument(RequestMethod.PUT, url.getURL(), parts, creds, cache);
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        conn.getXMLDocument(RequestMethod.PUT, url.getURL(), getDocument(Updatefilename), creds, cache);
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    assertEquals(200, getStatus);
    assertNotNull(doc);
    n = doc.selectSingleNode(xpath);
    assertNotNull(n);
    text = n.getText();
    assertEquals(expected, text);
    // log.info(doc.asXML());
    // Get
    // DELETE (Delete)
    int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals(200, status);
    // Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
    status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals(404, status);
    // GET once more to make sure it isn't there
    if (partname != null) {
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    // ensures CSPACE-209 hasn't regressed
    assertEquals(404, getStatus);
    assertNull(doc);
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) Node(org.dom4j.Node) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Example 13 with ReturnedMultipartDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.

the class TestService method testReporting.

// @Test
// remove test as never know if all the bits for the report are there to test
public void testReporting() throws Exception {
    ReturnedURL url;
    int getStatus;
    Document doc;
    String serviceurl = "acquisitions/";
    String partname = "acquisitions_common";
    String filename = "acquisitionXMLJSON.xml";
    String xpath = "acquisitions_common/accessionDate";
    String expected = "April 1, 2010";
    // POST (Create Acquisition Record)
    if (partname != null) {
        Map<String, Document> parts = new HashMap<String, Document>();
        parts.put(partname, getDocument(filename));
        url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
    } else {
        url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
    }
    assertEquals("Failed to receive 201 status code on create", 201, url.getStatus());
    // find report
    ReturnedDocument doc3 = conn.getXMLDocument(RequestMethod.GET, "reports?doctype=Acquisition", null, creds, cache);
    assertEquals(200, doc3.getStatus());
    Set<String> csids = new HashSet<String>();
    String reportcsid = "";
    for (Node n : (List<Node>) doc3.getDocument().selectNodes("abstract-common-list/list-item/csid")) {
        reportcsid = n.getText();
    }
    assertFalse("No Acquisition report to test with", reportcsid.equals(""));
    if (!reportcsid.equals("")) {
        // only runs if there is a report to run
        String reportsurl = "reports/" + reportcsid;
        String csid = url.getURLTail();
        Document report = getDocument("reportrun.xml");
        report.selectSingleNode("invocationContext/singleCSID").setText(csid);
        // DO REPORT
        // run report
        ReturnUnknown doc2 = conn.getReportDocument(RequestMethod.POST, reportsurl, report, creds, cache);
        JSONObject out = new JSONObject();
        out.put("getByteBody", doc2.getBytes());
        out.put("contenttype", doc2.getContentType());
        assertEquals("Failed to receive 200 status code on create", 200, doc2.getStatus());
    }
    // DELETE (Delete Acquisition)
    int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals("Failed to receive expected 200 status code on delete", 200, status);
    // Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
    status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, status);
    log.info("DELETE");
    // GET once more to make sure it isn't there
    if (partname != null) {
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, // ensures CSPACE-209 hasn't regressed
    getStatus);
    assertNull("Contents of deleted record were unexpectedly not null", doc);
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnUnknown(org.collectionspace.chain.csp.persistence.services.connection.ReturnUnknown) HashMap(java.util.HashMap) Node(org.dom4j.Node) 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) JSONObject(org.json.JSONObject) List(java.util.List) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) HashSet(java.util.HashSet)

Example 14 with ReturnedMultipartDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.

the class TestService method testAuthorityCreateUpdateDelete.

/*
	 * @Ignore public void testRolePermissionsPost() throws Exception { //
	 * ReturnedURLurl=conn.getURL(RequestMethod.POST,
	 * "authorization/roles/cbdb4f45-2fac-461b-93ef-6fec21a2ad97/permroles"
	 * ,getDocument("rolepermissions.xml"),creds,cache); //
	 * assertEquals(201,url.getStatus());
	 * 
	 * 
	 * 
	 * // TODO check whether should be commented back in - Chris was debugging
	 * permissions // NOTE this test is more complex than PostGetDelete and
	 * perhaps should remain as a separate test? //create a permission //
	 * Map<String,Document> parts=new HashMap<String,Document>(); // ReturnedURL
	 * url
	 * =conn.getURL(RequestMethod.POST,"authorization/permissions/",getDocument
	 * ("permissions.xml"),creds,cache); // assertEquals(201,url.getStatus());
	 * 
	 * //create permissionRole for the permission above // url =
	 * conn.getURL(RequestMethod.POST,
	 * "authorization/permissions/"+url.getURLTail()+"/permroles",
	 * getDocument("rolepermissions.xml"), creds, cache); // assertEquals(201,
	 * url.getStatus()); //delete the permissionRole // int
	 * status=conn.getNone(RequestMethod.DELETE,url.getURL(),null,creds,cache);
	 * // assertEquals(200,status); // XXX CSPACE-73, should be 404
	 * 
	 * //delete the permission //
	 * status=conn.getNone(RequestMethod.DELETE,url.getURL(),null,creds,cache);
	 * // assertEquals(200,status); // XXX CSPACE-73, should be 404 }
	 */
@Test
public void testAuthorityCreateUpdateDelete() throws Exception {
    Map<String, Document> parts = new HashMap<String, Document>();
    parts.put("personauthorities_common", getDocument("personAuth.xml"));
    Map<String, Document> parts1 = new HashMap<String, Document>();
    parts1.put("personauthorities_common", getDocument("personAuth.xml"));
    String id;
    // CREATE
    ReturnedURL url = conn.getMultipartURL(RequestMethod.POST, "personauthorities/", parts, creds, cache);
    assertEquals(201, url.getStatus());
    id = url.getURLTail();
    // UPDATE
    ReturnedMultipartDocument doc = conn.getMultipartXMLDocument(RequestMethod.PUT, "personauthorities/" + id, parts1, creds, cache);
    // XXX shouldn't this be 201?
    assertEquals(200, doc.getStatus());
    // DELETE
    conn.getNone(RequestMethod.DELETE, "personauthorities/" + id, null, creds, cache);
    assertEquals(200, doc.getStatus());
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) Test(org.junit.Test)

Example 15 with ReturnedMultipartDocument

use of org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument in project application by collectionspace.

the class TestService method testPostGetDelete.

private void testPostGetDelete(String serviceurl, String partname, String filename, String xpath, String expected) throws Exception {
    ReturnedURL url;
    log.info("Testing " + serviceurl + " with " + filename + " and partname=" + partname);
    // POST (Create)
    if (partname != null) {
        Map<String, Document> parts = new HashMap<String, Document>();
        parts.put(partname, getDocument(filename));
        url = conn.getMultipartURL(RequestMethod.POST, serviceurl, parts, creds, cache);
    } else {
        url = conn.getURL(RequestMethod.POST, serviceurl, getDocument(filename), creds, cache);
    }
    assertEquals("Failed to receive 201 status code on create", 201, url.getStatus());
    // assertTrue(url.getURL().startsWith("/"+serviceurl)); // ensures e.g.
    // CSPACE-305 hasn't regressed
    log.info("CREATED RECORD " + url.getURL());
    // GET (Read)
    int getStatus;
    Document doc;
    if (partname != null) {
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    assertEquals("Failed to receive expected 200 status code on read", 200, getStatus);
    log.trace("RETRIEVED RECORD " + doc.asXML());
    assertNotNull("Record received on read was unexpectedly null", doc);
    Node n = doc.selectSingleNode(xpath);
    assertNotNull("Expected XPath expression was not found in record", n);
    String text = n.getText();
    assertEquals("Expected value was not found in record", expected, text);
    // List
    log.info("LIST from " + serviceurl);
    ReturnedDocument rdoc1 = conn.getXMLDocument(RequestMethod.GET, "/" + serviceurl, null, creds, cache);
    getStatus = rdoc1.getStatus();
    doc = rdoc1.getDocument();
    assertEquals("Failed to receive expected 200 status code on list read", 200, getStatus);
    log.trace("LISTLISTLIST");
    log.trace(doc.asXML());
    log.trace("LISTLISTLIST");
    // DELETE (Delete)
    int status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals("Failed to receive expected 200 status code on delete", 200, status);
    // Now try to delete non-existent (make sure CSPACE-73 hasn't regressed)
    status = conn.getNone(RequestMethod.DELETE, url.getURL(), null, creds, cache);
    assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, status);
    log.info("DELETE");
    // GET once more to make sure it isn't there
    if (partname != null) {
        ReturnedMultipartDocument rdocs = conn.getMultipartXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdocs.getStatus();
        doc = rdocs.getDocument(partname);
    } else {
        ReturnedDocument rdoc = conn.getXMLDocument(RequestMethod.GET, url.getURL(), null, creds, cache);
        getStatus = rdoc.getStatus();
        doc = rdoc.getDocument();
    }
    assertEquals("Failed to receive expected 404 status code on repeated delete of same record", 404, // ensures CSPACE-209 hasn't regressed
    getStatus);
    assertNull("Contents of deleted record were unexpectedly not null", doc);
}
Also used : ReturnedURL(org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) HashMap(java.util.HashMap) Node(org.dom4j.Node) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)

Aggregations

ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)20 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)20 Document (org.dom4j.Document)18 HashMap (java.util.HashMap)13 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)10 ReturnedURL (org.collectionspace.chain.csp.persistence.services.connection.ReturnedURL)9 JSONObject (org.json.JSONObject)8 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)7 JSONException (org.json.JSONException)7 ExistException (org.collectionspace.csp.api.persistence.ExistException)6 Node (org.dom4j.Node)5 JSONArray (org.json.JSONArray)5 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)4 Group (org.collectionspace.chain.csp.schema.Group)4 Record (org.collectionspace.chain.csp.schema.Record)4 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)4 Test (org.junit.Test)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Field (org.collectionspace.chain.csp.schema.Field)3 IOException (java.io.IOException)1