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 */ 016package org.kuali.ole.select.document; 017/** This class is to assigning Default Value (Which are the specified in OleDefaultValue Maintenance document) to the specific field based on object 018 * 019 */ 020 021import org.kuali.ole.select.OleSelectConstant; 022import org.kuali.ole.select.businessobject.OleDefaultTableColumn; 023import org.kuali.ole.select.businessobject.OleDefaultValue; 024import org.kuali.ole.select.constants.OleSelectPropertyConstants; 025import org.kuali.ole.sys.context.SpringContext; 026import org.kuali.rice.core.api.membership.MemberType; 027import org.kuali.rice.core.api.util.type.KualiDecimal; 028import org.kuali.rice.kew.doctype.bo.DocumentType; 029import org.kuali.rice.kim.api.services.KimApiServiceLocator; 030import org.kuali.rice.krad.bo.PersistableBusinessObjectBase; 031import org.kuali.rice.krad.datadictionary.AttributeDefinition; 032import org.kuali.rice.krad.datadictionary.BusinessObjectEntry; 033import org.kuali.rice.krad.datadictionary.DocumentEntry; 034import org.kuali.rice.krad.document.TransactionalDocumentBase; 035import org.kuali.rice.krad.service.BusinessObjectService; 036import org.kuali.rice.krad.service.DataDictionaryService; 037import org.kuali.rice.krad.util.GlobalVariables; 038 039import java.lang.reflect.Field; 040import java.lang.reflect.Method; 041import java.math.BigDecimal; 042import java.util.*; 043 044//import org.kuali.rice.kim.impl.role.RoleBo; 045 046public class OleDefaultValueAssignment { 047 048 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDefaultValueAssignment.class); 049 050 private Object businessObject = null; 051 052 private Object documentObject = null; 053 054 private static List<OleDefaultTableColumn> defaultTableColumnList = null; 055 056 private List<Field> businessFields = null; 057 058 private List<Field> dictionaryFields = null; 059 060 public OleDefaultValueAssignment() { 061 } 062 063 public OleDefaultValueAssignment(String docName, Object businessObject, Object documentObject) { 064 065 if (docName != null) { 066 setDefaultTableColumnList(docName); 067 } 068 if (businessObject != null) { 069 this.businessObject = businessObject; 070 businessFields = getFields(businessObject.getClass()); 071 this.documentObject = null; 072 setDefaultValues(); 073 } 074 if (documentObject != null) { 075 this.documentObject = documentObject; 076 dictionaryFields = getFields(documentObject.getClass()); 077 this.businessObject = null; 078 setDefaultValues(); 079 } 080 081 082 } 083 084 /** 085 * This method to retrieve the fields(properties) according to class parameter 086 * 087 * @param Class type 088 * @return List of fields 089 */ 090 public List<Field> getFields(Class type) { 091 LOG.debug(" getFields Mehtod-----> Start"); 092 List<Field> result = new ArrayList<Field>(); 093 Class className = type; 094 while (className != PersistableBusinessObjectBase.class && className != TransactionalDocumentBase.class && className != null) { 095 for (Field field : className.getDeclaredFields()) { 096 result.add(field); 097 } 098 className = className.getSuperclass(); 099 } 100 LOG.debug(" getFields Method-------> End"); 101 if (result.size() != 0) 102 return result; 103 else 104 return null; 105 } 106 107 /** 108 * This method to set the default values according to either Business Object or Document Object 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 * This method performs to retrieve the roles based on the principalId 239 * 240 * @param principalId 241 * @return List of roles 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 // Map<String,String> criteria = new HashMap<String,String>( 2 ); 250 // criteria.put("members.memberId", principalId); 251 // criteria.put("members.memberTypeCode", MemberType.PRINCIPAL.getCode()); 252 LOG.debug(" getRolesForPrincipal method -----> End"); 253 // return (List<RoleBo>)SpringContext.getBean(BusinessObjectService.class).findMatching(RoleBo.class, criteria); 254 return (List<String>) KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.PRINCIPAL.getCode(), principalId); 255 } 256 257 /** 258 * This method to set default values according to given parameters using reflections 259 * 260 * @param className 261 * @param method 262 * @param params 263 * @param args 264 * @param object 265 */ 266 private void invoke(Class className, String method, Class[] params, Object[] args, Object object) { 267 //Object i=null; 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 * This method to retrieve the default values(Which are in DataBase) using DocumentTypeName(as a parameter) 280 * 281 * @param docName 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