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

📄 conflictfinder.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.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import org.rapla.entities.User;
import org.rapla.entities.domain.Allocatable;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.Permission;
import org.rapla.entities.domain.Reservation;
import org.rapla.facade.Conflict;
import org.rapla.framework.RaplaException;
import org.rapla.storage.StorageOperator;

public class ConflictFinder {
    StorageOperator operator;

    public ConflictFinder( StorageOperator operator ) {
        this.operator = operator;
    }

    public Conflict[] getConflicts(Date startDate, Reservation reservation) throws RaplaException {
        synchronized (operator.getLock()) {
            ArrayList conflictList = new ArrayList();
            Appointment[] appointments = reservation.getAppointments();
            for (int i=0;i<appointments.length;i++) {
                Iterator it = getOverlappingAppointments(appointments[i]).iterator();
                while (it.hasNext()) {
                    Appointment overlappingAppointment = (Appointment) it.next();
                    addConflicts(conflictList,appointments[i],overlappingAppointment, startDate);
                }
            }
            return (Conflict[]) conflictList.toArray(Conflict.CONFLICT_ARRAY);
        }
    }

    public Conflict [] getConflicts(Date startDate, User user) throws RaplaException {
        synchronized (operator.getLock()) {
            ArrayList conflictList = new ArrayList();
            // The admin should see all Conflicts
            SortedSet allAppointments = operator.getAppointments
                (
                 null
                 ,startDate
                 ,null
                 );

            SortedSet ownAppointments = operator.getAppointments
                (
                  user
                 ,startDate
                 ,null
                 );

            Iterator it = ownAppointments.iterator();
            while (it.hasNext()) {
                Appointment appointment = (Appointment) it.next();
                Date end = appointment.getMaxEnd();
                Iterator  it2 = allAppointments.tailSet(new Date(appointment.getStart().getTime() - 1)).iterator();
                while (it2.hasNext()) {
                    Appointment appointment2 = (Appointment) it2.next();
                    if (end != null && appointment2.getStart().after(end))
                        break;
                    if (appointment2.overlaps(appointment)) {
                        addConflicts(conflictList,appointment,appointment2, startDate);
                    }

                }
            }
            return (Conflict[]) conflictList.toArray(Conflict.CONFLICT_ARRAY);
        }
    }

    public Set getAllocatableBindings(Appointment forAppointment) throws RaplaException {
        Set allocatableSet = new HashSet();

        synchronized (operator.getLock()) {
            Iterator it = getOverlappingAppointments( forAppointment ).iterator();
            while (it.hasNext()) {
                Appointment appointment = (Appointment) it.next();
                Reservation reservation = appointment.getReservation();
                Allocatable[] allocatables = reservation.getAllocatables();
                for (int i=0;i<allocatables.length;i++) {
                    if (allocatableSet.contains(allocatables[i]))
                        continue;
                    if ( reservation.hasAllocated( allocatables[i], appointment ) )
                        allocatableSet.add( allocatables[i] );
                }
            }
            return allocatableSet;
        }
    }

    public boolean hasPermissionToAllocate( User user, Appointment appointment,Allocatable allocatable, Reservation original) {
        if ( user.isAdmin()) {
            return true;
        }
        Date start = appointment.getStart();
        Date end = appointment.getMaxEnd();
        Date today = operator.today();
        Permission[] permissions = allocatable.getPermissions();
        for ( int i = 0; i < permissions.length; i++) {
            Permission p = permissions[i];
            int accessLevel = p.getAccessLevel();
            if ( (!p.affectsUser( user )) ||  accessLevel< Permission.ALLOCATE) {
                continue;
            }
            if ( accessLevel ==  Permission.ADMIN ||
                 p.covers( start, end, today ) ) {
                // user has the right to allocate
                return true;
            }
            if ( original == null ) {
                continue;
            }

            // We must check if the changes of the existing appointment
            // are in a permisable timeframe (That should be allowed)

            // 1. check if appointment is old,
            // 2. check if allocatable was already assigned to the appointment
            Appointment originalAppointment = original.findAppointment( appointment );
            if ( originalAppointment == null
                 || !original.hasAllocated( allocatable, originalAppointment)
                 ) {
                continue;
            }

            // 3. check if the appointment has changed during
            // that time
            if ( appointment.matches( originalAppointment ) ) {
                return true;
            }

            Date maxTime =  p.getMaxAllowed( today );
            if (maxTime == null)
                maxTime = p.getMinAllowed( today);

            Date minChange =
                appointment.getFirstDifference( originalAppointment, maxTime );
            Date maxChange =
                appointment.getLastDifference( originalAppointment, maxTime );
            //System.out.println ( "minChange: " + minChange + ", maxChange: " + maxChange );

            if ( p.covers( minChange, maxChange, today ) ) {
                return true;
            }
        }
        return false;
    }

    private Collection getOverlappingAppointments( Appointment appointment) throws RaplaException {
        ArrayList appointmentList = new ArrayList();
        Iterator it = operator.getAppointments
            (
             null
             ,appointment.getStart()
             ,appointment.getMaxEnd()
             ).iterator();

        while (it.hasNext()) {
            Appointment appointment2 = (Appointment) it.next();
            if ( appointment.getReservation() != null && appointment.getReservation().isIdentical(appointment2.getReservation()))
                continue;
            //          System.out.println("Testing " + appointment);
            //          System.out.println(" for overlap with " + appointments[i]);
            if (appointment.overlaps(appointment2)) {
                appointmentList.add(appointment2);
                //                 System.out.println(" Appointment overlaps");
            }
        }
        return appointmentList;
    }


    private Conflict createConflict(Appointment a1,
                                    Appointment a2,Allocatable allocatable) {

        return new ConflictImpl(a1.getReservation()
                            ,a1
                            ,allocatable
                            ,a2.getReservation()
                            ,a2.getReservation().getOwner()
                            ,a2);
    }


    private boolean isConflict(Appointment a1,Appointment a2,Allocatable allocatable, Date startDate) {
        if (allocatable.isHoldBackConflicts())
            return false;
        if ( startDate != null) {
            if (a1.getMaxEnd() != null && startDate.after(a1.getMaxEnd()))
                    return false;
            if (a2.getMaxEnd() != null && startDate.after(a2.getMaxEnd()))
                    return false;
        }
        if ( a1.getReservation().hasAllocated( allocatable, a1 )
              && a2.getReservation().hasAllocated( allocatable, a2 )
              )
            return true;
        else
            return false;
    }

    private void addConflicts(List conflictList,Appointment appointment1,Appointment appointment2, Date startDate) {
        Reservation reservation = appointment1.getReservation();
        Reservation overlappingReservation = appointment2.getReservation();
        if (reservation.equals(overlappingReservation))
            return;
        Allocatable[] all= reservation.getAllocatables();
        Allocatable[] all2 = overlappingReservation.getAllocatables();
        for (int j=0;j<all.length;j++) {
            if (contains(all2,all[j])
                && isConflict(appointment1,appointment2,all[j], startDate))
            {
                conflictList.add(createConflict(appointment1,appointment2,all[j]));
            }
        }
    }

    private boolean contains(Allocatable[] allocatables,Allocatable allocatable) {
        for (int i=0;i<allocatables.length;i++)
            if (allocatables[i].isIdentical(allocatable))
                return true;
        return false;
    }



}

⌨️ 快捷键说明

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