1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.sys.document.service.impl;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.jsp.PageContext;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.kuali.ole.sys.businessobject.AccountingLine;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.document.AccountingDocument;
29  import org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentAuthorizerBase;
30  import org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationController;
31  import org.kuali.ole.sys.document.datadictionary.AccountingLineGroupDefinition;
32  import org.kuali.ole.sys.document.datadictionary.AccountingLineViewFieldDefinition;
33  import org.kuali.ole.sys.document.service.AccountingLineAuthorizationTransformer;
34  import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation;
35  import org.kuali.ole.sys.document.service.AccountingLineRenderingService;
36  import org.kuali.ole.sys.document.service.AccountingLineRenderingTransformation;
37  import org.kuali.ole.sys.document.service.AccountingLineTableTransformation;
38  import org.kuali.ole.sys.document.web.AccountingLineTableRow;
39  import org.kuali.ole.sys.document.web.TableJoining;
40  import org.kuali.ole.sys.document.web.renderers.CheckboxRenderer;
41  import org.kuali.ole.sys.document.web.renderers.CurrencyRenderer;
42  import org.kuali.ole.sys.document.web.renderers.DateRenderer;
43  import org.kuali.ole.sys.document.web.renderers.DropDownRenderer;
44  import org.kuali.ole.sys.document.web.renderers.FieldRenderer;
45  import org.kuali.ole.sys.document.web.renderers.HiddenRenderer;
46  import org.kuali.ole.sys.document.web.renderers.RadioButtonGroupRenderer;
47  import org.kuali.ole.sys.document.web.renderers.ReadOnlyRenderer;
48  import org.kuali.ole.sys.document.web.renderers.TextAreaRenderer;
49  import org.kuali.ole.sys.document.web.renderers.TextRenderer;
50  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
51  import org.kuali.rice.kns.datadictionary.MaintainableFieldDefinition;
52  import org.kuali.rice.kns.datadictionary.validation.fieldlevel.DateValidationPattern;
53  import org.kuali.rice.kns.service.DataDictionaryService;
54  import org.kuali.rice.kns.service.DocumentHelperService;
55  import org.kuali.rice.kns.util.KNSGlobalVariables;
56  import org.kuali.rice.kns.web.ui.Field;
57  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
58  import org.kuali.rice.krad.datadictionary.validation.ValidationPattern;
59  
60  
61  
62  
63  public class AccountingLineRenderingServiceImpl implements AccountingLineRenderingService {
64      protected final String KUALI_FORM_NAME = "KualiForm";
65      
66      private List<AccountingLineFieldRenderingTransformation> fieldTransformations;
67      private DataDictionaryService dataDictionaryService;
68      private AccountingLineAuthorizationTransformer accountingLineAuthorizationTransformer;
69      private List<AccountingLineRenderingTransformation> preTablificationTransformations;
70      private List<AccountingLineTableTransformation> postTablificationTransformations;
71      private DocumentHelperService documentHelperService;
72  
73      
74  
75  
76      public void performPreTablificationTransformations(List<TableJoining> elements, AccountingLineGroupDefinition groupDefinition, AccountingDocument accountingDocument, AccountingLine accountingLine, boolean newLine, Map unconvertedValues, String accountingLinePropertyName) {
77          performAuthorizationTransformations(elements, groupDefinition, accountingDocument, accountingLine, newLine, accountingLinePropertyName);
78          performFieldTransformations(elements, accountingDocument, accountingLine, unconvertedValues);
79          for (AccountingLineRenderingTransformation transformation : preTablificationTransformations) {
80              transformation.transformElements(elements, accountingLine);
81          }
82      }
83      
84      
85  
86  
87      public void performPostTablificationTransformations(List<AccountingLineTableRow> rows, AccountingLineGroupDefinition groupDefinition, AccountingDocument document, AccountingLine accountingLine, boolean newLine) {
88          for (AccountingLineTableTransformation transformation : postTablificationTransformations) {
89              transformation.transformRows(rows);
90          }
91      }
92  
93  
94      
95  
96  
97  
98  
99  
100 
101 
102     protected void performAuthorizationTransformations(List<TableJoining> elements, AccountingLineGroupDefinition accountingLineGroupDefinition, AccountingDocument accountingDocument, AccountingLine accountingLine, boolean newLine, String accountingLinePropertyName) {
103         accountingLineAuthorizationTransformer.transformElements(elements, accountingLine, accountingDocument, accountingLineGroupDefinition.getAccountingLineAuthorizer(), newLine, accountingLinePropertyName);
104     }
105     
106     
107 
108 
109 
110 
111 
112 
113     protected void performFieldTransformations(List<TableJoining> elements, AccountingDocument accountingDocument, AccountingLine accountingLine, Map unconvertedValues) {
114         for (TableJoining layoutElement : elements) {
115             layoutElement.performFieldTransformations(fieldTransformations, accountingLine, unconvertedValues);
116         }
117     }
118  
119     
120 
121 
122 
123 
124     protected FinancialSystemTransactionalDocumentAuthorizerBase getDocumentAuthorizer(AccountingDocument document) {
125         final FinancialSystemTransactionalDocumentAuthorizerBase authorizer = (FinancialSystemTransactionalDocumentAuthorizerBase) getDocumentHelperService().getDocumentAuthorizer(document);
126         return authorizer;
127     }
128     
129     
130 
131 
132 
133     protected FinancialSystemTransactionalDocumentPresentationController getPresentationController(AccountingDocument document) {
134         final FinancialSystemTransactionalDocumentPresentationController presentationController = (FinancialSystemTransactionalDocumentPresentationController) getDocumentHelperService().getDocumentPresentationController(document);
135         return presentationController;
136     }
137 
138     
139 
140 
141 
142     public List<AccountingLineTableRow> tablify(List<TableJoining> elements) {
143         List<AccountingLineTableRow> rows = createBlankTableRows(getMaxRowCount(elements));
144         tablifyElements(elements, rows);
145         return rows;
146     }
147     
148     
149 
150 
151 
152 
153     protected int getMaxRowCount(List<TableJoining> elements) {
154         int maxRowCount = 0;
155         for (TableJoining element : elements) {
156             int rowCount = element.getRequestedRowCount();
157             if (rowCount > maxRowCount) {
158                 maxRowCount = rowCount;
159             }
160         }
161         return maxRowCount;
162     }
163     
164     
165 
166 
167 
168 
169     protected List<AccountingLineTableRow> createBlankTableRows(int count) {
170         List<AccountingLineTableRow> rows = new ArrayList<AccountingLineTableRow>();
171         for (int i = 0; i < count; i++) {
172             rows.add(new AccountingLineTableRow());
173         }
174         return rows;
175     }
176 
177     
178 
179 
180 
181 
182     protected void tablifyElements(List<TableJoining> elements, List<AccountingLineTableRow> rows) {
183         for (TableJoining element : elements) {
184             element.joinTable(rows);
185         }
186     }
187 
188     
189 
190 
191 
192 
193     public FieldRenderer getFieldRendererForField(Field field, AccountingLine accountingLineToRender) {
194         FieldRenderer renderer = null;
195 
196         if (field.isReadOnly() || field.getFieldType().equals(Field.READONLY)) {
197             renderer = new ReadOnlyRenderer();
198         } 
199 
200 
201 
202 
203         else if (field.getFieldType().equals(Field.TEXT)) {
204             if (field.isDatePicker() || usesDateValidation(field.getPropertyName(), accountingLineToRender)) { 
205                 renderer = new DateRenderer();
206             } else {
207                 renderer = new TextRenderer();
208             }
209         } else if (field.getFieldType().equals(Field.TEXT_AREA)) {
210             renderer = new TextAreaRenderer();
211         } else if (field.getFieldType().equals(Field.HIDDEN)) {
212             renderer = new HiddenRenderer();
213         } else if (field.getFieldType().equals(Field.CURRENCY)) {
214             renderer = new CurrencyRenderer();
215         } else if (field.getFieldType().equals(Field.DROPDOWN)) {
216             renderer = new DropDownRenderer();
217         } else if (field.getFieldType().equals(Field.RADIO)) {
218             renderer = new RadioButtonGroupRenderer();
219         } else if (field.getFieldType().equals(Field.CHECKBOX)) {
220             renderer = new CheckboxRenderer();
221         }
222         
223         return renderer;
224     }
225     
226     
227 
228 
229 
230 
231 
232     protected boolean usesDateValidation(String propertyName, Object businessObject) {
233         final org.kuali.rice.krad.datadictionary.BusinessObjectEntry entry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(businessObject.getClass().getName());
234         AttributeDefinition attributeDefinition = entry.getAttributeDefinition(propertyName);
235         
236         if (attributeDefinition == null) {
237             if (!propertyName.contains(".")) return false;
238             final int firstNestingPoint = propertyName.indexOf(".");
239             final String toNestingPoint = propertyName.substring(0, firstNestingPoint);
240             final String fromNestingPoint = propertyName.substring(firstNestingPoint+1);
241             Object childObject = null;
242             try {
243                 final Class childClass = PropertyUtils.getPropertyType(businessObject, toNestingPoint);
244                 childObject = childClass.newInstance();
245             }
246             catch (IllegalAccessException iae) {
247                 new UnsupportedOperationException(iae);
248             }
249             catch (InvocationTargetException ite) {
250                 new UnsupportedOperationException(ite);
251             }
252             catch (NoSuchMethodException nsme) {
253                 new UnsupportedOperationException(nsme);
254             }
255             catch (InstantiationException ie) {
256                 throw new UnsupportedOperationException(ie);
257             }
258             return usesDateValidation(fromNestingPoint, childObject);
259         }
260         
261         final ValidationPattern validationPattern = attributeDefinition.getValidationPattern();
262         if (validationPattern == null) return false; 
263         return validationPattern instanceof DateValidationPattern;
264     }
265 
266     
267 
268 
269     public AccountingLineViewFieldDefinition createGenericAccountingLineViewFieldDefinition(MaintainableFieldDefinition currentDefinition) {
270         AccountingLineViewFieldDefinition fieldDefinition = new AccountingLineViewFieldDefinition();
271         
272         fieldDefinition.setRequired(currentDefinition.isRequired());
273         fieldDefinition.setUnconditionallyReadOnly(currentDefinition.isUnconditionallyReadOnly());
274         fieldDefinition.setReadOnlyAfterAdd(currentDefinition.isReadOnlyAfterAdd());
275         fieldDefinition.setNoLookup(currentDefinition.isNoLookup());
276         
277         fieldDefinition.setDefaultValue(currentDefinition.getDefaultValue());
278         fieldDefinition.setTemplate(currentDefinition.getTemplate());
279         fieldDefinition.setDefaultValueFinderClass(currentDefinition.getDefaultValueFinderClass());
280         
281         fieldDefinition.setOverrideLookupClass(currentDefinition.getOverrideLookupClass());
282         fieldDefinition.setOverrideFieldConversions(currentDefinition.getOverrideFieldConversions());
283         
284         return fieldDefinition;
285     }
286 
287     
288 
289 
290 
291     public List<AccountingLineFieldRenderingTransformation> getFieldTransformations() {
292         return fieldTransformations;
293     }
294 
295     
296 
297 
298 
299     public void setFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations) {
300         this.fieldTransformations = fieldTransformations;
301     }
302 
303     
304 
305 
306 
307     public AccountingLineAuthorizationTransformer getAccountingLineAuthorizationTransformer() {
308         return accountingLineAuthorizationTransformer;
309     }
310 
311     
312 
313 
314 
315     public void setAccountingLineAuthorizationTransformer(AccountingLineAuthorizationTransformer accountingLineAuthorizationTransformer) {
316         this.accountingLineAuthorizationTransformer = accountingLineAuthorizationTransformer;
317     }
318 
319     
320 
321 
322 
323     public DataDictionaryService getDataDictionaryService() {
324         return dataDictionaryService;
325     }
326 
327     
328 
329 
330 
331     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
332         this.dataDictionaryService = dataDictionaryService;
333     }
334 
335     
336 
337 
338 
339     public List<AccountingLineTableTransformation> getPostTablificationTransformations() {
340         return postTablificationTransformations;
341     }
342 
343     
344 
345 
346 
347     public void setPostTablificationTransformations(List<AccountingLineTableTransformation> postTablificationTransformations) {
348         this.postTablificationTransformations = postTablificationTransformations;
349     }
350 
351     
352 
353 
354 
355     public List<AccountingLineRenderingTransformation> getPreTablificationTransformations() {
356         return preTablificationTransformations;
357     }
358 
359     
360 
361 
362 
363     public void setPreTablificationTransformations(List<AccountingLineRenderingTransformation> preTablificationTransformations) {
364         this.preTablificationTransformations = preTablificationTransformations;
365     }
366 
367     
368 
369 
370     public KualiAccountingDocumentFormBase findForm(PageContext pageContext) {
371         if (pageContext.getRequest().getAttribute(KUALI_FORM_NAME) != null) return (KualiAccountingDocumentFormBase)pageContext.getRequest().getAttribute(KUALI_FORM_NAME);
372         
373         if (pageContext.getSession().getAttribute(KUALI_FORM_NAME) != null) return (KualiAccountingDocumentFormBase)pageContext.getSession().getAttribute(KUALI_FORM_NAME);
374         
375         return (KualiAccountingDocumentFormBase)KNSGlobalVariables.getKualiForm();
376     }
377     
378     protected DocumentHelperService getDocumentHelperService() {
379         if (documentHelperService == null) {
380             documentHelperService = SpringContext.getBean(DocumentHelperService.class);
381         }
382         return documentHelperService;
383     }
384 }
385