Adding a BaseEditConfigurationGengerator and changes to FieldVTwo.

This commit is contained in:
briancaruso 2011-11-04 16:48:50 +00:00
parent a298065fe1
commit 91c34b4fc9
3 changed files with 247 additions and 107 deletions

View file

@ -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<String, String>()));
(Map<String, String>) new HashMap<String, String>()));
//sparql for additional literals in scope
editConfig.setSparqlForAdditionalLiteralsInScope(
this.copy(this.getSparqlForAdditionalLiteralsInScope(),
(Map) new HashMap<String, String>()));
(Map<String, String>) new HashMap<String, String>()));
//sparql for existing literals
editConfig.setSparqlForExistingLiterals(
this.copy(this.getSparqlForExistingLiterals(),
(Map) new HashMap<String, String>()));
(Map<String, String>) new HashMap<String, String>()));
//sparql for existing uris
editConfig.setSparqlForExistingUris(
this.copy(this.getSparqlForExistingUris(),
(Map) new HashMap<String, String>()));
(Map<String, String>) new HashMap<String, String>()));
//TODO: Ensure this is true copy of field and not just shallow copy with same references
Map<String, FieldVTwo> 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<String,String> map = getNewResources();
if( map == null ) {
map = new HashMap<String,String>();
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<String,String> map = getSparqlForExistingLiterals();
if( map == null ) {
map = new HashMap<String,String>();
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<String,String> map = getSparqlForExistingUris();
if( map == null ) {
map = new HashMap<String,String>();
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<String,FieldVTwo> map = getFields();
if( map == null )
setFields( new HashMap<String, FieldVTwo>());
if( map.containsKey(field.getName() ))
throw new IllegalArgumentException("adding filed that is already in the fieild list");
map.put( field.getName(), field);
}
}

View file

@ -125,53 +125,9 @@ 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;
}
/**
* 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;
return this;
}
/* ****************** Getters and Setters ******************************* */
@ -184,40 +140,50 @@ public class FieldVTwo {
return retractions;
}
public void setRetractions(List<String> retractions) {
public FieldVTwo setRetractions(List<String> retractions) {
this.retractions = retractions;
return this;
}
public List<String> getAssertions() {
return assertions;
}
public void setAssertions(List<String> assertions) {
public FieldVTwo setAssertions(List<String> 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 <String> getValidators() {
return validators;
}
public void setValidators(List <String> v) {
public FieldVTwo setValidators(List <String> 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 <List<String>> getLiteralOptions() {
return literalOptions;
}
public void setLiteralOptions(List<List<String>> literalOptions) {
public FieldVTwo setLiteralOptions(List<List<String>> literalOptions) {
this.literalOptions = literalOptions;
}
/**
* 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<List<String>> out = new ArrayList<List<String>>( array.length() );
for(int i =0; i<array.length() ; i++){
JSONArray pair = array.optJSONArray(i);
if( pair == null ){
String value = array.optString(i);
if( value != null ){
List<String>option = new ArrayList<String>(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");
List<String>option = new ArrayList<String>(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;
return this;
}
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;
}
}

View file

@ -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<String>());
//
// //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<String, String> generateNewResources(VitroRequest vreq) ;
//
// abstract List<String> generateN3Optional() ;
//
// abstract List<String> 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);
}
}
}