001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.gl.web.struts;
017
018 import java.util.HashMap;
019 import java.util.Iterator;
020 import java.util.Map;
021 import java.util.StringTokenizer;
022
023 import javax.servlet.http.HttpServletRequest;
024
025 import org.apache.commons.lang.StringUtils;
026 import org.kuali.kfs.gl.GeneralLedgerConstants;
027 import org.kuali.kfs.gl.businessobject.Entry;
028 import org.kuali.kfs.sys.KFSConstants;
029 import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
030 import org.kuali.kfs.sys.businessobject.lookup.LookupableSpringContext;
031 import org.kuali.kfs.sys.context.SpringContext;
032 import org.kuali.rice.kns.lookup.LookupUtils;
033 import org.kuali.rice.kns.lookup.Lookupable;
034 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
035 import org.kuali.rice.kns.web.struts.form.LookupForm;
036 import org.kuali.rice.kns.web.ui.Field;
037 import org.kuali.rice.kns.web.ui.Row;
038
039 /**
040 * This class is the action form for balance inquiries.
041 */
042 public class BalanceInquiryForm extends LookupForm {
043 private static final long serialVersionUID = 1L;
044
045 private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(BalanceInquiryForm.class);
046
047 private String formKey;
048 private String backLocation;
049 private Map fields;
050 private String lookupableImplServiceName;
051 private String conversionFields;
052 private Map fieldConversions;
053 private String businessObjectClassName;
054 private Lookupable lookupable;
055 private Lookupable pendingEntryLookupable;
056 private boolean hideReturnLink = false;
057
058
059 /**
060 * Picks out business object name from the request to get retrieve a lookupable and set properties.
061 *
062 * @see org.kuali.rice.kns.web.struts.form.LookupForm#populate(javax.servlet.http.HttpServletRequest)
063 */
064 public void populate(HttpServletRequest request) {
065 super.populate(request);
066
067 try {
068 if (StringUtils.isBlank(request.getParameter(KFSConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME)) && StringUtils.isBlank(getLookupableImplServiceName())) {
069
070 // get the business object class for the lookup
071 String localBusinessObjectClassName = request.getParameter(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
072 setBusinessObjectClassName(localBusinessObjectClassName);
073
074 if (StringUtils.isBlank(localBusinessObjectClassName)) {
075 LOG.error("Business object class not passed to lookup.");
076 throw new RuntimeException("Business object class not passed to lookup.");
077 }
078
079 // call data dictionary service to get lookup impl for bo class
080 String lookupImplID = SpringContext.getBean(BusinessObjectDictionaryService.class).getLookupableID(Class.forName(localBusinessObjectClassName));
081 if (lookupImplID == null) {
082 lookupImplID = "lookupable";
083 }
084
085 setLookupableImplServiceName(lookupImplID);
086 }
087 setLookupable(LookupableSpringContext.getLookupable(getLookupableImplServiceName()));
088
089 if (getLookupable() == null) {
090 LOG.error("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
091 throw new RuntimeException("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
092 }
093
094 // (laran) I put this here to allow the Exception to be thrown if the localLookupable is null.
095 if (Entry.class.getName().equals(getBusinessObjectClassName())) {
096 setPendingEntryLookupable(LookupableSpringContext.getLookupable(GeneralLedgerConstants.LookupableBeanKeys.PENDING_ENTRY));
097 }
098
099 if (request.getParameter(KFSConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME) != null) {
100 setLookupableImplServiceName(request.getParameter(KFSConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME));
101 }
102
103 // check the doc form key is empty before setting so we don't override a restored lookup form
104 if (request.getAttribute(KFSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
105 setFormKey((String) request.getAttribute(KFSConstants.DOC_FORM_KEY));
106 }
107 else if (request.getParameter(KFSConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
108 setFormKey(request.getParameter(KFSConstants.DOC_FORM_KEY));
109 }
110
111 if (request.getParameter(KFSConstants.RETURN_LOCATION_PARAMETER) != null) {
112 setBackLocation(request.getParameter(KFSConstants.RETURN_LOCATION_PARAMETER));
113 }
114 if (request.getParameter(KFSConstants.CONVERSION_FIELDS_PARAMETER) != null) {
115 setConversionFields(request.getParameter(KFSConstants.CONVERSION_FIELDS_PARAMETER));
116 }
117
118 // init lookupable with bo class
119 getLookupable().setBusinessObjectClass(Class.forName(getBusinessObjectClassName()));
120 if (null != getPendingEntryLookupable()) {
121 getPendingEntryLookupable().setBusinessObjectClass(GeneralLedgerPendingEntry.class);
122 }
123
124 Map fieldValues = new HashMap();
125 Map formFields = getFields();
126 Class boClass = Class.forName(getBusinessObjectClassName());
127 for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) {
128 Row row = (Row) iter.next();
129
130 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
131 Field field = (Field) iterator.next();
132
133 // check whether form already has value for field
134 if (formFields != null && formFields.containsKey(field.getPropertyName())) {
135 field.setPropertyValue(formFields.get(field.getPropertyName()));
136 }
137
138 // override values with request
139 if (request.getParameter(field.getPropertyName()) != null) {
140 field.setPropertyValue(request.getParameter(field.getPropertyName()));
141 }
142
143 // force uppercase if necessary
144 field.setPropertyValue(LookupUtils.forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue()));
145
146 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
147 }
148 }
149 if (getLookupable().checkForAdditionalFields(fieldValues)) {
150 for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) {
151 Row row = (Row) iter.next();
152
153 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
154 Field field = (Field) iterator.next();
155
156 // check whether form already has value for field
157 if (formFields != null && formFields.containsKey(field.getPropertyName())) {
158 field.setPropertyValue(formFields.get(field.getPropertyName()));
159 }
160
161 // override values with request
162 if (request.getParameter(field.getPropertyName()) != null) {
163 field.setPropertyValue(request.getParameter(field.getPropertyName()));
164 }
165 fieldValues.put(field.getPropertyName(), field.getPropertyValue());
166 }
167 }
168 }
169 fieldValues.put(KFSConstants.DOC_FORM_KEY, this.getFormKey());
170 fieldValues.put(KFSConstants.BACK_LOCATION, this.getBackLocation());
171
172 this.setFields(fieldValues);
173
174 Map fieldConversionMap = new HashMap();
175 if (StringUtils.isNotEmpty(this.getConversionFields())) {
176 if (this.getConversionFields().indexOf(",") > 0) {
177 StringTokenizer token = new StringTokenizer(this.getConversionFields(), ",");
178 while (token.hasMoreTokens()) {
179 String element = token.nextToken();
180 fieldConversionMap.put(element.substring(0, element.indexOf(":")), element.substring(element.indexOf(":") + 1));
181 }
182 }
183 else {
184 fieldConversionMap.put(this.getConversionFields().substring(0, this.getConversionFields().indexOf(":")), this.getConversionFields().substring(this.getConversionFields().indexOf(":") + 1));
185 }
186 }
187 setFieldConversions(fieldConversionMap);
188 getLookupable().setFieldConversions(fieldConversionMap);
189 if (null != getPendingEntryLookupable()) {
190 getPendingEntryLookupable().setFieldConversions(fieldConversionMap);
191 }
192 }
193 catch (ClassNotFoundException e) {
194 LOG.error("Business Object class " + getBusinessObjectClassName() + " not found");
195 throw new RuntimeException("Business Object class " + getBusinessObjectClassName() + " not found", e);
196 }
197 }
198
199 /**
200 * @return Returns the lookupableImplServiceName.
201 */
202 public String getLookupableImplServiceName() {
203 return lookupableImplServiceName;
204 }
205
206 /**
207 * @param lookupableImplServiceName The lookupableImplServiceName to set.
208 */
209 public void setLookupableImplServiceName(String lookupableImplServiceName) {
210 this.lookupableImplServiceName = lookupableImplServiceName;
211 }
212
213
214 /**
215 * @return Returns the backLocation.
216 */
217 public String getBackLocation() {
218 return backLocation;
219 }
220
221 /**
222 * @param backLocation The backLocation to set.
223 */
224 public void setBackLocation(String backLocation) {
225 this.backLocation = backLocation;
226 }
227
228 /**
229 * @return Returns the formKey.
230 */
231 public String getFormKey() {
232 return formKey;
233 }
234
235 /**
236 * @param formKey The formKey to set.
237 */
238 public void setFormKey(String formKey) {
239 this.formKey = formKey;
240 }
241
242 /**
243 * @return Returns the fields.
244 */
245 public Map getFields() {
246 return fields;
247 }
248
249 /**
250 * @param fields The fields to set.
251 */
252 public void setFields(Map fields) {
253 this.fields = fields;
254 }
255
256
257 /**
258 * @return Returns the conversionFields.
259 */
260 public String getConversionFields() {
261 return conversionFields;
262 }
263
264 /**
265 * @param conversionFields The conversionFields to set.
266 */
267 public void setConversionFields(String conversionFields) {
268 this.conversionFields = conversionFields;
269 }
270
271 /**
272 * @return Returns the fieldConversions.
273 */
274 public Map getFieldConversions() {
275 return fieldConversions;
276 }
277
278 /**
279 * @param fieldConversions The fieldConversions to set.
280 */
281 public void setFieldConversions(Map fieldConversions) {
282 this.fieldConversions = fieldConversions;
283 }
284
285 /**
286 * @return Returns the businessObjectClassName.
287 */
288 public String getBusinessObjectClassName() {
289 return businessObjectClassName;
290 }
291
292 /**
293 * @param businessObjectClassName The businessObjectClassName to set.
294 */
295 public void setBusinessObjectClassName(String businessObjectClassName) {
296 this.businessObjectClassName = businessObjectClassName;
297 }
298
299
300 /**
301 * @return Returns the lookupable.
302 */
303 public Lookupable getLookupable() {
304 return lookupable;
305 }
306
307
308 /**
309 * @param lookupable The lookupable to set.
310 */
311 public void setLookupable(Lookupable lookupable) {
312 this.lookupable = lookupable;
313 }
314
315
316 /**
317 * @return Returns the hideReturnLink.
318 */
319 public boolean isHideReturnLink() {
320 return hideReturnLink;
321 }
322
323
324 /**
325 * @param hideReturnLink The hideReturnLink to set.
326 */
327 public void setHideReturnLink(boolean hideReturnLink) {
328 this.hideReturnLink = hideReturnLink;
329 }
330
331
332 /**
333 * @param pendingEntryLookupable
334 */
335 public void setPendingEntryLookupable(Lookupable pendingEntryLookupable) {
336 this.pendingEntryLookupable = pendingEntryLookupable;
337 }
338
339
340 /**
341 * @return Returns the pendingEntryLookupable.
342 */
343 public Lookupable getPendingEntryLookupable() {
344 return this.pendingEntryLookupable;
345 }
346 }