1 package org.cateproject.view.tools;
2
3 import java.util.Collection;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.apache.velocity.tools.view.tools.LinkTool;
9 import org.cateproject.view.tools.ViewUtils.Crumb;
10 import org.hibernate.proxy.HibernateProxy;
11 import org.springframework.security.authentication.AuthenticationTrustResolver;
12 import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
13 import org.springframework.security.core.Authentication;
14 import org.springframework.security.core.context.SecurityContextHolder;
15
16 import eu.etaxonomy.cdm.model.common.CdmBase;
17 import eu.etaxonomy.cdm.model.common.ICdmBase;
18 import eu.etaxonomy.cdm.model.common.User;
19 import eu.etaxonomy.cdm.model.description.TaxonDescription;
20 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
21 import eu.etaxonomy.cdm.model.name.NonViralName;
22 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
25 import eu.etaxonomy.cdm.model.view.AuditEvent;
26 import eu.etaxonomy.cdm.model.view.context.AuditEventContext;
27 import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
28 import eu.etaxonomy.cdm.persistence.query.OrderHint;
29 import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
30
31 public class CATELinkTool extends LinkTool {
32 public CATELinkTool() {
33 super.setXHTML(false);
34 }
35
36 private AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl();
37 public void setRequest(HttpServletRequest request) {
38 this.request = request;
39 }
40
41 public LinkTool addType(String type) {
42 if(type == null || type == "") {
43 return this;
44 } else {
45 if(this.getPath().indexOf('.') >= -1) {
46 setPath(this.getPath().substring(0,this.getPath().lastIndexOf(".") + 1) + type);
47 } else {
48 setPath(this.getPath() + '.' + type);
49 }
50 return this;
51 }
52 }
53
54 public LinkTool addOrderHints(Collection<OrderHint> orderHints) {
55 if(orderHints == null || orderHints.isEmpty()) {
56 return this;
57 } else {
58 LinkTool copy = this;
59 for(OrderHint orderHint : orderHints) {
60 if(orderHint.getSortOrder() == SortOrder.ASCENDING) {
61 copy = copy.addQueryData("sort", orderHint.getPropertyName() + "_asc");
62 } else {
63 copy = copy.addQueryData("sort", orderHint.getPropertyName() + "_desc");
64 }
65 }
66 return copy;
67 }
68 }
69
70 public void setResponse(HttpServletResponse response) {
71 this.response = response;
72 }
73
74 public LinkTool setSpeciesPage(Taxon taxon) {
75 return setSpeciesPage(taxon.getName());
76 }
77
78 public LinkTool setSpeciesPage(TaxonDescription taxonDescription) {
79 return setSpeciesPage(taxonDescription.getTaxon().getName());
80 }
81
82 public LinkTool setSpeciesPage(TaxonNameDescription taxonNameDescription) {
83 return setSpeciesPage(taxonNameDescription.getTaxonName());
84 }
85
86 public LinkTool setLink(Crumb crumb) {
87 return addAuditEvent(setRelative(crumb.getLink()),null);
88 }
89
90 public String getRawDirectory() {
91 String path = this.getRequestPath();
92 if(path.indexOf(".") > -1) {
93 return path.substring(0, path.indexOf("."));
94 } else {
95 return path;
96 }
97 }
98
99 public static String getSpeciesPage(TaxonDescription description) {
100 return CATELinkTool.getSpeciesPage(description.getTaxon());
101 }
102
103 public static String getSpeciesPage(TaxonBase taxon) {
104 return CATELinkTool.getSpeciesPage(taxon.getName());
105 }
106
107 public static String getSpeciesPage(TaxonNameDescription description) {
108 return CATELinkTool.getSpeciesPage(description.getTaxonName());
109 }
110
111 public static String getSpeciesPage(TaxonNameBase name) {
112 if (name instanceof HibernateProxy) {
113 name = (TaxonNameBase)((HibernateProxy)name).getHibernateLazyInitializer().getImplementation();
114 }
115 NonViralName nonViralName = (NonViralName) name;
116 StringBuffer stringBuffer = new StringBuffer();
117 stringBuffer.append("/taxonomy/");
118 stringBuffer.append(nonViralName.getGenusOrUninomial());
119
120 if(nonViralName.getSpecificEpithet() != null) {
121 stringBuffer.append("/" + nonViralName.getSpecificEpithet());
122 }
123
124 if(nonViralName.getInfraSpecificEpithet() != null) {
125 stringBuffer.append("/" + nonViralName.getInfraSpecificEpithet());
126 }
127 stringBuffer.append(".html");
128 return stringBuffer.toString();
129 }
130
131 public LinkTool setSpeciesPage(TaxonNameBase name) {
132 return addAuditEvent(setRelative(CATELinkTool.getSpeciesPage(name)),null);
133 }
134
135 protected LinkTool addAuditEvent(LinkTool link, AuditEvent auditEvent) {
136 AuditEventContext auditEventContext = AuditEventContextHolder.getContext();
137 if(auditEvent == null) {
138 auditEvent = auditEventContext.getAuditEvent();
139 if(auditEvent != null && auditEvent != AuditEvent.CURRENT_VIEW) {
140 return link.addQueryData("view", auditEvent.getUuid());
141 } else {
142 return link;
143 }
144 } else {
145 return link.addQueryData("view", auditEvent.getUuid());
146 }
147 }
148 public LinkTool setResourceVersion(String resource, ICdmBase cdmBase, AuditEvent auditEvent) {
149 return setResourceVersionAs(resource, cdmBase,auditEvent,".html");
150 }
151
152 public LinkTool setResourceVersionAs(String resource, ICdmBase cdmBase, AuditEvent auditEvent, String extension) {
153 StringBuffer stringBuffer = new StringBuffer();
154 stringBuffer.append(resource);
155 if(cdmBase instanceof User) {
156 stringBuffer.append("/" + ((User)cdmBase).getUsername());
157 } else {
158 stringBuffer.append("/" + ((CdmBase)cdmBase).getUuid());
159 }
160 stringBuffer.append(".html");
161 return addAuditEvent(setRelative(stringBuffer.toString()),auditEvent);
162 }
163
164 public LinkTool setResource(String resource, ICdmBase cdmBase) {
165 return setResourceAs(resource,cdmBase,".html");
166 }
167
168 public LinkTool setResourceAs(String resource, ICdmBase cdmBase, String extension) {
169 if(cdmBase == null) {
170 return null;
171 }
172 StringBuffer stringBuffer = new StringBuffer();
173 stringBuffer.append(resource);
174 if(cdmBase instanceof User) {
175 stringBuffer.append("/" + ((User)cdmBase).getUsername());
176 } else {
177 stringBuffer.append("/" + ((CdmBase)cdmBase).getUuid());
178 }
179 stringBuffer.append(".html");
180 return addAuditEvent(setRelative(stringBuffer.toString()),null);
181 }
182
183 public LinkTool setUserResource(String resource) {
184 StringBuffer stringBuffer = new StringBuffer();
185 stringBuffer.append(resource);
186 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
187
188 if(authentication != null && !authenticationTrustResolver.isAnonymous(authentication)) {
189 User authenticatedUser = (User)authentication.getPrincipal();
190 stringBuffer.append("/" + authenticatedUser.getUsername());
191 stringBuffer.append(".html");
192 return setRelative(stringBuffer.toString());
193 } else {
194 return null;
195 }
196 }
197
198 public LinkTool setUserSublist(String resource, String subList) {
199 StringBuffer stringBuffer = new StringBuffer();
200 stringBuffer.append(resource);
201 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
202 if(authentication != null && !authenticationTrustResolver.isAnonymous(authentication)) {
203 User authenticatedUser = (User)authentication.getPrincipal();
204 stringBuffer.append("/" + authenticatedUser.getUsername());
205 stringBuffer.append("/" + subList);
206 return setRelative(stringBuffer.toString());
207 } else {
208 return null;
209 }
210 }
211
212 public LinkTool setSublist(String resource, ICdmBase cdmBase, String subList) {
213 StringBuffer stringBuffer = new StringBuffer();
214 stringBuffer.append(resource);
215 if(cdmBase instanceof User) {
216 stringBuffer.append("/" + ((User)cdmBase).getUsername());
217 } else {
218 stringBuffer.append("/" + ((CdmBase)cdmBase).getUuid());
219 }
220 stringBuffer.append("/" + subList);
221 stringBuffer.append(".html");
222 return addAuditEvent(setRelative(stringBuffer.toString()),null);
223 }
224
225 public LinkTool setList(String resource) {
226 return setListAs(resource,".html");
227 }
228
229 public LinkTool setListAs(String resource, String extension) {
230 StringBuffer stringBuffer = new StringBuffer();
231 stringBuffer.append(resource);
232 stringBuffer.append(extension);
233 return addAuditEvent(setRelative(stringBuffer.toString()),null);
234 }
235
236 }