TimeFinder.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package com.borland.training.meetings.sessions;
- import java.util.*;
- import javax.ejb.*;
- import javax.naming.*;
- import com.borland.training.meetings.entities.*;
- public class TimeFinder {
- private static TimeFinder instance = null;
- private EJBHomeFactory homeFactory;
- private TimeFinder() throws NamingException {
- homeFactory = EJBHomeFactory.getInstance();
- }
- public static TimeFinder getInstance() throws NamingException {
- if(instance == null) {
- instance = new TimeFinder();
- }
- return instance;
- }
- public TimePeriod firstAvailableTime(Collection attendees, java.util.Date earliestTime)
- throws FinderException, NamingException {
- Collection meetings = null;
- int defaultInterval = 1;
- long maxAttempts = 10;
- int attempt = 0; // number of times the earliestTime was shifted to later
- TimePeriod result = new TimePeriod();
- MeetingHome meetingHome = (MeetingHome) homeFactory.lookupByLocalEJBReference("Meeting");
- boolean conflictFound = true;
- System.out.println("Entering the main loop");
- earliestSearch:
- while(attempt++ < maxAttempts && conflictFound) {
- conflictFound = false;
- System.out.println("Looking for meetings in progress at: " + earliestTime);
- // find all meetings that will be in progress at earliestTime
- meetings = meetingHome.findByTime(
- new java.sql.Timestamp(earliestTime.getTime()),
- new java.sql.Timestamp(earliestTime.getTime()));
- // for each meeting
- Iterator miter = meetings.iterator();
- while(miter.hasNext()) {
- Meeting meeting = (Meeting) miter.next();
- // for each attendee within each meeting
- System.out.println("Checking meeting: " + meeting.getId() +
- ", " + meeting.getTopic() + ", " + meeting.getStartTime() +
- ", " + meeting.getFinishTime());
- Iterator aiter = meeting.getAttendees().iterator();
- while(aiter.hasNext()) {
- Attendee attendee = (Attendee) aiter.next();
- System.out.println("tChecking attendee: " + attendee.getName());
- // if meeting is scheduled for one of the specified attendees
- if(attendees.contains(attendee)) {
- System.out.println("tConflict discovered for attendee: " + attendee.getName());
- // select a new earliest time (= when this meeting finishes)
- earliestTime = meeting.getFinishTime();
- conflictFound = true;
- continue earliestSearch;
- }
- }
- }
- }
- if(conflictFound) {
- System.out.println("No available time period");
- // No available time period
- return null;
- }
- // earliestTime found
- result.setStartTime(earliestTime);
- Calendar cal = Calendar.getInstance();
- cal.setTime(earliestTime);
- cal.add(Calendar.HOUR, defaultInterval);
- result.setFinishTime(cal.getTime());
- System.out.println("Proposed time period: " + result.getStartTime() + ", " + result.getFinishTime());
- // now search for timeInterval
- System.out.println("Checking if endTime conflicts with any meetings");
- meetings = meetingHome.findByTime(
- new java.sql.Timestamp(result.getStartTime().getTime()),
- new java.sql.Timestamp(result.getFinishTime().getTime()));
- Iterator miter = meetings.iterator();
- // find earliest starting time amongst pertinent meetings
- while(miter.hasNext()) {
- Meeting meeting = (Meeting)
- javax.rmi.PortableRemoteObject.narrow(miter.next(), Meeting.class);
- System.out.println("Checking meeting: " + meeting.getId() +
- ", " + meeting.getTopic() + ", " + meeting.getStartTime() +
- ", " + meeting.getFinishTime());
- Iterator aiter = meeting.getAttendees().iterator();
- while(aiter.hasNext()) {
- Attendee attendee = (Attendee) aiter.next();
- // if meeting is scheduled to include one of the attendees
- System.out.println("tChecking attendee: " + attendee.getName());
- if (attendees.contains(attendee)) {
- System.out.println("tAttendee in the list");
- java.util.Date startTime = meeting.getStartTime();
- Calendar calStart = Calendar.getInstance();
- calStart.setTime(startTime);
- Calendar calEnd = Calendar.getInstance();
- calEnd.setTime(result.getFinishTime());
- if(calStart.before(calEnd)) {
- result.setFinishTime(startTime);
- }
- System.out.println("tSetting endTime to: " + result.getFinishTime());
- }
- }
- }
- return result;
- }
- }