1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document;
17
18
19
20
21 import org.kuali.ole.select.OleSelectConstant;
22 import org.kuali.ole.select.businessobject.OleDefaultTableColumn;
23 import org.kuali.ole.select.businessobject.OleDefaultValue;
24 import org.kuali.ole.select.constants.OleSelectPropertyConstants;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.rice.core.api.membership.MemberType;
27 import org.kuali.rice.core.api.util.type.KualiDecimal;
28 import org.kuali.rice.kew.doctype.bo.DocumentType;
29 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
31 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
32 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
33 import org.kuali.rice.krad.datadictionary.DocumentEntry;
34 import org.kuali.rice.krad.document.TransactionalDocumentBase;
35 import org.kuali.rice.krad.service.BusinessObjectService;
36 import org.kuali.rice.krad.service.DataDictionaryService;
37 import org.kuali.rice.krad.util.GlobalVariables;
38
39 import java.lang.reflect.Field;
40 import java.lang.reflect.Method;
41 import java.math.BigDecimal;
42 import java.util.*;
43
44
45
46 public class OleDefaultValueAssignment {
47
48 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDefaultValueAssignment.class);
49
50 private Object businessObject = null;
51
52 private Object documentObject = null;
53
54 private static List<OleDefaultTableColumn> defaultTableColumnList = null;
55
56 private List<Field> businessFields = null;
57
58 private List<Field> dictionaryFields = null;
59
60 public OleDefaultValueAssignment() {
61 }
62
63 public OleDefaultValueAssignment(String docName, Object businessObject, Object documentObject) {
64
65 if (docName != null) {
66 setDefaultTableColumnList(docName);
67 }
68 if (businessObject != null) {
69 this.businessObject = businessObject;
70 businessFields = getFields(businessObject.getClass());
71 this.documentObject = null;
72 setDefaultValues();
73 }
74 if (documentObject != null) {
75 this.documentObject = documentObject;
76 dictionaryFields = getFields(documentObject.getClass());
77 this.businessObject = null;
78 setDefaultValues();
79 }
80
81
82 }
83
84
85
86
87
88
89
90 public List<Field> getFields(Class type) {
91 LOG.debug(" getFields Mehtod-----> Start");
92 List<Field> result = new ArrayList<Field>();
93 Class className = type;
94 while (className != PersistableBusinessObjectBase.class && className != TransactionalDocumentBase.class && className != null) {
95 for (Field field : className.getDeclaredFields()) {
96 result.add(field);
97 }
98 className = className.getSuperclass();
99 }
100 LOG.debug(" getFields Method-------> End");
101 if (result.size() != 0)
102 return result;
103 else
104 return null;
105 }
106
107
108
109
110 public void setDefaultValues() {
111
112 LOG.debug(" setDefaultValues method -----> Start");
113 String userId = null;
114 String documentColumn = null;
115 Object object = null;
116 Class c = null;
117 Class fieldType = null;
118 Map<String, Object> defaultTableColumnIdMap = new HashMap<String, Object>();
119 DataDictionaryService dataDictionaryService;
120 BusinessObjectEntry businessObjectEntry = null;
121 DocumentEntry dataDictionaryEntry = null;
122 AttributeDefinition attributeDefinition = null;
123 List<Field> tempFields = null;
124 try {
125 dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
126 if (businessObject != null) {
127 businessObjectEntry = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(businessObject.getClass().getName());
128 tempFields = businessFields;
129 } else if (documentObject != null) {
130 dataDictionaryEntry = (DocumentEntry) dataDictionaryService.getDataDictionary().getDictionaryObjectEntry(documentObject.getClass().getName());
131 tempFields = dictionaryFields;
132 }
133 for (int j = 0; j < defaultTableColumnList.size(); j++) {
134 String defaultValue = null;
135 documentColumn = defaultTableColumnList.get(j).getDocumentColumn();
136 Field field = null;
137 for (int i = 0; i < tempFields.size(); i++) {
138 field = (Field) tempFields.get(i);
139 if (businessObjectEntry != null) {
140 attributeDefinition = businessObjectEntry.getAttributeDefinition(field.getName());
141 } else if (dataDictionaryEntry != null) {
142 attributeDefinition = dataDictionaryEntry.getAttributeDefinition(field.getName());
143 }
144 if (attributeDefinition != null) {
145 String label = attributeDefinition.getLabel();
146 String shortLabel = attributeDefinition.getShortLabel();
147
148 if (documentColumn.equalsIgnoreCase(label) || documentColumn.equalsIgnoreCase(shortLabel)) {
149 BigDecimal defaultTableColumnId = defaultTableColumnList.get(j).getDefaultTableColumnId();
150 defaultTableColumnIdMap.put("defaultTableColumnId", defaultTableColumnId);
151 List<OleDefaultValue> defaultValueList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultValue.class, defaultTableColumnIdMap);
152 if (defaultValueList.size() > 0) {
153 userId = GlobalVariables.getUserSession().getPrincipalId();
154 for (int k = 0; k < defaultValueList.size(); k++) {
155 if (defaultValueList.get(k).getDefaultValueFor().equalsIgnoreCase(OleSelectConstant.DEFAULT_VALUE_USER)) {
156 if (userId.equalsIgnoreCase(defaultValueList.get(k).getUserId())) {
157 defaultValue = defaultValueList.get(k).getDefaultValue();
158 }
159 }
160 }
161 if (defaultValue == null) {
162 boolean roleFlag = true;
163 for (int k = 0; k < defaultValueList.size(); k++) {
164 if (defaultValueList.get(k).getDefaultValueFor().equalsIgnoreCase(OleSelectConstant.DEFAULT_VALUE_ROLE)) {
165 List<String> roleImpl = getRolesForPrincipal(userId);
166 Iterator itr = roleImpl.iterator();
167 while (itr.hasNext()) {
168 String kimrole = itr.next().toString();
169 if (kimrole.equalsIgnoreCase(defaultValueList.get(k).getRoleId()) && roleFlag) {
170 defaultValue = defaultValueList.get(k).getDefaultValue();
171 }
172 }
173 }
174 }
175 if (defaultValue == null) {
176 for (int k = 0; k < defaultValueList.size(); k++) {
177 if (defaultValueList.get(k).getDefaultValueFor().equalsIgnoreCase(OleSelectConstant.DEFAULT_VALUE_SYSTEM)) {
178 defaultValue = defaultValueList.get(k).getDefaultValue();
179 }
180 }
181 }
182 }
183 }
184
185
186 if (businessObject != null) {
187 c = businessObject.getClass();
188 fieldType = field.getType();
189 if (fieldType != null)
190 object = businessObject;
191 } else if (documentObject != null) {
192 c = documentObject.getClass();
193 fieldType = field.getType();
194 if (fieldType != null)
195 object = documentObject;
196 }
197 if (fieldType != null && defaultValue != null) {
198 String[] fieldType1 = fieldType.toString().split("\\.");
199 String fieldType2 = fieldType1[fieldType1.length - 1];
200 String field1 = field.getName();
201 char initialCaps = field1.toUpperCase().charAt(0);
202 String fieldNameRemaining = field1.substring(1, field1.length());
203 String method = OleSelectPropertyConstants.SET + initialCaps + fieldNameRemaining;
204 Class[] params = new Class[]{fieldType};
205 Object[] args;
206 if (fieldType2.equalsIgnoreCase("KualiDecimal")) {
207 args = new Object[]{new KualiDecimal(defaultValue)};
208 } else if (fieldType2.equalsIgnoreCase("BigDecimal")) {
209 args = new Object[]{new BigDecimal(defaultValue)};
210 } else if (fieldType2.equalsIgnoreCase("String")) {
211 args = new Object[]{new String(defaultValue)};
212 } else if (fieldType2.equalsIgnoreCase("Integer")) {
213 args = new Object[]{new Integer(defaultValue)};
214 } else if (fieldType2.equalsIgnoreCase("Long")) {
215 args = new Object[]{new Long(defaultValue)};
216 } else if (fieldType2.equalsIgnoreCase("double")) {
217 args = new Object[]{new Double(defaultValue)};
218 } else {
219 args = new Object[]{new Float(defaultValue)};
220 }
221
222 invoke(c, method, params, args, object);
223
224 }
225 }
226 }
227 }
228 }
229 } catch (Exception e) {
230 LOG.error("Exception while setting default values"+e);
231 throw new RuntimeException(e);
232 }
233 LOG.debug(" setDefaultValues method -----> End");
234
235 }
236
237
238
239
240
241
242
243 @SuppressWarnings("unchecked")
244 private List<String> getRolesForPrincipal(String principalId) {
245 LOG.debug(" getRolesForPrincipal method -----> Start");
246 if (principalId == null) {
247 return new ArrayList<String>();
248 }
249
250
251
252 LOG.debug(" getRolesForPrincipal method -----> End");
253
254 return (List<String>) KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.PRINCIPAL.getCode(), principalId);
255 }
256
257
258
259
260
261
262
263
264
265
266 private void invoke(Class className, String method, Class[] params, Object[] args, Object object) {
267
268 LOG.debug(" invoke method -----> Start");
269 try {
270 Method m = className.getMethod(method, params);
271 Object r = m.invoke(object, args);
272 } catch (Exception e) {
273 e.printStackTrace();
274 }
275 LOG.debug(" invoke method -----> End");
276 }
277
278
279
280
281
282
283 public void setDefaultTableColumnList(String docName) {
284 LOG.debug(" setDefaultTableColumnList method -----> Start");
285 Map<String, Object> defaultTableColumnMap = new HashMap<String, Object>();
286 Map<String, Object> documentTypes = new HashMap<String, Object>();
287 documentTypes.put("name", docName);
288 Collection<DocumentType> documentTypeList = SpringContext.getBean(BusinessObjectService.class).findMatching(DocumentType.class, documentTypes);
289 String documentTypeId = documentTypeList.iterator().next().getDocumentTypeId();
290 defaultTableColumnMap.put("documentTypeId", documentTypeId);
291 defaultTableColumnList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultTableColumn.class, defaultTableColumnMap);
292 LOG.debug(" setDefaultTableColumnList method -----> End");
293 }
294
295
296 }
297
298