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.Collection;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.ole.coa.businessobject.OrganizationReversion;
27 import org.kuali.ole.coa.businessobject.OrganizationReversionCategory;
28 import org.kuali.ole.coa.businessobject.OrganizationReversionGlobal;
29 import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalDetail;
30 import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalOrganization;
31 import org.kuali.ole.coa.service.OrganizationReversionService;
32 import org.kuali.ole.sys.OLEConstants;
33 import org.kuali.ole.sys.OLEPropertyConstants;
34 import org.kuali.ole.sys.context.SpringContext;
35 import org.kuali.ole.sys.document.FinancialSystemGlobalMaintainable;
36 import org.kuali.rice.kns.document.MaintenanceDocument;
37 import org.kuali.rice.kns.lookup.LookupResultsService;
38 import org.kuali.rice.kns.web.ui.Column;
39 import org.kuali.rice.kns.web.ui.ResultRow;
40 import org.kuali.rice.krad.bo.PersistableBusinessObject;
41 import org.kuali.rice.krad.maintenance.MaintenanceLock;
42 import org.kuali.rice.krad.util.GlobalVariables;
43
44
45
46
47
48
49
50
51 public class OrganizationReversionGlobalMaintainableImpl extends FinancialSystemGlobalMaintainable {
52 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionGlobalMaintainableImpl.class);
53
54 private static transient OrganizationReversionService organizationReversionService;
55
56
57
58
59 private class CategoryComparator implements Comparator<OrganizationReversionGlobalDetail> {
60 @Override
61 public int compare(OrganizationReversionGlobalDetail detailA, OrganizationReversionGlobalDetail detailB) {
62 OrganizationReversionCategory categoryA = detailA.getOrganizationReversionCategory();
63 OrganizationReversionCategory categoryB = detailB.getOrganizationReversionCategory();
64
65 String code0 = categoryA.getOrganizationReversionCategoryCode();
66 String code1 = categoryB.getOrganizationReversionCategoryCode();
67
68 return code0.compareTo(code1);
69 }
70 }
71
72
73
74
75
76
77
78 @Override
79 public List<MaintenanceLock> generateMaintenanceLocks() {
80 List<MaintenanceLock> locks = new ArrayList<MaintenanceLock>();
81 OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) this.getBusinessObject();
82 if (globalOrgRev.getUniversityFiscalYear() != null && globalOrgRev.getOrganizationReversionGlobalOrganizations() != null && globalOrgRev.getOrganizationReversionGlobalOrganizations().size() > 0) {
83 for (OrganizationReversionGlobalOrganization orgRevOrg : globalOrgRev.getOrganizationReversionGlobalOrganizations()) {
84 MaintenanceLock maintenanceLock = new MaintenanceLock();
85 maintenanceLock.setDocumentNumber(globalOrgRev.getDocumentNumber());
86
87 StringBuilder lockRep = new StringBuilder();
88 lockRep.append(OrganizationReversion.class.getName());
89 lockRep.append(OLEConstants.Maintenance.AFTER_CLASS_DELIM);
90 lockRep.append(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
91 lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
92 lockRep.append(orgRevOrg.getChartOfAccountsCode());
93 lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
94 lockRep.append(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
95 lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
96 lockRep.append(globalOrgRev.getUniversityFiscalYear().toString());
97 lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
98 lockRep.append(OLEPropertyConstants.ORGANIZATION_CODE);
99 lockRep.append(OLEConstants.Maintenance.AFTER_FIELDNAME_DELIM);
100 lockRep.append(orgRevOrg.getOrganizationCode());
101 lockRep.append(OLEConstants.Maintenance.AFTER_VALUE_DELIM);
102
103 maintenanceLock.setLockingRepresentation(lockRep.toString());
104 locks.add(maintenanceLock);
105 }
106 }
107
108 return locks;
109 }
110
111
112
113
114
115
116
117 @Override
118 public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
119 super.processAfterNew(document, requestParameters);
120 OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) getBusinessObject();
121 List<OrganizationReversionGlobalDetail> details = globalOrgRev.getOrganizationReversionGlobalDetails();
122 if (LOG.isDebugEnabled()) {
123 LOG.debug("Details size before adding categories = " + details.size());
124 }
125
126 if (details == null) {
127 details = new ArrayList<OrganizationReversionGlobalDetail>();
128 globalOrgRev.setOrganizationReversionGlobalDetails(details);
129 }
130
131 if (details.size() == 0) {
132
133 Collection<OrganizationReversionCategory> categories = getOrganizationReversionService().getCategoryList();
134 for (OrganizationReversionCategory category : categories) {
135 if (category.isActive()) {
136 OrganizationReversionGlobalDetail detail = new OrganizationReversionGlobalDetail();
137 detail.setOrganizationReversionCategoryCode(category.getOrganizationReversionCategoryCode());
138 detail.setOrganizationReversionCategory(category);
139 detail.setParentGlobalOrganizationReversion(globalOrgRev);
140 details.add(detail);
141 }
142 }
143 if (LOG.isDebugEnabled()) {
144 LOG.debug("Details size after adding categories = " + details.size());
145 }
146 Collections.sort(details, new CategoryComparator());
147 }
148 }
149
150
151
152
153
154
155
156 @SuppressWarnings("unchecked")
157 @Override
158 protected boolean isRelationshipRefreshable(Class boClass, String relationshipName) {
159 if (relationshipName.equals("organizationReversionGlobalDetails")) {
160 return false;
161 }
162 else {
163 return super.isRelationshipRefreshable(boClass, relationshipName);
164 }
165 }
166
167
168
169
170
171
172
173 @Override
174 protected void processGlobalsAfterRetrieve() {
175 super.processGlobalsAfterRetrieve();
176 for (OrganizationReversionGlobalDetail changeDetail : ((OrganizationReversionGlobal) businessObject).getOrganizationReversionGlobalDetails()) {
177 changeDetail.setNewCollectionRecord(false);
178 }
179 }
180
181
182
183
184
185 @SuppressWarnings("unchecked")
186 @Override
187 public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
188 super.refresh(refreshCaller, fieldValues, document);
189 final String lookupBusinessObjectClassName = (String)fieldValues.get(OLEConstants.LOOKUP_RESULTS_BO_CLASS_NAME);
190 if (!StringUtils.isBlank(lookupBusinessObjectClassName)) {
191 if (lookupBusinessObjectClassName.equals(OrganizationReversion.class.getName())) {
192 final String lookupResultsSequenceNumber = (String)fieldValues.get(OLEConstants.LOOKUP_RESULTS_SEQUENCE_NUMBER);
193 if (!StringUtils.isBlank(lookupResultsSequenceNumber)) {
194 final LookupResultsService lookupResultsService = SpringContext.getBean(LookupResultsService.class);
195 try {
196 final List<ResultRow> resultRows = lookupResultsService.retrieveResultsTable(lookupResultsSequenceNumber, GlobalVariables.getUserSession().getPrincipalId());
197 if (!resultRows.isEmpty()) {
198 final ResultRow topRow = resultRows.get(0);
199 for (Column column : topRow.getColumns()) {
200 if (column.getPropertyName().equals(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
201 final String universityFiscalYearAsString = column.getPropertyValue();
202 final Integer universityFiscalYear = Integer.parseInt(universityFiscalYearAsString);
203
204 final OrganizationReversionGlobal orgRevGlobal = ((OrganizationReversionGlobal)document.getNewMaintainableObject().getBusinessObject());
205 orgRevGlobal.setUniversityFiscalYear(universityFiscalYear);
206 }
207 }
208 }
209 }
210 catch (Exception ex) {
211 throw new RuntimeException("Could not retrieve lookup results to populate fiscal year", ex);
212 }
213
214
215 }
216 }
217 }
218
219 }
220
221 @Override
222 public Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass() {
223 return OrganizationReversion.class;
224 }
225
226 protected OrganizationReversionService getOrganizationReversionService() {
227 if ( organizationReversionService == null ) {
228 organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
229 }
230 return organizationReversionService;
231 }
232 }