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.component.BindingInfo;
28 import org.kuali.rice.krad.uif.component.Component;
29 import org.kuali.rice.krad.uif.field.DataField;
30 import org.kuali.rice.krad.uif.field.LinkField;
31 import org.kuali.rice.krad.uif.util.LookupInquiryUtils;
32 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
33 import org.kuali.rice.krad.uif.util.ViewModelUtils;
34 import org.kuali.rice.krad.uif.view.View;
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 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 super();
68
69 forceInquiry = false;
70 inquiryParameters = new HashMap<String, String>();
71 }
72
73
74
75
76
77 @Override
78 public void performFinalize(View view, Object model, Component parent) {
79 super.performFinalize(view, model, parent);
80
81
82 if (!isRender() || !isReadOnly()) {
83 return;
84 }
85
86
87 setRender(false);
88
89 DataField field = (DataField) parent;
90
91
92 Object propertyValue = ObjectPropertyUtils.getPropertyValue(model, field.getBindingInfo().getBindingPath());
93 if ((propertyValue == null) || StringUtils.isBlank(propertyValue.toString())) {
94 return;
95 }
96
97 setupLink(view, model, field);
98 }
99
100
101
102
103
104
105
106
107
108
109 public void setupLink(View view, Object model, DataField field) {
110 String propertyName = field.getBindingInfo().getBindingName();
111
112
113 if (StringUtils.isNotBlank(getDataObjectClassName()) && (getInquiryParameters() != null) &&
114 !getInquiryParameters().isEmpty()) {
115 Class<?> inquiryObjectClass = null;
116 try {
117 inquiryObjectClass = Class.forName(getDataObjectClassName());
118 } catch (ClassNotFoundException e) {
119 LOG.error("Unable to get class for: " + getDataObjectClassName());
120 throw new RuntimeException(e);
121 }
122
123 updateInquiryParameters(field.getBindingInfo());
124
125 buildInquiryLink(model, propertyName, inquiryObjectClass, getInquiryParameters());
126 }
127
128 else {
129
130 Object parentObject = ViewModelUtils.getParentObjectForMetadata(view, model, field);
131 view.getViewHelperService().buildInquiryLink(parentObject, propertyName, this);
132 }
133 }
134
135
136
137
138
139
140
141 public void updateInquiryParameters(BindingInfo bindingInfo) {
142 Map<String, String> adjustedInquiryParameters = new HashMap<String, String>();
143 for (String fromField : inquiryParameters.keySet()) {
144 String toField = inquiryParameters.get(fromField);
145 String adjustedFromFieldPath = bindingInfo.getPropertyAdjustedBindingPath(fromField);
146
147 adjustedInquiryParameters.put(adjustedFromFieldPath, toField);
148 }
149
150 this.inquiryParameters = adjustedInquiryParameters;
151 }
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 Properties urlParameters = new Properties();
165
166 urlParameters.put(UifParameters.DATA_OBJECT_CLASS_NAME, inquiryObjectClass.getName());
167 urlParameters.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.START);
168
169 for (Entry<String, String> inquiryParameter : inquiryParms.entrySet()) {
170 String parameterName = inquiryParameter.getKey();
171
172 Object parameterValue = ObjectPropertyUtils.getPropertyValue(dataObject, parameterName);
173
174
175 if (parameterValue == null) {
176 parameterValue = "";
177 } else if (parameterValue instanceof java.sql.Date) {
178 if (Formatter.findFormatter(parameterValue.getClass()) != null) {
179 Formatter formatter = Formatter.getFormatter(parameterValue.getClass());
180 parameterValue = formatter.format(parameterValue);
181 }
182 } else {
183 parameterValue = parameterValue.toString();
184 }
185
186
187
188 if (KRADServiceLocatorWeb.getDataObjectAuthorizationService()
189 .attributeValueNeedsToBeEncryptedOnFormsAndLinks(inquiryObjectClass, inquiryParameter.getValue())) {
190 try {
191 parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue);
192 } catch (GeneralSecurityException e) {
193 LOG.error("Exception while trying to encrypted value for inquiry framework.", e);
194 throw new RuntimeException(e);
195 }
196 }
197
198
199 urlParameters.put(inquiryParameter.getValue(), parameterValue);
200 }
201
202
203 String inquiryUrl = "";
204
205
206 ModuleService responsibleModuleService =
207 KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(inquiryObjectClass);
208 if (responsibleModuleService != null && responsibleModuleService.isExternalizable(inquiryObjectClass)) {
209 inquiryUrl = responsibleModuleService.getExternalizableDataObjectLookupUrl(inquiryObjectClass,
210 urlParameters);
211 } else {
212 inquiryUrl = UrlFactory.parameterizeUrl(getBaseInquiryUrl(), urlParameters);
213 }
214
215 getInquiryLinkField().setHrefText(inquiryUrl);
216
217
218 String linkTitle = createTitleText(inquiryObjectClass);
219 linkTitle = LookupInquiryUtils.getLinkTitleText(linkTitle, inquiryObjectClass, getInquiryParameters());
220 getInquiryLinkField().setTitle(linkTitle);
221
222 setRender(true);
223 }
224
225
226
227
228
229
230
231 public String createTitleText(Class<?> dataObjectClass) {
232 String titleText = "";
233
234 String titlePrefixProp =
235 KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(INQUIRY_TITLE_PREFIX);
236 if (StringUtils.isNotBlank(titlePrefixProp)) {
237 titleText += titlePrefixProp + " ";
238 }
239
240 String objectLabel = KRADServiceLocatorWeb.getDataDictionaryService().getDataDictionary()
241 .getDataObjectEntry(dataObjectClass.getName()).getObjectLabel();
242 if (StringUtils.isNotBlank(objectLabel)) {
243 titleText += objectLabel + " ";
244 }
245
246 return titleText;
247 }
248
249
250
251
252 @Override
253 public List<Component> getComponentsForLifecycle() {
254 List<Component> components = super.getComponentsForLifecycle();
255
256 components.add(getInquiryLinkField());
257
258 return components;
259 }
260
261 public String getBaseInquiryUrl() {
262 return this.baseInquiryUrl;
263 }
264
265 public void setBaseInquiryUrl(String baseInquiryUrl) {
266 this.baseInquiryUrl = baseInquiryUrl;
267 }
268
269 public String getDataObjectClassName() {
270 return this.dataObjectClassName;
271 }
272
273 public void setDataObjectClassName(String dataObjectClassName) {
274 this.dataObjectClassName = dataObjectClassName;
275 }
276
277 public String getViewName() {
278 return this.viewName;
279 }
280
281 public void setViewName(String viewName) {
282 this.viewName = viewName;
283 }
284
285 public boolean isForceInquiry() {
286 return this.forceInquiry;
287 }
288
289 public void setForceInquiry(boolean forceInquiry) {
290 this.forceInquiry = forceInquiry;
291 }
292
293 public Map<String, String> getInquiryParameters() {
294 return this.inquiryParameters;
295 }
296
297 public void setInquiryParameters(Map<String, String> inquiryParameters) {
298 this.inquiryParameters = inquiryParameters;
299 }
300
301 public LinkField getInquiryLinkField() {
302 return this.inquiryLinkField;
303 }
304
305 public void setInquiryLinkField(LinkField inquiryLinkField) {
306 this.inquiryLinkField = inquiryLinkField;
307 }
308 }