001/**
002 * Copyright 2005-2015 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 */
016package org.kuali.rice.krad.maintenance;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.criteria.QueryByCriteria;
020import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026/**
027 * Default implementation of {@link BulkUpdateMaintainable}.
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class BulkUpdateMaintainableImpl extends MaintainableImpl implements BulkUpdateMaintainable {
032    private static final long serialVersionUID = 6656390440709425848L;
033
034    /**
035     * The bulk update maintenance document including the data objects to be updated by this bulk update maintenance
036     * document needs to be persistable.
037     *
038     * @see BulkUpdateMaintainable#isPersistable
039     */
040    @Override
041    public boolean isPersistable() {
042        if (StringUtils.isBlank(getDocumentNumber())) {
043            return false;
044        }
045
046        BulkUpdateMaintenanceDataObject dataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
047        List<?> targetUpdateDataObjects = dataObject.getTargetUpdateDataObjects();
048
049        for (Object targetUpdateDataObject : targetUpdateDataObjects) {
050            if (!getLegacyDataAdapter().hasPrimaryKeyFieldValues(targetUpdateDataObject)) {
051                return false;
052            }
053        }
054
055        return true;
056    }
057
058    /**
059     * @see BulkUpdateMaintainable#generateUpdatableObjects()
060     */
061    @Override
062    public List<Object> generateUpdatableObjects() {
063        List<Object> updatableObjects = new ArrayList<Object>();
064
065        BulkUpdateMaintenanceDataObject bulkUpdateMaintenanceDataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
066        List<?> targetUpdateDataObjects = bulkUpdateMaintenanceDataObject.getTargetUpdateDataObjects();
067
068        for (Object targetUpdateDataObject : targetUpdateDataObjects) {
069            Map<String, Object> primaryKeys = getDataObjectService().wrap(targetUpdateDataObject).getPrimaryKeyValues();
070            Object updatableObject = getDataObjectService().findUnique(targetUpdateDataObject.getClass(),
071                    QueryByCriteria.Builder.andAttributes(primaryKeys).build());
072
073            for (Map.Entry<String, ?> updateFieldValue : bulkUpdateMaintenanceDataObject.getUpdateFieldValues().entrySet()) {
074                String propertyName = updateFieldValue.getKey();
075                Object propertyValue = updateFieldValue.getValue();
076
077                if (ObjectPropertyUtils.isWritableProperty(updatableObject, propertyName)) {
078                    ObjectPropertyUtils.setPropertyValue(updatableObject, propertyName, propertyValue);
079                    updatableObjects.add(updatableObject);
080                }
081            }
082        }
083
084        return updatableObjects;
085    }
086
087    /**
088     * @see Maintainable#generateMaintenanceLocks()
089     */
090    @Override
091    public List<MaintenanceLock> generateMaintenanceLocks() {
092        List<MaintenanceLock> maintenanceLocks = super.generateMaintenanceLocks();
093
094        BulkUpdateMaintenanceDataObject dataObject = (BulkUpdateMaintenanceDataObject) getDataObject();
095
096        for (Object targetUpdateDataObjects : dataObject.getTargetUpdateDataObjects()) {
097            String documentTypeName = getDocumentDictionaryService().getMaintenanceDocumentTypeName(targetUpdateDataObjects.getClass());
098            maintenanceLocks.addAll(generateMaintenanceLocks(getDocumentNumber(), documentTypeName, targetUpdateDataObjects.getClass(), targetUpdateDataObjects));
099        }
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}