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 org.kuali.rice.kns.service.KNSServiceLocator;
19  import org.kuali.rice.krad.bo.GlobalBusinessObject;
20  import org.kuali.rice.krad.bo.GlobalBusinessObjectDetail;
21  import org.kuali.rice.krad.bo.PersistableBusinessObject;
22  import org.kuali.rice.krad.document.MaintenanceLock;
23  import org.kuali.rice.krad.service.BusinessObjectService;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.util.KRADPropertyConstants;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl {
33      private static final long serialVersionUID = 4814145799502207182L;
34  
35      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiGlobalMaintainableImpl.class);
36  
37      /**
38       * @see org.kuali.rice.krad.maintenance.Maintainable#prepareForSave()
39       */
40      @Override
41      public void prepareForSave() {
42          if (businessObject != null) {
43              prepareGlobalsForSave();
44          }
45      }
46  
47      /**
48       * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterRetrieve()
49       */
50      @Override
51      public void processAfterRetrieve() {
52          if (businessObject != null) {
53              processGlobalsAfterRetrieve();
54          }
55      }
56  
57      /**
58       * This method does special-case handling for Globals, doing various tasks that need to be done to make a global doc valid after
59       * its been loaded from the db/maintenance-system.
60       */
61      protected void processGlobalsAfterRetrieve() {
62  
63          // TODO: this needs refactoring ... its kind of lame that we have this set of
64          // compound list statements, this should all be refactored. This could be moved
65          // into a method on all GBOs, like GBO.prepareForSave(), or even better, subclass
66          // KualiGlobalMaintainableImpl for each global, since this is all
67          // maintainable-related stuff.
68  
69          GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
70          Class gboClass = businessObject.getClass();
71          String finDocNumber = gbo.getDocumentNumber();
72  
73          // TODO: remove this whole pseudo-assertion code block once this gets moved into a doc-specific
74          // maintainableImpl class.
75  
76          // This whole mess is to fail-fast if my assumptions about the nature of the parent bo of all
77          // global-maintenance-documents is wrong
78          boolean assumptionIsWrong = false;
79          //TODO: Revisit this. Changing since getPrimaryKeys and listPrimaryKeyFieldNames are apparently same.
80          //May be we might want to replace listPrimaryKeyFieldNames with getPrimaryKeys... Not sure.
81          List primaryKeys = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass);
82          if (primaryKeys == null) {
83              assumptionIsWrong = true;
84          }
85          else if (primaryKeys.isEmpty()) {
86              assumptionIsWrong = true;
87          }
88          else if (primaryKeys.size() != 1) {
89              assumptionIsWrong = true;
90          }
91          else if (!primaryKeys.get(0).getClass().equals(String.class)) {
92              assumptionIsWrong = true;
93          }
94          else if (!KRADPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) {
95              assumptionIsWrong = true;
96          }
97          if (assumptionIsWrong) {
98              throw new RuntimeException("An assertion about the nature of the primary keys for this GBO has " + "failed, and processing cannot continue.");
99          }
100 
101         // ASSUMPTION: This next section assumes that all GBOs have documentNumber as
102         // their only primary key field, and that its named as follows. This will
103         // either fail loudly or break silently if this assumption is not true. Once we
104         // move this sort of thing into the global-doc-specific subclasses of
105         // KualiGlobalMaintainableImpl, this will simplify tremendously.
106         Map pkMap = new HashMap();
107         pkMap.put(KRADPropertyConstants.DOCUMENT_NUMBER, finDocNumber);
108         PersistableBusinessObject newBo = null;
109         newBo = (PersistableBusinessObject) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(gboClass, pkMap);
110         if (newBo == null) {
111             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.");
112         }
113         
114         // property newCollectionRecord of PersistableObjectBase is not persisted, but is always true for globals
115         try {
116             ObjectUtils.setObjectPropertyDeep(newBo, KRADPropertyConstants.NEW_COLLECTION_RECORD, boolean.class, true, 2);
117         }
118         catch (Exception e) {
119             LOG.error("unable to set newCollectionRecord property: " + e.getMessage());
120             throw new RuntimeException("unable to set newCollectionRecord property: " + e.getMessage());
121         }
122 
123         // replace the GBO loaded from XML with the GBO loaded from the DB
124         businessObject = newBo;
125     }
126 
127     /**
128      * This method does special-case handling for Globals, filling out various fields that need to be filled, etc.
129      */
130     protected void prepareGlobalsForSave() {
131         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
132 
133         // set the documentNumber for all
134         gbo.setDocumentNumber(getDocumentNumber());
135 
136         List<? extends GlobalBusinessObjectDetail> details = gbo.getAllDetailObjects();
137         for ( GlobalBusinessObjectDetail detail : details ) {
138             detail.setDocumentNumber(getDocumentNumber());
139         }
140     }
141 
142     /**
143      * This overrides the standard version in KualiMaintainableImpl which works for non-global maintenance documents
144      * 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)
145      * @see org.kuali.rice.krad.maintenance.Maintainable#generateMaintenanceLocks()
146      */
147     @Override
148     public abstract List<MaintenanceLock> generateMaintenanceLocks();
149 
150     /**
151      * @see org.kuali.rice.krad.maintenance.Maintainable#saveBusinessObject()
152      */
153     @Override
154     public void saveBusinessObject() {
155         BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
156         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
157 
158         // delete any indicated BOs
159         List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();
160         if (bosToDeactivate != null) {
161             if (!bosToDeactivate.isEmpty()) {
162                 boService.save(bosToDeactivate);
163             }
164         }
165 
166         // persist any indicated BOs
167         List<PersistableBusinessObject> bosToPersist = gbo.generateGlobalChangesToPersist();
168         if (bosToPersist != null) {
169             if (!bosToPersist.isEmpty()) {
170                 boService.save(bosToPersist);
171             }
172         }
173 
174     }
175     
176     public abstract Class<? extends PersistableBusinessObject> getPrimaryEditedBusinessObjectClass();
177 }