001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.maintenance;
017:
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.kuali.RicePropertyConstants;
023: import org.kuali.core.bo.GlobalBusinessObject;
024: import org.kuali.core.bo.GlobalBusinessObjectDetail;
025: import org.kuali.core.bo.PersistableBusinessObject;
026: import org.kuali.core.document.MaintenanceLock;
027: import org.kuali.core.service.BusinessObjectService;
028: import org.kuali.core.util.ObjectUtils;
029: import org.kuali.rice.KNSServiceLocator;
030:
031: public abstract class KualiGlobalMaintainableImpl extends
032: KualiMaintainableImpl {
033: private static final long serialVersionUID = 4814145799502207182L;
034:
035: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(KualiGlobalMaintainableImpl.class);
037:
038: /**
039: * @see org.kuali.core.maintenance.Maintainable#prepareForSave()
040: */
041: @Override
042: public void prepareForSave() {
043: if (businessObject != null) {
044: prepareGlobalsForSave();
045: }
046: }
047:
048: /**
049: * @see org.kuali.core.maintenance.Maintainable#processAfterRetrieve()
050: */
051: @Override
052: public void processAfterRetrieve() {
053: if (businessObject != null) {
054: processGlobalsAfterRetrieve();
055: }
056: }
057:
058: /**
059: * This method does special-case handling for Globals, doing various tasks that need to be done to make a global doc valid after
060: * its been loaded from the db/maintenance-system.
061: */
062: protected void processGlobalsAfterRetrieve() {
063:
064: // TODO: this needs refactoring ... its kind of lame that we have this set of
065: // compound list statements, this should all be refactored. This could be moved
066: // into a method on all GBOs, like GBO.prepareForSave(), or even better, subclass
067: // KualiGlobalMaintainableImpl for each global, since this is all
068: // maintainable-related stuff.
069:
070: GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
071: Class gboClass = businessObject.getClass();
072: String finDocNumber = gbo.getDocumentNumber();
073:
074: // TODO: remove this whole pseudo-assertion code block once this gets moved into a doc-specific
075: // maintainableImpl class.
076:
077: // This whole mess is to fail-fast if my assumptions about the nature of the parent bo of all
078: // global-maintenance-documents is wrong
079: boolean assumptionIsWrong = false;
080: List primaryKeys = KNSServiceLocator
081: .getPersistenceStructureService().getPrimaryKeys(
082: gboClass);
083: if (primaryKeys == null) {
084: assumptionIsWrong = true;
085: } else if (primaryKeys.isEmpty()) {
086: assumptionIsWrong = true;
087: } else if (primaryKeys.size() != 1) {
088: assumptionIsWrong = true;
089: } else if (!primaryKeys.get(0).getClass().equals(String.class)) {
090: assumptionIsWrong = true;
091: } else if (!RicePropertyConstants.DOCUMENT_NUMBER
092: .equalsIgnoreCase((String) primaryKeys.get(0))) {
093: assumptionIsWrong = true;
094: }
095: if (assumptionIsWrong) {
096: throw new RuntimeException(
097: "An assertion about the nature of the primary keys for this GBO has "
098: + "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(RicePropertyConstants.DOCUMENT_NUMBER, finDocNumber);
108: PersistableBusinessObject newBo = null;
109: newBo = (PersistableBusinessObject) KNSServiceLocator
110: .getBusinessObjectService().findByPrimaryKey(gboClass,
111: pkMap);
112: if (newBo == null) {
113: throw new RuntimeException(
114: "The Global Business Object could not be retrieved from the DB. "
115: + "This should never happen under normal circumstances. If this is a legitimate case "
116: + "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: ObjectUtils.setObjectPropertyDeep(newBo,
122: RicePropertyConstants.NEW_COLLECTION_RECORD,
123: boolean.class, true, 2);
124: } catch (Exception e) {
125: LOG.error("unable to set newCollectionRecord property: "
126: + e.getMessage());
127: throw new RuntimeException(
128: "unable to set newCollectionRecord property: "
129: + e.getMessage());
130: }
131:
132: // replace the GBO loaded from XML with the GBO loaded from the DB
133: businessObject = newBo;
134: }
135:
136: /**
137: * This method does special-case handling for Globals, filling out various fields that need to be filled, etc.
138: */
139: protected void prepareGlobalsForSave() {
140: GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
141:
142: // set the documentNumber for all
143: gbo.setDocumentNumber(documentNumber);
144:
145: List<? extends GlobalBusinessObjectDetail> details = gbo
146: .getAllDetailObjects();
147: for (GlobalBusinessObjectDetail detail : details) {
148: detail.setDocumentNumber(documentNumber);
149: }
150: }
151:
152: /**
153: * This overrides the standard version in KualiMaintainableImpl which works for non-global maintenance documents
154: * 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)
155: * @see org.kuali.core.maintenance.Maintainable#generateMaintenanceLocks()
156: */
157: @Override
158: public abstract List<MaintenanceLock> generateMaintenanceLocks();
159:
160: /**
161: * @see org.kuali.core.maintenance.Maintainable#saveBusinessObject()
162: */
163: @Override
164: public void saveBusinessObject() {
165: BusinessObjectService boService = KNSServiceLocator
166: .getBusinessObjectService();
167: GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
168:
169: // delete any indicated BOs
170: List<PersistableBusinessObject> bosToDeactivate = gbo
171: .generateDeactivationsToPersist();
172: if (bosToDeactivate != null) {
173: if (!bosToDeactivate.isEmpty()) {
174: boService.save(bosToDeactivate);
175: }
176: }
177:
178: // persist any indicated BOs
179: List<PersistableBusinessObject> bosToPersist = gbo
180: .generateGlobalChangesToPersist();
181: if (bosToPersist != null) {
182: if (!bosToPersist.isEmpty()) {
183: boService.save(bosToPersist);
184: }
185: }
186:
187: }
188: }
|