View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.web.struts;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.StringTokenizer;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.gl.GeneralLedgerConstants;
27  import org.kuali.ole.gl.businessobject.Entry;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
30  import org.kuali.ole.sys.businessobject.lookup.LookupableSpringContext;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.rice.kns.lookup.Lookupable;
33  import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
34  import org.kuali.rice.kns.web.struts.form.LookupForm;
35  import org.kuali.rice.kns.web.ui.Field;
36  import org.kuali.rice.kns.web.ui.Row;
37  import org.kuali.rice.krad.bo.BusinessObject;
38  
39  /**
40   * This class is the action form for balance inquiries.
41   */
42  public class BalanceInquiryForm extends LookupForm {
43      private static final long serialVersionUID = 1L;
44  
45      private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(BalanceInquiryForm.class);
46  
47      private String formKey;
48      private String backLocation;
49      private Map fields;
50      private String lookupableImplServiceName;
51      private String conversionFields;
52      private Map fieldConversions;
53      private String businessObjectClassName;
54      private Lookupable lookupable;
55      private Lookupable pendingEntryLookupable;
56      private boolean hideReturnLink = false;
57  
58  
59      /**
60       * Picks out business object name from the request to get retrieve a lookupable and set properties.
61       * 
62       * @see org.kuali.rice.kns.web.struts.form.LookupForm#populate(javax.servlet.http.HttpServletRequest)
63       *      
64       * KRAD Conversion: Lookupable performs customization of the fields and check for additional fields.
65       *  
66       * Fields are in data dictionary for bo Balance.
67       */
68      public void populate(HttpServletRequest request) {
69          super.populate(request);
70  
71          try {
72              if (StringUtils.isBlank(request.getParameter(OLEConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME)) && StringUtils.isBlank(getLookupableImplServiceName())) {
73  
74                  // get the business object class for the lookup
75                  String localBusinessObjectClassName = request.getParameter(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
76                  setBusinessObjectClassName(localBusinessObjectClassName);
77  
78                  if (StringUtils.isBlank(localBusinessObjectClassName)) {
79                      LOG.error("Business object class not passed to lookup.");
80                      throw new RuntimeException("Business object class not passed to lookup.");
81                  }
82  
83                  // call data dictionary service to get lookup impl for bo class
84                  String lookupImplID = SpringContext.getBean(BusinessObjectDictionaryService.class).getLookupableID(Class.forName(localBusinessObjectClassName));
85                  if (lookupImplID == null) {
86                      lookupImplID = "lookupable";
87                  }
88  
89                  setLookupableImplServiceName(lookupImplID);
90              }
91              setLookupable(LookupableSpringContext.getLookupable(getLookupableImplServiceName()));
92  
93              if (getLookupable() == null) {
94                  LOG.error("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
95                  throw new RuntimeException("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
96              }
97  
98              // (laran) I put this here to allow the Exception to be thrown if the localLookupable is null.
99              if (Entry.class.getName().equals(getBusinessObjectClassName())) {
100                 setPendingEntryLookupable(LookupableSpringContext.getLookupable(GeneralLedgerConstants.LookupableBeanKeys.PENDING_ENTRY));
101             }
102 
103             if (request.getParameter(OLEConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME) != null) {
104                 setLookupableImplServiceName(request.getParameter(OLEConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME));
105             }
106 
107             // check the doc form key is empty before setting so we don't override a restored lookup form
108             if (request.getAttribute(OLEConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
109                 setFormKey((String) request.getAttribute(OLEConstants.DOC_FORM_KEY));
110             }
111             else if (request.getParameter(OLEConstants.DOC_FORM_KEY) != null && StringUtils.isBlank(this.getFormKey())) {
112                 setFormKey(request.getParameter(OLEConstants.DOC_FORM_KEY));
113             }
114 
115             if (request.getParameter(OLEConstants.RETURN_LOCATION_PARAMETER) != null) {
116                 setBackLocation(request.getParameter(OLEConstants.RETURN_LOCATION_PARAMETER));
117             }
118             if (request.getParameter(OLEConstants.CONVERSION_FIELDS_PARAMETER) != null) {
119                 setConversionFields(request.getParameter(OLEConstants.CONVERSION_FIELDS_PARAMETER));
120             }
121 
122             // init lookupable with bo class
123             getLookupable().setBusinessObjectClass((Class<? extends BusinessObject>)Class.forName(getBusinessObjectClassName()));
124             if (null != getPendingEntryLookupable()) {
125                 getPendingEntryLookupable().setBusinessObjectClass(GeneralLedgerPendingEntry.class);
126             }
127 
128             Map fieldValues = new HashMap();
129             Map formFields = getFields();
130             Class boClass = Class.forName(getBusinessObjectClassName());
131             for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) {
132                 Row row = (Row) iter.next();
133 
134                 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
135                     Field field = (Field) iterator.next();
136 
137                     // check whether form already has value for field
138                     if (formFields != null && formFields.containsKey(field.getPropertyName())) {
139                         field.setPropertyValue(formFields.get(field.getPropertyName()));
140                     }
141 
142                     // override values with request
143                     if (request.getParameter(field.getPropertyName()) != null) {
144                         field.setPropertyValue(request.getParameter(field.getPropertyName()));
145                     }
146 
147                     // force uppercase if necessary
148                     field.setPropertyValue(org.kuali.rice.krad.lookup.LookupUtils.forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue()));
149 
150                     fieldValues.put(field.getPropertyName(), field.getPropertyValue());
151                 }
152             }
153             if (getLookupable().checkForAdditionalFields(fieldValues)) {
154                 for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) {
155                     Row row = (Row) iter.next();
156 
157                     for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) {
158                         Field field = (Field) iterator.next();
159 
160                         // check whether form already has value for field
161                         if (formFields != null && formFields.containsKey(field.getPropertyName())) {
162                             field.setPropertyValue(formFields.get(field.getPropertyName()));
163                         }
164 
165                         // override values with request
166                         if (request.getParameter(field.getPropertyName()) != null) {
167                             field.setPropertyValue(request.getParameter(field.getPropertyName()));
168                         }
169                         fieldValues.put(field.getPropertyName(), field.getPropertyValue());
170                     }
171                 }
172             }
173             fieldValues.put(OLEConstants.DOC_FORM_KEY, this.getFormKey());
174             fieldValues.put(OLEConstants.BACK_LOCATION, this.getBackLocation());
175 
176             this.setFields(fieldValues);
177 
178             Map fieldConversionMap = new HashMap();
179             if (StringUtils.isNotEmpty(this.getConversionFields())) {
180                 if (this.getConversionFields().indexOf(",") > 0) {
181                     StringTokenizer token = new StringTokenizer(this.getConversionFields(), ",");
182                     while (token.hasMoreTokens()) {
183                         String element = token.nextToken();
184                         fieldConversionMap.put(element.substring(0, element.indexOf(":")), element.substring(element.indexOf(":") + 1));
185                     }
186                 }
187                 else {
188                     fieldConversionMap.put(this.getConversionFields().substring(0, this.getConversionFields().indexOf(":")), this.getConversionFields().substring(this.getConversionFields().indexOf(":") + 1));
189                 }
190             }
191             setFieldConversions(fieldConversionMap);
192             getLookupable().setFieldConversions(fieldConversionMap);
193             if (null != getPendingEntryLookupable()) {
194                 getPendingEntryLookupable().setFieldConversions(fieldConversionMap);
195             }
196         }
197         catch (ClassNotFoundException e) {
198             LOG.error("Business Object class " + getBusinessObjectClassName() + " not found");
199             throw new RuntimeException("Business Object class " + getBusinessObjectClassName() + " not found", e);
200         }
201     }
202 
203     /**
204      * @return Returns the lookupableImplServiceName.
205      */
206     public String getLookupableImplServiceName() {
207         return lookupableImplServiceName;
208     }
209 
210     /**
211      * @param lookupableImplServiceName The lookupableImplServiceName to set.
212      */
213     public void setLookupableImplServiceName(String lookupableImplServiceName) {
214         this.lookupableImplServiceName = lookupableImplServiceName;
215     }
216 
217 
218     /**
219      * @return Returns the backLocation.
220      */
221     public String getBackLocation() {
222         return backLocation;
223     }
224 
225     /**
226      * @param backLocation The backLocation to set.
227      */
228     public void setBackLocation(String backLocation) {
229         this.backLocation = backLocation;
230     }
231 
232     /**
233      * @return Returns the formKey.
234      */
235     public String getFormKey() {
236         return formKey;
237     }
238 
239     /**
240      * @param formKey The formKey to set.
241      */
242     public void setFormKey(String formKey) {
243         this.formKey = formKey;
244     }
245 
246     /**
247      * @return Returns the fields.
248      */
249     public Map getFields() {
250         return fields;
251     }
252 
253     /**
254      * @param fields The fields to set.
255      */
256     public void setFields(Map fields) {
257         this.fields = fields;
258     }
259 
260 
261     /**
262      * @return Returns the conversionFields.
263      */
264     public String getConversionFields() {
265         return conversionFields;
266     }
267 
268     /**
269      * @param conversionFields The conversionFields to set.
270      */
271     public void setConversionFields(String conversionFields) {
272         this.conversionFields = conversionFields;
273     }
274 
275     /**
276      * @return Returns the fieldConversions.
277      */
278     public Map getFieldConversions() {
279         return fieldConversions;
280     }
281 
282     /**
283      * @param fieldConversions The fieldConversions to set.
284      */
285     public void setFieldConversions(Map fieldConversions) {
286         this.fieldConversions = fieldConversions;
287     }
288 
289     /**
290      * @return Returns the businessObjectClassName.
291      */
292     public String getBusinessObjectClassName() {
293         return businessObjectClassName;
294     }
295 
296     /**
297      * @param businessObjectClassName The businessObjectClassName to set.
298      */
299     public void setBusinessObjectClassName(String businessObjectClassName) {
300         this.businessObjectClassName = businessObjectClassName;
301     }
302 
303 
304     /**
305      * @return Returns the lookupable.
306      */
307     public Lookupable getLookupable() {
308         return lookupable;
309     }
310 
311 
312     /**
313      * @param lookupable The lookupable to set.
314      */
315     public void setLookupable(Lookupable lookupable) {
316         this.lookupable = lookupable;
317     }
318 
319 
320     /**
321      * @return Returns the hideReturnLink.
322      */
323     public boolean isHideReturnLink() {
324         return hideReturnLink;
325     }
326 
327 
328     /**
329      * @param hideReturnLink The hideReturnLink to set.
330      */
331     public void setHideReturnLink(boolean hideReturnLink) {
332         this.hideReturnLink = hideReturnLink;
333     }
334 
335 
336     /**
337      * @param pendingEntryLookupable
338      */
339     public void setPendingEntryLookupable(Lookupable pendingEntryLookupable) {
340         this.pendingEntryLookupable = pendingEntryLookupable;
341     }
342 
343 
344     /**
345      * @return Returns the pendingEntryLookupable.
346      */
347     public Lookupable getPendingEntryLookupable() {
348         return this.pendingEntryLookupable;
349     }
350 }