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