1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.krad.uif.widget; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
20 | |
import org.kuali.rice.core.web.format.Formatter; |
21 | |
import org.kuali.rice.krad.bo.ExternalizableBusinessObject; |
22 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
23 | |
import org.kuali.rice.krad.service.KRADServiceLocatorWeb; |
24 | |
import org.kuali.rice.krad.service.ModuleService; |
25 | |
import org.kuali.rice.krad.uif.UifConstants; |
26 | |
import org.kuali.rice.krad.uif.UifParameters; |
27 | |
import org.kuali.rice.krad.uif.field.DataField; |
28 | |
import org.kuali.rice.krad.uif.view.View; |
29 | |
import org.kuali.rice.krad.uif.component.BindingInfo; |
30 | |
import org.kuali.rice.krad.uif.component.Component; |
31 | |
import org.kuali.rice.krad.uif.field.LinkField; |
32 | |
import org.kuali.rice.krad.uif.util.LookupInquiryUtils; |
33 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
34 | |
import org.kuali.rice.krad.uif.util.ViewModelUtils; |
35 | |
import org.kuali.rice.krad.util.UrlFactory; |
36 | |
|
37 | |
import java.security.GeneralSecurityException; |
38 | |
import java.util.HashMap; |
39 | |
import java.util.List; |
40 | |
import java.util.Map; |
41 | |
import java.util.Map.Entry; |
42 | |
import java.util.Properties; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class Inquiry extends WidgetBase { |
50 | |
private static final long serialVersionUID = -2154388007867302901L; |
51 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(Inquiry.class); |
52 | |
|
53 | |
public static final String INQUIRY_TITLE_PREFIX = "title.inquiry.url.value.prependtext"; |
54 | |
|
55 | |
private String baseInquiryUrl; |
56 | |
|
57 | |
private String dataObjectClassName; |
58 | |
private String viewName; |
59 | |
|
60 | |
private Map<String, String> inquiryParameters; |
61 | |
|
62 | |
private boolean forceInquiry; |
63 | |
|
64 | |
private LinkField inquiryLinkField; |
65 | |
|
66 | |
public Inquiry() { |
67 | 0 | super(); |
68 | |
|
69 | 0 | forceInquiry = false; |
70 | 0 | inquiryParameters = new HashMap<String, String>(); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
@Override |
78 | |
public void performFinalize(View view, Object model, Component parent) { |
79 | 0 | super.performFinalize(view, model, parent); |
80 | |
|
81 | |
|
82 | 0 | if (!isRender() || !isReadOnly()) { |
83 | 0 | return; |
84 | |
} |
85 | |
|
86 | |
|
87 | 0 | setRender(false); |
88 | |
|
89 | 0 | DataField field = (DataField) parent; |
90 | |
|
91 | |
|
92 | 0 | Object propertyValue = ObjectPropertyUtils.getPropertyValue(model, field.getBindingInfo().getBindingPath()); |
93 | 0 | if ((propertyValue == null) || StringUtils.isBlank(propertyValue.toString())) { |
94 | 0 | return; |
95 | |
} |
96 | |
|
97 | 0 | setupLink(view, model, field); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public void setupLink(View view, Object model, DataField field) { |
110 | 0 | String propertyName = field.getBindingInfo().getBindingName(); |
111 | |
|
112 | |
|
113 | 0 | if (StringUtils.isNotBlank(getDataObjectClassName()) && (getInquiryParameters() != null) && |
114 | |
!getInquiryParameters().isEmpty()) { |
115 | 0 | Class<?> inquiryObjectClass = null; |
116 | |
try { |
117 | 0 | inquiryObjectClass = Class.forName(getDataObjectClassName()); |
118 | 0 | } catch (ClassNotFoundException e) { |
119 | 0 | LOG.error("Unable to get class for: " + getDataObjectClassName()); |
120 | 0 | throw new RuntimeException(e); |
121 | 0 | } |
122 | |
|
123 | 0 | updateInquiryParameters(field.getBindingInfo()); |
124 | |
|
125 | 0 | buildInquiryLink(model, propertyName, inquiryObjectClass, getInquiryParameters()); |
126 | 0 | } |
127 | |
|
128 | |
else { |
129 | |
|
130 | 0 | Object parentObject = ViewModelUtils.getParentObjectForMetadata(view, model, field); |
131 | 0 | view.getViewHelperService().buildInquiryLink(parentObject, propertyName, this); |
132 | |
} |
133 | 0 | } |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void updateInquiryParameters(BindingInfo bindingInfo) { |
142 | 0 | Map<String, String> adjustedInquiryParameters = new HashMap<String, String>(); |
143 | 0 | for (String fromField : inquiryParameters.keySet()) { |
144 | 0 | String toField = inquiryParameters.get(fromField); |
145 | 0 | String adjustedFromFieldPath = bindingInfo.getPropertyAdjustedBindingPath(fromField); |
146 | |
|
147 | 0 | adjustedInquiryParameters.put(adjustedFromFieldPath, toField); |
148 | 0 | } |
149 | |
|
150 | 0 | this.inquiryParameters = adjustedInquiryParameters; |
151 | 0 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public void buildInquiryLink(Object dataObject, String propertyName, Class<?> inquiryObjectClass, |
163 | |
Map<String, String> inquiryParms) { |
164 | 0 | Properties urlParameters = new Properties(); |
165 | |
|
166 | 0 | urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName()); |
167 | 0 | urlParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START); |
168 | |
|
169 | 0 | for (Entry<String, String> inquiryParameter : inquiryParms.entrySet()) { |
170 | 0 | String parameterName = inquiryParameter.getKey(); |
171 | |
|
172 | 0 | Object parameterValue = ObjectPropertyUtils.getPropertyValue(dataObject, parameterName); |
173 | |
|
174 | |
|
175 | 0 | if (parameterValue == null) { |
176 | 0 | parameterValue = ""; |
177 | 0 | } else if (parameterValue instanceof java.sql.Date) { |
178 | 0 | if (Formatter.findFormatter(parameterValue.getClass()) != null) { |
179 | 0 | Formatter formatter = Formatter.getFormatter(parameterValue.getClass()); |
180 | 0 | parameterValue = formatter.format(parameterValue); |
181 | 0 | } |
182 | |
} else { |
183 | 0 | parameterValue = parameterValue.toString(); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | 0 | if (KRADServiceLocatorWeb.getDataObjectAuthorizationService() |
191 | |
.attributeValueNeedsToBeEncryptedOnFormsAndLinks(inquiryObjectClass, inquiryParameter.getValue())) { |
192 | |
try { |
193 | 0 | parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue); |
194 | 0 | } catch (GeneralSecurityException e) { |
195 | 0 | LOG.error("Exception while trying to encrypted value for inquiry framework.", e); |
196 | 0 | throw new RuntimeException(e); |
197 | 0 | } |
198 | |
} |
199 | |
|
200 | |
|
201 | 0 | urlParameters.put(inquiryParameter.getValue(), parameterValue); |
202 | 0 | } |
203 | |
|
204 | |
|
205 | 0 | ModuleService responsibleModuleService = |
206 | |
KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass); |
207 | 0 | if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) { |
208 | 0 | Class<? extends ExternalizableBusinessObject> implLookupObjectClass = |
209 | |
responsibleModuleService.getExternalizableBusinessObjectImplementation( |
210 | |
inquiryObjectClass.asSubclass(ExternalizableBusinessObject.class)); |
211 | |
|
212 | |
|
213 | 0 | if (implLookupObjectClass != null) { |
214 | 0 | urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, implLookupObjectClass.getName()); |
215 | |
} else { |
216 | 0 | throw new RuntimeException( |
217 | |
"Unable to find implementation class for EBO: " + inquiryObjectClass.getName()); |
218 | |
} |
219 | |
} |
220 | |
|
221 | 0 | String inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters); |
222 | 0 | getInquiryLinkField().setHrefText(inquiryUrl); |
223 | |
|
224 | |
|
225 | 0 | String linkTitle = createTitleText(inquiryObjectClass); |
226 | 0 | linkTitle = LookupInquiryUtils.getLinkTitleText(linkTitle, inquiryObjectClass, getInquiryParameters()); |
227 | 0 | getInquiryLinkField().setTitle(linkTitle); |
228 | |
|
229 | 0 | setRender(true); |
230 | 0 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public String createTitleText(Class<?> dataObjectClass) { |
239 | 0 | String titleText = ""; |
240 | |
|
241 | 0 | String titlePrefixProp = |
242 | |
KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(INQUIRY_TITLE_PREFIX); |
243 | 0 | if (StringUtils.isNotBlank(titlePrefixProp)) { |
244 | 0 | titleText += titlePrefixProp + " "; |
245 | |
} |
246 | |
|
247 | 0 | String objectLabel = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary() |
248 | |
.getDataObjectEntry(dataObjectClass.getName()).getObjectLabel(); |
249 | 0 | if (StringUtils.isNotBlank(objectLabel)) { |
250 | 0 | titleText += objectLabel + " "; |
251 | |
} |
252 | |
|
253 | 0 | return titleText; |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
@Override |
260 | |
public List<Component> getComponentsForLifecycle() { |
261 | 0 | List<Component> components = super.getComponentsForLifecycle(); |
262 | |
|
263 | 0 | components.add(getInquiryLinkField()); |
264 | |
|
265 | 0 | return components; |
266 | |
} |
267 | |
|
268 | |
public String getBaseInquiryUrl() { |
269 | 0 | return this.baseInquiryUrl; |
270 | |
} |
271 | |
|
272 | |
public void setBaseInquiryUrl(String baseInquiryUrl) { |
273 | 0 | this.baseInquiryUrl = baseInquiryUrl; |
274 | 0 | } |
275 | |
|
276 | |
public String getDataObjectClassName() { |
277 | 0 | return this.dataObjectClassName; |
278 | |
} |
279 | |
|
280 | |
public void setDataObjectClassName(String dataObjectClassName) { |
281 | 0 | this.dataObjectClassName = dataObjectClassName; |
282 | 0 | } |
283 | |
|
284 | |
public String getViewName() { |
285 | 0 | return this.viewName; |
286 | |
} |
287 | |
|
288 | |
public void setViewName(String viewName) { |
289 | 0 | this.viewName = viewName; |
290 | 0 | } |
291 | |
|
292 | |
public boolean isForceInquiry() { |
293 | 0 | return this.forceInquiry; |
294 | |
} |
295 | |
|
296 | |
public void setForceInquiry(boolean forceInquiry) { |
297 | 0 | this.forceInquiry = forceInquiry; |
298 | 0 | } |
299 | |
|
300 | |
public Map<String, String> getInquiryParameters() { |
301 | 0 | return this.inquiryParameters; |
302 | |
} |
303 | |
|
304 | |
public void setInquiryParameters(Map<String, String> inquiryParameters) { |
305 | 0 | this.inquiryParameters = inquiryParameters; |
306 | 0 | } |
307 | |
|
308 | |
public LinkField getInquiryLinkField() { |
309 | 0 | return this.inquiryLinkField; |
310 | |
} |
311 | |
|
312 | |
public void setInquiryLinkField(LinkField inquiryLinkField) { |
313 | 0 | this.inquiryLinkField = inquiryLinkField; |
314 | 0 | } |
315 | |
} |