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