use of org.collectionspace.chain.csp.schema.Instance in project application by collectionspace.
the class NullResolver method doMapping.
/**
* Some simple mappings to help with the unfinished functionality to
* allow changing of content on templates based on returned item type
* @param pathinfo
* @param allmappings
* @param spec
* @return
*/
private Map<String, Object> doMapping(String pathinfo, UIMapping[] allmappings, Spec spec) {
Map<String, Object> out = new HashMap<String, Object>();
for (UIMapping map : allmappings) {
out.put("map", map);
out.put("isConfig", false);
out.put("isRecord", false);
out.put("instance", null);
out.put("record", null);
out.put("RecordFile", map.getFile());
out.put("ConfigFile", "");
out.put("File", "");
if (map.hasUrl()) {
if (map.getUrl().equals(pathinfo)) {
return out;
}
} else if (map.hasType()) {
for (Record r : spec.getAllRecords()) {
if (r.isType(map.getType())) {
if (r.isType("authority")) {
for (Instance ins : r.getAllInstances()) {
// config
String tenantInstanceconfig = "/" + spec.getAdminData().getTenantName() + "/config/" + ins.getWebURL() + ".json";
String tenantAuthconfig = "/" + spec.getAdminData().getTenantName() + "/config/" + r.getWebURL() + ".json";
if (pathinfo.equals(tenantInstanceconfig)) {
out.put("instance", ins);
out.put("record", r);
out.put("isConfig", true);
out.put("ConfigFile", tenantAuthconfig);
out.put("File", tenantAuthconfig);
return out;
} else if (pathinfo.equals("/config/" + ins.getWebURL() + ".json")) {
out.put("instance", ins);
out.put("record", r);
out.put("isConfig", true);
out.put("ConfigFile", tenantAuthconfig);
out.put("File", tenantAuthconfig);
return out;
}
// record
if (pathinfo.equals("/" + spec.getAdminData().getTenantName() + "/" + ins.getUIURL())) {
out.put("record", r);
out.put("isRecord", true);
out.put("instance", ins);
out.put("File", map.getFile());
return out;
}
if (pathinfo.equals("/" + spec.getAdminData().getTenantName() + "/html/" + ins.getUIURL())) {
out.put("record", r);
out.put("isRecord", true);
out.put("instance", ins);
out.put("File", map.getFile());
return out;
}
}
}
String test = "/" + spec.getAdminData().getTenantName() + "/html/" + r.getUIURL();
if (pathinfo.equals(test)) {
out.put("record", r);
out.put("isRecord", true);
out.put("File", map.getFile());
return out;
}
}
}
}
}
return null;
}
use of org.collectionspace.chain.csp.schema.Instance in project application by collectionspace.
the class RecordCreateUpdate method assignTerms.
private void assignTerms(Storage storage, String path, JSONObject data) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException, UIException {
JSONObject fields = data.optJSONObject("fields");
String insId = "";
if (fields.has("terms")) {
Record vr = this.spec.getRecord("vocab");
Record thisr = spec.getRecord("vocab");
String sid = fields.getString("shortIdentifier");
String name = fields.getString("displayName");
insId = "vocab-" + sid;
if (create) {
Map<String, String> options = new HashMap<String, String>();
options.put("id", insId);
options.put("title", name);
options.put("web-url", sid);
options.put("title-ref", sid);
Instance ins = new Instance(thisr, options);
vr.addInstance(ins);
}
ctl.get(storage, sid, vr, 0);
}
}
use of org.collectionspace.chain.csp.schema.Instance in project application by collectionspace.
the class UISchema method uiotherschema.
/**
* Create all the other weird schemas that the UI wants
* @param storage
* @param params
* @return
* @throws UIException
* @throws UnauthorizedException
*/
private JSONObject uiotherschema(UISession session, Storage storage, String params) throws UIException, UnauthorizedException {
JSONObject out = new JSONObject();
String sectionname = "";
String sectionid = params.toLowerCase();
if (schema != null) {
sectionid = schema.getID();
sectionname = schema.getWebURL();
}
try {
if (sectionid.toLowerCase().equals("recordlist")) {
JSONObject schema = new JSONObject();
JSONArray recrds = new JSONArray();
for (Record rc : this.spec.getAllRecords()) {
if (rc.isShowType("authority")) {
if (rc.isInRecordList()) {
for (Instance ins : rc.getAllInstances()) {
recrds.put(ins.getWebURL());
}
}
} else if (rc.isShowType("record") || rc.isShowType("procedure")) {
if (rc.isInRecordList()) {
recrds.put(rc.getWebURL());
}
}
}
schema.put("type", "array");
schema.put("default", recrds);
out.put(sectionname, schema);
} else if (sectionid.toLowerCase().equals("namespaces")) {
assertLoginStatus(session);
JSONObject namespaces = new JSONObject();
JSONObject namespacesProps = new JSONObject();
for (Record rc : this.spec.getAllRecords()) {
if (rc.isInRecordList()) {
if (rc.isShowType("authority")) {
JSONObject authInfoProps = new JSONObject();
int cardinal = 0;
for (Instance ins : rc.getAllInstances()) {
JSONObject instanceInfo = new JSONObject();
JSONObject instanceProps = new JSONObject();
JSONObject nptAllowed = new JSONObject();
nptAllowed.put("type", "boolean");
nptAllowed.put("default", ins.getNPTAllowed());
instanceProps.put("nptAllowed", nptAllowed);
// Preserve the order of the namespaces
JSONObject orderProp = new JSONObject();
orderProp.put("type", "integer");
orderProp.put("default", cardinal);
instanceProps.put("order", orderProp);
JSONObject workflowState = new JSONObject();
workflowState.put("type", "string");
workflowState.put("default", getWorkflowState(storage, ins));
instanceProps.put("workflowState", workflowState);
instanceInfo.put("type", "object");
instanceInfo.put("properties", instanceProps);
authInfoProps.put(ins.getWebURL(), instanceInfo);
cardinal++;
}
JSONObject authorityInfo = new JSONObject();
authorityInfo.put("type", "object");
authorityInfo.put("properties", authInfoProps);
namespacesProps.put(rc.getWebURL(), authorityInfo);
}
}
}
namespaces.put("type", "object");
namespaces.put("properties", namespacesProps);
out.put("namespaces", namespaces);
/**
*{
* "namespaces": {
* "type": "object",
* "properties": {
* "person": {
* "type": "object"
* "properties": {
* "person" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* },
* "persontest" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* }
* }
* },
* "organization": {
* "type": "object"
* "properties": {
* "organization" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* },
* "organizationtest" : {
* "type": "object"
* "properties": {
* "nptAllowed": {
* "type": "boolean",
* "default": true
* }
* }
* }
* }
* },
* ...
* }
* }
*}
*/
} else if (sectionid.toLowerCase().equals("recordtypes")) {
JSONObject procedures_schema = new JSONObject();
JSONObject authorities_schema = new JSONObject();
JSONObject admin_schema = new JSONObject();
JSONObject cataloging_schema = new JSONObject();
JSONObject searchAll_schema = new JSONObject();
JSONArray procedures_records = new JSONArray();
JSONArray authorities_records = new JSONArray();
JSONArray admin_records = new JSONArray();
JSONArray cataloging_records = new JSONArray();
JSONArray searchAll_records = new JSONArray();
/**
* { "procedures": { "type": "array", "default": ["loanout",
* "movement", ...] }, "vocabularies": { "type": "array",
* "default": ["person", "organization", ...] }, "cataloging": {
* "type": "array", "default": ["cataloging"] } }
*/
for (Record rc : this.spec.getAllRecords()) {
if (rc.isInRecordList()) {
if (rc.isShowType("procedure")) {
procedures_records.put(rc.getWebURL());
} else if (rc.isShowType("authority")) {
for (Instance ins : rc.getAllInstances()) {
authorities_records.put(ins.getWebURL());
}
} else if (rc.isShowType("record")) {
// FIXME Assumes that "records" are either
// procedures, authorities, or cataloging.
// Should instead have a type "cataloging"
cataloging_records.put(rc.getWebURL());
} else if (rc.isShowType("searchall")) {
searchAll_records.put(rc.getWebURL());
} else if (rc.isShowType("authorizationdata") || rc.isShowType("userdata")) {
admin_records.put(rc.getWebURL());
}
}
}
procedures_schema.put("type", "array");
procedures_schema.put("default", procedures_records);
authorities_schema.put("type", "array");
authorities_schema.put("default", authorities_records);
admin_schema.put("type", "array");
admin_schema.put("default", admin_records);
cataloging_schema.put("type", "array");
cataloging_schema.put("default", cataloging_records);
searchAll_schema.put("type", "array");
searchAll_schema.put("default", searchAll_records);
JSONObject record_types = new JSONObject();
JSONObject types_list = new JSONObject();
types_list.put("procedures", procedures_schema);
types_list.put("vocabularies", authorities_schema);
types_list.put("cataloging", cataloging_schema);
types_list.put("all", searchAll_schema);
types_list.put("administration", admin_schema);
record_types.put("type", "object");
record_types.put("properties", types_list);
out.put("recordtypes", record_types);
}
} catch (JSONException e) {
throw new UIException("Cannot generate UISpec due to JSONException", e);
}
return out;
}
use of org.collectionspace.chain.csp.schema.Instance in project application by collectionspace.
the class WebAutoComplete method doAuthorityAutocomplete.
private JSONArray doAuthorityAutocomplete(CSPRequestCache cache, Storage storage, String fieldname, String start, String vocabConstraint, String pageSize, String pageNum) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException, ConfigException {
FieldSet fs = r.getFieldFullList(fieldname);
JSONArray out = new JSONArray();
Instance[] allInstances = null;
if (fs == null || !(fs instanceof Field)) {
if (r.hasHierarchyUsed("screen")) {
// Configures the hierarchy section.
Structure s = r.getStructure("screen");
if (s.hasOption(fieldname)) {
// This is one of the hierarchy fields
if (vocabConstraint != null) {
allInstances = new Instance[1];
String fullname = r.getID() + "-" + vocabConstraint;
allInstances[0] = r.getSpec().getInstance(fullname);
} else {
Option a = s.getOption(fieldname);
String[] data = a.getName().split(",");
allInstances = new Instance[data.length];
for (int i = 0; i < data.length; i++) {
allInstances[i] = (r.getSpec().getInstance(data[i]));
}
}
} else {
fs = r.getSpec().getRecord("hierarchy").getFieldFullList(fieldname);
if (fs instanceof Field) {
allInstances = ((Field) fs).getAllAutocompleteInstances();
}
}
}
} else {
allInstances = ((Field) fs).getAllAutocompleteInstances();
}
if (allInstances == null) {
// Cannot autocomplete
return out;
}
// support multiassign of autocomplete instances
for (Instance n : allInstances) {
try {
if (n == null) {
// Field has no autocomplete
} else {
String path = n.getRecord().getID() + "/" + n.getTitleRef();
JSONObject restriction = new JSONObject();
if (pageSize != null) {
restriction.put("pageSize", pageSize);
}
if (pageNum != null) {
restriction.put("pageNum", pageNum);
}
FieldSet dispNameFS = n.getRecord().getDisplayNameField();
if (dispNameFS == null) {
throw new ConfigException("WebAutoComplete for Instance has no displayName configured: " + n.getID());
}
String displayNameFieldID = dispNameFS.getID();
// May be something other than display name
restriction.put(displayNameFieldID, start);
JSONObject results = storage.getPathsJSON(path, restriction);
String[] paths = (String[]) results.get("listItems");
for (String csid : paths) {
JSONObject data = storage.retrieveJSON(path + "/" + csid + "/view", new JSONObject());
JSONObject entry = new JSONObject();
// TODO - handle multiple name matches
String displayNameString = data.getString(displayNameFieldID);
JSONArray displayNames = JSONUtils.createJSONArrayFromSeparatedString(displayNameString);
String primaryDN = displayNames.getString(0);
String refid = data.getString("refid");
// HACK - transition period with full instead of base URN value
if (refid.endsWith("'" + primaryDN + "'"))
refid = refid.substring(0, refid.length() - (primaryDN.length() + 2));
entry.put("baseUrn", refid);
entry.put("csid", data.getString("csid"));
entry.put("type", n.getRecord().getWebURL());
entry.put("displayNames", displayNames);
// RefName.AuthorityItem item = RefName.AuthorityItem.parse(refid);
// entry.put("namespace",item.getParentShortIdentifier());
entry.put("namespace", data.getString("namespace"));
entry.put("workflow", data.getString("workflow"));
out.put(entry);
}
}
} catch (UnderlyingStorageException x) {
if (x.getStatus() == 403) {
// permission error - keep calm and carry on
} else {
throw x;
}
}
}
// Instance n=((Field)fs).getAutocompleteInstance();
return out;
}
use of org.collectionspace.chain.csp.schema.Instance in project application by collectionspace.
the class WebReset method reset.
private void reset(Storage storage, UIRequest request, String path) throws UIException {
// remember to log into the front end before trying to run this
JSONObject data = new JSONObject();
TTYOutputter tty = request.getTTYOutputter();
// Temporary hack to reset db
try {
data = storage.getPathsJSON("/", null);
String[] paths = (String[]) data.get("listItems");
if (!path.equals("nodelete")) {
// Delete existing records
for (String dir : paths) {
Record r = null;
log.info(dir);
if ("direct".equals(dir) || "relations".equals(dir))
continue;
try {
r = this.spec.getRecord(dir);
} catch (Exception e) {
continue;
}
if (r.isType("procedure")) {
if ("termlistitem".equals(dir) || "termlist".equals(dir))
continue;
// Nothing to do for the pseudo-records
if (r.isType("searchall")) {
continue;
}
} else if (r.isType("authority")) {
continue;
} else if (r.isType("record")) {
if (// Filter out self-renderers, etc
"hierarchy".equals(dir) || !r.isRealRecord())
continue;
log.info("S");
} else if (r.isType("authorizationdata")) {
continue;
} else if (r.isType("userdata")) {
continue;
} else {
// ignore - have no idea what it is
continue;
}
// if("place".equals(dir) || "vocab".equals(dir) || "contact".equals(dir) || "location".equals(dir) || "person".equals(dir) || "organization".equals(dir) || "taxon".equals(dir)){
// continue;
// }
// ignore authorization
// if("rolePermission".equals(dir) || "accountrole".equals(dir) || "accountroles".equals(dir) || "userperm".equals(dir)|| "permrole".equals(dir) || "permission".equals(dir) || "role".equals(dir)|| "userrole".equals(dir) || "users".equals(dir) ){
// continue;
// }
// ignore other - tho we do need to clean these up
// if("termlistitem".equals(dir) ||"termlist".equals(dir) || "reports".equals(dir) || "reporting".equals(dir) || "output".equals(dir) )
// continue;
// // ignore other - tho we do need to clean these up
// if("hierarchy".equals(dir) || "dimension".equals(dir) ||"structureddate".equals(dir) ||"blobs".equals(dir) ||"relations".equals(dir) || "direct".equals(dir) || "id".equals(dir) )
// continue;
log.info("Deleteing data associated with : " + dir);
tty.line("Deleteing data associated with : " + dir);
JSONObject data2 = storage.getPathsJSON(dir, null);
String[] paths2 = (String[]) data2.get("listItems");
for (int i = 0; i < paths2.length; i++) {
tty.line("path : " + dir + "/" + paths2[i]);
try {
storage.deleteJSON(dir + "/" + paths2[i]);
} catch (UnimplementedException e) {
tty.line("UnimplementedException" + e);
tty.line("ux");
} catch (UnderlyingStorageException e) {
tty.line("UnderlyingStorageEception" + e);
}
tty.line("ok");
tty.flush();
}
}
}
log.info("Creating records and procedures: this might take some time, go get a cup of tea and be patient");
tty.line("Creating records and procedures: this might take some time, go get a cup of tea and be patient");
// Create records anew
tty.line("Create records anew");
String schedule = getResource("reset.txt");
for (String line : schedule.split("\n")) {
String[] parts = line.split(" +", 2);
if (!parts[0].equals("")) {
tty.line("Creating " + parts[0]);
log.info("Creating " + parts[0]);
storage.autocreateJSON(parts[0], getJSONResource(parts[1]), null);
tty.flush();
}
}
log.info("Delete existing vocab/auth entries");
tty.line("Delete existing vocab/auth entries");
// Delete existing vocab entries
JSONObject myjs = new JSONObject();
myjs.put("pageSize", "10");
myjs.put("pageNum", "0");
for (String dir : paths) {
try {
if (this.spec.hasRecord(dir)) {
if (dir.equalsIgnoreCase("vocab") && path.equalsIgnoreCase("keepVocabs")) {
// don't reset the term lists
continue;
}
Record r = this.spec.getRecord(dir);
if (r.isType("authority")) {
for (Instance n : r.getAllInstances()) {
String url = r.getID() + "/" + n.getTitleRef();
try {
storage.getPathsJSON(url, new JSONObject()).toString();
} catch (UnderlyingStorageException x) {
JSONObject fields = new JSONObject("{'displayName':'" + n.getTitle() + "','shortIdentifier':'" + n.getWebURL() + "'}");
String base = r.getID();
storage.autocreateJSON(base, fields, null);
log.info("Instance " + n.getID() + " Created");
tty.line("Instance " + n.getID() + " Created");
}
deletall(n, r, url, "Deleting " + url, storage, data, tty, myjs);
}
}
}
} catch (Exception e) {
log.info("that was weird but probably not an issue " + e.getMessage());
}
}
log.info("Creating Dummy data");
tty.line("Creating Dummy data");
tty.flush();
// Create vocab entries
String names = getResource("names.txt");
int i = 0;
for (String nextName : names.split("\n")) {
i++;
JSONObject entry = createTrivialAuthItem(PERSON_TERMLIST_ELEMENT, nextName);
storage.autocreateJSON("/person/person", entry, null);
tty.line("Created Person " + entry);
log.info("Created Person " + entry);
tty.flush();
if (quick && i > 20)
break;
}
// Create vocab entries
String orgs = getResource("orgs.txt");
i = 0;
for (String nextName : orgs.split("\n")) {
i++;
JSONObject entry = createTrivialAuthItem(ORG_TERMLIST_ELEMENT, nextName);
storage.autocreateJSON("/organization/organization", entry, null);
tty.line("Created Organisation " + nextName);
log.info("Created Organisation " + nextName);
tty.flush();
if (quick && i > 20)
break;
}
tty.line("done");
log.info("done");
} catch (ExistException e) {
log.info("ExistException " + e.getLocalizedMessage());
tty.line("ExistException " + e.getLocalizedMessage());
throw new UIException("Existence problem", e);
} catch (UnimplementedException e) {
log.info("UnimplementedException " + e.getLocalizedMessage());
tty.line("UnimplementedException " + e.getLocalizedMessage());
throw new UIException("Unimplemented ", e);
} catch (UnderlyingStorageException x) {
log.info("UnderlyingStorageException " + x.getLocalizedMessage());
tty.line("UnderlyingStorageException " + x.getLocalizedMessage());
throw new UIException("Problem storing" + x.getLocalizedMessage(), x.getStatus(), x.getUrl(), x);
} catch (JSONException e) {
log.info("JSONException " + e.getLocalizedMessage());
tty.line("JSONException " + e.getLocalizedMessage());
throw new UIException("Invalid JSON", e);
} catch (IOException e) {
log.info("IOException " + e.getLocalizedMessage());
tty.line("IOException " + e.getLocalizedMessage());
throw new UIException("IOException", e);
}
}
Aggregations