⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 allocationchangefinder.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.facade.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.rapla.entities.RaplaObject;
import org.rapla.entities.RaplaType;
import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Reservation;
import org.rapla.facade.AllocationChangeEvent;
import org.rapla.storage.UpdateResult;

/** listens for allocation changes.
    Collects AllocationChangeEvents.
 */
class AllocationChangeFinder extends AbstractLogEnabled
{
    ArrayList changeList = new ArrayList();
    UpdateResult updateResult;

    AllocationChangeFinder(Logger logger, UpdateResult updateResult) {
        enableLogging( logger );
        if ( updateResult == null)
            return;
        User user = updateResult.getUser();
        for (Iterator it = updateResult.getOperations( UpdateResult.Add.class );it.hasNext();) {
            UpdateResult.Add addOp = (UpdateResult.Add) it.next();
            added( (RaplaObject) addOp.getNew(), user );
        }
        for (Iterator it = updateResult.getOperations( UpdateResult.Remove.class );it.hasNext();) {
            UpdateResult.Remove removeOp = (UpdateResult.Remove) it.next();
            removed( (RaplaObject) removeOp.getCurrent(), user );
        }
        for (Iterator it = updateResult.getOperations( UpdateResult.Change.class );it.hasNext();) {
            UpdateResult.Change changeOp = (UpdateResult.Change) it.next();
            RaplaObject old = (RaplaObject) changeOp.getOld();
            RaplaObject newObj =(RaplaObject) changeOp.getNew();
            changed(old , newObj, user );
        }
    }

    public List getTriggerEvents() {
        return changeList;
    }

    private void added(RaplaObject entity, User user) {
        RaplaType raplaType = entity.getRaplaType();
        if ( raplaType.is( Reservation.TYPE )) {
            Reservation newRes = (Reservation) entity;
            addAppointmentAdd(
                              user
                              ,newRes
                              ,Arrays.asList(newRes.getAllocatables())
                              ,Arrays.asList(newRes.getAppointments())
                              );
        }
    }

    private void removed(RaplaObject entity,User user) {
        RaplaType raplaType = entity.getRaplaType();
        if ( raplaType.is( Reservation.TYPE )) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Reservation removed: " + entity);
            Reservation oldRes = (Reservation) entity;
            addAppointmentRemove(
                                 user
                                 ,oldRes
                                 ,oldRes
                                 ,Arrays.asList(oldRes.getAllocatables())
                                 ,Arrays.asList(oldRes.getAppointments())
                                );
        }
    }

    private void changed(RaplaObject oldEntity,RaplaObject newEntity, User user) {
        RaplaType raplaType = oldEntity.getRaplaType();
        if (raplaType.is( Reservation.TYPE )) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Reservation changed: " + oldEntity);
            Reservation oldRes = (Reservation) oldEntity;
            Reservation newRes = (Reservation) newEntity;
            List alloc1 = Arrays.asList(oldRes.getAllocatables());
            List alloc2 = Arrays.asList(newRes.getAllocatables());
            List app1 = Arrays.asList(oldRes.getAppointments());
            List app2 = Arrays.asList(newRes.getAppointments());

            ArrayList removeList = new ArrayList(alloc1);
            removeList.removeAll(alloc2);
            // add removed allocations to the change list
            addAppointmentRemove(user, oldRes,newRes, removeList, app1);

            ArrayList addList = new ArrayList(alloc2);
            addList.removeAll(alloc1);
            // add new allocations to the change list
            addAppointmentAdd(user, newRes, addList, app2);

            ArrayList changeList = new ArrayList(alloc2);
            changeList.retainAll(alloc1);
            addAllocationDiff(user, changeList,oldRes,newRes);
        }
        if ( Appointment.TYPE.equals( raplaType )) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Appointment changed: " + oldEntity + " to " + newEntity);
        }
    }

    /*
    private void printList(List list) {
        Iterator it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
    */

    /**
     * Calculates the allocations that have changed
     */
    private void addAllocationDiff(User user,List allocatableList,Reservation oldRes,Reservation newRes) {
        List app1 = Arrays.asList(oldRes.getAppointments());
        List app2 = Arrays.asList(newRes.getAppointments());
        ArrayList removeList = new ArrayList(app1);
        removeList.removeAll(app2);
        addAppointmentRemove(user, oldRes,newRes,allocatableList,removeList);
        ArrayList addList = new ArrayList(app2);
        addList.removeAll(app1);
        addAppointmentAdd(user, newRes,allocatableList,addList);
        /*
        System.out.println("OLD appointments");
        printList(app1);
        System.out.println("NEW appointments");
        printList(app2);
        */
        ArrayList newList = new ArrayList(app2);
        newList.retainAll(app1);
        Collections.sort(newList);

        ArrayList oldList = new ArrayList(app1);
        oldList.retainAll(app2);
        Collections.sort(oldList);

        for (int i=0;i<oldList.size();i++) {
            Appointment oldApp = (Appointment) oldList.get(i);
            Appointment newApp = (Appointment) newList.get(i);
            Iterator it = allocatableList.iterator();
            while ( it.hasNext() ) {
                Allocatable allocatable = (Allocatable) it.next();
                boolean oldAllocated = oldRes.hasAllocated(allocatable, oldApp);
                boolean newAllocated = newRes.hasAllocated(allocatable, newApp);
                if (!oldAllocated && !newAllocated) {
                    continue;
                }
                else if (!oldAllocated && newAllocated)
                {
                    changeList.add(new AllocationChangeEvent(AllocationChangeEvent.ADD,user,newRes,allocatable,newApp));
                }
                else if (oldAllocated && !newAllocated)
                {
                    changeList.add(new AllocationChangeEvent(AllocationChangeEvent.REMOVE,user,newRes,allocatable,newApp));
                }
                else if (!newApp.matches(oldApp))
                {
                    getLogger().debug("\n" + newApp + " doesn't match \n" + oldApp);
                    changeList.add(new AllocationChangeEvent(user,newRes,allocatable,newApp,oldApp));
                }
            }
        }
    }

    private void addAppointmentAdd(User user,Reservation newRes,List allocatables,List appointments) {
        Iterator it = allocatables.iterator();
        while ( it.hasNext()) {
            Allocatable allocatable = (Allocatable) it.next();
            Iterator it2 = appointments.iterator();
            while ( it2.hasNext()) {
                Appointment appointment = (Appointment) it2.next();
                if (!newRes.hasAllocated(allocatable,appointment))
                    continue;

                changeList.add(new AllocationChangeEvent(AllocationChangeEvent.ADD,user, newRes,allocatable,appointment));
            }
        }
    }

    private void addAppointmentRemove(User user,Reservation oldRes,Reservation newRes,List allocatables,List appointments) {
        Iterator it = allocatables.iterator();
        while ( it.hasNext()) {
            Allocatable allocatable = (Allocatable) it.next();
            Iterator it2 = appointments.iterator();
            while ( it2.hasNext()) {
                Appointment appointment = (Appointment) it2.next();
                if (!oldRes.hasAllocated(allocatable,appointment))
                    continue;

                changeList.add(new AllocationChangeEvent(AllocationChangeEvent.REMOVE,user, newRes,allocatable,appointment));
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -