From e13564c983985b4b36e8f2150bbd51b5f2b5e8ae Mon Sep 17 00:00:00 2001 From: bdc34 Date: Tue, 11 Jan 2011 16:19:54 +0000 Subject: [PATCH] Fixing SiteAdminController so the tempaltes get resonable values when classgroups lack rdfs:label NIHVIVO-1635 --- .../freemarker/SiteAdminController.java | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SiteAdminController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SiteAdminController.java index 7b6002f51..0430ba1bc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SiteAdminController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SiteAdminController.java @@ -3,10 +3,12 @@ package edu.cornell.mannlib.vitro.webapp.controller.freemarker; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,10 +21,8 @@ import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMa import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route; 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.controller.login.LoginTemplateHelper; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener; -import freemarker.template.Configuration; public class SiteAdminController extends FreemarkerHttpServlet { @@ -90,13 +90,27 @@ public class SiteAdminController extends FreemarkerHttpServlet { WebappDaoFactory wadf = vreq.getFullWebappDaoFactory(); // Create map for data input entry form options list - List classGroups = wadf.getVClassGroupDao().getPublicGroupsWithVClasses(true,true,false); // order by displayRank, include uninstantiated classes, don't get the counts of individuals + List classGroups = wadf.getVClassGroupDao().getPublicGroupsWithVClasses(true,true,false); // order by displayRank, include uninstantiated classes, don't get the counts of individuals + + boolean classGroupDisplayAssumptionsMeet = checkClassGroupDisplayAssumptions(classGroups); + + Set seenGroupNames = new HashSet(); + Iterator classGroupIt = classGroups.iterator(); LinkedHashMap orderedClassGroups = new LinkedHashMap(classGroups.size()); while (classGroupIt.hasNext()) { - VClassGroup group = (VClassGroup)classGroupIt.next(); - List classes = group.getVitroClassList(); - orderedClassGroups.put(group.getPublicName(),FormUtils.makeOptionListFromBeans(classes,"URI","PickListName",null,null,false)); + VClassGroup group = (VClassGroup)classGroupIt.next(); + List opts = FormUtils.makeOptionListFromBeans(group.getVitroClassList(),"URI","PickListName",null,null,false); + if( seenGroupNames.contains(group.getPublicName() )){ + //have a duplicat classgroup name, stick in the URI + orderedClassGroups.put(group.getPublicName() + " ("+group.getURI()+")", opts); + }else if( group.getPublicName() == null ){ + //have an unlabeled group, use stick in the URI + orderedClassGroups.put("unnamed group ("+group.getURI()+")", opts); + }else{ + orderedClassGroups.put(group.getPublicName(),opts); + seenGroupNames.add(group.getPublicName()); + } } map.put("groupedClassOptions", orderedClassGroups); @@ -195,4 +209,42 @@ public class SiteAdminController extends FreemarkerHttpServlet { return map; } + /* + * There is a problem where labels are being used as keys for a map of + * classgroups that gets passed to the templates. This representation isn't an accurate + * reflection of the model since classgroups could have two labels, multiple classgroups + * could have the same label, and a classgroup could have no lables. + * + * Check the assumptions and use the URIs as the key if the assumptions are not + * meet. see issue NIHVIVO-1635. + */ + private boolean checkClassGroupDisplayAssumptions( List groups){ + //Assumption A: all of the classgroups have a non-null rdfs:label + //Assumption B: none of the classgroups have the same rdfs:label + //the assumption that all classgroups have only one rdfs:label is not checked + boolean rvalue = true; + Set seenPublicNames = new HashSet(); + + for( VClassGroup group :groups ){ + //check Assumption A + if( group.getPublicName() == null){ + rvalue = false; + break; + } + + //check Assumption B + if( seenPublicNames.contains(group.getPublicName()) ){ + rvalue = false; + break; + } + seenPublicNames.add(group.getPublicName()); + } + + + if( !rvalue ) + log.error("The rdfs:labels on the classgroups in the system do " + + "not meet the display assumptions. Falling back to alternative."); + return rvalue; + } + }