1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
39  
40      @Override
41      public void prepareForSave() {
42          if (businessObject != null) {
43              prepareGlobalsForSave();
44          }
45      }
46  
47      
48  
49  
50      @Override
51      public void processAfterRetrieve() {
52          if (businessObject != null) {
53              processGlobalsAfterRetrieve();
54          }
55      }
56  
57      
58  
59  
60  
61      protected void processGlobalsAfterRetrieve() {
62  
63          
64          
65          
66          
67          
68  
69          GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
70          Class gboClass = businessObject.getClass();
71          String finDocNumber = gbo.getDocumentNumber();
72  
73          
74          
75  
76          
77          
78          boolean assumptionIsWrong = false;
79          
80          
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         
102         
103         
104         
105         
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         
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         
124         businessObject = newBo;
125     }
126 
127     
128 
129 
130     protected void prepareGlobalsForSave() {
131         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
132 
133         
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 
144 
145 
146 
147     @Override
148     public abstract List<MaintenanceLock> generateMaintenanceLocks();
149 
150     
151 
152 
153     @Override
154     public void saveBusinessObject() {
155         BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
156         GlobalBusinessObject gbo = (GlobalBusinessObject) businessObject;
157 
158         
159         List<PersistableBusinessObject> bosToDeactivate = gbo.generateDeactivationsToPersist();
160         if (bosToDeactivate != null) {
161             if (!bosToDeactivate.isEmpty()) {
162                 boService.save(bosToDeactivate);
163             }
164         }
165 
166         
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 }