1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.web.struts.form;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.CoreApiServiceLocator;
21 import org.kuali.rice.kim.api.KimConstants;
22 import org.kuali.rice.kns.service.KNSServiceLocator;
23 import org.kuali.rice.krad.document.TransactionalDocument;
24 import org.kuali.rice.krad.service.DataDictionaryService;
25 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26 import org.kuali.rice.krad.util.KRADConstants;
27
28 import javax.servlet.http.HttpServletRequest;
29 import java.util.HashMap;
30 import java.util.Map;
31
32
33
34
35
36 public class KualiTransactionalDocumentFormBase extends KualiDocumentFormBase {
37 private static final Logger LOG = Logger.getLogger(KualiTransactionalDocumentFormBase.class);
38
39
40
41 private static final long serialVersionUID = 6463383454050206811L;
42 @SuppressWarnings("unchecked")
43 protected Map forcedReadOnlyFields;
44
45
46
47
48 @SuppressWarnings("unchecked")
49 public KualiTransactionalDocumentFormBase() {
50 super();
51
52
53 forcedReadOnlyFields = new HashMap();
54 }
55
56
57
58
59 @SuppressWarnings("unchecked")
60 @Override
61 public void populate(HttpServletRequest request) {
62 super.populate(request);
63 populationSpecialEmptyFields (request);
64 }
65
66
67
68
69
70
71 public TransactionalDocument getTransactionalDocument() {
72 return (TransactionalDocument) getDocument();
73 }
74
75
76
77
78
79
80
81 protected String discoverDocumentTypeName() {
82 return ((DataDictionaryService) KRADServiceLocatorWeb.getDataDictionaryService()).getDataDictionary().getDocumentEntry(getDocument().getClass().getName()).getDocumentTypeName();
83 }
84
85
86
87
88
89
90
91
92 protected static String formatReversalDate(java.sql.Date reversalDate) {
93 if (reversalDate == null) {
94 return "";
95 }
96
97 return CoreApiServiceLocator.getDateTimeService().toString(reversalDate, "MMM d, yyyy");
98 }
99
100
101
102
103
104
105 @SuppressWarnings("unchecked")
106 public Map getForcedReadOnlyFields() {
107 return forcedReadOnlyFields;
108 }
109
110
111
112
113
114
115 @SuppressWarnings("unchecked")
116 public void setForcedReadOnlyFields(Map forcedReadOnlyFields) {
117 this.forcedReadOnlyFields = forcedReadOnlyFields;
118 }
119
120
121
122
123
124
125
126 protected void populateFalseCheckboxes(HttpServletRequest request) {
127 Map<String, String[]> parameterMap = request.getParameterMap();
128 if (parameterMap.get("checkboxToReset") != null) {
129 final String[] checkboxesToReset = request.getParameterValues("checkboxToReset");
130 if(checkboxesToReset != null && checkboxesToReset.length > 0) {
131 for (int i = 0; i < checkboxesToReset.length; i++) {
132 String propertyName = (String) checkboxesToReset[i];
133 if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
134 populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_FALSE_STR_VALUE_DISPLAY, parameterMap);
135 }
136 else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
137 populateForProperty(propertyName, KimConstants.KIM_ATTRIBUTE_BOOLEAN_TRUE_STR_VALUE_DISPLAY, parameterMap);
138 }
139 }
140 }
141 }
142 }
143
144
145
146
147
148
149
150 protected void populateEmptyMultiSelect(HttpServletRequest request) {
151 Map<String, String[]> parameterMap = request.getParameterMap();
152 if (parameterMap.get("multiSelectToReset") != null) {
153 final String[] multiSelectToReset = request.getParameterValues("multiSelectToReset");
154 if(multiSelectToReset != null && multiSelectToReset.length > 0) {
155 for (int i = 0; i < multiSelectToReset.length; i++) {
156 String propertyName = (String) multiSelectToReset[i];
157 if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) == null ) {
158 populateForProperty(propertyName, "", parameterMap);
159 }
160 else if ( !StringUtils.isBlank(propertyName) && parameterMap.get(propertyName) != null && parameterMap.get(propertyName).length >= 1 && parameterMap.get(propertyName)[0].equalsIgnoreCase("on") ) {
161 populateForProperty(propertyName, request.getParameter(propertyName), parameterMap);
162 }
163 }
164 }
165 }
166 }
167
168 protected void populationSpecialEmptyFields (HttpServletRequest request) {
169 populateFalseCheckboxes(request);
170 populateEmptyMultiSelect(request);
171 }
172
173 @SuppressWarnings("unchecked")
174 protected TransactionalDocument instantiateTransactionalDocumentByDocumentTypeName( String documentTypeName ) {
175 Class<TransactionalDocument> transDocClass = KNSServiceLocator.getTransactionalDocumentDictionaryService().getDocumentClassByName(documentTypeName);
176 if ( transDocClass != null ) {
177 try {
178 return transDocClass.newInstance();
179 } catch (Exception ex) {
180 LOG.error( "Unable to instantiate transDocClass: " + transDocClass, ex);
181 }
182 } else {
183 LOG.error( "Unable to retrieve transactional document class for type: " + documentTypeName);
184 }
185 return null;
186 }
187
188
189
190
191
192
193 @Override
194 public boolean shouldMethodToCallParameterBeUsed(
195 String methodToCallParameterName,
196 String methodToCallParameterValue, HttpServletRequest request) {
197 if (methodToCallParameterName.startsWith(KRADConstants.DISPATCH_REQUEST_PARAMETER + "." + KRADConstants.POST_TEXT_AREA_TO_PARENT)) {
198 return true;
199 }
200 return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
201 }
202 }