NIHVIVO-2508 Preparation for exposing some template model methods as methods rather than properties. Also condensed code used to generate editing links for menu management.

This commit is contained in:
ryounes 2011-08-03 14:55:18 +00:00
parent 44ef90bf6a
commit c356b16807
20 changed files with 83 additions and 51 deletions

View file

@ -140,7 +140,7 @@ public class IndividualController extends FreemarkerHttpServlet {
* This is still safe, because we are only putting BaseTemplateModel objects * This is still safe, because we are only putting BaseTemplateModel objects
* into the data model: no real data can be modified. * into the data model: no real data can be modified.
*/ */
//body.put("individual", wrap(itm, BeansWrapper.EXPOSE_SAFE)); // body.put("individual", wrap(itm, BeansWrapper.EXPOSE_SAFE));
body.put("individual", wrap(itm, new ReadOnlyBeansWrapper())); body.put("individual", wrap(itm, new ReadOnlyBeansWrapper()));
body.put("headContent", getRdfLinkTag(itm)); body.put("headContent", getRdfLinkTag(itm));

View file

@ -272,7 +272,7 @@ public class UrlBuilder {
} }
if (profileUrl != null) { if (profileUrl != null) {
HashMap<String, String> specialParams = getSpecialParams(vreq); HashMap<String, String> specialParams = getModelParams(vreq);
if(specialParams.size() != 0) { if(specialParams.size() != 0) {
profileUrl = addParams(profileUrl, new ParamMap(specialParams)); profileUrl = addParams(profileUrl, new ParamMap(specialParams));
} }
@ -321,7 +321,7 @@ public class UrlBuilder {
//To be used in different property templates so placing method for reuse here //To be used in different property templates so placing method for reuse here
//Check if special params included, specifically for menu management and other models //Check if special params included, specifically for menu management and other models
public static HashMap<String,String> getSpecialParams(VitroRequest vreq) { public static HashMap<String,String> getModelParams(VitroRequest vreq) {
HashMap<String,String> specialParams = new HashMap<String, String>(); HashMap<String,String> specialParams = new HashMap<String, String>();
if(vreq != null) { if(vreq != null) {

View file

@ -58,7 +58,7 @@ public abstract class BaseTemplateModel {
AntiScript.cleanMapValues(map, getServletContext()); AntiScript.cleanMapValues(map, getServletContext());
} }
public static ServletContext getServletContext() { protected static ServletContext getServletContext() {
return servletContext; return servletContext;
} }
@ -66,5 +66,6 @@ public abstract class BaseTemplateModel {
servletContext = context; servletContext = context;
} }
/* Template properties */
} }

View file

@ -29,10 +29,6 @@ public class LinkTemplateModel extends BaseTemplateModel {
setUrl(path, params); setUrl(path, params);
} }
public String getUrl() {
return cleanTextForDisplay( url );
}
protected void setUrl(String path) { protected void setUrl(String path) {
url = UrlBuilder.getUrl(path); url = UrlBuilder.getUrl(path);
} }
@ -45,12 +41,17 @@ public class LinkTemplateModel extends BaseTemplateModel {
url = UrlBuilder.getUrl(path, params); url = UrlBuilder.getUrl(path, params);
} }
public String getText() {
return cleanTextForDisplay(text);
}
protected void setText(String text) { protected void setText(String text) {
this.text = StringEscapeUtils.escapeHtml(text); this.text = StringEscapeUtils.escapeHtml(text);
} }
/* Template properties */
public String getUrl() {
return cleanTextForDisplay( url );
}
public String getText() {
return cleanTextForDisplay(text);
}
} }

View file

@ -16,6 +16,8 @@ public class Tags extends BaseTemplateModel {
this.tags = tags; this.tags = tags;
} }
/* Template methods */
public void add(String... tags) { public void add(String... tags) {
for (String tag : tags) { for (String tag : tags) {
add(tag); add(tag);

View file

@ -43,6 +43,10 @@ public class User extends BaseTemplateModel {
return url; return url;
} }
/* Template methods */
/* Template properties */
public boolean isLoggedIn() { public boolean isLoggedIn() {
return currentUser != null; return currentUser != null;
} }

View file

@ -284,7 +284,7 @@ public class CollatedObjectPropertyTemplateModel extends ObjectPropertyTemplateM
// } // }
/* Access methods for templates */ /* Template properties */
public List<SubclassTemplateModel> getSubclasses() { public List<SubclassTemplateModel> getSubclasses() {
return subclasses; return subclasses;

View file

@ -96,7 +96,10 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
} }
/* Access methods for templates */ /* Template properties */
/* Template methods */
public String getValue() { public String getValue() {
return value; return value;
@ -113,11 +116,8 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
params.put("deleteProhibited", "prohibited"); params.put("deleteProhibited", "prohibited");
} }
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
editUrl = UrlBuilder.getUrl(EDIT_PATH, params); editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
} }
return editUrl; return editUrl;
@ -132,11 +132,7 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat
"datapropKey", dataPropHash, "datapropKey", dataPropHash,
"cmd", "delete"); "cmd", "delete");
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params); deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params);
} }

View file

@ -104,11 +104,8 @@ public class DataPropertyTemplateModel extends PropertyTemplateModel {
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri); "predicateUri", propertyUri);
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
addUrl = UrlBuilder.getUrl(EDIT_PATH, params); addUrl = UrlBuilder.getUrl(EDIT_PATH, params);
} }
return addUrl; return addUrl;

View file

@ -426,12 +426,15 @@ public class GroupedPropertyList extends BaseTemplateModel {
return label; return label;
} }
/* Access methods for templates */ /* Template properties */
public List<PropertyGroupTemplateModel> getAll() { public List<PropertyGroupTemplateModel> getAll() {
return groups; return groups;
} }
/* Template methods */
public PropertyTemplateModel getProperty(String propertyUri) { public PropertyTemplateModel getProperty(String propertyUri) {
for (PropertyGroupTemplateModel pgtm : groups) { for (PropertyGroupTemplateModel pgtm : groups) {

View file

@ -82,11 +82,7 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
params.put("deleteProhibited", "prohibited"); params.put("deleteProhibited", "prohibited");
} }
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
editUrl = UrlBuilder.getUrl(EDIT_PATH, params); editUrl = UrlBuilder.getUrl(EDIT_PATH, params);
} }
@ -105,6 +101,7 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
"predicateUri", propertyUri, "predicateUri", propertyUri,
"objectUri", objectUri, "objectUri", objectUri,
"cmd", "delete"); "cmd", "delete");
for ( String key : data.keySet() ) { for ( String key : data.keySet() ) {
String value = data.get(key); String value = data.get(key);
// Remove an entry with a null value instead of letting it get passed // Remove an entry with a null value instead of letting it get passed
@ -116,13 +113,10 @@ public class ObjectPropertyStatementTemplateModel extends PropertyStatementTempl
params.put("statement_" + key, data.get(key)); params.put("statement_" + key, data.get(key));
} }
} }
params.put("templateName", templateName); params.put("templateName", templateName);
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params); deleteUrl = UrlBuilder.getUrl(EDIT_PATH, params);

View file

@ -583,11 +583,7 @@ public abstract class ObjectPropertyTemplateModel extends PropertyTemplateModel
"subjectUri", subjectUri, "subjectUri", subjectUri,
"predicateUri", propertyUri); "predicateUri", propertyUri);
//Check if special parameters being sent params.putAll(UrlBuilder.getModelParams(vreq));
HashMap<String, String> specialParams = UrlBuilder.getSpecialParams(vreq);
if(specialParams.size() > 0) {
params.putAll(specialParams);
}
addUrl = UrlBuilder.getUrl(EDIT_PATH, params); addUrl = UrlBuilder.getUrl(EDIT_PATH, params);

View file

@ -21,6 +21,8 @@ public class MainMenuItem extends MenuItem {
this.active = active; this.active = active;
} }
/* Template properties */
public boolean isActive() { public boolean isActive() {
return active; return active;
} }

View file

@ -26,6 +26,8 @@ public class Menu extends BaseTemplateModel {
items.add(new MenuItem(text, path)); items.add(new MenuItem(text, path));
} }
/* Template properties */
public List<MenuItem> getItems() { public List<MenuItem> getItems() {
return items; return items;
} }

View file

@ -26,6 +26,8 @@ public class MenuItem extends BaseTemplateModel {
this.active = active; this.active = active;
} }
/* Template properties */
public String getLinkText() { public String getLinkText() {
return text; return text;
} }

View file

@ -43,7 +43,7 @@ public abstract class BaseIndividualSearchResult extends BaseTemplateModel {
return models; return models;
} }
/* Access methods for templates */ /* Template properties */
public String getProfileUrl() { public String getProfileUrl() {
return UrlBuilder.getIndividualProfileUrl(individual, vreq); return UrlBuilder.getIndividualProfileUrl(individual, vreq);

View file

@ -36,4 +36,16 @@ public class ReadOnlyBeansWrapper extends BeansWrapper {
} }
} }
// For exposing a method as a property (when it's not named getX or isX). Note that this is not
// just a syntactic change in the template from X() to X, but also makes the value get precomputed.
// private void exposeAsProperty(Method method, MethodAppearanceDecision decision) {
// try {
// PropertyDescriptor pd = new PropertyDescriptor(method.getName(), method, null);
// decision.setExposeAsProperty(pd);
// decision.setMethodShadowsProperty(false);
// } catch (IntrospectionException e) {
// log.error(e, e);
// }
// }
} }

View file

@ -0,0 +1,20 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package freemarker.ext.beans;
import freemarker.template.TemplateModel;
/**
* Class to extract information about the wrapper used to wrap an object in
* the template model.
*/
public class WrapperExtractor {
public static BeansWrapper getWrapper(BeanModel model) {
return model.wrapper;
}
public static int getWrapperExposureLevel(BeanModel model) {
return model.wrapper.getExposureLevel();
}
}

View file

@ -321,7 +321,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put(Key.TYPE.toString(), object.getClass().getName()); map.put(Key.TYPE.toString(), object.getClass().getName());
// Compile the sets of properties and methods available to template // Compile the collections of properties and methods available to the template
SortedMap<String, Object> properties = new TreeMap<String, Object>(); SortedMap<String, Object> properties = new TreeMap<String, Object>();
List<String> methods = new ArrayList<String>(); List<String> methods = new ArrayList<String>();
@ -379,6 +379,8 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
} }
// Else look for the entire methodName in the key set. Include those // Else look for the entire methodName in the key set. Include those
// starting with "get" or "is" that were not found above. // starting with "get" or "is" that were not found above.
// NB This does not properly account for methods exposed as properties
// using BeansWrapper.finetuneMethodAppearance().
if (keySet.contains(methodName)) { if (keySet.contains(methodName)) {
String methodDisplayName = getMethodDisplayName(method); String methodDisplayName = getMethodDisplayName(method);
methods.add(methodDisplayName); methods.add(methodDisplayName);

View file

@ -40,7 +40,6 @@
</#list> </#list>
</#macro> </#macro>
<#macro objectProperty property editable template=property.template> <#macro objectProperty property editable template=property.template>
<#if property.collatedBySubclass> <#-- collated --> <#if property.collatedBySubclass> <#-- collated -->
<@collatedObjectPropertyList property editable template /> <@collatedObjectPropertyList property editable template />
@ -52,7 +51,6 @@
</#if> </#if>
</#macro> </#macro>
<#macro collatedObjectPropertyList property editable template=property.template > <#macro collatedObjectPropertyList property editable template=property.template >
<#assign subclasses = property.subclasses> <#assign subclasses = property.subclasses>
<#list subclasses as subclass> <#list subclasses as subclass>