|
From: Aron S. <bi...@gm...> - 2006-07-08 01:48:13
|
User: asogor
Date: 06/07/07 21:48:10
Added: src/calendar/calendarejb/src/java/org/jboss/mail/calendar/util/availability
AvailabilityCalculator.java
Log:
This is the base for getting suggestion for meeting times
Revision Changes Path
1.1 date: 2006/07/08 01:48:10; author: asogor; state: Exp;jboss-mail/src/calendar/calendarejb/src/java/org/jboss/mail/calendar/util/availability/AvailabilityCalculator.java
Index: AvailabilityCalculator.java
===================================================================
package org.jboss.mail.calendar.util.availability;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.mail.calendar.data.CalendarEvent;
import org.jboss.mail.calendar.data.Invite;
/**
* AvailabilityCalculator is a utility to figure out possible timeslots for a meeting
*
* @author Aron Sogor
*
*/
public class AvailabilityCalculator {
private Log log = LogFactory.getLog(AvailabilityCalculator.class);
/**
* The logic of this calculator is simple:
*
* Create segments using the increment between start date and end date.
* Check which segments are free, meaning there are no meeting during that segment.
* Find numberOfunits continous free segments.
* Return all event proposals.
*
* @param invites all the attendees events
* @param startTime the begining of the search range
* @param endTime the end of the search range
* @param incrementSize the size of an increment in minutes
* @param numberOfunits the number of increments the meeting should last
*/
public static CalendarEvent[] calculate(Invite[] invites,Date startTime,Date endTime,int incrementSize,int numberOfunits)
{
return (new AvailabilityCalculator()).calculateEvents(invites,startTime,endTime,incrementSize,numberOfunits);
}
private CalendarEvent[] calculateEvents(Invite[] invites,Date startTime,Date endTime,int incrementSize,int numberOfunits)
{
ArrayList<CalendarEvent> result = new ArrayList<CalendarEvent>();
if(startTime.after(endTime))
{
log.warn("Availibilty calculator was passed stared and end date reversed");
return new CalendarEvent[0];
}
double segmentTotal = Math.ceil((startTime.getTime() - endTime.getTime())/(60000 * incrementSize));
//generate the segments
Segment firstSegment = setupSegments(startTime,endTime,segmentTotal);
//mark each segment free(true) or busy(false)
//check each segment as valid
return (CalendarEvent[])result.toArray(new CalendarEvent[0]);
}
private Segment setupSegments(Date startTime,Date endTime,double segmentTotal)
{
Segment firstSegment = null;
Segment currentSegment = null;
for (int segmentCnt = 0; segmentCnt < segmentTotal; segmentCnt++) {
if(currentSegment == null)
{
currentSegment.setNextSegment(new Segment(startTime,endTime,true));
firstSegment = currentSegment;
}
else
currentSegment.setNextSegment(new Segment(startTime,endTime,true));
currentSegment = currentSegment.getNextSegment();
}
return firstSegment;
}
private class Segment{
private Date startTime = null;
private Date endTime = null;
private boolean isBusy = false;
private Segment nextSegment = null;
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public boolean isBusy() {
return isBusy;
}
public void setBusy(boolean isBusy) {
this.isBusy = isBusy;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Segment getNextSegment() {
return nextSegment;
}
public void setNextSegment(Segment nextSegment) {
this.nextSegment = nextSegment;
}
public Segment(Date startTime, Date endTime, boolean isBusy) {
super();
// TODO Auto-generated constructor stub
this.startTime = startTime;
this.endTime = endTime;
this.isBusy = isBusy;
}
}
}
|