diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java index 1a70f91b5..da35832ec 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/EditConfigurationVTwo.java @@ -26,6 +26,7 @@ import org.json.JSONObject; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.FieldVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfiguration; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.ModelSelector; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.StandardModelSelector; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.StandardWDFSelector; @@ -211,19 +212,19 @@ public class EditConfigurationVTwo { //sparql for additional uris in scope editConfig.setSparqlForAdditionalUrisInScope( this.copy(this.getSparqlForAdditionalUrisInScope(), - (Map) new HashMap())); + (Map) new HashMap())); //sparql for additional literals in scope editConfig.setSparqlForAdditionalLiteralsInScope( this.copy(this.getSparqlForAdditionalLiteralsInScope(), - (Map) new HashMap())); + (Map) new HashMap())); //sparql for existing literals editConfig.setSparqlForExistingLiterals( this.copy(this.getSparqlForExistingLiterals(), - (Map) new HashMap())); + (Map) new HashMap())); //sparql for existing uris editConfig.setSparqlForExistingUris( this.copy(this.getSparqlForExistingUris(), - (Map) new HashMap())); + (Map) new HashMap())); //TODO: Ensure this is true copy of field and not just shallow copy with same references Map fields = this.getFields(); editConfig.setFields(fields); @@ -935,4 +936,59 @@ public class EditConfigurationVTwo { // TODO Auto-generated method stub return this.formSpecificData; } + + public void addNewResource(String key, String namespace){ + if( key == null || key.isEmpty() ) throw new IllegalArgumentException("key of new resource must not be null"); + Map map = getNewResources(); + if( map == null ) { + map = new HashMap(); + map.put(key, namespace); + setNewResources(map); + }else{ + map.put(key, namespace); + } + } + + public void addSparqlForExistingLiteral(String key, String sparql){ + if( key == null || key.isEmpty() ) throw new IllegalArgumentException("key must not be null"); + if( sparql == null || sparql .isEmpty() ) throw new IllegalArgumentException("sparql must not be null"); + + Map map = getSparqlForExistingLiterals(); + if( map == null ) { + map = new HashMap(); + map.put(key, sparql); + setSparqlForExistingLiterals(map); + }else{ + map.put(key, sparql); + } + } + + public void addSparqlForExistingUris(String key, String sparql){ + if( key == null || key.isEmpty() ) throw new IllegalArgumentException("key must not be null"); + if( sparql == null || sparql .isEmpty() ) throw new IllegalArgumentException("sparql must not be null"); + + Map map = getSparqlForExistingUris(); + if( map == null ) { + map = new HashMap(); + map.put(key, sparql); + setSparqlForExistingUris(map); + }else{ + map.put(key, sparql); + } + } + + public void addField( FieldVTwo field){ + if( field == null ) throw new IllegalArgumentException("field must not be null"); + if( field.getName() == null || field.getName().isEmpty() ) throw new IllegalArgumentException("field must not have null or empty name"); + + Map map = getFields(); + if( map == null ) + setFields( new HashMap()); + + if( map.containsKey(field.getName() )) + throw new IllegalArgumentException("adding filed that is already in the fieild list"); + + map.put( field.getName(), field); + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/FieldVTwo.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/FieldVTwo.java index 6c6d1849e..b5fa2d5d6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/FieldVTwo.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/VTwo/FieldVTwo.java @@ -125,55 +125,11 @@ public class FieldVTwo { private static String[] parameterNames = {"editElement","newResource","validators","optionsType","predicateUri","objectClassUri","rangeDatatypeUri","rangeLang","literalOptions","assertions"}; static{ Arrays.sort(parameterNames); } - public void setEditElement(EditElementVTwo editElement){ + public FieldVTwo setEditElement(EditElementVTwo editElement){ this.editElement = editElement; + return this; } - - /** - * A field may specify a class for additional features. - */ - private void setEditElement(JSONObject fieldConfigObj, String fieldName) { - String className = fieldConfigObj.optString("editElement"); - if( className == null || className.isEmpty() ) - return; - setOptionsType(FieldVTwo.OptionsType.UNDEFINED); - Class clz = null; - try { - clz = Class.forName(className); - } catch (ClassNotFoundException e) { - log.error("Java Class " + className + " not found for field " + name); - return; - } catch (SecurityException e) { - log.error("Problem with Java Class " + className + " for field " + name, e); - return; - } catch (IllegalArgumentException e) { - log.error("Problem with Java Class " +className + " for field " + name, e); - return; - } - - Class[] types = new Class[]{ FieldVTwo.class }; - Constructor cons; - try { - cons = clz.getConstructor(types); - } catch (SecurityException e) { - log.error("Problem with Java Class " + className + " for field " + name, e); - return; - } catch (NoSuchMethodException e) { - log.error("Java Class " + className + " must have a constructor that takes a Field.", e); - return; - } - Object[] args = new Object[] { this }; - Object obj; - try { - obj = cons.newInstance(args); - } catch (Exception e) { - log.error("Problem with Java Class " + className + " for field " + name, e); - return; - } - - editElement = (EditElementVTwo)obj; - } - + /* ****************** Getters and Setters ******************************* */ public String getName(){ @@ -184,40 +140,50 @@ public class FieldVTwo { return retractions; } - public void setRetractions(List retractions) { + public FieldVTwo setRetractions(List retractions) { this.retractions = retractions; + return this; } public List getAssertions() { return assertions; } - public void setAssertions(List assertions) { + public FieldVTwo setAssertions(List assertions) { this.assertions = assertions; + return this; } + public FieldVTwo setAssertions( String ... assertions ){ + return setAssertions( Arrays.asList( assertions )); + } + public boolean isNewResource() { return newResource; } - public void setNewResource(boolean b) { + public FieldVTwo setNewResource(boolean b) { newResource = b; + return this; } public List getValidators() { return validators; } - public void setValidators(List v) { + public FieldVTwo setValidators(List v) { validators = v; + return this; } public OptionsType getOptionsType() { return optionsType; } - public void setOptionsType(OptionsType ot) { + public FieldVTwo setOptionsType(OptionsType ot) { optionsType = ot; + return this; } - public void setOptionsType(String s) { + public FieldVTwo setOptionsType(String s) { setOptionsType( getOptionForString(s)); + return this; } public static OptionsType getOptionForString(String s){ @@ -255,93 +221,57 @@ public class FieldVTwo { public String getPredicateUri() { return predicateUri; } - public void setPredicateUri(String s) { + public FieldVTwo setPredicateUri(String s) { predicateUri = s; + return this; } public String getObjectClassUri() { return objectClassUri; } - public void setObjectClassUri(String s) { + public FieldVTwo setObjectClassUri(String s) { objectClassUri = s; + return this; } public String getRangeDatatypeUri() { return rangeDatatypeUri; } - public void setRangeDatatypeUri(String r) { + public FieldVTwo setRangeDatatypeUri(String r) { if( rangeLang != null && rangeLang.trim().length() > 0 ) throw new IllegalArgumentException("A Field object may not have both rangeDatatypeUri and rangeLanguage set"); rangeDatatypeUri = r; + return this; } public List > getLiteralOptions() { return literalOptions; } - public void setLiteralOptions(List> literalOptions) { + public FieldVTwo setLiteralOptions(List> literalOptions) { this.literalOptions = literalOptions; + return this; } - - /** - * Expects a JSONArray of JSONArrays like: - * [ ["http://example.org/bob", "bob"] , ["http://example.org/kate", "kate"] ] - */ - private void setLiteralOptions(JSONArray array) { - if( array == null ) - literalOptions = Collections.EMPTY_LIST; - - literalOptions = Collections.EMPTY_LIST; - List> out = new ArrayList>( array.length() ); - - for(int i =0; ioption = new ArrayList(2); - option.add(value); - option.add(value); - out.add( option ); - } else { log.warn("could not get option list for " + this.name ); } - }else{ - if( pair.length() == 0 ){ - log.warn("option list too short for " + this.name + ": " + array.opt(i)); - continue; - } - if( pair.length() > 2 ) - log.warn("option list too long for " + this.name + ": " + array.opt(i) + " using first two items"); - - Listoption = new ArrayList(2); - option.add(pair.optString(0)); - if( pair.length() > 1 ) - option.add(pair.optString(1)); - else - option.add(pair.optString(0)); - out.add( option ); - } - } - literalOptions = out; - } - + public String getRangeLang() { return rangeLang; } - public void setRangeLang(String rangeLang) { + public FieldVTwo setRangeLang(String rangeLang) { if( rangeDatatypeUri != null && rangeDatatypeUri.trim().length() > 0) throw new IllegalArgumentException("A Field object may not have both rangeDatatypeUri and rangeLanguage set"); this.rangeLang = rangeLang; + return this; } public EditElementVTwo getEditElement(){ return editElement; } - - /* this is mainly for unit testing */ - public void setName(String name){ + + public FieldVTwo setName(String name){ this.name = name; + return this; } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/BaseEditConfigurationGenerator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/BaseEditConfigurationGenerator.java new file mode 100644 index 000000000..cb432158a --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/BaseEditConfigurationGenerator.java @@ -0,0 +1,154 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ +package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpSession; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditN3GeneratorVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.EditConfiguration; + +public abstract class BaseEditConfigurationGenerator implements EditConfigurationGenerator { + +// @Override +// public EditConfigurationVTwo getEditConfiguration(VitroRequest vreq, +// HttpSession session) { +// EditConfigurationVTwo editConfiguration = new EditConfigurationVTwo(); +// //Set n3 generator +// editConfiguration.setN3Generator(new EditN3GeneratorVTwo(editConfiguration)); +// +// //process subject, predicate, object parameters +// this.initPropertyParameters(vreq, session, editConfiguration); +// +// //Assumes this is a simple case of subject predicate var +// editConfiguration.setN3Required(generateN3Required(vreq)); +// +// //n3 optional +// editConfiguration.setN3Optional(generateN3Optional()); +// +// //Todo: what do new resources depend on here? +// //In original form, these variables start off empty +// editConfiguration.setNewResources(generateNewResources(vreq)); +// //In scope +// setUrisAndLiteralsInScope(editConfiguration, vreq); +// +// //on Form +// setUrisAndLiteralsOnForm(editConfiguration, vreq); +// +// editConfiguration.setFilesOnForm(new ArrayList()); +// +// //Sparql queries +// setSparqlQueries(editConfiguration, vreq); +// +// //set fields +// setFields(editConfiguration, vreq, EditConfigurationUtils.getPredicateUri(vreq)); +// +// prepareForUpdate(vreq, session, editConfiguration); +// +// //Form title and submit label now moved to edit configuration template +// //TODO: check if edit configuration template correct place to set those or whether +// //additional methods here should be used and reference instead, e.g. edit configuration template could call +// //default obj property form.populateTemplate or some such method +// //Select from existing also set within template itself +// setTemplate(editConfiguration, vreq); +// +// //Set edit key +// setEditKey(editConfiguration, vreq); +// +// //Add validator +// setValidators(editConfiguration, vreq); +// +// //Add preprocessors +// addPreprocessors(editConfiguration, vreq.getWebappDaoFactory()); +// +// //Adding additional data, specifically edit mode +// addFormSpecificData(editConfiguration, vreq); +// +// return editConfiguration; +// +//} +// +// abstract void setValidators(EditConfigurationVTwo editConfiguration, VitroRequest vreq) ; +// +// abstract void addFormSpecificData(EditConfigurationVTwo editConfiguration, +// VitroRequest vreq) ; +// +// abstract void addPreprocessors(EditConfigurationVTwo editConfiguration, +// WebappDaoFactory webappDaoFactory) ; +// +// abstract void setEditKey(EditConfigurationVTwo editConfiguration, +// VitroRequest vreq) ; +// +// abstract void setTemplate(EditConfigurationVTwo editConfiguration, +// VitroRequest vreq) ; +// +// abstract void prepareForUpdate(VitroRequest vreq, HttpSession session, +// EditConfigurationVTwo editConfiguration) ; +// +// abstract void setFields(EditConfigurationVTwo editConfiguration, +// VitroRequest vreq, String predicateUri) ; +// +// abstract void setSparqlQueries(EditConfigurationVTwo editConfiguration, +// VitroRequest vreq) ; +// +// abstract void setUrisAndLiteralsOnForm( +// EditConfigurationVTwo editConfiguration, VitroRequest vreq) ; +// +// abstract void setUrisAndLiteralsInScope( +// EditConfigurationVTwo editConfiguration, VitroRequest vreq) ; +// +// abstract Map generateNewResources(VitroRequest vreq) ; +// +// abstract List generateN3Optional() ; +// +// abstract List generateN3Required(VitroRequest vreq) ; + + /* constants */ + public static final String DEFAULT_NS_FOR_NEW_RESOURCE= ""; + + /* Utility Methods */ + + /** + * Sets up the things that should be done for just about every form. + */ + void initBasics(EditConfigurationVTwo editConf, VitroRequest vreq){ + editConf.setN3Generator( new EditN3GeneratorVTwo(editConf) ); + + editConf.setEditKey( (String) vreq.getAttribute("editKey")); + + String formUrl = EditConfigurationUtils.getFormUrl(vreq); + editConf.setFormUrl(formUrl); + } + + /** + * Method that setups up a form for basic object or data property editing. + */ + void initPropertyParameters(VitroRequest vreq, HttpSession session, EditConfigurationVTwo editConfiguration) { + //set up the subject URI based on request + String subjectUri = EditConfigurationUtils.getSubjectUri(vreq); + editConfiguration.setSubjectUri(subjectUri); + + //set up predicate URI based on request + String predicateUri = EditConfigurationUtils.getPredicateUri(vreq); + editConfiguration.setPredicateUri(predicateUri); + + editConfiguration.setUrlPatternToReturnTo("/individual"); + editConfiguration.setEntityToReturnTo(subjectUri); + } + + void initObjectPropForm(EditConfigurationVTwo editConfiguration,VitroRequest vreq) { + editConfiguration.setObject( EditConfigurationUtils.getObjectUri(vreq) ); + + //this needs to be set for the editing to be triggered properly, otherwise the 'prepare' method + //pretends this is a data property editing statement and throws an error + //TODO: Check if null in case no object uri exists but this is still an object property + if(editConfiguration.getObject() != null ) { + editConfiguration.setObjectResource(true); + } + } + +}