Search in sources :

Example 16 with Field

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

the class WebReset method deletall.

private JSONObject deletall(Instance n, Record thisr, String path, String msg, Storage storage, JSONObject data, TTYOutputter tty, JSONObject myjs) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException, UIException {
    int resultsize;
    int check;
    String checkpagination;
    resultsize = 1;
    check = 0;
    checkpagination = "";
    while (resultsize > 0) {
        myjs.put("pageNum", check);
        // String url = thisr.getID()+"/"+n.getTitleRef();
        if (thisr == null || n == null) {
            String[] bits = path.split("/");
            thisr = this.spec.getRecordByWebUrl(bits[1]);
            n = thisr.getInstance(bits[1] + "-" + bits[2]);
        }
        try {
            data = storage.getPathsJSON(path, myjs);
        } 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[] res = (String[]) data.get("listItems");
        if (res.length == 0 || checkpagination.equals(res[0])) {
            resultsize = 0;
            break;
        // testing whether we have actually returned the same page or the next page - all csid returned should be unique
        } else {
            checkpagination = res[0];
        }
        resultsize = res.length;
        for (String urn : res) {
            try {
                storage.deleteJSON(path + "/" + urn);
                tty.line(msg + urn);
                log.info(msg + urn);
            } catch (Exception e) {
            /* Sometimes records are wdged */
            }
            tty.flush();
        }
    }
    return data;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException) JSONException(org.json.JSONException) UIException(org.collectionspace.csp.api.ui.UIException) 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) ConfigException(org.collectionspace.chain.csp.config.ConfigException)

Example 17 with Field

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

the class CacheTermList method controlledLists.

public JSONArray controlledLists(Storage storage, String vocabname, Record vr, Integer limit) throws JSONException {
    JSONArray displayNames = new JSONArray();
    try {
        // Get List
        int resultsize = 1;
        int pagenum = 0;
        int pagesize = 200;
        if (limit != 0 && limit < pagesize) {
            pagesize = limit;
        }
        while (resultsize > 0) {
            JSONObject restriction = new JSONObject();
            restriction.put("pageNum", pagenum);
            restriction.put("pageSize", pagesize);
            String url = vr.getID() + "/" + vocabname;
            JSONObject data = null;
            try {
                data = storage.getPathsJSON(url, restriction);
            } catch (UnderlyingStorageException e) {
                // need to initialise this vocab
                log.error("Could not retreive term list with URL '%s' from Services layer.", url);
                // REM 8/28/2015 - This code is broken? See https://issues.collectionspace.org/browse/CSPACE-6151
                Instance n = vr.getInstance(vr.getID() + "-" + vocabname);
                JSONObject fields = new JSONObject("{'displayName':'" + n.getTitle() + "','shortIdentifier':'" + n.getWebURL() + "'}");
                if (vr.getFieldFullList("termStatus") instanceof Field) {
                    fields.put("termStatus", ((Field) vr.getFieldFullList("termStatus")).getOptionDefault());
                }
                storage.autocreateJSON(vr.getID(), fields, restriction);
                data = storage.getPathsJSON(url, restriction);
            }
            if (data.has("listItems")) {
                String[] results = (String[]) data.get("listItems");
                /* Get a view of each */
                for (String result : results) {
                    // change csid into displayName
                    JSONObject namedata = getDisplayNameList(storage, vr.getID(), vocabname, result);
                    displayNames.put(namedata);
                }
                Integer total = data.getJSONObject("pagination").getInt("totalItems");
                pagesize = data.getJSONObject("pagination").getInt("pageSize");
                // Integer itemsInPage = data.getJSONObject("pagination").getInt("itemsInPage");
                pagenum = data.getJSONObject("pagination").getInt("pageNum");
                pagenum++;
                // are there more results
                if (total <= (pagesize * (pagenum))) {
                    break;
                }
                // have we got enough results?
                if (limit != 0 && limit <= (pagesize * (pagenum))) {
                    break;
                }
            } else {
                resultsize = 0;
            }
        }
    } catch (ExistException e) {
        throw new JSONException("Exist exception");
    } catch (UnimplementedException e) {
        throw new JSONException("Unimplemented exception");
    } catch (UnderlyingStorageException e) {
        throw new JSONException("Underlying storage exception" + vocabname + e);
    }
    return displayNames;
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) JSONObject(org.json.JSONObject) Instance(org.collectionspace.chain.csp.schema.Instance) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ExistException(org.collectionspace.csp.api.persistence.ExistException) UnimplementedException(org.collectionspace.csp.api.persistence.UnimplementedException)

Example 18 with Field

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

the class MakeXsd method generateDataEntry.

private void generateDataEntry(Element ele, FieldSet fs, Namespace ns, Element root, Boolean unbounded) throws Exception {
    // 
    if (fs.isInServices() == false) {
        sendToDebugLog(String.format("Field set is not part of the Services schema %s:%s", fs.getSection(), fs.getID()));
        return;
    }
    String sectionName = fs.getSection();
    String listName = fs.getServicesTag();
    // to the "GroupField's" structured types -e.g., structuredData and dimension types
    if (fs.isAGroupField() == true) {
        String groupFieldType = generateFieldGroup(fs, ele, ns, root);
        fs.setServicesType(groupFieldType);
    }
    if (fs.isAGroupField() && fs.isAStructureDate() == false) {
        // 
        return;
    }
    if (fs instanceof Field || fs instanceof Group) {
        String servicesTag = fs.getServicesTag();
        if (isOrphaned(fs) == true) {
            // If we have a Repeat with a single child that is a "Group" with a "ui-type=groupfield/foo" attribute.
            servicesTag = fs.getParentID();
            unbounded = true;
        }
        // <xs:element name="csid" type="xs:string"/>
        Element field = ele.addElement(new QName("element", ns));
        if (fs.isAGroupField() && fs.isAStructureDate() == false) {
            servicesTag = fs.getServicesType();
        }
        field.addAttribute("name", servicesTag);
        String servicesType = fs.getServicesType();
        String fieldType = "xs:string";
        if (servicesType != null) {
            fieldType = servicesType;
        }
        field.addAttribute("type", fieldType);
        if (unbounded == true) {
            field.addAttribute("minOccurs", "0");
            field.addAttribute("maxOccurs", "unbounded");
        }
    }
    if (isRepeatType(fs) == true) {
        // Has to be a Repeat class instance and not any descendant (e.g. not a Group instance)
        Element fieldElement = root;
        Repeat rfs = (Repeat) fs;
        if (rfs.hasServicesParent()) {
            // group repeatable
            // <xs:element name="objectNameList" type="ns:objectNameList"/>
            Element newField = ele.addElement(new QName("element", ns));
            newField.addAttribute("name", rfs.getServicesParent()[0]);
            String fieldType = rfs.getServicesParent()[0];
            newField.addAttribute("type", FieldSet.NS + fieldType);
        } else {
            // single repeatable
            // <xs:element name="responsibleDepartments"
            // type="responsibleDepartmentList"/>
            fieldElement = ele.addElement(new QName("element", ns));
            fieldElement.addAttribute("name", rfs.getServicesTag());
            FieldSet[] fieldSetArray = rfs.getChildren("");
            if (fieldSetArray != null && fieldSetArray.length > 0) {
                if (rfs.isServicesAnonymousType() == true) {
                    // Ends up creating an embedded anonymous complex type
                    listName = null;
                } else {
                    listName = rfs.getChildren("")[0].getServicesTag() + "List";
                    fieldElement.addAttribute("type", FieldSet.NS + listName);
                    fieldElement = fieldElement.getParent();
                }
            } else {
                // If there is no children to define the type, there better be an explicit services type declaration
                String servicesType = rfs.getServicesType();
                if (servicesType != null) {
                    fieldElement.addAttribute("type", servicesType);
                } else {
                    sendToErrorLog("Repeat/Group fieldset child array was null or the first element was null. Field attribute name: " + fieldElement.toString());
                }
            }
        }
        generateRepeat(rfs, fieldElement, listName, ns, root);
    }
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) Group(org.collectionspace.chain.csp.schema.Group) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) QName(org.dom4j.QName) Element(org.dom4j.Element) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 19 with Field

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

the class UserStorage method getPaths.

@Override
@SuppressWarnings("unchecked")
public String[] getPaths(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        List<String> out = new ArrayList<String>();
        Iterator rit = restrictions.keys();
        StringBuffer args = new StringBuffer();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            FieldSet fs = r.getFieldTopLevel(key);
            if (!(fs instanceof Field))
                continue;
            String filter = ((Field) fs).getServicesFilterParam();
            if (filter == null)
                continue;
            args.append('&');
            args.append(filter);
            args.append('=');
            args.append(URLEncoder.encode(restrictions.getString(key), "UTF-8"));
        }
        // pagination
        String tail = args.toString();
        String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), tail, false, "");
        ReturnedDocument doc = conn.getXMLDocument(RequestMethod.GET, path, null, creds, cache);
        if (doc.getStatus() < 200 || doc.getStatus() > 399)
            throw new UnderlyingStorageException("Cannot retrieve account list", doc.getStatus(), path);
        Document list = doc.getDocument();
        List<Node> objects = list.selectNodes(r.getServicesListPath());
        for (Node object : objects) {
            List<Node> fields = object.selectNodes("*");
            String csid = object.selectSingleNode("csid").getText();
            for (Node field : fields) {
                if ("csid".equals(field.getName())) {
                    int idx = csid.lastIndexOf("/");
                    if (idx != -1)
                        csid = csid.substring(idx + 1);
                    out.add(csid);
                } else if ("uri".equals(field.getName())) {
                // Skip!
                } else {
                    String json_name = view_map.get(field.getName());
                    if (json_name != null) {
                        String value = field.getText();
                        // XXX hack to cope with multi values
                        if (value == null || "".equals(value)) {
                            List<Node> inners = field.selectNodes("*");
                            for (Node n : inners) {
                                value += n.getText();
                            }
                        }
                        setGleanedValue(cache, r.getServicesURL() + "/" + csid, json_name, value);
                    }
                }
            }
        }
        return out.toArray(new String[0]);
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    }
}
Also used : Node(org.dom4j.Node) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) Document(org.dom4j.Document) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ReturnedDocument(org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Example 20 with Field

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

the class UserStorage method getPathsJSON.

@Override
@SuppressWarnings("unchecked")
public JSONObject getPathsJSON(ContextualisedStorage root, CSPRequestCredentials creds, CSPRequestCache cache, String rootPath, JSONObject restrictions) throws ExistException, UnimplementedException, UnderlyingStorageException {
    try {
        JSONObject out = new JSONObject();
        List<String> listitems = new ArrayList<String>();
        Iterator rit = restrictions.keys();
        StringBuffer args = new StringBuffer();
        while (rit.hasNext()) {
            String key = (String) rit.next();
            FieldSet fs = r.getFieldTopLevel(key);
            if (!(fs instanceof Field))
                continue;
            String filter = ((Field) fs).getServicesFilterParam();
            if (filter == null)
                continue;
            args.append('&');
            args.append(filter);
            args.append('=');
            args.append(URLEncoder.encode(restrictions.getString(key), "UTF-8"));
        }
        // pagination
        String tail = args.toString();
        String path = getRestrictedPath(r.getServicesURL(), restrictions, r.getServicesSearchKeyword(), tail, false, "");
        if (r.hasSoftDeleteMethod()) {
            path = softpath(path);
        }
        if (r.hasHierarchyUsed("screen")) {
            path = hierarchicalpath(path);
        }
        JSONObject data = getListView(creds, cache, path, r.getServicesListPath(), "csid", false, r);
        return data;
    } catch (ConnectionException e) {
        throw new UnderlyingStorageException("Service layer exception" + e.getLocalizedMessage(), e.getStatus(), e.getUrl(), e);
    } catch (UnsupportedEncodingException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        throw new UnderlyingStorageException("Exception building query" + e.getLocalizedMessage(), e);
    }
}
Also used : Field(org.collectionspace.chain.csp.schema.Field) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) UnderlyingStorageException(org.collectionspace.csp.api.persistence.UnderlyingStorageException) ConnectionException(org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)

Aggregations

Field (org.collectionspace.chain.csp.schema.Field)45 JSONObject (org.json.JSONObject)37 JSONArray (org.json.JSONArray)26 FieldSet (org.collectionspace.chain.csp.schema.FieldSet)22 UnderlyingStorageException (org.collectionspace.csp.api.persistence.UnderlyingStorageException)14 Group (org.collectionspace.chain.csp.schema.Group)12 Record (org.collectionspace.chain.csp.schema.Record)12 ConnectionException (org.collectionspace.chain.csp.persistence.services.connection.ConnectionException)9 Repeat (org.collectionspace.chain.csp.schema.Repeat)9 JSONException (org.json.JSONException)9 ReturnedDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedDocument)7 Document (org.dom4j.Document)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ReturnedMultipartDocument (org.collectionspace.chain.csp.persistence.services.connection.ReturnedMultipartDocument)6 Element (org.dom4j.Element)6 ArrayList (java.util.ArrayList)5 Instance (org.collectionspace.chain.csp.schema.Instance)5 UnimplementedException (org.collectionspace.csp.api.persistence.UnimplementedException)5 HashMap (java.util.HashMap)4 Option (org.collectionspace.chain.csp.schema.Option)4