001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kns.maintenance;
017    
018    import org.kuali.rice.kns.service.KNSServiceLocator;
019    import org.kuali.rice.krad.bo.GlobalBusinessObject;
020    import org.kuali.rice.krad.bo.GlobalBusinessObjectDetail;
021    import org.kuali.rice.krad.bo.PersistableBusinessObject;
022    import org.kuali.rice.krad.maintenance.MaintenanceLock;
023    import org.kuali.rice.krad.service.BusinessObjectService;
024    import org.kuali.rice.krad.service.KRADServiceLocator;
025    import org.kuali.rice.krad.util.KRADPropertyConstants;
026    import org.kuali.rice.krad.util.ObjectUtils;
027    
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    
032    public abstract class KualiGlobalMaintainableImpl extends KualiMaintainableImpl {
033        private static final long serialVersionUID = 4814145799502207182L;
034    
035        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiGlobalMaintainableImpl.class);
036    
037        /**
038         * @see org.kuali.rice.krad.maintenance.Maintainable#prepareForSave()
039         */
040        @Override
041        public void prepareForSave() {
042            if (businessObject != null) {
043                prepareGlobalsForSave();
044            }
045        }
046    
047        /**
048         * @see org.kuali.rice.krad.maintenance.Maintainable#processAfterRetrieve()
049         */
050        @Override
051        public void processAfterRetrieve() {
052            if (businessObject != null) {
053                processGlobalsAfterRetrieve();
054            }
055        }
056    
057        /**
058         * This method does special-case handling for Globals, doing various tasks that need to be done to make a global doc valid after
059         * its been loaded from the db/maintenance-system.
060         */
061        protected void processGlobalsAfterRetrieve() {
062    
063            // TODO: this needs refactoring ... its kind of lame that we have this set of
064            // compound list statements, this should all be refactored. This could be moved
065            // into a method on all GBOs, like GBO.prepareForSave(), or even better, subclass
066            // KualiGlobalMaintainableImpl for each global, since this is all
067            // maintainable-related stuff.
068    
069            GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
070            Class gboClass = businessObject.getClass();
071            String finDocNumber = gbo.getDocumentNumber();
072    
073            // TODO: remove this whole pseudo-assertion code block once this gets moved into a doc-specific
074            // maintainableImpl class.
075    
076            // This whole mess is to fail-fast if my assumptions about the nature of the parent bo of all
077            // global-maintenance-documents is wrong
078            boolean assumptionIsWrong = false;
079            //TODO: Revisit this. Changing since getPrimaryKeys and listPrimaryKeyFieldNames are apparently same.
080            //May be we might want to replace listPrimaryKeyFieldNames with getPrimaryKeys... Not sure.
081            List primaryKeys = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(gboClass);
082            if (primaryKeys == null) {
083                assumptionIsWrong = true;
084            }
085            else if (primaryKeys.isEmpty()) {
086                assumptionIsWrong = true;
087            }
088            else if (primaryKeys.size() != 1) {
089                assumptionIsWrong = true;
090            }
091            else if (!primaryKeys.get(0).getClass().equals(String.class)) {
092                assumptionIsWrong = true;
093            }
094            else if (!KRADPropertyConstants.DOCUMENT_NUMBER.equalsIgnoreCase((String) primaryKeys.get(0))) {
095                assumptionIsWrong = true;
096            }
097            if (assumptionIsWrong) {
098                throw new RuntimeException("An assertion about the nature of the primary keys for this GBO has " + "failed, and processing cannot continue.");
099            }
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            setBusinessObject(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    }