diff --git a/productMods/edit/forms/js/mailingAddressUtils.js b/productMods/edit/forms/js/mailingAddressUtils.js new file mode 100644 index 00000000..4b3c0a06 --- /dev/null +++ b/productMods/edit/forms/js/mailingAddressUtils.js @@ -0,0 +1,140 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +var mailingAddressUtils = { + + onLoad: function(mode,country) { + this.initObjectReferences(); + this.bindEventListeners(); + this.sortCountrySelector(mode,country); + + if ( mode == "add" ) { + this.containerDiv.hide(); + this.submitButton.hide(); + } + else { + this.processCountryRelatedFields(); + } + }, + + initObjectReferences: function() { + this.form = $('#personHasMailingAddress'); + + // The external auth ID field and messages + this.countrySelector = $('#country'); + this.countrySelectorOptions = $('#country option'); + this.address1Field = $('#addrLineOne'); + this.cityField = $('#city'); + this.stateField = $('#state'); + this.stateSelector= $('#stateSelect'); + this.stateLabel = $('#stateLabel'); + this.postalCodeField = $('#postalCode'); + this.postalCodeLabel = $('#postalCodeLabel'); + this.subjectField = $('#subjectName'); + this.rdfsLabel = $('#addrLabel'); + this.addrTypeField = $('#addressType'); + this.submitButton = $('#submit'); + this.containerDiv = $('#addressDetails'); + this.orSpan = $('span.or'); + + }, + + bindEventListeners: function() { + this.idCache = {}; + + this.countrySelector.change(function() { + mailingAddressUtils.processCountryRelatedFields(); + mailingAddressUtils.showHiddenElements(); + }); + + this.form.submit(function() { + mailingAddressUtils.buildAddressLabel(); + }); + + this.stateSelector.change(function() { + mailingAddressUtils.setStateValue(); + }); + }, + + addressClassIsNonUS: function() { + var country = this.countrySelector.val(); + if ( country.search( 'United States' ) == -1 ) { + return true; + } + else { + return false; + } + }, + + buildAddressLabel: function() { + if ( mailingAddressUtils.addressClassIsNonUS() ) { + this.rdfsLabel.val(this.address1Field.val() + " " + this.cityField.val() + " " + this.countrySelector.val()); + } + else { + this.rdfsLabel.val(this.address1Field.val() + " " + this.cityField.val() + " " + this.stateField.val()); + } + }, + + processCountryRelatedFields: function() { + if ( mailingAddressUtils.addressClassIsNonUS() ) { + this.stateLabel.text("Province or Region"); + this.postalCodeField.attr('size', '40'); + this.stateSelector.hide(); + this.stateField.show(); + this.addrTypeField.val("http://vivoweb.org/ontology/core#Address"); + } + else { + this.stateLabel.text("State"); + this.postalCodeField.attr('size', '8'); + this.stateField.hide(); + this.stateSelector.show(); + this.addrTypeField.val("http://vivoweb.org/ontology/core#USPostalAddress"); + } + }, + + showHiddenElements: function() { + this.containerDiv.show(); + this.submitButton.show(); + this.orSpan.show(); + }, + + setStateValue: function() { + this.stateField.val(this.stateSelector.val()); + }, + + // in the ftl we remove the "the" that precedes some countries, so we need to + // re-sort them alphabetically + sortCountrySelector: function(mode,country) { + // Get options from select box + var the_options = this.countrySelectorOptions; + // sort alphabetically + the_options.sort(function(a,b) { + if (a.text > b.text) return 1; + else if (a.text < b.text) return -1; + else return 0 + }) + //replace with sorted the_options; + this.countrySelector.append( the_options ); + + // if it's add mode, add the "select one" option have it be selected; + // if it's edit mode, add the "Select one" option but have the correct country selected. + // if it's repair mode, add the "Select one" option but only select it if there's no country + if ( mode == "add" ) { + this.countrySelector.prepend($("") + .attr("value","") + .text("Select one")); + } + else if ( mode == "edit" || country.length > 1 ) { + this.countrySelector.prepend($("") + .attr("value","") + .text("Select one")); + this.countrySelector.val(country); + } + else if ( country.length == 0 ) { + this.countrySelector.prepend($("") + .attr("value","") + .text("Select one")); + this.countrySelector.val(country); + } + } + +} \ No newline at end of file diff --git a/productMods/templates/freemarker/edit/forms/personHasMailingAddress.ftl b/productMods/templates/freemarker/edit/forms/personHasMailingAddress.ftl new file mode 100644 index 00000000..05c2bd94 --- /dev/null +++ b/productMods/templates/freemarker/edit/forms/personHasMailingAddress.ftl @@ -0,0 +1,211 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- this is in request.subject.name --> + +<#-- leaving this edit/add mode code in for reference in case we decide we need it --> + +<#import "lib-vivo-form.ftl" as lvf> + +<#--Retrieve certain edit configuration information--> +<#if editConfiguration.objectUri?has_content> + <#assign editMode = "edit"> +<#else> + <#assign editMode = "add"> + + +<#assign htmlForElements = editConfiguration.pageData.htmlForElements /> + +<#--Retrieve variables needed--> +<#assign addrLabelValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "addrLabel") /> +<#assign addressTypeValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "addressType") /> +<#assign addrLineOneValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "addrLineOne") /> +<#assign addrLineTwoValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "addrLineTwo") /> +<#assign addrLineThreeValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "addrLineThree") /> +<#assign cityValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "city") /> +<#assign stateValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "state") /> +<#assign postalCodeValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "postalCode") /> +<#assign countryValue = lvf.getFormFieldValue(editSubmission, editConfiguration, "country") /> + +<#--If edit submission exists, then retrieve validation errors if they exist--> +<#if editSubmission?has_content && editSubmission.submissionExists = true && editSubmission.validationErrors?has_content> + <#assign submissionErrors = editSubmission.validationErrors/> + + +<#if editMode == "edit"> + <#assign titleVerb="Edit"> + <#assign submitButtonText="Edit Mailing Address"> + <#assign disabledVal="disabled"> +<#else> + <#assign titleVerb="Create"> + <#assign submitButtonText="Create Mailing Address"> + <#assign disabledVal=""/> + + +<#assign requiredHint = " *" /> + +

${titleVerb} mailing address for ${editConfiguration.subjectName}

+ +<#--Display error messages if any--> +<#if submissionErrors?has_content> + + + +<@lvf.unsupportedBrowser urls.base /> + +
+ +
+ + +

+ + <#assign countryOpts = editConfiguration.pageData.country /> + +

+ + + +
+

+ + +

+ +

+ + +

+ +

+ + +

+ +

+ + +

+ +

+ + + +

+ +

+ + +

+ +
+ + + + + +

+ or + Cancel +

+ +

* required fields

+ +
+ +
+ + + + + +${stylesheets.add('')} +${stylesheets.add('')} +${stylesheets.add('')} + +${scripts.add('', + '', + '', + '', + '')} + + diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/PersonHasMailingAddressGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/PersonHasMailingAddressGenerator.java new file mode 100644 index 00000000..4820e2bb --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/PersonHasMailingAddressGenerator.java @@ -0,0 +1,237 @@ +/* $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.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +import javax.servlet.http.HttpSession; + +import com.hp.hpl.jena.vocabulary.RDFS; +import com.hp.hpl.jena.vocabulary.XSD; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.DateTimeIntervalValidationVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.DateTimeWithPrecisionVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.FieldVTwo; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.validators.AntiXssValidation; +import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode; +import edu.cornell.mannlib.vitro.webapp.utils.generators.EditModeUtils; + +public class PersonHasMailingAddressGenerator extends VivoBaseGenerator implements + EditConfigurationGenerator { + + final static String addressClass = vivoCore + "Address"; + final static String countryPred = vivoCore + "addressCountry"; + final static String countryClass = vivoCore + "Country"; + final static String addrLine1Pred =vivoCore+"address1" ; + final static String addrLine2Pred =vivoCore+"address2" ; + final static String addrLine3Pred =vivoCore+"address3" ; + final static String cityPred =vivoCore+"addressCity" ; + final static String statePred =vivoCore+"addressState" ; + final static String postalCodePred =vivoCore+"addressPostalCode" ; + final static String mailingAddressPred =vivoCore+"mailingAddress" ; + + public PersonHasMailingAddressGenerator() {} + + // There are 4 modes that this form can be in: + // 1. Add. There is a subject and a predicate but no position and + // nothing else. + // + // 2. Normal edit where everything should already be filled out. + // There is a subject, a object and an individual on + // the other end of the object's core:personInOrganization stmt. + // + // 3. Repair a bad role node. There is a subject, predicate and object + // but there is no individual on the other end of the object's + // core:personInOrganization stmt. This should be similar to an add + // but the form should be expanded. + // + // 4. Really bad node. multiple core:personInOrganization statements. + + @Override + public EditConfigurationVTwo getEditConfiguration(VitroRequest vreq, + HttpSession session) { + + EditConfigurationVTwo conf = new EditConfigurationVTwo(); + + initBasics(conf, vreq); + initPropertyParameters(vreq, session, conf); + initObjectPropForm(conf, vreq); + + conf.setTemplate("personHasMailingAddress.ftl"); + + conf.setVarNameForSubject("person"); + conf.setVarNameForPredicate("predicate"); + conf.setVarNameForObject("address"); + + conf.setN3Required( Arrays.asList( n3ForNewAddress, + addrLabelAssertion, + addressTypeAssertion ) ); + conf.setN3Optional( Arrays.asList( addrLineOneAssertion, addrLineTwoAssertion, addrLineThreeAssertion, cityAssertion, stateAssertion, countryAssertion, postalCodeAssertion ) ); + + conf.addNewResource("address", DEFAULT_NS_FOR_NEW_RESOURCE); + + //uris in scope: none + //literals in scope: none + + conf.setUrisOnform(Arrays.asList("addressType")); + conf.setLiteralsOnForm(Arrays.asList("addrLineOne", "addrLineTwo", "addrLineThree", "city", "postalCode", "addrLabel","country", "state" )); + + conf.addSparqlForExistingLiteral("addrLabel", addrLabelQuery); + conf.addSparqlForExistingLiteral("addrLineOne", addrLineOneQuery); + conf.addSparqlForExistingLiteral("addrLineTwo", addrLineTwoQuery); + conf.addSparqlForExistingLiteral("addrLineThree", addrLineThreeQuery); + conf.addSparqlForExistingLiteral("city", cityQuery); + conf.addSparqlForExistingLiteral("postalCode", postalCodeQuery); + conf.addSparqlForExistingLiteral("state", stateQuery); + conf.addSparqlForExistingLiteral("country", countryQuery); + + conf.addSparqlForExistingUris("addressType", addressTypeQuery); + + conf.addField( new FieldVTwo(). + setName("country"). + setOptionsType( FieldVTwo.OptionsType.INDIVIDUALS_VIA_VCLASS ). + setObjectClassUri( countryClass ) . + setValidators( list("nonempty") ) + ); + + conf.addField( new FieldVTwo(). + setName("addrLineOne") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("nonempty") ) + ); + + conf.addField( new FieldVTwo(). + setName("addrLineTwo") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("datatype:" + XSD.xstring.toString()) ) + ); + + conf.addField( new FieldVTwo(). + setName("addrLineThree") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("datatype:" + XSD.xstring.toString()) ) + ); + + conf.addField( new FieldVTwo(). + setName("postalCode") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("nonempty") ) + ); + + conf.addField( new FieldVTwo(). + setName("city") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("nonempty") ) + ); + + conf.addField( new FieldVTwo(). + setName("state") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("datatype:" + XSD.xstring.toString()) ) + ); + + conf.addField( new FieldVTwo(). + setName("addrLabel") + .setRangeDatatypeUri( XSD.xstring.toString() ). + setValidators( list("datatype:" + XSD.xstring.toString()) ) + ); + + conf.addField( new FieldVTwo(). + setName("addressType"). + setOptionsType(FieldVTwo.OptionsType.CHILD_VCLASSES). + setObjectClassUri(addressClass) + ); + + conf.addValidator(new AntiXssValidation()); + + prepare(vreq, conf); + return conf; + } + + final static String n3ForNewAddress = + "@prefix vivo: <" + vivoCore + "> . \n\n" + + "?person vivo:mailingAddress ?address . \n" + + "?address a vivo:Address . \n" + +// "?address a ?addressType . \n" + + "?address vivo:mailingAddressFor ?person . \n" ; + + final static String addrLineOneAssertion = + "?address <"+ addrLine1Pred +"> ?addrLineOne ."; + + final static String addrLineTwoAssertion = + "?address <"+ addrLine2Pred +"> ?addrLineTwo ."; + + final static String addrLineThreeAssertion = + "?address <"+ addrLine3Pred +"> ?addrLineThree ."; + + final static String cityAssertion = + "?address <"+ cityPred +"> ?city ."; + + final static String postalCodeAssertion = + "?address <"+ postalCodePred +"> ?postalCode ."; + + final static String stateAssertion = + "?address <"+ statePred +"> ?state ."; + + final static String countryAssertion = + "?address <" + countryPred + "> ?country ."; + + final static String addrLabelAssertion = + "?address <" + label + "> ?addrLabel ."; + + final static String addressTypeAssertion = + "?address a ?addressType ."; + + final static String addrLabelQuery = + "SELECT ?existingAddrLabel WHERE { \n" + +// " ?person <"+ mailingAddressPred +"> ?address . \n" + + " ?address <" + label + "> ?existingAddrLabel . \n" + + "}"; + + final static String addrLineOneQuery = + "SELECT ?existingaddrLineOne WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ addrLine1Pred +"> ?existingaddrLineOne . }"; + + final static String addrLineTwoQuery = + "SELECT ?existingaddrLineTwo WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ addrLine2Pred +"> ?existingaddrLineTwo . }"; + + final static String addrLineThreeQuery = + "SELECT ?existingaddrLineThree WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ addrLine3Pred +"> ?existingaddrLineThree . }"; + + final static String cityQuery = + "SELECT ?existingCity WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ cityPred +"> ?existingCity . }"; + + final static String stateQuery = + "SELECT ?existingState WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ statePred +"> ?existingState . }"; + + final static String postalCodeQuery = + "SELECT ?existingPostalCode WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ postalCodePred +"> ?existingPostalCode . }"; + + final static String countryQuery = + "SELECT ?existingCountry WHERE {\n"+ +// "?person <"+ mailingAddressPred +"> ?address . \n" + + "?address <"+ countryPred +"> ?existingCountry . }"; + + final static String addressTypeQuery = + "PREFIX vitro: <" + VitroVocabulary.vitroURI + "> \n" + + "SELECT ?existingAddressType WHERE { \n" + + "?address vitro:mostSpecificType ?existingAddressType . }"; + +}