Search in sources :

Example 46 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class RelateCreateUpdate method relate.

private void relate(Storage storage, UIRequest request, String path) throws UIException {
    try {
        JSONObject data = request.getJSONBody();
        boolean and_reverse = false;
        if (!data.optBoolean("one-way"))
            and_reverse = true;
        if (data.has("type")) {
            // Single
            if (!create) {
                // Update may mean tinkering with other relations because of two-way-ness
                /* NOTE at the moment there's no support in the service to indicate partner, so we assume that
					 * an opposite relation is evidence of a two way relationship. This creates a potential bug if
					 * the user has set up two independent one way relationships and wants to maintain them
					 * independently. It's arguable that this is the behaviour they expect, arguable that it is not.
					 */
                // Delete the reverse record for an update
                log.debug("forward is " + path);
                String csid_rev = find_reverse(storage, path);
                log.debug("reverse is " + csid_rev);
                if (csid_rev != null)
                    storage.deleteJSON("/relations/main/" + csid_rev);
            }
            relate_one(storage, data, path, false);
            if (and_reverse) {
                /* If we're not one way and we're updating, create reverse (we just deleted the old one) */
                relate_one(storage, data, null, true);
            }
        } else if (data.has("items")) {
            // Multiple
            JSONArray relations = data.getJSONArray("items");
            for (int i = 0; i < relations.length(); i++) {
                JSONObject itemdata = relations.getJSONObject(i);
                if (!create)
                    throw new UIException("Cannot use multiple syntax for update");
                if (!itemdata.optBoolean("one-way")) {
                    and_reverse = true;
                }
                relate_one(storage, relations.getJSONObject(i), path, false);
                if (and_reverse)
                    relate_one(storage, relations.getJSONObject(i), path, true);
            }
        } else
            throw new UIException("Bad JSON data");
        request.sendJSONResponse(data);
        request.setOperationPerformed(create ? Operation.CREATE : Operation.UPDATE);
        if (create)
            // XXX should be derivable
            request.setSecondaryRedirectPath(new String[] { "relationships", path });
    } catch (JSONException x) {
        throw new UIException("Failed to parse json: ", x);
    } catch (ExistException x) {
        throw new UIException("Existence exception: ", x);
    } catch (UnimplementedException x) {
        throw new UIException("Unimplemented exception: ", x);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 47 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class RelateDelete method relate_delete.

private void relate_delete(Storage storage, UIRequest request, String path) throws UIException {
    try {
        String source = request.getRequestArgument(RELATION_SOURCE_PARAM);
        String target = request.getRequestArgument(RELATION_TARGET_PARAM);
        String type = request.getRequestArgument(RELATION_TYPE_PARAM);
        String oneway = request.getRequestArgument(RELATION_ONE_WAY_PARAM);
        if (oneway != null && oneway != "") {
            one_way = Boolean.parseBoolean(oneway);
        }
        if (request.isJSON()) {
            JSONObject data = request.getJSONBody();
            if (data.has("source") && data.has("target") && data.has("type")) {
                source = data.getJSONObject("source").getString("recordtype") + "/" + data.getJSONObject("source").getString("csid");
                target = data.getJSONObject("target").getString("recordtype") + "/" + data.getJSONObject("target").getString("csid");
                type = data.getString("type");
                if (data.has("one-way")) {
                    one_way = data.getBoolean("one-way");
                }
            }
        }
        if (source != null && target != null && type != null && source != "" && target != "" && type != "") {
            path = getRelationShipID(storage, source, target, type);
            if (!one_way) {
                // easier to find reverse if have actually sub items
                String reverse = getRelationShipID(storage, target, source, type);
                if (reverse != null)
                    storage.deleteJSON("/relations/main/" + reverse);
            }
        } else {
            // find csids' if delete sent with just relationship csid rather than sub items
            if (!one_way) {
                String rev = findReverse(storage, path);
                if (rev != null)
                    storage.deleteJSON("/relations/main/" + rev);
            }
        }
        storage.deleteJSON("/relations/main/" + path);
        request.setOperationPerformed(Operation.DELETE);
    } catch (ExistException e) {
        throw new UIException("Exist exception deleting ", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented exception deleting ", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    } catch (JSONException e) {
        throw new UIException("Exception building JSON ", e);
    }
}
Also used : JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 48 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class RelateRead method relate_get.

private void relate_get(Storage storage, UIRequest request, String path) throws UIException {
    try {
        JSONObject relation = convertPayload(storage, storage.retrieveJSON("/relations/" + searchPath + "/" + path, new JSONObject()), path);
        request.sendJSONResponse(relation);
    } catch (ExistException e) {
        throw new UIException("JSON Not found ", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    } catch (JSONException e) {
        throw new UIException("Could not build JSON ", e);
    }
}
Also used : JSONObject(org.json.JSONObject) UIException(org.collectionspace.csp.api.ui.UIException) JSONException(org.json.JSONException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 49 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class AuthorizationStorage method updateJSON.

@Override
public void updateJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String filePath, JSONObject jsonObject, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        Map<String, Document> parts = new HashMap<String, Document>();
        Document doc = null;
        for (String section : r.getServicesRecordPathKeys()) {
            String path = r.getServicesRecordPath(section);
            String[] record_path = path.split(":", 2);
            doc = XmlJsonConversion.convertToXml(r, jsonObject, section, "PUT");
            parts.put(record_path[0], doc);
        }
        int status = 0;
        if (r.isMultipart()) {
            ReturnedMultipartDocument docm = conn.getMultipartXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, parts, creds, cache);
            status = docm.getStatus();
        } else {
            ReturnedDocument docm = conn.getXMLDocument(RequestMethod.PUT, r.getServicesURL() + "/" + filePath, doc, creds, cache);
            status = docm.getStatus();
        }
        if (status == 404)
            throw new ExistException("Not found: " + r.getServicesURL() + "/" + filePath);
        if (status > 299 || status < 200)
            throw new UnderlyingStorageException("Bad response " + status, status, r.getServicesURL() + "/" + filePath);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception", e);
    } catch (JSONException e) {
        throw new UnimplementedException("JSONException", e);
    }
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) Document(org.dom4j.Document) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ReturnedMultipartDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 50 with ExistException

use of org.collectionspace.csp.api.persistence.ExistException in project application by collectionspace.

the class AuthoritiesVocabulariesInitialize method initializeVocab.

private void initializeVocab(Storage storage, UIRequest request, String path) throws UIException {
    try {
        if (fInstance == null) {
            // For now simply loop through all the instances one after the other.
            for (Instance instance : r.getAllInstances()) {
                log.info(instance.getID());
                // does instance exist?
                if (createIfMissingAuthority(storage, null, this.r, instance) == -1) {
                    log.warn(String.format("The currently authenticated user does not have sufficient permission to determine if the '%s' authority/term-list is properly initialized.", instance.getID()));
                }
                resetvocabdata(storage, request, instance);
            }
        } else {
            log.info(fInstance.getID());
            resetvocabdata(storage, request, this.fInstance);
        }
    } catch (JSONException e) {
        throw new UIException("Cannot generate JSON", e);
    } catch (ExistException e) {
        throw new UIException("Exist exception", e);
    } catch (UnimplementedException e) {
        throw new UIException("Unimplemented exception", e);
    } catch (UnderlyingStorageException x) {
        UIException uiexception = new UIException(x.getMessage(), x.getStatus(), x.getUrl(), x);
        request.sendJSONResponse(uiexception.getJSON());
    }
}
Also used : Instance(org.collectionspace.chain.csp.schema.Instance) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Aggregations

ExistException (org.collectionspace.csp.api.persistence.ExistException)64 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)57 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)50 JSONObject (org.json.JSONObject)50 JSONException (org.json.JSONException)49 UIException (org.collectionspace.csp.api.ui.UIException)40 JSONArray (org.json.JSONArray)23 IOException (java.io.IOException)10 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)10 Record (org.collectionspace.chain.csp.schema.Record)9 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)8 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)8 Document (org.dom4j.Document)7 ConfigException (org.collectionspace.chain.csp.config.ConfigException)5 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)5 File (java.io.File)4 Field (org.collectionspace.chain.csp.schema.Field)4 Instance (org.collectionspace.chain.csp.schema.Instance)4 Storage (org.collectionspace.csp.api.persistence.Storage)4 RefName (org.collectionspace.services.common.api.RefName)4