/*
 * Copyright 2005-2010 The Kuali Foundation
 * 
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.kra.bo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.kuali.kra.infrastructure.KraServiceLocator;
import org.kuali.kra.maintenance.KraMaintainableImpl;
import org.kuali.kra.service.UnitService;
import org.kuali.rice.kns.document.MaintenanceDocument;
import org.kuali.rice.kns.maintenance.Maintainable;
import org.kuali.rice.krad.service.BusinessObjectService;

public class UnitMaintainableImpl extends KraMaintainableImpl implements Maintainable {


    @SuppressWarnings("deprecation")
    @Override
    public void prepareForSave() {
        super.prepareForSave();

    }

    @SuppressWarnings("deprecation")
    @Override
    public void deleteBusinessObject() {
        super.deleteBusinessObject();
/*        Unit newUnit = (Unit) this.getBusinessObject();
        UnitService unitService = KraServiceLocator.getService(UnitService.class);
        BusinessObjectService boService = KraServiceLocator.getService(BusinessObjectService.class);
        if(isOldBusinessObjectInDocument()) {
            Unit oldUnit = boService.findBySinglePrimaryKey(Unit.class, newUnit.getUnitNumber());
            if(oldUnit != null) {
                List<UnitAdministrator> administrators = unitService.retrieveUnitAdministratorsByUnitNumber(oldUnit.getUnitNumber());
                for(UnitAdministrator admin : administrators) {
                    admin.setUnitNumber(newUnit.getUnitNumber());
                    boService.save(admin);
                }
            }
        }*/
    }

    /*
     * this is to prevent Unit Admins from being deleted when modifying an editable portion of the Unit.
     * see KRACOEUS-6830
     * Setting auto-delete="none" and auto-update="none/link" did not remedy this...
     */
    @Override
    public void saveBusinessObject() {
        Unit newUnit = (Unit) this.getBusinessObject();
        UnitService unitService = KraServiceLocator.getService(UnitService.class);
        BusinessObjectService boService = KraServiceLocator.getService(BusinessObjectService.class);
        List<UnitAdministrator> administrators = new ArrayList<UnitAdministrator>();
        boolean deactivating = false;
        
        if(isOldBusinessObjectInDocument()) {
            Unit oldUnit = boService.findBySinglePrimaryKey(Unit.class, newUnit.getUnitNumber());
            if(oldUnit != null) {
                if(oldUnit.isActive() && newUnit.isActive()) {
                    administrators.addAll(unitService.retrieveUnitAdministratorsByUnitNumber(oldUnit.getUnitNumber()));
                }
                //Deletion is (somewhat) understandable if the unit is being inactivated.
            }
        }
        
        super.saveBusinessObject();
        
        //re-add the deleted admins if the record remains active.
        for(UnitAdministrator admin : administrators) {
            admin.setUnitNumber(newUnit.getUnitNumber());
            boService.save(admin);
        }
    }

}

@@ -1159,1070 +1159,8 @@
 %0D%0A%0D%0A
-%0D%0A    @SuppressWarnings(%22deprecation%22)%0D%0A    @Override%0D%0A    public void prepareForSave() %7B%0D%0A        super.prepareForSave();%0D%0A%0D%0A    %7D%0D%0A%0D%0A    @SuppressWarnings(%22deprecation%22)%0D%0A    @Override%0D%0A    public void deleteBusinessObject() %7B%0D%0A        super.deleteBusinessObject();%0D%0A/*        Unit newUnit = (Unit) this.getBusinessObject();%0D%0A        UnitService unitService = KraServiceLocator.getService(UnitService.class);%0D%0A        BusinessObjectService boService = KraServiceLocator.getService(BusinessObjectService.class);%0D%0A        if(isOldBusinessObjectInDocument()) %7B%0D%0A            Unit oldUnit = boService.findBySinglePrimaryKey(Unit.class, newUnit.getUnitNumber());%0D%0A            if(oldUnit != null) %7B%0D%0A                List%3CUnitAdministrator%3E administrators = unitService.retrieveUnitAdministratorsByUnitNumber(oldUnit.getUnitNumber());%0D%0A                for(UnitAdministrator admin : administrators) %7B%0D%0A                    admin.setUnitNumber(newUnit.getUnitNumber());%0D%0A                    boService.save(admin);%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D*/%0D%0A    %7D%0D%0A%0D%0A
     
@@ -1266,16 +1266,23 @@
 the Unit
+ (NAME)
 .%0D%0A     

remove unused overrides