-
Notifications
You must be signed in to change notification settings - Fork 86
/
BaseIndividualTemplateModel.java
262 lines (226 loc) · 9.59 KB
/
BaseIndividualTemplateModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.web.templatemodels.individual;
import static edu.cornell.mannlib.vitro.webapp.auth.requestedAction.RequestedAction.SOME_LITERAL;
import static edu.cornell.mannlib.vitro.webapp.auth.requestedAction.RequestedAction.SOME_PREDICATE;
import static edu.cornell.mannlib.vitro.webapp.auth.requestedAction.RequestedAction.SOME_URI;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginStatusBean;
import edu.cornell.mannlib.vitro.webapp.auth.permissions.SimplePermission;
import edu.cornell.mannlib.vitro.webapp.auth.policy.PolicyHelper;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.AddDataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.AddObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.ParamMap;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder.Route;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
public abstract class BaseIndividualTemplateModel extends BaseTemplateModel {
private static final Log log = LogFactory.getLog(BaseIndividualTemplateModel.class);
private static final String EDIT_PATH = "editRequestDispatch";
protected final Individual individual;
protected final LoginStatusBean loginStatusBean;
protected final VitroRequest vreq;
private final ServletContext ctx;
private final boolean editing;
protected GroupedPropertyList propertyList;
public BaseIndividualTemplateModel(Individual individual, VitroRequest vreq) {
this.vreq = vreq;
this.ctx = vreq.getSession().getServletContext();
this.individual = individual;
this.loginStatusBean = LoginStatusBean.getBean(vreq);
// Needed for getting portal-sensitive urls. Remove if multi-portal support is removed.
this.editing = isEditable();
}
protected boolean isVClass(String vClassUri) {
boolean isVClass = individual.isVClass(vClassUri);
// If reasoning is asynchronous, this inference may not have been made yet.
// Check the superclasses of the individual's vclass.
SimpleReasoner simpleReasoner = (SimpleReasoner) ctx.getAttribute(SimpleReasoner.class.getName());
if (!isVClass && simpleReasoner != null && simpleReasoner.isABoxReasoningAsynchronous()) {
log.debug("Checking superclasses to see if individual is a " + vClassUri + " because reasoning is asynchronous");
List<VClass> directVClasses = individual.getVClasses(true);
for (VClass directVClass : directVClasses) {
VClassDao vcDao = vreq.getWebappDaoFactory().getVClassDao();
List<String> superClassUris = vcDao.getAllSuperClassURIs(directVClass.getURI());
if (superClassUris.contains(vClassUri)) {
isVClass = true;
break;
}
}
}
return isVClass;
}
/* Template properties */
public String getProfileUrl() {
return UrlBuilder.getIndividualProfileUrl(individual, vreq);
}
public String getImageUrl() {
String imageUrl = individual.getImageUrl();
return imageUrl == null ? null : getUrl(imageUrl);
}
public String getThumbUrl() {
String thumbUrl = individual.getThumbUrl();
return thumbUrl == null ? null : getUrl(thumbUrl);
}
// Used to create a link to generate the individual's rdf.
public String getRdfUrl() {
String individualUri = getUri();
String profileUrl = getProfileUrl();
if (UrlBuilder.isUriInDefaultNamespace(individualUri, vreq)) {
return UrlBuilder.getUrl("/individual/" + getLocalName() + "/" + getLocalName() + ".rdf") ;
} else {
return UrlBuilder.addParams(profileUrl, "format", "rdfxml");
}
}
public GroupedPropertyList getPropertyList() {
if (propertyList == null) {
propertyList = new GroupedPropertyList(individual, vreq, editing);
}
return propertyList;
}
/**
* This page is editable if the user is authorized to add a data property or
* an object property to the Individual being shown.
*/
public boolean isEditable() {
AddDataPropertyStatement adps = new AddDataPropertyStatement(
vreq.getJenaOntModel(), individual.getURI(),
SOME_URI, SOME_LITERAL);
AddObjectPropertyStatement aops = new AddObjectPropertyStatement(
vreq.getJenaOntModel(), individual.getURI(),
SOME_PREDICATE, SOME_URI);
return PolicyHelper.isAuthorizedForActions(vreq, adps.or(aops));
}
public boolean getShowAdminPanel() {
return PolicyHelper.isAuthorizedForActions(vreq,
SimplePermission.SEE_INDVIDUAL_EDITING_PANEL.ACTION);
}
/* rdfs:label needs special treatment, because it is not possible to construct a
* DataProperty from it. It cannot be handled the way the vitro links and vitro public image
* are handled like ordinary ObjectProperty instances.
*/
public NameStatementTemplateModel getNameStatement() {
return new NameStatementTemplateModel(getUri(), vreq);
}
/* These methods simply forward to the methods of the wrapped individual. It would be desirable to
* implement a scheme for proxying or delegation so that the methods don't need to be simply listed here.
* A Ruby-style method missing method would be ideal.
* Update: DynamicProxy doesn't work because the proxied object is of type Individual, so we cannot
* declare new methods here that are not declared in the Individual interface.
*/
public String getName() {
return individual.getName();
}
public String getDeleteUrl() {
Collection<String> types = getMostSpecificTypes();
ParamMap params = new ParamMap(
"objectUri", individual.getURI(),
"cmd", "delete",
"individualName",getNameStatement().getValue()
);
Iterator<String> typesIterator = types.iterator();
if (types.iterator().hasNext()) {
String type = typesIterator.next();
params.put("individualType", type);
}
return UrlBuilder.getUrl(EDIT_PATH, params);
}
public Collection<String> getMostSpecificTypes() {
ObjectPropertyStatementDao opsDao = vreq.getWebappDaoFactory().getObjectPropertyStatementDao();
Map<String, String> types = opsDao.getMostSpecificTypesInClassgroupsForIndividual(getUri());
return types.values();
}
public String getUri() {
return individual.getURI();
}
public String getLocalName() {
return individual.getLocalName();
}
// For testing dump
// // Properties
// public String getANullString() {
// return null;
// }
// public String getAnEmptyString() {
// return "";
// }
// public String[] getANullArray() {
// return null;
// }
// public String[] getAnEmptyArray() {
// String[] a = {};
// return a;
// }
// public List<String> getANullList() {
// return null;
// }
// public List<String> getAnEmptyList() {
// return Collections.emptyList();
// }
// public Map<String, String> getANullMap() {
// return null;
// }
// public Map<String, String> getAnEmptyMap() {
// return Collections.emptyMap();
// }
//
// // Methods
// public String nullString() {
// return null;
// }
// public String emptyString() {
// return "";
// }
// public String[] nullArray() {
// return null;
// }
// public String[] emptyArray() {
// String[] a = {};
// return a;
// }
// public List<String> nullList() {
// return null;
// }
// public List<String> emptyList() {
// return Collections.emptyList();
// }
// public Map<String, String> nullMap() {
// return null;
// }
// public Map<String, String> emptyMap() {
// return Collections.emptyMap();
// }
/* Template methods */
public String selfEditingId() {
String id = null;
String idMatchingProperty =
ConfigurationProperties.getBean(ctx).getProperty("selfEditing.idMatchingProperty");
if (! StringUtils.isBlank(idMatchingProperty)) {
WebappDaoFactory wdf = vreq.getUnfilteredWebappDaoFactory();
Collection<DataPropertyStatement> ids =
wdf.getDataPropertyStatementDao().getDataPropertyStatementsForIndividualByDataPropertyURI(individual, idMatchingProperty);
if (ids.size() > 0) {
id = ids.iterator().next().getData();
}
}
return id;
}
public String controlPanelUrl() {
return UrlBuilder.getUrl(Route.INDIVIDUAL_EDIT, "uri", getUri());
}
}