VIVO-140 Modify VClassGroupCache to provide a language-aware extract
With very little change in the method calls, client code can get the VClasses and VClassGroups with labels that are appropriate for the preferred language in the current request.
This commit is contained in:
parent
d2177ab3ce
commit
24562b6746
4 changed files with 135 additions and 0 deletions
|
@ -129,6 +129,29 @@ public class VClass extends BaseResourceBean implements Comparable<VClass>
|
|||
localName = uri.getLocalName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the VClass as a deep copy of an existing VClass.
|
||||
*/
|
||||
public VClass( VClass vc) {
|
||||
this.URI = vc.URI;
|
||||
this.namespace = vc.namespace;
|
||||
this.localName = vc.localName;
|
||||
this.myName = vc.myName;
|
||||
this.myExample = vc.myExample;
|
||||
this.myDescription = vc.myDescription;
|
||||
this.myShortDefinition = vc.myShortDefinition;
|
||||
this.myEntityCount = vc.myEntityCount;
|
||||
this.displayLimit = vc.displayLimit;
|
||||
this.displayRank = vc.displayRank;
|
||||
this.quickEditJsp = vc.quickEditJsp;
|
||||
this.groupURI = vc.groupURI;
|
||||
this.group = (vc.group == null) ? null : new VClassGroup(vc.group);
|
||||
this.customEntryForm = vc.customEntryForm;
|
||||
this.customDisplayView = vc.customDisplayView;
|
||||
this.customShortView = vc.customShortView;
|
||||
this.customSearchView = vc.customSearchView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts alphabetically by name
|
||||
*/
|
||||
|
|
|
@ -62,6 +62,15 @@ public class VClassGroup extends LinkedList <VClass> implements Comparable<VClas
|
|||
this.displayRank = rank;
|
||||
this.publicName = name;
|
||||
}
|
||||
|
||||
public VClassGroup(VClassGroup vcg) {
|
||||
this.URI = vcg.URI;
|
||||
this.namespace = vcg.namespace;
|
||||
this.localName = vcg.localName;
|
||||
this.publicName = vcg.publicName;
|
||||
this.displayRank = vcg.displayRank;
|
||||
this.individualCount = vcg.individualCount;
|
||||
}
|
||||
|
||||
public String getURI() {
|
||||
return URI;
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache;
|
||||
|
||||
/**
|
||||
* A request-based image of the VClassGroupCache. That means that the names of
|
||||
* the classes and groups are in the correct language for this request.
|
||||
*/
|
||||
public class VClassGroupsForRequest {
|
||||
private static final Log log = LogFactory
|
||||
.getLog(VClassGroupsForRequest.class);
|
||||
|
||||
private final HttpServletRequest req;
|
||||
private final Map<String, VClassGroup> groupMap = new LinkedHashMap<>();
|
||||
private final Map<String, VClass> classMap = new HashMap<>();
|
||||
|
||||
public VClassGroupsForRequest(HttpServletRequest req, VClassGroupCache cache) {
|
||||
this.req = req;
|
||||
|
||||
for (VClassGroup vcGroup : cache.getGroups()) {
|
||||
loadGroup(vcGroup);
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("groups: " + groupMap);
|
||||
log.debug("classes: " + classMap);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadGroup(VClassGroup vcg) {
|
||||
VClassGroup newVcg = new VClassGroup(vcg);
|
||||
newVcg.setPublicName(getNameForGroup(vcg));
|
||||
groupMap.put(newVcg.getURI(), newVcg);
|
||||
|
||||
for (VClass vClass : vcg) {
|
||||
loadClass(vClass, newVcg);
|
||||
}
|
||||
}
|
||||
|
||||
private String getNameForGroup(VClassGroup vcGroup) {
|
||||
VClassGroup g = ModelAccess.on(req).getWebappDaoFactory()
|
||||
.getVClassGroupDao().getGroupByURI(vcGroup.getURI());
|
||||
return (g == null) ? vcGroup.getPublicName() : g.getPublicName();
|
||||
}
|
||||
|
||||
private void loadClass(VClass vc, VClassGroup newVcg) {
|
||||
VClass newVc = new VClass(vc);
|
||||
newVc.setName(getNameForVClass(vc));
|
||||
newVc.setGroup(newVcg);
|
||||
newVcg.add(newVc);
|
||||
classMap.put(newVc.getURI(), newVc);
|
||||
}
|
||||
|
||||
private String getNameForVClass(VClass vClass) {
|
||||
VClass vc = ModelAccess.on(req).getWebappDaoFactory().getVClassDao()
|
||||
.getVClassByURI(vClass.getURI());
|
||||
return (vc == null) ? vClass.getName() : vc.getName();
|
||||
}
|
||||
|
||||
public VClassGroup getGroup(String vClassGroupURI) {
|
||||
VClassGroup vcg = groupMap.get(vClassGroupURI);
|
||||
log.debug("getGroup(" + vClassGroupURI + ") = " + vcg);
|
||||
return vcg;
|
||||
}
|
||||
|
||||
public List<VClassGroup> getGroups() {
|
||||
ArrayList<VClassGroup> groups = new ArrayList<>(groupMap.values());
|
||||
log.debug("getGroups() returned " + groups.size() + " groups.");
|
||||
return groups;
|
||||
}
|
||||
|
||||
public VClass getCachedVClass(String classUri) {
|
||||
VClass vc = classMap.get(classUri);
|
||||
log.debug("getCachedVClass(" + classUri + ") = " + vc);
|
||||
return vc;
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@ import java.util.Map;
|
|||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -33,6 +34,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupsForRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
|
||||
|
@ -227,6 +229,14 @@ public class VClassGroupCache implements IndexingEventListener {
|
|||
return (VClassGroupCache) sc.getAttribute(ATTRIBUTE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use getVClassGroups(HttpServletRequest) to get a language-aware image of
|
||||
* the cached groups and classes.
|
||||
*/
|
||||
public static VClassGroupsForRequest getVClassGroups(HttpServletRequest req) {
|
||||
return new VClassGroupsForRequest(req, getVClassGroupCache(req.getSession().getServletContext()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that rebuilds the cache. This will use a WebappDaoFactory,
|
||||
* a SolrSever and maybe a ProhibitedFromSearch from the cache.context.
|
||||
|
|
Loading…
Add table
Reference in a new issue