001/*
002 * Copyright 2006 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 */
016package org.kuali.ole.gl.web.struts;
017
018import java.util.HashMap;
019import java.util.Iterator;
020import java.util.Map;
021import java.util.StringTokenizer;
022
023import javax.servlet.http.HttpServletRequest;
024
025import org.apache.commons.lang.StringUtils;
026import org.kuali.ole.gl.GeneralLedgerConstants;
027import org.kuali.ole.gl.businessobject.Entry;
028import org.kuali.ole.sys.OLEConstants;
029import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
030import org.kuali.ole.sys.businessobject.lookup.LookupableSpringContext;
031import org.kuali.ole.sys.context.SpringContext;
032import org.kuali.rice.kns.lookup.Lookupable;
033import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
034import org.kuali.rice.kns.web.struts.form.LookupForm;
035import org.kuali.rice.kns.web.ui.Field;
036import org.kuali.rice.kns.web.ui.Row;
037import org.kuali.rice.krad.bo.BusinessObject;
038
039/**
040 * This class is the action form for balance inquiries.
041 */
042public 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     * KRAD Conversion: Lookupable performs customization of the fields and check for additional fields.
065     *  
066     * Fields are in data dictionary for bo Balance.
067     */
068    public void populate(HttpServletRequest request) {
069        super.populate(request);
070
071        try {
072            if (StringUtils.isBlank(request.getParameter(OLEConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME)) && StringUtils.isBlank(getLookupableImplServiceName())) {
073
074                // get the business object class for the lookup
075                String localBusinessObjectClassName = request.getParameter(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE);
076                setBusinessObjectClassName(localBusinessObjectClassName);
077
078                if (StringUtils.isBlank(localBusinessObjectClassName)) {
079                    LOG.error("Business object class not passed to lookup.");
080                    throw new RuntimeException("Business object class not passed to lookup.");
081                }
082
083                // call data dictionary service to get lookup impl for bo class
084                String lookupImplID = SpringContext.getBean(BusinessObjectDictionaryService.class).getLookupableID(Class.forName(localBusinessObjectClassName));
085                if (lookupImplID == null) {
086                    lookupImplID = "lookupable";
087                }
088
089                setLookupableImplServiceName(lookupImplID);
090            }
091            setLookupable(LookupableSpringContext.getLookupable(getLookupableImplServiceName()));
092
093            if (getLookupable() == null) {
094                LOG.error("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
095                throw new RuntimeException("Lookup impl not found for lookup impl name " + getLookupableImplServiceName());
096            }
097
098            // (laran) I put this here to allow the Exception to be thrown if the localLookupable is null.
099            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}