MeetingManager.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.*;
- class MeetingManager {
- private static MeetingManager instance = null;
- private MeetingValueFactory meetingValueFactory;
- private RoomValueFactory roomValueFactory;
- private AttendeeValueFactory attendeeValueFactory;
- private EJBHomeFactory homeFactory;
- private MeetingManager() throws NamingException {
- homeFactory = EJBHomeFactory.getInstance();
- meetingValueFactory = MeetingValueFactory.getInstance();
- attendeeValueFactory = AttendeeValueFactory.getInstance();
- roomValueFactory = RoomValueFactory.getInstance();
- }
- public static MeetingManager getInstance() throws NamingException {
- if(instance == null) {
- instance = new MeetingManager();
- }
- return instance;
- }
- public Long createMeeting(MeetingValue meetingValue)
- throws FinderException, NamingException {
- try {
- MeetingHome meetingHome =
- (MeetingHome) homeFactory.lookupByLocalEJBReference("Meeting");
- Meeting meeting = meetingHome.create(meetingValue.getTopic(),
- meetingValue.getStartTime(),
- meetingValue.getFinishTime());
- Room room = roomValueFactory.findRoom(meetingValue.getRoomName());
- meeting.setRoom(room);
- Iterator iterator = meetingValue.getAttendeeIds().iterator();
- Long id;
- Attendee attendee = null;
- Collection meetingAttendees = meeting.getAttendees();
- while(iterator.hasNext()) {
- id = (Long) iterator.next();
- attendee = attendeeValueFactory.findAttendee(id);
- meetingAttendees.add(attendee);
- }
- return meeting.getId();
- }
- catch(Exception e) {
- throw new EJBException(e);
- }
- }
- public void updateMeeting(MeetingValue meetingValue)
- throws FinderException, NamingException {
- try {
- Meeting meeting = meetingValueFactory.findMeeting(meetingValue.getId());
- meeting.setTopic(meetingValue.getTopic());
- meeting.setStartTime(meetingValue.getStartTime());
- meeting.setFinishTime(meetingValue.getFinishTime());
- if(!meeting.getRoom().getName().equals(meetingValue.getRoomName())) {
- Room newRoom = roomValueFactory.findRoom(meetingValue.getRoomName());
- meeting.setRoom(newRoom);
- }
- // Map attendee ids to references
- Long id;
- Attendee attendee = null;
- Collection attendeeIds = meetingValue.getAttendeeIds();
- Collection attendees = new Vector(attendeeIds.size());
- Iterator iterator = attendeeIds.iterator();
- while(iterator.hasNext()) {
- id = (Long) iterator.next();
- attendee = attendeeValueFactory.findAttendee(id);
- attendees.add(attendee);
- }
- // Update the attendee list.
- // Note: for large numbers of attendees, it might be more efficient to determine
- // attendees that are beeing removed or added, and remove or add them individually.
- meeting.setAttendees(attendees);
- }
- catch(Exception e) {
- throw new EJBException(e);
- }
- }
- public void removeMeeting(Long id)
- throws FinderException, NamingException {
- try {
- Meeting meeting = meetingValueFactory.findMeeting(id);
- Iterator iterator = meeting.getAttendees().iterator();
- while(iterator.hasNext()) {
- iterator.remove();
- }
- meeting.remove();
- }
- catch(Exception e) {
- throw new EJBException(e);
- }
- }
- public void removeAllMeetings()
- throws FinderException, NamingException {
- try {
- Collection meetings = meetingValueFactory.findAllMeetings();
- Iterator iterator = meetings.iterator();
- Meeting meeting = null;
- while(iterator.hasNext()) {
- meeting = (Meeting) iterator.next();
- meeting.remove();
- }
- }
- catch(Exception e) {
- throw new EJBException(e);
- }
- }
- }