View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.maintenance;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * Default implementation of {@link BulkUpdateMaintainable}.
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class BulkUpdateMaintainableImpl extends MaintainableImpl implements BulkUpdateMaintainable {
32      private static final long serialVersionUID = 6656390440709425848L;
33  
34      /**
35       * The bulk update maintenance document including the data objects to be updated by this bulk update maintenance
36       * document needs to be persistable.
37       *
38       * @see BulkUpdateMaintainable#isPersistable
39       */
40      @Override
41      public boolean isPersistable() {
42          if (StringUtils.isBlank(getDocumentNumber())) {
43              return false;
44          }
45  
46          BulkUpdateMaintenanceDataObject dataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
47          List<?> targetUpdateDataObjects = dataObject.getTargetUpdateDataObjects();
48  
49          for (Object targetUpdateDataObject : targetUpdateDataObjects) {
50              if (!getLegacyDataAdapter().hasPrimaryKeyFieldValues(targetUpdateDataObject)) {
51                  return false;
52              }
53          }
54  
55          return true;
56      }
57  
58      /**
59       * @see BulkUpdateMaintainable#generateUpdatableObjects()
60       */
61      @Override
62      public List<Object> generateUpdatableObjects() {
63          List<Object> updatableObjects = new ArrayList<Object>();
64  
65          BulkUpdateMaintenanceDataObject bulkUpdateMaintenanceDataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
66          List<?> targetUpdateDataObjects = bulkUpdateMaintenanceDataObject.getTargetUpdateDataObjects();
67  
68          for (Object targetUpdateDataObject : targetUpdateDataObjects) {
69              Map<String, Object> primaryKeys = getDataObjectService().wrap(targetUpdateDataObject).getPrimaryKeyValues();
70              Object updatableObject = getDataObjectService().findUnique(targetUpdateDataObject.getClass(),
71                      QueryByCriteria.Builder.andAttributes(primaryKeys).build());
72  
73              for (Map.Entry<String, ?> updateFieldValue : bulkUpdateMaintenanceDataObject.getUpdateFieldValues().entrySet()) {
74                  String propertyName = updateFieldValue.getKey();
75                  Object propertyValue = updateFieldValue.getValue();
76  
77                  if (ObjectPropertyUtils.isWritableProperty(updatableObject, propertyName)) {
78                      ObjectPropertyUtils.setPropertyValue(updatableObject, propertyName, propertyValue);
79                      updatableObjects.add(updatableObject);
80                  }
81              }
82          }
83  
84          return updatableObjects;
85      }
86  
87      /**
88       * @see Maintainable#generateMaintenanceLocks()
89       */
90      @Override
91      public List<MaintenanceLock> generateMaintenanceLocks() {
92          List<MaintenanceLock> maintenanceLocks = super.generateMaintenanceLocks();
93  
94          BulkUpdateMaintenanceDataObject dataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
95  
96          for (Object targetUpdateDataObjects : dataObject.getTargetUpdateDataObjects()) {
97              String documentTypeName = getDocumentDictionaryService().getMaintenanceDocumentTypeName(targetUpdateDataObjects.getClass());
98              maintenanceLocks.addAll(generateMaintenanceLocks(getDocumentNumber(), documentTypeName, targetUpdateDataObjects.getClass(), targetUpdateDataObjects));
99          }
100 
101         return maintenanceLocks;
102     }
103 
104     /**
105      * @see Maintainable#saveDataObject()
106      */
107     @Override
108     public void saveDataObject() {
109         for (Object updateableObject : generateUpdatableObjects()) {
110             getLegacyDataAdapter().linkAndSave(updateableObject);
111         }
112     }
113 
114 }