diff --git a/productMods/WEB-INF/web.xml b/productMods/WEB-INF/web.xml index 029348f1..62e582d4 100644 --- a/productMods/WEB-INF/web.xml +++ b/productMods/WEB-INF/web.xml @@ -952,6 +952,15 @@ /UMLSTermsRetrieval + + ConceptSearchService + edu.cornell.mannlib.vitro.webapp.servlet.ConceptSearchServlet + + + ConceptSearchService + /conceptSearchService + + ProcessTerminologyAnnotation edu.cornell.mannlib.vitro.webapp.servlet.ProcessTerminologyController diff --git a/productMods/templates/freemarker/edit/forms/addGrantRoleToPerson.ftl b/productMods/templates/freemarker/edit/forms/addGrantRoleToPerson.ftl index be67db61..bee68305 100644 --- a/productMods/templates/freemarker/edit/forms/addGrantRoleToPerson.ftl +++ b/productMods/templates/freemarker/edit/forms/addGrantRoleToPerson.ftl @@ -10,20 +10,31 @@ <#assign sparqlForAcFilter = editConfiguration.pageData.sparqlForAcFilter /> <#assign disabledVal = ""/> <#if editMode = "edit"> - <#assign disabledVal = "" /> + <#assign disabledVal = "disabled=\"disabled\"" /> <#--the heading and submit button label depend on the predicate uri--> + <#assign formHeading = "investigator entry for "/> -<#assign submitButtonLabel = "Edit Investigator" /> +<#assign submitButtonLabel = "Investigator" /> <#if editConfiguration.predicateUri?ends_with("hasPrincipalInvestigatorRole") > <#assign formHeading = "principal investigator entry for "/> - <#assign submitButtonLabel = "Edit Principal Investigator" /> + <#assign submitButtonLabel = "Principal Investigator" /> <#elseif editConfiguration.predicateUri?ends_with("hasCo-PrincipalInvestigatorRole") > <#assign formHeading = "co-principal investigator entry for "/> - <#assign submitButtonLabel = "Edit Co-Principal Investigator" /> + <#assign submitButtonLabel = "Co-Principal Investigator" /> +<#if editMode = "add"> + <#assign formHeading> Create ${formHeading} +<#else> + <#assign formHeading> Edit ${formHeading} + <#assign submitButtonLabel> Edit ${submitButtonLabel} + + + + + <#--Get existing value for specific data literals and uris--> diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddGrantRoleToPersonGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddGrantRoleToPersonGenerator.java index d63588c0..94de20b0 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddGrantRoleToPersonGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddGrantRoleToPersonGenerator.java @@ -32,6 +32,7 @@ import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditN3GeneratorVTwo; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.FieldVTwo; import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils; import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode; +import edu.cornell.mannlib.vitro.webapp.utils.generators.AddRoleUtils; /** * Custom form for adding a grant to an person for the predicates hasCo-PrincipalInvestigatorRole and hasPrincipalInvestigatorRole. @@ -648,6 +649,25 @@ public class AddGrantRoleToPersonGenerator implements EditConfigurationGenerator } } + /**Methods for checking edit mode ** + * + */ + public EditMode getEditMode(VitroRequest vreq) { + List roleToGrantPredicates = getPossibleRoleToGrantPredicates(); + return AddRoleUtils.getEditMode(vreq, roleToGrantPredicates); + } + + private boolean isAddMode(VitroRequest vreq) { + return AddRoleUtils.isAddMode(getEditMode(vreq)); + } + + private boolean isEditMode(VitroRequest vreq) { + return AddRoleUtils.isEditMode(getEditMode(vreq)); + } + + private boolean isRepairMode(VitroRequest vreq) { + return AddRoleUtils.isRepairMode(getEditMode(vreq)); + } @@ -728,80 +748,14 @@ public class AddGrantRoleToPersonGenerator implements EditConfigurationGenerator } - public List getPossibleroleToGrantPredicates() { + public List getPossibleRoleToGrantPredicates() { return ModelUtils.getPossiblePropertiesForRole(); } - public List getPossiblegrantToRolePredicates() { + public List getPossibleGrantToRolePredicates() { return ModelUtils.getPossibleInversePropertiesForRole(); } - /** - * Methods that check edit mode - */ - - //Get edit mode - private EditMode getEditMode(VitroRequest vreq) { - List roleToGrantPredicates = getPossibleroleToGrantPredicates(); - //We're making some assumptions here: That there is only one role objec tot one activity object - //pairing, i.e. the same role object can't be related to a different activity object - //That said, there should only be one role to Activity predicate linking a role to an activity - //So if - Individual object = EditConfigurationUtils.getObjectIndividual(vreq); - boolean foundErrorMode = false; - int numberEditModes = 0; - int numberRepairModes = 0; - int numberPredicates = roleToGrantPredicates.size(); - for(String predicate:roleToGrantPredicates) { - EditMode mode = FrontEndEditingUtils.getEditMode(vreq, object, predicate); - //Any error mode should result in error - if(mode == EditMode.ERROR) { - foundErrorMode = true; - break; - } - if(mode == EditMode.EDIT) { - numberEditModes++; - } - else if(mode == EditMode.REPAIR) { - numberEditModes++; - } - - } - - //if found an error or if more than one edit mode returned, incorrect - - if(foundErrorMode || numberEditModes > 1) - { - return EditMode.ERROR; - } - EditMode mode = EditMode.ADD; - //if exactly one edit mode found, then edit mode - if(numberEditModes == 1) { - mode = EditMode.EDIT; - } - //if all modes are repair, this means that all of them have zero statements returning - //which is incorrect - if(numberRepairModes == numberPredicates) { - mode = EditMode.REPAIR; - } - //otherwise all the modes are Add and Add will be returned - return mode; - } - private boolean isAddMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.ADD); - } - - private boolean isEditMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.EDIT); - } - - private boolean isRepairMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.REPAIR); - } - /** * Methods to return URIS for various predicates diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddRoleToPersonTwoStageGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddRoleToPersonTwoStageGenerator.java index 9daf872f..28eaeb35 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddRoleToPersonTwoStageGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddRoleToPersonTwoStageGenerator.java @@ -49,6 +49,7 @@ import edu.cornell.mannlib.vitro.webapp.web.MiscWebUtils; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils; import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode; +import edu.cornell.mannlib.vitro.webapp.utils.generators.AddRoleUtils; /** * Generates the edit configuration for adding a Role to a Person. @@ -1092,68 +1093,26 @@ public abstract class AddRoleToPersonTwoStageGenerator implements EditConfigurat * Methods that check edit mode */ - //Get edit mode - private EditMode getEditMode(VitroRequest vreq) { - List roleToActivityPredicates = getPossibleRoleToActivityPredicates(); - //We're making some assumptions here: That there is only one role objec tot one activity object - //pairing, i.e. the same role object can't be related to a different activity object - //That said, there should only be one role to Activity predicate linking a role to an activity - //So if - Individual object = EditConfigurationUtils.getObjectIndividual(vreq); - boolean foundErrorMode = false; - int numberEditModes = 0; - int numberRepairModes = 0; - int numberPredicates = roleToActivityPredicates.size(); - for(String predicate:roleToActivityPredicates) { - EditMode mode = FrontEndEditingUtils.getEditMode(vreq, object, predicate); - //Any error mode should result in error - if(mode == EditMode.ERROR) { - foundErrorMode = true; - break; - } - if(mode == EditMode.EDIT) { - numberEditModes++; - } - else if(mode == EditMode.REPAIR) { - numberEditModes++; - } - - } - - //if found an error or if more than one edit mode returned, incorrect + + /**Methods for checking edit mode ** + * + */ + public EditMode getEditMode(VitroRequest vreq) { + List roleToGrantPredicates = getPossibleRoleToActivityPredicates(); + return AddRoleUtils.getEditMode(vreq, roleToGrantPredicates); + } - if(foundErrorMode || numberEditModes > 1) - { - return EditMode.ERROR; - } - EditMode mode = EditMode.ADD; - //if exactly one edit mode found, then edit mode - if(numberEditModes == 1) { - mode = EditMode.EDIT; - } - //if all modes are repair, this means that all of them have zero statements returning - //which is incorrect - if(numberRepairModes == numberPredicates) { - mode = EditMode.REPAIR; - } - //otherwise all the modes are Add and Add will be returned - return mode; - } - private boolean isAddMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.ADD); + private boolean isAddMode(VitroRequest vreq) { + return AddRoleUtils.isAddMode(getEditMode(vreq)); } private boolean isEditMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.EDIT); + return AddRoleUtils.isEditMode(getEditMode(vreq)); } private boolean isRepairMode(VitroRequest vreq) { - EditMode mode = getEditMode(vreq); - return (mode == EditMode.REPAIR); + return AddRoleUtils.isRepairMode(getEditMode(vreq)); } - /** * Methods to return URIS for various predicates diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/NewIndividualFormGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/NewIndividualFormGenerator.java index a1bb5eef..c672d166 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/NewIndividualFormGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/NewIndividualFormGenerator.java @@ -163,7 +163,9 @@ public class NewIndividualFormGenerator implements EditConfigurationGenerator { editConfiguration.setVarNameForSubject("subjectNotUsed"); editConfiguration.setSubjectUri(subjectUri); - editConfiguration.setEntityToReturnTo("?newInd"); + //Note, the spaces are important - they were added by ProcessRdfFormController earlier + //as a means of ensuring the substitution worked correctly - as the regex expects spaces + editConfiguration.setEntityToReturnTo(" ?newInd "); editConfiguration.setVarNameForPredicate("predicateNotUsed"); editConfiguration.setPredicateUri(predicateUri); @@ -213,7 +215,7 @@ public class NewIndividualFormGenerator implements EditConfigurationGenerator { List n3Optional = new ArrayList(); String editString = "@prefix foaf: ." + " ?newInd foaf:firstName ?firstName ; " + - " ?newInd foaf:lastName ?lastName ."; + " foaf:lastName ?lastName ."; n3Optional.add(editString); n3Optional.add("?newInd <" + RDFS.label.getURI() + "> ?label ."); return n3Optional; diff --git a/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java b/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java new file mode 100644 index 00000000..249db533 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/servlet/ConceptSearchServlet.java @@ -0,0 +1,70 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.servlet; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.ServletOutputStream; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet; +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHttpServlet; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualController; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ExceptionResponseValues; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; +import edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.ConceptSearchServiceUtils; +import edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual.IndividualTemplateModel; +import freemarker.ext.beans.BeansWrapper; +import freemarker.template.DefaultObjectWrapper; + +public class ConceptSearchServlet extends VitroHttpServlet { + + private static final long serialVersionUID = 1L; + private static final Log log = LogFactory.getLog(UMLSTerminologyAnnotation.class); + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + VitroRequest vreq = new VitroRequest(req); + + try{ + ServletContext ctx = vreq.getSession().getServletContext(); + String results = ConceptSearchServiceUtils.getSearchResults(ctx, vreq); + + try{ + ServletOutputStream sos = resp.getOutputStream(); + + resp.setCharacterEncoding("UTF-8"); + resp.setContentType("application/json;charset=UTF-8"); + + sos.print(results); + + } catch(Exception ex) { + log.error("error occurred in servlet", ex); + } + + }catch(Exception ex){ + log.warn(ex,ex); + } + } + +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/AbstractConceptSearch.java b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/AbstractConceptSearch.java new file mode 100644 index 00000000..fd77c7d9 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/AbstractConceptSearch.java @@ -0,0 +1,24 @@ +package edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService; + +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.AddRoleToPersonTwoStageGenerator; + +abstract public class AbstractConceptSearch implements ConceptSearchInterface{ + private Log log = LogFactory.getLog(AbstractConceptSearch.class); + + public String doSearch(ServletContext context, VitroRequest vreq ) { + String searchEntry = vreq.getParameter("searchTerm"); + String results = processResults(searchEntry); + + return results; + } + + abstract public String processResults(String searchEntry); + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchInterface.java b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchInterface.java new file mode 100644 index 00000000..d4c0bbfb --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchInterface.java @@ -0,0 +1,18 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService; + +import java.util.Map; + +import javax.servlet.ServletContext; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; + +import org.json.JSONObject; + +public interface ConceptSearchInterface{ + String doSearch(ServletContext context, VitroRequest vreq ); + + String processResults(String searchEntry); +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchServiceUtils.java b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchServiceUtils.java new file mode 100644 index 00000000..94bc8630 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/ConceptSearchServiceUtils.java @@ -0,0 +1,97 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService; + +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; + +import com.hp.hpl.jena.ontology.OntModel; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.RDFNode; +import com.hp.hpl.jena.rdf.model.ResourceFactory; +import com.hp.hpl.jena.rdf.model.Statement; +import com.hp.hpl.jena.rdf.model.StmtIterator; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.PageDao; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; +import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.EditConfigurationGenerator; + +/** + * This will pass these variables to the template: + * classGroupUri: uri of the classgroup associated with this page. + * vClassGroup: a data structure that is the classgroup associated with this page. + */ +public class ConceptSearchServiceUtils { + private static final Log log = LogFactory.getLog(ConceptSearchServiceUtils.class); + //Get the appropriate search service class + + //Get the class that corresponds to the appropriate search + public static String getConceptSearchServiceClassName(String searchServiceName) { + HashMap map = getMapping(); + if(map.containsKey(searchServiceName)) { + return map.get(searchServiceName); + } + return null; + } + + + + //Get the hashmap mapping service name to Service class + private static HashMap getMapping() { + HashMap map = new HashMap(); + map.put("UMLS", "edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.UMLSConceptSearch"); + return map; + } + + public static String getSearchResults(ServletContext context, VitroRequest vreq) { + String searchServiceName = getSearchServiceName(vreq); + + String searchServiceClassName = getConceptSearchServiceClassName(searchServiceName); + + ConceptSearchInterface conceptSearchClass = null; + + Object object = null; + try { + Class classDefinition = Class.forName(searchServiceClassName); + object = classDefinition.newInstance(); + conceptSearchClass = (ConceptSearchInterface) object; + } catch (InstantiationException e) { + System.out.println(e); + } catch (IllegalAccessException e) { + System.out.println(e); + } catch (ClassNotFoundException e) { + System.out.println(e); + } + + if(conceptSearchClass == null){ + log.error("could not find Concept Search Class for " + searchServiceName); + return null; + } + return conceptSearchClass.doSearch(context, vreq); + } + + + + private static String getSearchServiceName(VitroRequest vreq) { + return vreq.getParameter("searchServiceName"); + } +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/UMLSConceptSearch.java b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/UMLSConceptSearch.java new file mode 100644 index 00000000..cc8cf90a --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/ConceptSearchService/UMLSConceptSearch.java @@ -0,0 +1,69 @@ +package edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.net.URL; +import java.net.URLEncoder; + +import javax.servlet.ServletContext; +import javax.servlet.ServletOutputStream; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; + +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.servlet.UMLSTerminologyAnnotation; + +public class UMLSConceptSearch extends AbstractConceptSearch{ + private static final String submissionUrl = "http://link.informatics.stonybrook.edu/MeaningLookup/MlServiceServlet?"; + private static final Log log = LogFactory.getLog(UMLSConceptSearch.class); + + public String processResults(String searchEntry) { + + String dataUrl = submissionUrl + "textToProcess=" + URLEncoder.encode(searchEntry) + "&format=json"; + String results = null; + try{ + StringWriter sw = new StringWriter(); + URL rss = new URL(dataUrl); + + + BufferedReader in = new BufferedReader(new InputStreamReader(rss.openStream())); + String inputLine; + while((inputLine = in.readLine()) != null) { + sw.write(inputLine); + } + in.close(); + + results = sw.toString(); + + } catch(Exception ex) { + log.error("error occurred in servlet", ex); + } + + + results = processOutput(results); + return results; + } + + //Returning string with + private String processOutput(String results) { + String newResults = null; + try { + JSONObject json = new JSONObject(results); + //Return label and CUID of object + if(json.has("All")) { + + } + + if(json.has("BestMatch")) { + + } + } catch(Exception ex) { + log.error("Error making json object out of output"); + } + return newResults; + } + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/generators/AddRoleUtils.java b/src/edu/cornell/mannlib/vitro/webapp/utils/generators/AddRoleUtils.java new file mode 100644 index 00000000..4181e1df --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/generators/AddRoleUtils.java @@ -0,0 +1,79 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.generators; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils; +import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils; +import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode; + +public class AddRoleUtils { + private static Log log = LogFactory.getLog(AddRoleUtils.class); + + + /* *************** Static utility methods used in role-based generators *********** */ + public static EditMode getEditMode(VitroRequest vreq, List possiblePredicates) { + //We're making some assumptions here: That there is only one role objec tot one activity object + //pairing, i.e. the same role object can't be related to a different activity object + //That said, there should only be one role to Activity predicate linking a role to an activity + //So if + Individual object = EditConfigurationUtils.getObjectIndividual(vreq); + boolean foundErrorMode = false; + int numberEditModes = 0; + int numberRepairModes = 0; + int numberPredicates = possiblePredicates.size(); + for(String predicate:possiblePredicates) { + EditMode mode = FrontEndEditingUtils.getEditMode(vreq, object, predicate); + //Any error mode should result in error + if(mode == EditMode.ERROR) { + foundErrorMode = true; + break; + } + if(mode == EditMode.EDIT) { + numberEditModes++; + } + else if(mode == EditMode.REPAIR) { + numberRepairModes++; + } + + } + + //if found an error or if more than one edit mode returned, incorrect + + if(foundErrorMode || numberEditModes > 1) + { + return EditMode.ERROR; + } + EditMode mode = EditMode.ADD; + //if exactly one edit mode found, then edit mode + if(numberEditModes == 1) { + mode = EditMode.EDIT; + } + //if all modes are repair, this means that all of them have zero statements returning + //which is incorrect + if(numberRepairModes == numberPredicates) { + mode = EditMode.REPAIR; + } + //otherwise all the modes are Add and Add will be returned + return mode; + } + + public static boolean isAddMode(EditMode mode) { + return (mode == EditMode.ADD); + } + + public static boolean isEditMode(EditMode mode) { + return (mode == EditMode.EDIT); + } + + public static boolean isRepairMode(EditMode mode) { + return (mode == EditMode.REPAIR); + } + +}