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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations