Enterprise Java and UML 2nd Edition PHẦN 4 docx

10 279 0
Enterprise Java and UML 2nd Edition PHẦN 4 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

{ /** Answers a reference to the newly created Activity bean. */ public RecordTimeWorkflow create(String username) throws RemoteException, CreateException; } RecordTimeWorkflowBean.java RecordTimeWorkflowBean.java is the implementation class for the RecordTimeWork- flow session bean. Most of this code should be somewhere between familiar and monotonous, by this point. However, there is one new wrinkle, as the ejbCreate method finds a User entity bean based on the username parameter. This bean reference is kept for the duration of the stateful session. The RecordTimeWorkflow session bean wraps the data for a timecard into a custom TimecardDTO object. package com.wiley.compBooks.EJwithUML.TimeCardWorkflow; import com.wiley.compBooks.EJwithUML.Dtos.*; import com.wiley.compBooks.EJwithUML.TimeCardDomain.*; import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*; import com.wiley.compBooks.EJwithUML.Base.DateUtil; import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*; import java.util.*; import java.rmi.*; import javax.ejb.*; import javax.naming.*; /** * The RecordTimeWorkflow allows client objects to record their time. * RecordTimeWorkflowBean is the actual session bean implementation. */ public class RecordTimeWorkflowBean extends BasicSessionBean { private UserLocal user; public void ejbCreate(String username) throws CreateException { try { System.out.println(“creating RecordTimeWorkflowBean with user -” Æ + username); Context initialContext = getInitialContext(); UserLocalHome userHome = (UserLocalHome)initialContext.lookup( EjbReferenceNames. Æ USER_HOME); Collection users = userHome.findByUserName(username); Iterator userIterator = users.iterator(); 30 RecordTimeWorkflowSessionBean 267783 WS06.qxd 5/5/03 9:18 AM Page 30 if (userIterator.hasNext()) { this.user = (UserLocal) userIterator.next(); } System.out.println(“done creating RecordTimeWorkflowBean with user -” + username); } catch (NamingException e) { throw new CreateException(“User Bean Not Found”); } catch (FinderException e) { throw new CreateException(“User(“ + username + “) Not Found”); } } public void ejbPostCreate(String username) { } /** Answers the current timecard. If first time, creates one. */ public TimecardDTO getCurrentTimecard() throws DataCreateException, FatalApplicationException, RemoteException { try { System.out.println(“in getCurrentTimecard().”); TimecardLocal timecard = user.getCurrentTimecard(); if (timecard == null) { // creates for the first time timecard = createTimecard(timecard); } TimecardDTO tcDTO = new TimecardDTO(new Date(timecard.getStartDate()), new Date(timecard.getEndDate())); Collection timeEntries = timecard.getTimeEntries(); Iterator timeEntryIterator = timeEntries.iterator(); System.out.println(“total entries#” + timeEntries.size()); while(timeEntryIterator.hasNext()) { TimeEntryLocal timeEntry = (TimeEntryLocal) Æ timeEntryIterator.next(); ChargeCodeLocal chargeCode = (ChargeCodeLocal) Æ timeEntry.getChargeCode(); TimeEntryDTO timeEntryDTO = new TimeEntryDTO( ((TimeEntryPK)timeEntry.getPrimaryKey()).id, chargeCode.getProject().getClient().getName(), chargeCode.getName(), chargeCode.getProject().getName(), RecordTimeWorkflowSessionBean 31 267783 WS06.qxd 5/5/03 9:18 AM Page 31 timeEntry.getHours(), timeEntry.getDate()); tcDTO.addEntry(timeEntryDTO); } System.out.println(tcDTO); return tcDTO; } catch(InvalidDataException e) { throw new DataCreateException(Origin.TIMECARD_WORKFLOW, e, “failed to retrieve current Æ timecard.”); } } /** Marks the current timecard as closed and creates a new one. */ public void submitTimecard() throws DataCreateException, FatalApplicationException, RemoteException { System.out.println(“in submitTimecard().”); TimecardLocal currentTimecard = user.getCurrentTimecard(); createTimecard(currentTimecard); } /** Adds/Updates/modifies the entries of the current timecard. */ public void updateTimecard(TimecardDTO tcDTO) throws Æ DataUpdateException, DataNotFoundException, Æ FatalApplicationException { try { System.out.println(“in getUpdateTimecard().”); Context initialContext = getInitialContext(); TimeEntryLocalHome tehome = Æ (TimeEntryLocalHome)initialContext.lookup( EjbReferenceNames. Æ TIME_ENTRY_HOME); ChargeCodeLocalHome cchome = (ChargeCodeLocalHome) Æ initialContext.lookup( EjbReferenceNames. Æ CHARGECODE_HOME); Iterator teIterator = tcDTO.getEntries(); System.out.println(“total entries#” + tcDTO.getTotalEntries()); while(teIterator.hasNext()) { TimeEntryDTO entryDTO = (TimeEntryDTO)teIterator.next(); TimeEntryLocal tentry = null; ChargeCodeLocal ccode = null; Collection ccodes = cchome.findByName(entryDTO. Æ getProjectName(), 32 RecordTimeWorkflowSessionBean 267783 WS06.qxd 5/5/03 9:18 AM Page 32 entryDTO. Æ getChargeCodeName()); if (ccodes.size() == 1) { // we have single hit ccode = (ChargeCodeLocal) ccodes.iterator().next(); } else { //we have multiple hits; we need to narrow it down to 1 Iterator ccIterator = ccodes.iterator(); while(ccIterator.hasNext()) { ChargeCodeLocal nextCcode = (ChargeCodeLocal) Æ ccIterator.next(); if (nextCcode.getProject().getClient().getName().equals( entryDTO.getClientName())) { ccode = nextCcode; break; } } if (ccode == null) { System.out.println(“could not find the charge code. Æ cannot update/create - “ + entryDTO); continue; } } if (entryDTO.isUpdated()) { try { tentry = (TimeEntryLocal) tehome.findByPrimaryKey(new TimeEntryPK Æ (entryDTO.getId())); /* makes sure that the entry belongs to the right timecard*/ if (!tentry.getTimecard().getPrimaryKey().equals( user.getCurrentTimecard().getPrimaryKey())) { throw new DataUpdateException(Origin.TIMECARD_WORKFLOW, “Duplicate Time Entry-” + Æ entryDTO.toString()); } System.out.println(“updating entry-” + entryDTO); tentry.setDate(entryDTO.getDate()); tentry.setHours(entryDTO.getHours()); tentry.setChargeCode(ccode); } RecordTimeWorkflowSessionBean 33 267783 WS06.qxd 5/5/03 9:18 AM Page 33 catch(FinderException exception) { throw new DataUpdateException(Origin.TIMECARD_WORKFLOW, “Marked as an update, but no entry found-” + Æ entryDTO.toString()); } } else if (entryDTO.isNew()) { // new entry- we need to add it; new entry has partial key; we // need to add the timecard id to it. see TimeEntryDTO for // more details. String newId =((TimecardPK)user.getCurrentTimecard Æ ().getPrimaryKey()).id + “-” + entryDTO.getId(); System.out.println(“adding a new entry-” + entryDTO); System.out.println(“actual id=” + newId); tehome.create(newId,entryDTO.getDate(), entryDTO.getHours(), ccode, user.getCurrentTimecard()); } } System.out.println(“total entries#” + user.getCurrentTimecard(). Æ getTimeEntries().size()); System.out.println(“done getUpdateTimecard().”); } catch(NamingException exception) { // invalid charge code throw new FatalApplicationException(Origin.TIMECARD_WORKFLOW, Æ exception, “ChargeCode/TimeEntry Bean Not Æ Found”); } catch(FinderException exception) { // invalid charge code throw new DataNotFoundException(Origin.TIMECARD_WORKFLOW, exception, “invalid charge code, failed to update Æ timecard.” ); } catch (CreateException e) { throw new DataUpdateException(Origin.TIMECARD_WORKFLOW, e, “Failed to update timecard.”); } } /** 34 RecordTimeWorkflowSessionBean 267783 WS06.qxd 5/5/03 9:18 AM Page 34 * Answers a ClientDTO with client related information- projects and * charge codes. */ public ClientDTO getClient(String clientName) throws Æ DataNotFoundException, Æ FatalApplicationException { try { System.out.println(“in getClient().”); Context initialContext = getInitialContext(); ClientLocalHome chome = (ClientLocalHome)initialContext.lookup( Æ EjbReferenceNames.CLIENT_HOME); Collection clients = chome.findByName(clientName); Iterator clientIterator = clients.iterator(); ClientLocal client = null; if (clientIterator.hasNext()) { //should be unique; so we only get to look for one. client = (ClientLocal) clientIterator.next(); } ClientDTO cDTO = new ClientDTO(client.getName()); extractClientInfo(client, cDTO); System.out.println(“done getClient().”); return cDTO; } catch (NamingException exception) { // invalid charge code throw new FatalApplicationException(Origin.TIMECARD_WORKFLOW, Æ exception, “Client Bean Not Found”); } catch (FinderException exception) { throw new DataNotFoundException(Origin.TIMECARD_WORKFLOW, Æ exception, “client(“ + clientName + “) not found.” ); } } /** * Answers a Collection of ClientDTOs with client related information- * projects and charge codes. */ public Collection getAllClients() throws DataNotFoundException, FatalApplicationException, Æ RemoteException RecordTimeWorkflowSessionBean 35 267783 WS06.qxd 5/5/03 9:18 AM Page 35 { try { System.out.println(“in getAllClients().”); Context initialContext = getInitialContext(); ClientLocalHome chome = (ClientLocalHome)initialContext.lookup( Æ EjbReferenceNames.CLIENT_HOME); Collection clients = chome.findAll(); Iterator clientIterator = clients.iterator(); Collection clientDtos = new ArrayList(); ClientLocal client = null; while(clientIterator.hasNext()) { client = (ClientLocal) clientIterator.next(); ClientDTO cDTO = new ClientDTO(client.getName()); extractClientInfo(client, cDTO); clientDtos.add(cDTO); } System.out.println(“done getAllClients().”); return clientDtos; } catch (NamingException exception) { // invalid charge code throw new FatalApplicationException(Origin.TIMECARD_WORKFLOW, Æ exception, “Client Bean Not Found”); } catch (FinderException exception) { throw new DataNotFoundException(Origin.TIMECARD_WORKFLOW, Æ exception, “no client found.” ); } } private void extractClientInfo(ClientLocal client, ClientDTO cDTO) { System.out.println(“in extractClientInfo().”); Collection projects = client.getProjects(); Iterator projectIterator = projects.iterator(); while(projectIterator.hasNext()) { ProjectLocal project = (ProjectLocal) projectIterator.next(); ProjectDTO pDTO = new ProjectDTO(project.getName()); Collection chargeCodes = project.getChargeCodes(); Iterator ccIterator = chargeCodes.iterator(); while(ccIterator.hasNext()) { 36 RecordTimeWorkflowSessionBean 267783 WS06.qxd 5/5/03 9:18 AM Page 36 ChargeCodeLocal chargeCode = (ChargeCodeLocal) Æ ccIterator.next(); pDTO.addChargeCode(chargeCode.getName()); } cDTO.addProject(pDTO); System.out.println(“done extractClientInfo().”); } } private TimecardLocal createTimecard(TimecardLocal currentTimecard) throws DataCreateException, FatalApplicationException, Æ RemoteException { try { System.out.println(“creating a new Timecard.”); /* * closes the current timecard by creating a new one. * timecard id = userid + “-”+ year+ “-”+ “week”; * Timecard starts on Monday and ends on Sunday. Duration of each Æ timecard * is 7 days. */ Date startDate = null; if (currentTimecard == null) { // first timecard startDate = DateUtil.getCurrentWeekMonday(new Date()); } else { startDate = DateUtil.getNextWeekMonday(new Æ Date(currentTimecard.getStartDate())); } String id = ((UserPK)user.getPrimaryKey()).id + “-” + DateUtil.getCurrentYear(startDate) + “-” + DateUtil.getCurrentWeek(startDate); Date endDate = DateUtil.getCurrentWeekSunday(startDate); System.out.println(“startDate= “ + Æ DateUtil.toDateString(startDate) + “ endDate=” + DateUtil.toDateString(endDate)); Context initialContext = getInitialContext(); TimecardLocalHome tchome = (TimecardLocalHome) Æ initialContext.lookup( EjbReferenceNames. Æ TIMECARD_HOME); TimecardLocal nextTimecard = tchome.create(id, startDate, Æ endDate, true, user); //makes the current timecard to be not current, and makes the new RecordTimeWorkflowSessionBean 37 267783 WS06.qxd 5/5/03 9:18 AM Page 37 //one to be current. if(user.getCurrentTimecard()!= null) { user.getCurrentTimecard().setIsCurrent(false); } user.setCurrentTimecard(nextTimecard); System.out.println(“done creating a new Timecard.”); return nextTimecard; } catch (NamingException e) { throw new FatalApplicationException(Origin.TIMECARD_WORKFLOW, e, “Timecard Bean Not Found”); } catch (CreateException e) { throw new DataCreateException(Origin.TIMECARD_WORKFLOW, e, “Failed to create timecard.”); } } } 38 RecordTimeWorkflowSessionBean 267783 WS06.qxd 5/5/03 9:18 AM Page 38 39 SelectChargeCodeServlet.java The SelectChargeCodeServlet uses the RecordTimeWorkflow to get a collection of charge codes that are valid for the system. It uses TablePrimitive, TextPrimitive, and LinkPrimitive to build a list of charge codes with an associated “Add” link for each charge code. When the user clicks on an “Add” link, RecordTimeServlet uses the request parameters to identify the correct charge code to add to the timecard. package com.wiley.compBooks.EJwithUML.TimecardUI; import javax.servlet.http.*; import javax.servlet.*; import javax.naming.*; import javax.ejb.*; import java.rmi.*; import javax.rmi.PortableRemoteObject; import java.io.*; import java.util.*; import java.text.*; import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.Core.*; SelectChargeCodeServletjava 267783 WS07.qxd 5/5/03 9:18 AM Page 39 . com.wiley.compBooks.EJwithUML.TimecardUI; import javax.servlet.http.*; import javax.servlet.*; import javax.naming.*; import javax.ejb.*; import java. rmi.*; import javax.rmi.PortableRemoteObject; import java. io.*; import. com.wiley.compBooks.EJwithUML.Base.DateUtil; import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*; import java. util.*; import java. rmi.*; import javax.ejb.*; import javax.naming.*; /** * The RecordTimeWorkflow. com.wiley.compBooks.EJwithUML.TimeCardWorkflow; import com.wiley.compBooks.EJwithUML.Dtos.*; import com.wiley.compBooks.EJwithUML.TimeCardDomain.*; import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*; import

Ngày đăng: 12/08/2014, 16:21

Từ khóa liên quan

Mục lục

  • Enterprise Java and UML, 2nd Edition

    • SelectChargeCodeServletjava

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan