Search in sources :

Example 11 with Record

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

the class MakeXsd method isDefinedInAnotherTenant.

private String isDefinedInAnotherTenant(String complexTypeName, String xsdDef, String currentFieldName) throws Exception {
    String result = null;
    for (String tenantConfigName : xsdGeneration.getTenantConfigMap().keySet()) {
        ServiceConfigGeneration x = xsdGeneration.getTenantConfigMap().get(tenantConfigName);
        Map<Record, Map<String, Map<String, String>>> recordComplexTypesMap = x.getRecordDefinedComplexTypesMap();
        for (Record record : recordComplexTypesMap.keySet()) {
            Map<String, Map<String, String>> schemaComplexTypesMap = recordComplexTypesMap.get(record);
            for (String schemaName : schemaComplexTypesMap.keySet()) {
                Map<String, String> complexTypesMap = schemaComplexTypesMap.get(schemaName);
                if (complexTypesMap.get(complexTypeName) != null) {
                    if (complexTypesMap.get(complexTypeName).equals(xsdDef)) {
                        // return the tenantConfig + record name + schema name
                        return tenantConfigName + ':' + record.getRecordName() + ":" + schemaName;
                    } else {
                        String msg = String.format("The group type (XSD complex type) '%s' of field '%s' in schema '%s' of record '%s' was previously defined differently in record '%s' inside schema '%s' of tenant config '%s'.", complexTypeName, currentFieldName, currentSchemaName, this.xsdRecord.getRecordName(), record.getRecordName(), schemaName, tenantConfigName);
                        sendToErrorLog(msg);
                        logXsdDiffs(this.configFileName, complexTypesMap.get(complexTypeName), xsdDef);
                        throw new Exception(msg);
                    }
                }
            }
        }
    }
    return result;
}
Also used : Record(org.collectionspace.chain.csp.schema.Record) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 12 with Record

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

the class MakeXsd method isDefinedInAnotherRecord.

/**
 * Check to see if a complex type has already been defined in one of the schemas in one of the records of the current tenant.  If we find a
 * duplicate definition, check that definition is identical.  If not, throw an exception.  If identical, return a concatenation of the defining record and schema
 * @param complexTypeName
 * @param xsdDef
 * @param currentFieldName
 * @return
 * @throws Exception
 */
private String isDefinedInAnotherRecord(String complexTypeName, String xsdDef, String currentFieldName) throws Exception {
    String result = null;
    Map<Record, Map<String, Map<String, String>>> recordDefinedGroupTypes = this.xsdGeneration.getRecordDefinedComplexTypesMap();
    for (Record record : recordDefinedGroupTypes.keySet()) {
        for (String schemaName : recordDefinedGroupTypes.get(record).keySet()) {
            Map<String, String> groupTypeMap = recordDefinedGroupTypes.get(record).get(schemaName);
            if (groupTypeMap.get(complexTypeName) != null) {
                if (groupTypeMap.get(complexTypeName).equalsIgnoreCase(xsdDef)) {
                    // return the recordname and schema name
                    return record.getRecordName() + ":" + schemaName;
                } else {
                    String msg = String.format("The group type (XSD type) '%s' of field '%s' in schema '%s' of record '%s' was previously defined differently in record '%s' inside schema '%s'.", complexTypeName, currentFieldName, currentSchemaName, this.xsdRecord.getRecordName(), record.getRecordName(), schemaName);
                    sendToErrorLog(msg);
                    logXsdDiffs(configFileName, groupTypeMap.get(complexTypeName), xsdDef);
                    throw new Exception(msg);
                }
            }
        }
    }
    return result;
}
Also used : Record(org.collectionspace.chain.csp.schema.Record) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 13 with Record

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

the class MakeXsd method generateFieldGroup.

/*
	 * This method generates an XML Schema complex type definition for "ui-type" config attributes -e.g., "groupfield/structureddate"
	 */
private String generateFieldGroup(FieldSet fieldSet, Element ele, Namespace ns, Element root) throws Exception {
    String fieldSetServicesType = fieldSet.getServicesType(false);
    Spec spec = fieldSet.getRecord().getSpec();
    // find a record that corresponds to the fieldset's service type
    Record record = spec.getRecord(fieldSetServicesType);
    if (record == null) {
        // try again, this time try a group type
        record = spec.getRecord(fieldSetServicesType);
    }
    if (record == null) {
        String msg = String.format("Could not find parent record for field '%s'.", fieldSet.toString());
        throw new Exception(msg);
    }
    String servicesType = record.getServicesType();
    // 
    // Special case for backwards compat with existing "dateGroup" table/schema in Services
    // 
    String servicesGroupType = fieldSet.getServicesGroupType(false);
    if (fieldSetServicesType.equalsIgnoreCase(servicesGroupType) == false) {
        servicesType = servicesGroupType;
    }
    if (servicesType == null) {
        servicesType = fieldSetServicesType;
    }
    boolean definedNewType = false;
    Element currentElement = ele;
    Element complexElement = currentElement;
    // 
    if (fieldSet.isAStructureDate() == true) {
        @SuppressWarnings("unused") String // for debugging puposes only
        parentRecordName = fieldSet.getRecord().getID();
        // We'll add the structured date type def to the root/outer level
        currentElement = root;
        complexElement = root.addElement(new QName("complexType", ns));
        complexElement.addAttribute("name", servicesType);
        definedNewType = true;
        Element sequenced = complexElement.addElement(new QName("sequence", ns));
        currentElement = sequenced;
    } else {
        String msg = String.format("Not a structuredDate fieldset.  Field set services type: '%s', Services type: '%s'", fieldSetServicesType, servicesType);
        sendToDebugLog(msg);
    }
    for (FieldSet subRecordFieldSet : record.getAllFieldTopLevel("")) {
        generateDataEntry(currentElement, subRecordFieldSet, ns, root, false);
    }
    if (definedNewType == true) {
        String complexElementXSD = complexElement.asXML();
        if (isValidNewType(servicesType, complexElementXSD, fieldSet.getID()) == true) {
            if (isDefinedLocally(servicesType, complexElementXSD) == false) {
                recordDefinedComplexTypes.put(servicesType, complexElementXSD);
                sendToDebugLog(String.format("The complex type '%s' is defined as: %s", servicesType, complexElementXSD));
            } else {
                // No need to define this type more than once in the current schema, so remove the element we just created.
                root.remove(complexElement);
                sendToDebugLog(String.format("The complex type '%s'  redefinition will be ingnored.  Def of: %s", servicesType, complexElementXSD));
            }
        }
    }
    return servicesType;
}
Also used : FieldSet(org.collectionspace.chain.csp.schema.FieldSet) QName(org.dom4j.QName) Element(org.dom4j.Element) Record(org.collectionspace.chain.csp.schema.Record) TenantSpec(org.collectionspace.chain.csp.persistence.services.TenantSpec) Spec(org.collectionspace.chain.csp.schema.Spec) IOException(java.io.IOException)

Example 14 with Record

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

the class ConfiguredVocabStorage method getXPathForField.

/**
 * Returns an XPath-conformant string that specifies the full (X)path to this field.
 * May recurse to handle nested fields. Will choose primary, in lists (i.e, 1st element)
 * This should probably live in Field.java, not here (but needs to be in both Field.java
 * and Repeat.java - do I hear "Base Class"?!?!)
 *
 * @param fieldSet the containing fieldSet
 * @return NXQL conformant specifier.
 */
public static String getXPathForField(FieldSet fieldSet) {
    String specifier = fieldSet.getServicesTag();
    // leaf, and the first part is held in the "services parent"
    if (fieldSet.hasServicesParent()) {
        // Prepend the services parent field, and make the child a wildcard
        String[] svcsParent = fieldSet.getServicesParent();
        if (svcsParent[0] != null && !svcsParent[0].isEmpty()) {
            specifier = svcsParent[0] + "/";
            // Note that we do not handle paths more the 2 in length - makes no sense
            if (svcsParent.length < 2) {
                specifier += XPATH_GENERIC_FIRST_EL;
            } else if (svcsParent[1] == null) {
                // Work around ridiculous hack/nonsense in Repeat init
                specifier += fieldSet.getServicesTag();
            } else {
                specifier += svcsParent[1];
            }
            specifier += XPATH_FIRST_EL;
        }
    }
    FieldParent parent = fieldSet.getParent();
    // Assume we are recursing until we see otherwise
    boolean isRootLevelField = false;
    if (parent instanceof Record) {
        // A simple reference to base field.
        isRootLevelField = true;
        log.debug("Specifier for root-level field: " + specifier + " is: " + specifier);
    } else {
        FieldSet parentFieldSet = (FieldSet) parent;
        // "repeator" marks things for some expansion - not handled here (?)
        if (parentFieldSet.getSearchType().equals("repeator")) {
            isRootLevelField = true;
        } else {
            // First, recurse to get the fully qualified path to the parent.
            if (log.isDebugEnabled()) {
                String parentID = parentFieldSet.getID();
                log.debug("Recursing for parent: " + parentID);
            }
            specifier = getXPathForField(parentFieldSet);
            // Is parent a scalar list or a complex list?
            Repeat rp = (Repeat) parentFieldSet;
            FieldSet[] children = rp.getChildren("");
            int size = children.length;
            // or a complex schema from which only 1 field is used, will break this.
            if (size > 1) {
                // The parent is a complex schema, not just a scalar repeat
                // Append the field name to build an XPath-like specifier.
                specifier += "/" + fieldSet.getServicesTag();
            } else {
            // Leave specifier as is. We just search on the parent name,
            // as the backend is smart about scalar lists.
            }
        }
        log.debug("Specifier for non-leaf field: " + fieldSet.getServicesTag() + " is: " + specifier);
    }
    if (isRootLevelField) {
    // TODO - map leaf names like "titleGroupList/titleGroup" to "titleGroupList/*"
    }
    return specifier;
}
Also used : FieldParent(org.collectionspace.chain.csp.schema.FieldParent) FieldSet(org.collectionspace.chain.csp.schema.FieldSet) Record(org.collectionspace.chain.csp.schema.Record) Repeat(org.collectionspace.chain.csp.schema.Repeat)

Example 15 with Record

use of org.collectionspace.chain.csp.schema.Record 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)

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