View Javadoc

1   /*
2    * Copyright 2006-2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.maintenance;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.kns.bo.GlobalBusinessObject;
23  import org.kuali.rice.kns.bo.GlobalBusinessObjectDetail;
24  import org.kuali.rice.kns.bo.PersistableBusinessObject;
25  import org.kuali.rice.kns.document.MaintenanceLock;
26  import org.kuali.rice.kns.service.BusinessObjectService;
27  import org.kuali.rice.kns.service.KNSServiceLocator;
28  import org.kuali.rice.kns.util.KNSPropertyConstants;
29  import org.kuali.rice.kns.util.ObjectUtils;
30  
31  public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl {
32      private static final long serialVersionUID = 4814145799502207182L;
33  
34      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiGlobalMaintainableImpl.class);
35  
36      /**
37       * @see org.kuali.rice.kns.maintenance.Maintainable#prepareForSave()
38       */
39      @Override
40      public void prepareForSave() {
41          if (businessObject != null) {
42              prepareGlobalsForSave();
43          }
44      }
45  
46      /**
47       * @see org.kuali.rice.kns.maintenance.Maintainable#processAfterRetrieve()
48       */
49      @Override
50      public void processAfterRetrieve() {
51          if (businessObject != null) {
52              processGlobalsAfterRetrieve();
53          }
54      }
55  
56      /**
57       * This method does special-case handling for Globals, doing various tasks that need to be done to make a global doc valid after
58       * its been loaded from the db/maintenance-system.
59       */
60      protected void processGlobalsAfterRetrieve() {
61  
62          // TODO: this needs refactoring ... its kind of lame that we have this set of
63          // compound list statements, this should all be refactored. This could be moved
64          // into a method on all GBOs, like GBO.prepareForSave(), or even better, subclass
65          // KualiGlobalMaintainableImpl for each global, since this is all
66          // maintainable-related stuff.
67  
68          GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
69          Class gboClass = businessObject.getClass();
70          String finDocNumber = gbo.getDocumentNumber();
71  
72          // TODO: remove this whole pseudo-assertion code block once this gets moved into a doc-specific
73          // maintainableImpl class.
74  
75          // This whole mess is to fail-fast if my assumptions about the nature of the parent bo of all
76          // global-maintenance-documents is wrong
77          boolean assumptionIsWrong = false;
78          //TODO: Revisit this. Changing since getPrimaryKeys and listPrimaryKeyFieldNames are apparently same.
79          //May be we might want to replace listPrimaryKeyFieldNames with getPrimaryKeys... Not sure.
80          List primaryKeys = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass);
81          if (primaryKeys == null) {
82              assumptionIsWrong = true;
83          }
84          else if (primaryKeys.isEmpty()) {
85              assumptionIsWrong = true;
86          }
87          else if (primaryKeys.size() != 1) {
88              assumptionIsWrong = true;
89          }
90          else if (!primaryKeys.get(0).getClass().equals(String.class)) {
91              assumptionIsWrong = true;
92          }
93          else if (!KNSPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) {
94              assumptionIsWrong = true;
95          }
96          if (assumptionIsWrong) {
97              throw new RuntimeException("An assertion about the nature of the primary keys for this GBO has " + "failed, and processing cannot continue.");
98          }
99  
100         // ASSUMPTION: This next section assumes that all GBOs have documentNumber as
101         // their only primary key field, and that its named as follows. This will
102         // either fail loudly or break silently if this assumption is not true. Once we
103         // move this sort of thing into the global-doc-specific subclasses of
104         // KualiGlobalMaintainableImpl, this will simplify tremendously.
105         Map pkMap = new HashMap();
106         pkMap.put(KNSPropertyConstants.DOCUMENT_NUMBER, finDocNumber);
107         PersistableBusinessObject newBo = null;
108         newBo = (PersistableBusinessObject) KNSServiceLocator.getBusinessObjectService().findByPrimaryKey(gboClass, pkMap);
109         if (newBo == null) {
110             throw new RuntimeException("The Global Business Object could not be retrieved from the DB.  " + "This should never happen under normal circumstances.  If this is a legitimate case " + "Then this exception should be removed.");
111         }
112         
113         // property newCollectionRecord of PersistableObjectBase is not persisted, but is always true for globals
114         try {
115             ObjectUtils.setObjectPropertyDeep(newBo, KNSPropertyConstants.NEW_COLLECTION_RECORD, boolean.class, true, 2);
116         }
117         catch (Exception e) {
118             LOG.error("unable to set newCollectionRecord property: " + e.getMessage());
119             throw new RuntimeException("unable to set newCollectionRecord property: " + e.getMessage());
120         }
121 
122         // replace the GBO loaded from XML with the GBO loaded from the DB
123         businessObject = newBo;
124     }
125 
126     /**
127      * This method does special-case handling for Globals, filling out various fields that need to be filled, etc.
128      */
129     protected void prepareGlobalsForSave() {
130         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
131 
132         // set the documentNumber for all
133         gbo.setDocumentNumber(documentNumber);
134 
135         List<? extends GlobalBusinessObjectDetail> details = gbo.getAllDetailObjects();
136         for ( GlobalBusinessObjectDetail detail : details ) {
137             detail.setDocumentNumber(documentNumber);
138         }
139     }
140 
141     /**
142      * This overrides the standard version in KualiMaintainableImpl which works for non-global maintenance documents
143      * Each global document must in turn override this with its own locking representation, since it varies from document to document (some have one detail class and others have two, and the way to combine the two detail classes is unique to document with two detail classes)
144      * @see org.kuali.rice.kns.maintenance.Maintainable#generateMaintenanceLocks()
145      */
146     @Override
147     public abstract List<MaintenanceLock> generateMaintenanceLocks();
148 
149     /**
150      * @see org.kuali.rice.kns.maintenance.Maintainable#saveBusinessObject()
151      */
152     @Override
153     public void saveBusinessObject() {
154         BusinessObjectService boService = KNSServiceLocator.getBusinessObjectService();
155         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
156 
157         // delete any indicated BOs
158         List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();
159         if (bosToDeactivate != null) {
160             if (!bosToDeactivate.isEmpty()) {
161                 boService.save(bosToDeactivate);
162             }
163         }
164 
165         // persist any indicated BOs
166         List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist();
167         if (bosToPersist != null) {
168             if (!bosToPersist.isEmpty()) {
169                 boService.save(bosToPersist);
170             }
171         }
172 
173     }
174     
175     public abstract Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass();
176 }