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.kuali.ole.coa.businessobject.OrganizationReversion;
26  import org.kuali.ole.coa.businessobject.OrganizationReversionCategory;
27  import org.kuali.ole.coa.businessobject.OrganizationReversionDetail;
28  import org.kuali.ole.coa.service.OrganizationReversionDetailTrickleDownInactivationService;
29  import org.kuali.ole.coa.service.OrganizationReversionService;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.ole.sys.document.FinancialSystemMaintainable;
32  import org.kuali.rice.kns.document.MaintenanceDocument;
33  import org.kuali.rice.kns.maintenance.Maintainable;
34  import org.kuali.rice.kns.web.ui.Field;
35  import org.kuali.rice.kns.web.ui.Row;
36  import org.kuali.rice.kns.web.ui.Section;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  
40  
41  
42  
43  
44  
45  
46  public class OrganizationReversionMaintainableImpl extends FinancialSystemMaintainable {
47      private static transient OrganizationReversionService organizationReversionService;
48      private static transient OrganizationReversionDetailTrickleDownInactivationService trickleDownInactivationService;
49  
50      
51  
52  
53      protected static class CategoryComparator implements Comparator<OrganizationReversionDetail> {
54  
55          @Override
56          public int compare(OrganizationReversionDetail detail0, OrganizationReversionDetail detail1) {
57  
58              OrganizationReversionCategory category0 = detail0.getOrganizationReversionCategory();
59              OrganizationReversionCategory category1 = detail1.getOrganizationReversionCategory();
60  
61              String code0 = category0.getOrganizationReversionCategoryCode();
62              String code1 = category1.getOrganizationReversionCategoryCode();
63  
64              return code0.compareTo(code1);
65          }
66  
67      }
68  
69      @Override
70      public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
71          super.processAfterNew(document, requestParameters);
72  
73          OrganizationReversion organizationReversion = (OrganizationReversion) getBusinessObject();
74          List<OrganizationReversionDetail> details = organizationReversion.getOrganizationReversionDetail();
75  
76          if (details == null) {
77              details = new ArrayList<OrganizationReversionDetail>();
78              organizationReversion.setOrganizationReversionDetail(details);
79          }
80  
81          if (details.size() == 0) {
82  
83              Collection<OrganizationReversionCategory> categories = getOrganizationReversionService().getCategoryList();
84  
85              for (OrganizationReversionCategory category : categories) {
86                  if (category.isActive()) {
87                      OrganizationReversionDetail detail = new OrganizationReversionDetail();
88                      detail.setOrganizationReversionCategoryCode(category.getOrganizationReversionCategoryCode());
89                      detail.setOrganizationReversionCategory(category);
90                      details.add(detail);
91                  }
92              }
93  
94              Collections.sort(details, new CategoryComparator());
95          }
96      }
97  
98      
99  
100 
101 
102 
103 
104     @Override
105     protected boolean isRelationshipRefreshable(Class boClass, String relationshipName) {
106         if (relationshipName.equals("organizationReversionDetail")) {
107             return false;
108         } else {
109             return super.isRelationshipRefreshable(boClass, relationshipName);
110         }
111     }
112     
113     
114 
115 
116 
117     protected boolean isInactivatingOrganizationReversion() {
118         
119         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && !((OrganizationReversion) getBusinessObject()).isActive()) {
120             OrganizationReversion existingOrganizationReversionFromDB = retrieveExistingOrganizationReversion();
121             if (ObjectUtils.isNotNull(existingOrganizationReversionFromDB)) {
122                 
123                 if (existingOrganizationReversionFromDB.isActive()) {
124                     return true;
125                 }
126             }
127         }
128         return false;
129     }
130     
131     
132 
133 
134 
135     protected boolean isActivatingOrganizationReversion() {
136         
137         if (KRADConstants.MAINTENANCE_EDIT_ACTION.equals(getMaintenanceAction()) && ((OrganizationReversion) getBusinessObject()).isActive()) {
138             OrganizationReversion existingOrganizationReversionFromDB = retrieveExistingOrganizationReversion();
139             if (ObjectUtils.isNotNull(existingOrganizationReversionFromDB)) {
140                 
141                 if (!existingOrganizationReversionFromDB.isActive()) {
142                     return true;
143                 }
144             }
145         }
146         return false;
147     }
148     
149     
150 
151 
152 
153     protected OrganizationReversion retrieveExistingOrganizationReversion() {
154         OrganizationReversion orgRev = (OrganizationReversion)getBusinessObject();
155         OrganizationReversion oldOrgRev = getOrganizationReversionService().getByPrimaryId(orgRev.getUniversityFiscalYear(), orgRev.getChartOfAccountsCode(), orgRev.getOrganizationCode());
156         return oldOrgRev;
157     }
158 
159     
160 
161 
162 
163     @Override
164     public void saveBusinessObject() {
165         super.saveBusinessObject();
166         
167         if (isActivatingOrganizationReversion()) {
168             getTrickleDownInactivationService().trickleDownActiveOrganizationReversionDetails((OrganizationReversion)getBusinessObject(), getDocumentNumber());
169         } else if (isInactivatingOrganizationReversion()) {
170             getTrickleDownInactivationService().trickleDownInactiveOrganizationReversionDetails((OrganizationReversion)getBusinessObject(), getDocumentNumber());
171         }
172     }
173 
174     
175 
176 
177 
178 
179 
180     @Override
181     public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
182         List<Section> sections = super.getSections(document, oldMaintainable);
183         for (Section section : sections) {
184             for (Row row : section.getRows()) {
185                 List<Field> updatedFields = new ArrayList<Field>();
186                 for (Field field : row.getFields()) {
187                     if (shouldIncludeField(field)) {
188                         updatedFields.add(field);
189                     }
190                 }
191                 row.setFields(updatedFields);
192             }
193         }
194         return sections;
195     }
196     
197     
198 
199 
200 
201 
202 
203 
204 
205     protected boolean shouldIncludeField(Field field) {
206         boolean includeField = true;
207         if (field.getContainerRows() != null) {
208             for (Row containerRow : field.getContainerRows()) {
209                 for (Field containedField : containerRow.getFields()) {
210                     if (containedField.getPropertyName().matches("organizationReversionDetail\\[\\d+\\]\\.organizationReversionCategory\\.organizationReversionCategoryName")) {
211                         final String categoryValue = containedField.getPropertyValue();
212                         includeField = getOrganizationReversionService().isCategoryActiveByName(categoryValue);
213                     }
214                 }
215             }
216         }
217         return includeField;
218     }
219 
220     protected OrganizationReversionService getOrganizationReversionService() {
221         if ( organizationReversionService == null ) {
222             organizationReversionService = SpringContext.getBean(OrganizationReversionService.class);
223         }
224         return organizationReversionService;
225     }
226 
227     protected static OrganizationReversionDetailTrickleDownInactivationService getTrickleDownInactivationService() {
228         if ( trickleDownInactivationService == null ) {
229             trickleDownInactivationService = SpringContext.getBean(OrganizationReversionDetailTrickleDownInactivationService.class);
230         }
231         return trickleDownInactivationService;
232     }
233 }