From 0ec8dcbce11c20522535b27339822bebe26c6bdc Mon Sep 17 00:00:00 2001 From: hudajkhan Date: Tue, 24 Sep 2013 13:44:12 -0400 Subject: [PATCH] updates for labels and uri method in webappdaofactory --- .../webapp/dao/jena/WebappDaoFactoryJena.java | 36 +----- .../ManageLabelsForIndividualGenerator.java | 112 +++--------------- .../generators/ManageLabelsGenerator.java | 24 ++++ .../FoafNameToRdfsLabelPreprocessor.java | 5 + .../vitro/webapp/utils/jena/URIUtils.java | 53 +++++++++ 5 files changed, 101 insertions(+), 129 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsGenerator.java create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/URIUtils.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java index a002b7ea9..76b6d0664 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java @@ -49,6 +49,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactoryConfig; import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase; +import edu.cornell.mannlib.vitro.webapp.utils.jena.URIUtils; public class WebappDaoFactoryJena implements WebappDaoFactory { @@ -204,40 +205,13 @@ public class WebappDaoFactoryJena implements WebappDaoFactory { //Check if URI already in use or not either as resource OR as property public boolean hasExistingURI(String uriStr) { - boolean existingURI = false; OntModel ontModel = ontModelSelector.getFullModel(); - ontModel.enterCriticalSection(Lock.READ); - try { - Resource newURIAsRes = ResourceFactory.createResource(uriStr); - Property newURIAsProp = ResourceFactory.createProperty(uriStr); - StmtIterator closeIt = ontModel.listStatements( - newURIAsRes, null, (RDFNode)null); - if (closeIt.hasNext()) { - existingURI = true; - - } - //if not in the subject position, check in object position - if (!existingURI) { - closeIt = ontModel.listStatements(null, null, newURIAsRes); - if (closeIt.hasNext()) { - existingURI= true; - } - } - //Check for property - if (!existingURI) { - closeIt = ontModel.listStatements( - null, newURIAsProp, (RDFNode)null); - if (closeIt.hasNext()) { - existingURI = true; - } - } - } finally { - ontModel.leaveCriticalSection(); - } - - return existingURI; + return URIUtils.hasExistingURI(uriStr, ontModel); } + + + public WebappDaoFactory getUserAwareDaoFactory(String userURI) { return new WebappDaoFactoryJena(this, userURI); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsForIndividualGenerator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsForIndividualGenerator.java index d34c1c5da..2645e638b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsForIndividualGenerator.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsForIndividualGenerator.java @@ -17,42 +17,25 @@ import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import com.hp.hpl.jena.ontology.OntModel; -import com.hp.hpl.jena.query.Dataset; -import com.hp.hpl.jena.query.QueryExecution; -import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Literal; -import com.hp.hpl.jena.rdf.model.RDFNode; -import com.hp.hpl.jena.shared.Lock; -import com.hp.hpl.jena.sparql.resultset.ResultSetMem; import com.hp.hpl.jena.vocabulary.RDFS; -import com.hp.hpl.jena.vocabulary.XSD; import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.ifaces.RequestActionConstants; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.AddDataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.AddObjectPropertyStatement; -import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; -import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; -import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap; -import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess; -import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils; 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.fields.FieldVTwo; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.FoafNameToRdfsLabelPreprocessor; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.ManageLabelsForIndividualPreprocessor; -import edu.cornell.mannlib.vitro.webapp.i18n.selection.LocaleSelectionDataGetter; -import edu.cornell.mannlib.vitro.webapp.i18n.selection.LocaleSelectorUtilities; import edu.cornell.mannlib.vitro.webapp.i18n.selection.SelectedLocale; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.DataPropertyStatementTemplateModel; @@ -114,6 +97,14 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen prepare(vreq, config); return config; } + + /**With ISF Changes**/ + //For addition of a label, with ISF changes, the name is now linked to a vcard which in turn is linked to a "fullname" that then has first/middle/last names + /* + private void addNewResources(EditConfigurationVTwo editConfiguration) { + conf.addNewResource("fullName", DEFAULT_NS_FOR_NEW_RESOURCE); + conf.addNewResource("individualVcard", DEFAULT_NS_FOR_NEW_RESOURCE); + }*/ private void setUrlToReturnTo(EditConfigurationVTwo editConfiguration, VitroRequest vreq) { editConfiguration.setUrlPatternToReturnTo(EditConfigurationUtils.getFormUrlWithoutContext(vreq)); @@ -134,11 +125,8 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen private List generateN3Optional(VitroRequest vreq) { List n3Optional = new ArrayList(); String predicateUri = EditConfigurationUtils.getPredicateUri(vreq); - String n3 = "?subject <" + predicateUri + "> ?label "; - //n3 used if the subject is a person - String personN3 = this.N3_PREFIX + "?subject foaf:firstName ?firstName ; foaf:lastName ?lastName ."; + String n3 = "?subject <" + predicateUri + "> ?label "; n3Optional.add(n3); - n3Optional.add(personN3); return n3Optional; } @@ -151,41 +139,14 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen setName("label"). setValidators(getLabelValidators(vreq, editConfiguration))); editConfiguration.addField(new FieldVTwo( - ).setName("newLabelLanguage")); - //no validators since all of this is optional - //there should be error-checking client side though - editConfiguration.addField(new FieldVTwo(). - setName("firstName"). - setValidators(getFirstNameValidators(vreq, editConfiguration))); - - editConfiguration.addField(new FieldVTwo(). - setName("lastName"). - setValidators(getLastNameValidators(vreq, editConfiguration))); + ).setName("newLabelLanguage")); } - //first and last name have validators if is person is true - private List getFirstNameValidators(VitroRequest vreq, EditConfigurationVTwo config) { - List validators = new ArrayList(); - if(isPersonType(vreq, config)) { - validators.add("nonempty"); - } - return validators; - } - - private List getLastNameValidators(VitroRequest vreq, EditConfigurationVTwo config) { - List validators = new ArrayList(); - if(isPersonType(vreq, config)) { - validators.add("nonempty"); - } - return validators; - } + //validate label if person is not true private List getLabelValidators(VitroRequest vreq, EditConfigurationVTwo config) { List validators = new ArrayList(); - if(!isPersonType(vreq, config)) { - validators.add("nonempty"); - } return validators; } @@ -196,9 +157,6 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen List literalsOnForm = new ArrayList(); literalsOnForm.add("label"); literalsOnForm.add("newLabelLanguage"); - //optional for person - literalsOnForm.add("firstName"); - literalsOnForm.add("lastName"); config.setLiteralsOnForm(literalsOnForm); } @@ -219,19 +177,7 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen private void initExistingLabels(EditConfigurationVTwo config, VitroRequest vreq) { - this.existingLabelLiterals = this.getExistingLabels(config.getSubjectUri(), vreq); - // this.labelsSortedByLanguage = this.getLabelsSortedByLanguage(config,vreq); - //language names sorted for the existing languages - // this.existingSortedLanguageNameList = getExistingSortedLanguageNamesList(); - - //Generate a label to language code hash map - //TODO: - - //HashMap labelToLanguageCode = new HashMap(); - - //this.labels = getExistingLabels(config.getSubjectUri(), vreq); - //this.labelsSortedByLanguage = getLabelsSortedByLanguage(config.getSubjectUri(), vreq); - + this.existingLabelLiterals = this.getExistingLabels(config.getSubjectUri(), vreq); } @@ -284,16 +230,7 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen }else{ config.addFormSpecificData("subjectName", null); } - - //Put in whether or not person type - if(isPersonType(vreq, config)) { - //Doing this b/c unsure how freemarker will handle boolean value from JAVA - config.addFormSpecificData("isPersonType", "true"); - } else { - config.addFormSpecificData("isPersonType", "false"); - - } - + //Include whether or not editable to enable edit/remove links and add to show up config.addFormSpecificData("editable", isEditable(vreq, config)); } @@ -341,28 +278,7 @@ public class ManageLabelsForIndividualGenerator extends BaseEditConfigurationGen } - //Copied from NewIndividualFormGenerator - //TODO: Refactor so common code can be used by both generators - public String getFOAFPersonClassURI() { - return "http://xmlns.com/foaf/0.1/Person"; - } - - public boolean isPersonType(VitroRequest vreq, EditConfigurationVTwo config) { - WebappDaoFactory wdf = vreq.getWebappDaoFactory(); - Boolean isPersonType = Boolean.FALSE; - String foafPersonType = getFOAFPersonClassURI(); - List vclasses = this.getVClasses(config, vreq); - if( vclasses != null ){ - for( VClass v: vclasses){ - String typeUri = v.getURI(); - if( foafPersonType.equals(typeUri)) { - isPersonType = Boolean.TRUE; - break; - } - } - } - return isPersonType; - } + //how to get the type of the individual in question public List getVClasses(EditConfigurationVTwo config, VitroRequest vreq) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsGenerator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsGenerator.java new file mode 100644 index 000000000..37a647862 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/ManageLabelsGenerator.java @@ -0,0 +1,24 @@ +/* $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 javax.servlet.http.HttpSession; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; + +/** + *This generator selects the actual generator to be employed. This generator can be overwritten in applications + *that use/extend Vitro to allow for class-based label handling. + */ +public class ManageLabelsGenerator extends BaseEditConfigurationGenerator implements EditConfigurationGenerator { + public static Log log = LogFactory.getLog(ManageLabelsForIndividualGenerator.class); + @Override + public EditConfigurationVTwo getEditConfiguration(VitroRequest vreq, HttpSession session) { + ManageLabelsForIndividualGenerator g = new ManageLabelsForIndividualGenerator(); + return g.getEditConfiguration(vreq, session); + } + +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/FoafNameToRdfsLabelPreprocessor.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/FoafNameToRdfsLabelPreprocessor.java index bbb280f2a..0a1fcedf9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/FoafNameToRdfsLabelPreprocessor.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/preprocessors/FoafNameToRdfsLabelPreprocessor.java @@ -22,6 +22,9 @@ public class FoafNameToRdfsLabelPreprocessor implements ModelChangePreprocessor HttpServletRequest request) { Property firstNameP = additionsModel.getProperty(FOAF+"firstName"); Property lastNameP = additionsModel.getProperty(FOAF+"lastName"); + //middle name is optional + Property middleNameP = additionsModel.getProperty(FOAF+"middleName"); + Property rdfsLabelP = additionsModel.getProperty(VitroVocabulary.LABEL); ResIterator subs = @@ -30,12 +33,14 @@ public class FoafNameToRdfsLabelPreprocessor implements ModelChangePreprocessor Resource sub = subs.nextResource(); Statement fname = sub.getProperty( firstNameP ); Statement lname = sub.getProperty( lastNameP ); + Statement mname = sub.getProperty(middleNameP)l if( fname != null && lname != null && fname.getString() != null && lname.getString() != null ){ //Check if there are languages associated with first name and last name and add the language //attribute to the label //This preprocessor is used in multiple places, including for managing labels Literal firstNameLiteral = fname.getLiteral(); Literal lastNameLiteral = lname.getLiteral(); + String firstNameLanguage = firstNameLiteral.getLanguage(); String lastNameLanguage = lastNameLiteral.getLanguage(); String newLabel = lname.getString() + ", " + fname.getString(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/URIUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/URIUtils.java new file mode 100644 index 000000000..618751696 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/URIUtils.java @@ -0,0 +1,53 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.jena; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.rdf.model.StmtIterator; +import com.hp.hpl.jena.shared.Lock; + +public class URIUtils { + + private static final Log log = LogFactory.getLog(URIUtils.class.getName()); + + public static boolean hasExistingURI(String uriStr, OntModel ontModel) { + boolean existingURI = false; + ontModel.enterCriticalSection(Lock.READ); + try { + Resource newURIAsRes = ResourceFactory.createResource(uriStr); + Property newURIAsProp = ResourceFactory.createProperty(uriStr); + StmtIterator closeIt = ontModel.listStatements( + newURIAsRes, null, (RDFNode)null); + if (closeIt.hasNext()) { + existingURI = true; + + } + //if not in the subject position, check in object position + if (!existingURI) { + closeIt = ontModel.listStatements(null, null, newURIAsRes); + if (closeIt.hasNext()) { + existingURI= true; + } + } + //Check for property + if (!existingURI) { + closeIt = ontModel.listStatements( + null, newURIAsProp, (RDFNode)null); + if (closeIt.hasNext()) { + existingURI = true; + } + } + } finally { + ontModel.leaveCriticalSection(); + } + + return existingURI; + } +}