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.module.ar.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.sys.KFSConstants; 027 import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry; 028 import org.kuali.kfs.sys.businessobject.lookup.LookupableSpringContext; 029 import org.kuali.kfs.sys.context.SpringContext; 030 import org.kuali.rice.kns.lookup.LookupUtils; 031 import org.kuali.rice.kns.lookup.Lookupable; 032 import org.kuali.rice.kns.service.BusinessObjectDictionaryService; 033 import org.kuali.rice.kns.web.struts.form.LookupForm; 034 import org.kuali.rice.kns.web.ui.Field; 035 import org.kuali.rice.kns.web.ui.Row; 036 037 038 /** 039 * This class is the action form for Customer Aging Reports. 040 */ 041 public class CustomerAgingReportForm extends LookupForm { 042 private static final long serialVersionUID = 1L; 043 044 private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(CustomerAgingReportForm.class); 045 046 private String formKey; 047 private String backLocation; 048 private Map fields; 049 private String lookupableImplServiceName; 050 private String conversionFields; 051 private Map fieldConversions; 052 private String businessObjectClassName; 053 private Lookupable lookupable; 054 private Lookupable pendingEntryLookupable; 055 private boolean hideReturnLink = false; 056 057 private String total0to30; 058 private String total31to60; 059 private String total61to90; 060 private String total91toSYSPR; 061 private String totalSYSPRplus1orMore; 062 063 064 /** 065 * Picks out business object name from the request to get retrieve a lookupable and set properties. 066 * 067 * @see org.kuali.rice.kns.web.struts.form.LookupForm#populate(javax.servlet.http.HttpServletRequest) 068 */ 069 public void populate(HttpServletRequest request) { 070 super.populate(request); 071 072 try { 073 if (StringUtils.isBlank(request.getParameter(KFSConstants.LOOKUPABLE_IMPL_ATTRIBUTE_NAME)) && StringUtils.isBlank(getLookupableImplServiceName())) { 074 075 // get the business object class for the lookup 076 String localBusinessObjectClassName = request.getParameter(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE); 077 setBusinessObjectClassName(localBusinessObjectClassName); 078 079 if (StringUtils.isBlank(localBusinessObjectClassName)) { 080 LOG.error("Business object class not passed to lookup."); 081 throw new RuntimeException("Business object class not passed to lookup."); 082 } 083 084 // call data dictionary service to get lookup impl for bo class 085 String lookupImplID = SpringContext.getBean(BusinessObjectDictionaryService.class).getLookupableID(Class.forName(localBusinessObjectClassName)); 086 if (lookupImplID == null) { 087 lookupImplID = "lookupable"; 088 } 089 090 setLookupableImplServiceName(lookupImplID); 091 } 092 setLookupable(LookupableSpringContext.getLookupable(getLookupableImplServiceName())); 093 094 if (getLookupable() == null) { 095 LOG.error("Lookup impl not found for lookup impl name " + getLookupableImplServiceName()); 096 throw new RuntimeException("Lookup impl not found for lookup impl name " + getLookupableImplServiceName()); 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 119 // init lookupable with bo class 120 getLookupable().setBusinessObjectClass(Class.forName(getBusinessObjectClassName())); 121 if (null != getPendingEntryLookupable()) { 122 getPendingEntryLookupable().setBusinessObjectClass(GeneralLedgerPendingEntry.class); 123 } 124 125 Map fieldValues = new HashMap(); 126 Map formFields = getFields(); 127 Class boClass = Class.forName(getBusinessObjectClassName()); 128 // LOG.info("\n\n\n\nBusiness Object class " + getBusinessObjectClassName() + " is found\n\n\n\n"); 129 for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) { 130 Row row = (Row) iter.next(); 131 132 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { 133 Field field = (Field) iterator.next(); 134 135 // check whether form already has value for field 136 if (formFields != null && formFields.containsKey(field.getPropertyName())) { 137 field.setPropertyValue(formFields.get(field.getPropertyName())); 138 } 139 140 // override values with request 141 if (request.getParameter(field.getPropertyName()) != null) { 142 field.setPropertyValue(request.getParameter(field.getPropertyName())); 143 } 144 145 // force uppercase if necessary 146 field.setPropertyValue(LookupUtils.forceUppercase(boClass, field.getPropertyName(), field.getPropertyValue())); 147 148 fieldValues.put(field.getPropertyName(), field.getPropertyValue()); 149 } 150 } 151 if (getLookupable().checkForAdditionalFields(fieldValues)) { 152 for (Iterator iter = getLookupable().getRows().iterator(); iter.hasNext();) { 153 Row row = (Row) iter.next(); 154 155 for (Iterator iterator = row.getFields().iterator(); iterator.hasNext();) { 156 Field field = (Field) iterator.next(); 157 158 // check whether form already has value for field 159 if (formFields != null && formFields.containsKey(field.getPropertyName())) { 160 field.setPropertyValue(formFields.get(field.getPropertyName())); 161 LOG.info("\n\n\n\n"); 162 LOG.info("field " + field.toString() + " = " + field.getPropertyValue() + " ***\n\n"); 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(KFSConstants.DOC_FORM_KEY, this.getFormKey()); 174 fieldValues.put(KFSConstants.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 351 /** 352 * Gets the total0to30 attribute. 353 * 354 * @return Returns the total0to30. 355 */ 356 public String getTotal0to30() { 357 return total0to30; 358 } 359 360 /** 361 * Sets the total0to30 attribute value. 362 * 363 * @param total0to30 The total0to30 to set. 364 */ 365 public void setTotal0to30(String total0to30) { 366 this.total0to30 = total0to30; 367 } 368 369 /** 370 * Gets the total31to60 attribute. 371 * 372 * @return Returns the total31to60. 373 */ 374 public String getTotal31to60() { 375 return total31to60; 376 } 377 378 /** 379 * Sets the total31to60 attribute value. 380 * 381 * @param total31to60 The total31to60 to set. 382 */ 383 public void setTotal31to60(String total31to60) { 384 this.total31to60 = total31to60; 385 } 386 387 /** 388 * Gets the total61to90 attribute. 389 * 390 * @return Returns the total61to90. 391 */ 392 public String getTotal61to90() { 393 return total61to90; 394 } 395 396 /** 397 * Sets the total61to90 attribute value. 398 * 399 * @param total61to90 The total61to90 to set. 400 */ 401 public void setTotal61to90(String total61to90) { 402 this.total61to90 = total61to90; 403 } 404 405 /** 406 * Gets the total91toSYSPR attribute. 407 * 408 * @return Returns the total91toSYSPR. 409 */ 410 public String getTotal91toSYSPR() { 411 return total91toSYSPR; 412 } 413 414 /** 415 * Sets the total91toSYSPR attribute value. 416 * 417 * @param total91toSYSPR The total91toSYSPR to set. 418 */ 419 public void setTotal91toSYSPR(String total91toSYSPR) { 420 this.total91toSYSPR = total91toSYSPR; 421 } 422 423 /** 424 * Gets the totalSYSPRplus1orMore attribute. 425 * 426 * @return Returns the totalSYSPRplus1orMore. 427 */ 428 public String getTotalSYSPRplus1orMore() { 429 return totalSYSPRplus1orMore; 430 } 431 432 /** 433 * Sets the totalSYSPRplus1orMore attribute value. 434 * 435 * @param totalSYSPRplus1orMore The totalSYSPRplus1orMore to set. 436 */ 437 public void setTotalSYSPRplus1orMore(String totalSYSPRplus1orMore) { 438 this.totalSYSPRplus1orMore = totalSYSPRplus1orMore; 439 } 440 441 442 }