Changing ViewFinder.java to not check the vclasses for custom short views. NIHVIVO-2024

This commit is contained in:
bdc34 2011-02-03 20:28:36 +00:00
parent 8be9eaaeae
commit e956a3668a

View file

@ -80,101 +80,101 @@ public class ViewFinder {
} }
private String findCustomTemplateByVClasses(Individual individual, VitroRequest vreq) { private String findCustomTemplateByVClasses(Individual individual, VitroRequest vreq) {
//
Method method = view.getMethod(); // Method method = view.getMethod();
TemplateLoader templateLoader = ((Configuration) vreq.getAttribute("freemarkerConfig")).getTemplateLoader(); // TemplateLoader templateLoader = ((Configuration) vreq.getAttribute("freemarkerConfig")).getTemplateLoader();
//
/* RY The logic here is incorrect. The vclasses are // /* RY The logic here is incorrect. The vclasses are
* returned in a random order, whereas we need to // * returned in a random order, whereas we need to
* traverse the class hierarchy and find the most // * traverse the class hierarchy and find the most
* specific custom view applicable to the individual. // * specific custom view applicable to the individual.
* The logic is complex because individuals can belong // * The logic is complex because individuals can belong
* to multiple classes, and classes can subclass multiple // * to multiple classes, and classes can subclass multiple
* classes. If there are two competing custom views at the // * classes. If there are two competing custom views at the
* same level of specificity, what should we do? Also, if we // * same level of specificity, what should we do? Also, if we
* are displaying a list of individuals belonging to a certain // * are displaying a list of individuals belonging to a certain
* class, we may want to use only a custom view defined for that // * class, we may want to use only a custom view defined for that
* class and NOT a more specific one. See NIHVIVO-568. Similarly // * class and NOT a more specific one. See NIHVIVO-568. Similarly
* when we're displaying an object property: if we are displaying // * when we're displaying an object property: if we are displaying
* #hasPrincipalInvestigatorRole, the object should be displayed // * #hasPrincipalInvestigatorRole, the object should be displayed
* as a PrincipalInvestigatorRole object rather than some other type. // * as a PrincipalInvestigatorRole object rather than some other type.
* // *
* For now, iterate first through asserted classes, and if no custom view // * For now, iterate first through asserted classes, and if no custom view
* found there, iterate through inferred classes. Modeled on MiscWebUtils.getCustomShortView(). // * found there, iterate through inferred classes. Modeled on MiscWebUtils.getCustomShortView().
*/ // */
String customTemplate = null; // String customTemplate = null;
VClassDao vcDao = vreq.getWebappDaoFactory().getVClassDao(); // VClassDao vcDao = vreq.getWebappDaoFactory().getVClassDao();
List<VClass> vclasses = individual.getVClasses(true); // get directly asserted vclasses // List<VClass> vclasses = individual.getVClasses(true); // get directly asserted vclasses
Set<String> superClasses = new HashSet<String>(); // Set<String> superClasses = new HashSet<String>();
//
// First try directly asserted classes. There is no useful decision // // First try directly asserted classes. There is no useful decision
// mechanism for the case where two directly asserted classes // // mechanism for the case where two directly asserted classes
// define a custom template. // // define a custom template.
// RY If we're getting the custom short view with reference to an object property. // // RY If we're getting the custom short view with reference to an object property.
// should we use the property's getRangeVClass() method instead? // // should we use the property's getRangeVClass() method instead?
for (VClass vclass : vclasses) { // for (VClass vclass : vclasses) {
// Use this class's custom template, if there is one // // Use this class's custom template, if there is one
customTemplate = findCustomTemplateForVClass(vclass, method, templateLoader); // customTemplate = findCustomTemplateForVClass(vclass, method, templateLoader);
if (customTemplate != null) { // if (customTemplate != null) {
return customTemplate; // return customTemplate;
} // }
// Otherwise, add superclass to list of vclasses to check for custom // // Otherwise, add superclass to list of vclasses to check for custom
// templates. // // templates.
String vclassUri = vclass.getURI(); // String vclassUri = vclass.getURI();
superClasses.addAll(vcDao.getAllSuperClassURIs(vclassUri)); // superClasses.addAll(vcDao.getAllSuperClassURIs(vclassUri));
} // }
//
// Next try superclasses. There is no useful decision mechanism for // // Next try superclasses. There is no useful decision mechanism for
// the case where two superclasses have a custom template defined. // // the case where two superclasses have a custom template defined.
for (String superClassUri : superClasses) { // for (String superClassUri : superClasses) {
VClass vc = vcDao.getVClassByURI(superClassUri); // VClass vc = vcDao.getVClassByURI(superClassUri);
customTemplate = findCustomTemplateForVClass(vc, method, templateLoader); // customTemplate = findCustomTemplateForVClass(vc, method, templateLoader);
if (customTemplate != null) { // if (customTemplate != null) {
return customTemplate; // return customTemplate;
} // }
} // }
return null; return null;
} }
private String findCustomTemplateForVClass(VClass vclass, Method method, TemplateLoader templateLoader) { // private String findCustomTemplateForVClass(VClass vclass, Method method, TemplateLoader templateLoader) {
String customTemplate = null; // String customTemplate = null;
String vClassCustomTemplate = null; // String vClassCustomTemplate = null;
//
try { // try {
vClassCustomTemplate = (String) method.invoke(vclass); // vClassCustomTemplate = (String) method.invoke(vclass);
} catch (IllegalArgumentException e) { // } catch (IllegalArgumentException e) {
log.error("Incorrect arguments passed to method " + method.getName() + " in findCustomTemplateForVClass()."); // log.error("Incorrect arguments passed to method " + method.getName() + " in findCustomTemplateForVClass().");
} catch (IllegalAccessException e) { // } catch (IllegalAccessException e) {
log.error("Method " + method.getName() + " cannot be accessed in findCustomTemplateForVClass()."); // log.error("Method " + method.getName() + " cannot be accessed in findCustomTemplateForVClass().");
} catch (InvocationTargetException e) { // } catch (InvocationTargetException e) {
log.error("Exception thrown by method " + method.getName() + " in findCustomTemplateForVClass()."); // log.error("Exception thrown by method " + method.getName() + " in findCustomTemplateForVClass().");
} // }
//
if (!StringUtils.isEmpty(vClassCustomTemplate)) { // if (!StringUtils.isEmpty(vClassCustomTemplate)) {
log.debug("Custom template " + vClassCustomTemplate + " defined for class " + vclass.getName()); // log.debug("Custom template " + vClassCustomTemplate + " defined for class " + vclass.getName());
try { // try {
// Make sure the template exists // // Make sure the template exists
if (templateLoader.findTemplateSource(vClassCustomTemplate) != null) { // if (templateLoader.findTemplateSource(vClassCustomTemplate) != null) {
log.debug("Found defined custom template " + vClassCustomTemplate + " for class " + vclass.getName()); // log.debug("Found defined custom template " + vClassCustomTemplate + " for class " + vclass.getName());
customTemplate = vClassCustomTemplate; // customTemplate = vClassCustomTemplate;
} else { // } else {
log.warn("Custom template " + vClassCustomTemplate + " for class " + vclass.getName() + " defined but does not exist."); // log.warn("Custom template " + vClassCustomTemplate + " for class " + vclass.getName() + " defined but does not exist.");
} // }
} catch (IOException e) { // } catch (IOException e) {
log.error("IOException looking for source for template " + vClassCustomTemplate); // log.error("IOException looking for source for template " + vClassCustomTemplate);
} // }
} // }
//
if (log.isDebugEnabled()) { // if (log.isDebugEnabled()) {
if (customTemplate != null) { // if (customTemplate != null) {
log.debug("Using custom template " + customTemplate + " for class " + vclass.getName()); // log.debug("Using custom template " + customTemplate + " for class " + vclass.getName());
} else { // } else {
log.debug("No custom template found for class " + vclass.getName()); // log.debug("No custom template found for class " + vclass.getName());
} // }
} // }
return customTemplate; // return customTemplate;
} // }
} }