1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.coa.document;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.kuali.ole.coa.businessobject.ObjectCode;
24 import org.kuali.ole.coa.businessobject.ObjectCodeGlobal;
25 import org.kuali.ole.coa.businessobject.ObjectCodeGlobalDetail;
26 import org.kuali.ole.coa.service.ObjectCodeService;
27 import org.kuali.ole.coa.service.SubObjectTrickleDownInactivationService;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.context.SpringContext;
30 import org.kuali.ole.sys.document.FinancialSystemGlobalMaintainable;
31 import org.kuali.rice.krad.bo.GlobalBusinessObject;
32 import org.kuali.rice.krad.bo.PersistableBusinessObject;
33 import org.kuali.rice.krad.maintenance.MaintenanceLock;
34 import org.kuali.rice.krad.service.BusinessObjectService;
35 import org.kuali.rice.krad.util.KRADConstants;
36 import org.kuali.rice.krad.util.ObjectUtils;
37
38
39
40
41
42
43 public class ObjectCodeGlobalMaintainableImpl extends FinancialSystemGlobalMaintainable {
44 private static String CHANGE_DETAIL_COLLECTION = "objectCodeGlobalDetails";
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 protected void prepareGlobalsForSave() {
90
91 ObjectCodeGlobal objectCodeGlobal = (ObjectCodeGlobal) getBusinessObject();
92
93 for (ObjectCodeGlobalDetail detail : objectCodeGlobal.getObjectCodeGlobalDetails()) {
94 detail.setFinancialObjectCode(objectCodeGlobal.getFinancialObjectCode());
95 }
96 super.prepareGlobalsForSave();
97 }
98
99
100
101
102
103
104 @Override
105 public List<MaintenanceLock> generateMaintenanceLocks() {
106 ObjectCodeGlobal objectCodeGlobal = (ObjectCodeGlobal) getBusinessObject();
107 List<MaintenanceLock> maintenanceLocks = new ArrayList();
108 SubObjectTrickleDownInactivationService subObjectTrickleDownInactivationService = SpringContext.getBean(SubObjectTrickleDownInactivationService.class);
109
110 for (ObjectCodeGlobalDetail detail : objectCodeGlobal.getObjectCodeGlobalDetails()) {
111 MaintenanceLock maintenanceLock = new MaintenanceLock();
112 StringBuffer lockrep = new StringBuffer();
113
114 lockrep.append(ObjectCode.class.getName() + OLEConstants.Maintenance.AFTER_CLASS_DELIM);
115 lockrep.append("universityFiscalYear" + OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
116 lockrep.append(detail.getUniversityFiscalYear() + OLEConstants.Maintenance.AFTER_VALUE_DELIM);
117 lockrep.append("chartOfAccountsCode" + OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
118 lockrep.append(detail.getChartOfAccountsCode() + OLEConstants.Maintenance.AFTER_VALUE_DELIM);
119 lockrep.append("financialObjectCode" + OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
120 lockrep.append(detail.getFinancialObjectCode());
121
122 maintenanceLock.setDocumentNumber(objectCodeGlobal.getDocumentNumber());
123 maintenanceLock.setLockingRepresentation(lockrep.toString());
124 maintenanceLocks.add(maintenanceLock);
125
126 ObjectCode objectCode = new ObjectCode();
127 objectCode.setUniversityFiscalYear(detail.getUniversityFiscalYear());
128 objectCode.setChartOfAccountsCode(detail.getChartOfAccountsCode());
129 objectCode.setFinancialObjectCode(detail.getFinancialObjectCode());
130 objectCode.setActive(objectCodeGlobal.isFinancialObjectActiveIndicator());
131
132 if (isInactivatingObjectCode(objectCode)) {
133
134
135 maintenanceLocks.addAll(subObjectTrickleDownInactivationService.generateTrickleDownMaintenanceLocks(objectCode, getDocumentNumber()));
136 }
137 }
138 return maintenanceLocks;
139 }
140
141
142
143
144 @Override
145 public void saveBusinessObject() {
146 BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
147
148 GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
149
150
151 List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();
152 if (bosToDeactivate != null) {
153 if (!bosToDeactivate.isEmpty()) {
154 boService.save(bosToDeactivate);
155 }
156 }
157
158
159
160
161
162 Map<String, Boolean> objectCodeActiveStatusCache = buildObjectCodeActiveStatusCache((ObjectCodeGlobal) gbo);
163
164 SubObjectTrickleDownInactivationService subObjectTrickleDownInactivationService = SpringContext.getBean(SubObjectTrickleDownInactivationService.class);
165
166 List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist();
167 if (bosToPersist != null) {
168 if (!bosToPersist.isEmpty()) {
169 for (PersistableBusinessObject bo : bosToPersist) {
170 ObjectCode objectCode = (ObjectCode) bo;
171
172 boService.save(objectCode);
173
174 if (isInactivatingObjectCode(objectCode, objectCodeActiveStatusCache)) {
175 subObjectTrickleDownInactivationService.trickleDownInactivateSubObjects(objectCode, getDocumentNumber());
176 }
177 }
178 }
179 }
180 }
181
182 protected boolean isInactivatingObjectCode(ObjectCode objectCode) {
183 ObjectCodeService objectCodeService = SpringContext.getBean(ObjectCodeService.class);
184 if (!objectCode.isActive()) {
185 ObjectCode objectCodeFromDB = objectCodeService.getByPrimaryId(objectCode.getUniversityFiscalYear(), objectCode.getChartOfAccountsCode(), objectCode.getFinancialObjectCode());
186 if (objectCodeFromDB != null && objectCodeFromDB.isActive()) {
187 return true;
188 }
189 }
190 return false;
191 }
192
193 protected boolean isInactivatingObjectCode(ObjectCode objectCode, Map<String, Boolean> objectCodeActiveStatusCache) {
194 if (!objectCode.isActive()) {
195 if (Boolean.TRUE.equals(objectCodeActiveStatusCache.get(buildObjectCodeCachingKey(objectCode)))) {
196 return true;
197 }
198 }
199 return false;
200 }
201
202 protected String buildObjectCodeCachingKey(ObjectCode objectCode) {
203 return objectCode.getUniversityFiscalYear() + KRADConstants.Maintenance.LOCK_AFTER_VALUE_DELIM + objectCode.getChartOfAccountsCode() +
204 KRADConstants.Maintenance.LOCK_AFTER_VALUE_DELIM + objectCode.getFinancialObjectCode();
205 }
206
207 protected Map<String, Boolean> buildObjectCodeActiveStatusCache(ObjectCodeGlobal objectCodeGlobal) {
208 ObjectCodeService objectCodeService = SpringContext.getBean(ObjectCodeService.class);
209 Map<String, Boolean> cache = new HashMap<String, Boolean>();
210 for (ObjectCodeGlobalDetail detail : objectCodeGlobal.getObjectCodeGlobalDetails()) {
211 ObjectCode objectCodeFromDB = objectCodeService.getByPrimaryId(detail.getUniversityFiscalYear(), detail.getChartOfAccountsCode(), objectCodeGlobal.getFinancialObjectCode());
212 if (ObjectUtils.isNotNull(objectCodeFromDB)) {
213 cache.put(buildObjectCodeCachingKey(objectCodeFromDB), Boolean.valueOf(objectCodeFromDB.isActive()));
214 }
215 }
216 return cache;
217 }
218
219 @Override
220 public Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass() {
221 return ObjectCode.class;
222 }
223 }