1. Trang chủ
  2. » Công Nghệ Thông Tin

Enterprise Java and UML 2nd Edition PHẦN 3 pot

10 350 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 245,97 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.HtmlPrimi

Trang 1

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.*;

import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.FormPrimitives.*; import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.Layout.*;

import com.wiley.compBooks.EJwithUML.Base.HtmlPrimitives.ContentElements.*; import com.wiley.compBooks.EJwithUML.TimeCardWorkflow.*;

import com.wiley.compBooks.EJwithUML.Dtos.*;

import com.wiley.compBooks.EJwithUML.TimecardProducers.*;

import com.wiley.compBooks.EJwithUML.Base.EjbUtil.*;

import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;

/**

* The RecordTimeServlet uses the TimecardWorkflow and

* HtmlProduction packages to create the formatted HTML

* for the time entry form and to capture the entered hours

*/

public class RecordTimeServlet extends BasicTimecardServlet {

private static final DateFormat longFormat = new SimpleDateFormat Æ (“MMM dd, yyyy”);

private static final DateFormat shortFormat = new Æ SimpleDateFormat(“MMM dd”);

private static final DateFormat keyFormat = new Æ SimpleDateFormat(“MMM.dd.yyyy”);

private static final DateFormat dayOfWeekFormat = new Æ SimpleDateFormat(“EE”);

public void doGet(HttpServletRequest request, HttpServletResponse Æ response) throws ServletException, IOException

{ doPost(request, response);

}

/** Overrides method from HttpServlet doPost is called by the Æ servlet engine */

public void doPost(HttpServletRequest request, HttpServletResponse Æ response) throws ServletException, IOException

{ System.out.println(“RecordTimeServlet: doPost”);

try {

Trang 2

// extract username from session HttpSession session = request.getSession(false);

String username = (String) session.getAttribute(TimecardKey.USERNAME.getKeyText());

// extract parameters String client = request.getParameter(TimecardKey.CLIENT.getKeyText());

String project = request.getParameter(TimecardKey.PROJECT.getKeyText());

String code = request.getParameter(TimecardKey.CODE.getKeyText());

// Get workflow and timecard Context initial = new InitialContext();

Object objref = initial.lookup(EjbReferenceNames.RECORD_TIME_ Æ WORKFLOW_HOME);

RecordTimeWorkflowHome rtwhome = (RecordTimeWorkflowHome) Æ PortableRemoteObject.narrow(objref, RecordTimeWorkflowHome.class);

RecordTimeWorkflow rtwf = rtwhome.create(username);

TimecardDTO timecard = rtwf.getCurrentTimecard();

// update and submit timecard

if (request.getParameterMap().containsKey(TimecardKey.SUBMIT_ Æ TIMECARD.getKeyText()))

{ updateTimecardFromRequest(timecard, request);

rtwf.updateTimecard(timecard);

rtwf.submitTimecard();

}

// save timecard else if

(request.getParameterMap().containsKey(TimecardKey.SAVE_TIMECARD Æ getKeyText()))

{ updateTimecardFromRequest(timecard, request);

rtwf.updateTimecard(timecard);

}

// add charge code

if (client != null && !client.trim().equals(“”)) {

List dates = getDatesInOrder(timecard);

Iterator date_it = dates.iterator();

while (date_it.hasNext()) {

Date current_date = (Date) date_it.next();

TimeEntryDTO te = new TimeEntryDTO(client, code, project, 0, Æ current_date);

timecard.addEntry(te);

Trang 3

}

// build page from timecard timecard = rtwf.getCurrentTimecard();

page = buildEntryPage(timecard, username);

// write page to response StringBuffer buffer = new StringBuffer();

page.buildContent(buffer);

response.getWriter().println(buffer.toString());

response.getWriter().flush();

response.getWriter().close();

} catch (Exception e) {

System.out.println(“RecordTimeServlet e:” +e);

throw new ServletException(e);

} }

private PagePrimitive buildEntryPage(TimecardDTO timecard, String Æ username)

{ PagePrimitive page = new TimecardPageProducer(“Enter Hours”);

// add welcome message String start_date = this.longFormat.format(timecard.getStartDate()); String end_date = this.longFormat.format(timecard.getEndDate()); ParagraphPrimitive welcome = new

ParagraphPrimitive(TimecardStyle.IMPORTANT_TEXT, “Welcome “ Æ +username+ “ Please enter Hours for “ +start_date+ “ to “ +end_date); page.addToBody(welcome);

// add link to select charge code LinkPrimitive select_code_link = new LinkPrimitive(“/Timecard/SelectChargeCodeServlet”);

select_code_link.setStyle(TimecardStyle.IMPORTANT_TEXT);

select_code_link.addText(“Select a Charge Code”);

page.addToBody(select_code_link);

// derive ordered list of dates, ordered list of charge codes and Æ map of date_code to entries

List dates = getDatesInOrder(timecard);

List codes = getChargeCodesInOrder(timecard);

Map entry_map = getEntriesMap(timecard);

if (!entry_map.isEmpty())

Trang 4

TablePrimitive table = new TablePrimitive();

// build column headings from dates Iterator date_it = dates.iterator();

int col_ctr = 1;

while (date_it.hasNext()) {

Date current_date = (Date) date_it.next();

SpanPrimitive date_span = new SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, “”);

date_span.addText(dayOfWeekFormat.format(current_date));

date_span.addLineBreak();

date_span.addText(shortFormat.format(current_date));

table.setPrimitiveAt(0, col_ctr++, date_span);

}

// build row headings from charge codes Iterator codes_it = codes.iterator();

int row_ctr = 1;

while (codes_it.hasNext()) {

String code = (String) codes_it.next();

SpanPrimitive code_span = new SpanPrimitive(TimecardStyle.IMPORTANT_TEXT, code);

table.setPrimitiveAt(row_ctr++, 0, code_span);

}

// build each text entry field date_it = dates.iterator();

col_ctr = 1;

while (date_it.hasNext()) {

Date date = (Date) date_it.next();

String date_string = keyFormat.format(date);

codes_it = codes.iterator();

row_ctr = 1;

while (codes_it.hasNext()) {

String code = (String) codes_it.next();

String key = “entry_” +date_string+ “_” +code;

TimeEntryDTO entry = (TimeEntryDTO) entry_map.get(key);

String hours = “0”;

if (entry != null) {

hours = “” + entry.getHours();

}

Trang 5

text_box.setWidth(5);

table.setPrimitiveAt(row_ctr, col_ctr, text_box);

row_ctr++;

} col_ctr++;

}

// add save button SubmitPrimitive save_button = new SubmitPrimitive(“Save”);

save_button.setName(TimecardKey.SAVE_TIMECARD.getKeyText()); table.setPrimitiveAt(row_ctr, 1, save_button);

// add submit button SubmitPrimitive submit_button = new SubmitPrimitive(“Submit”); submit_button.setName(TimecardKey.SUBMIT_TIMECARD.getKeyText()); table.setPrimitiveAt(row_ctr, 2, submit_button);

FormPrimitive form = new FormPrimitive(“/Timecard/ Æ RecordTimeServlet”, “rcif”);

form.addPrimitive(table);

page.addToBody(form);

}

return page;

}

private List getDatesInOrder(TimecardDTO timecard) {

ArrayList dates = new ArrayList();

Calendar calendar = Calendar.getInstance();

// add dates from start to end Date current = timecard.getStartDate();

Date end = timecard.getEndDate();

while (current.before(end)) {

String dbg = longFormat.format(current);

dates.add(current);

calendar.setTime(current);

calendar.add(Calendar.DAY_OF_MONTH, 1);

current = calendar.getTime();

} dates.add(end);

return dates;

}

Trang 6

{ ArrayList charge_codes = new ArrayList();

Iterator entries = timecard.getEntries();

while (entries.hasNext()) {

TimeEntryDTO entry = (TimeEntryDTO) entries.next();

String code = entry.getClientName()+”.”+entry.getProjectName()+”.”+entry Æ getChargeCodeName();

if (!charge_codes.contains(code)) {

charge_codes.add(code);

} }

Collections.sort(charge_codes);

return charge_codes;

}

/** Build a map of entries with date_chargecode as key*/

private Map getEntriesMap(TimecardDTO timecard) {

Map entries_map = new HashMap();

Iterator entries = timecard.getEntries();

while (entries.hasNext()) {

TimeEntryDTO entry = (TimeEntryDTO) entries.next();

String date = this.keyFormat.format(entry.getDate());

String code = entry.getClientName()+”.”+entry.getProjectName()+”.”+entry Æ getChargeCodeName();

String key = “entry_” +date+ “_” +code;

entries_map.put(key, entry);

}

return entries_map;

}

/** Extract hours from the request */

private void updateTimecardFromRequest(TimecardDTO timecard, Æ HttpServletRequest request)

{ Map entries_map = getEntriesMap(timecard);

Enumeration parameters = request.getParameterNames();

while (parameters.hasMoreElements()) {

Trang 7

if (parameter.startsWith(“entry”)) {

String key = parameter;

String hours = request.getParameter(parameter);

TimeEntryDTO entry = (TimeEntryDTO) entries_map.get(key); entry.setHours(Integer.parseInt(hours));

} } } }

Trang 8

RecordTimeWorkflowInt.java

RecordTimeWorkflowInt.java is the local interface for the RecordTimeWorkflow entity bean It defines the remotely accessible methods for the RecordTimeWorkflow entity bean Since RecordTimeWorkflow is a stateful session bean, each method assumes a particular session, which involves a single user and a single current timecard This allows the getChargeCodes to have an empty parameter list Again, notice that each return type is either Java data type or a custom Data Transfer Object (DTO).

package com.wiley.compBooks.EJwithUML.TimeCardWorkflow;

import java.rmi.*;

import javax.ejb.*;

import java.util.*;

import com.wiley.compBooks.EJwithUML.TimeCardDomain.*;

import com.wiley.compBooks.EJwithUML.Dtos.*;

import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;

/**

* The RecordTimeWorkflowInt is the interface that ties the Bean

* with the Remote interface to provide compile time type checking

*/

public interface RecordTimeWorkflowInt

RecordTimeWorkflow-SessionBean

Trang 9

{ /** Answers the current timecard */

public TimecardDTO getCurrentTimecard() throws DataCreateException,FatalApplicationException, RemoteException;

/** Marks the current timecard as closed and creates a new one */ public void submitTimecard() throws DataCreateException,

FatalApplicationException,RemoteException;

/** Adds/Updates/modifies the entries of the current timecard */ public void updateTimecard(TimecardDTO currentTimecard) throws Æ DataUpdateException,DataNotFoundException,FatalApplicationException, Æ RemoteException;

/**

* Answers a ClientDTO with client related information- projects

* and charge codes

*/

public ClientDTO getClient(String clientName) throws Æ DataNotFoundException,FatalApplicationException, RemoteException;

/**

* Answers a Collection of ClientDTOs with client related

* information- projects and charge codes

*/

public Collection getAllClients() throws DataNotFoundException,

FatalApplicationException, RemoteException;

}

RecordTimeWorkflow.java

RecordTimeWorkflow.java is the remote EJB interface that inherits from RecordTime-WorkflowInt and hence it has an empty body This is in line with what we have set as our implementation strategy for all of our EJBs This is shown in Figure 03.

Figure 03 Remote interface of the RecordTimeWorkflow session bean

<<SessionRemote>>

RecordTimeWorkflow

+ getCurrentTimecard() : TimecardDTO + submitTimecard() : void

+ updateTimecard(currentTimecard : TimecardDTO) : void + getClient(name : String) : ClientDTO

28 RecordTimeWorkflowSessionBean

Trang 10

package com.wiley.compBooks.EJwithUML.TimeCardWorkflow;

import javax.ejb.*;

import java.util.*;

import com.wiley.compBooks.EJwithUML.TimeCardDomain.*;

import com.wiley.compBooks.EJwithUML.Dtos.*;

import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;

/**

* The RecordTimeWorkflow allows client objects to record their

* time RecordTimeWorkflow is the remote interface through which

* clients access the underlying session bean

*/

public interface RecordTimeWorkflow extends EJBObject, Æ RecordTimeWorkflowInt

{ }

RecordTimeWorkflowHome.java

RecordTimeWorkflowHome.java contains the methods for creating RecordTimeWork-flowHome session beans The create method requires a username This associates the RecordTimeWorkflow session bean with a single user for the life of the session This is shown in Figure 04.

Figure 04 Remote Home interface of the RecordTimeWorkflowHome session bean

package com.wiley.compBooks.EJwithUML.TimeCardWorkflow;

import java.util.*;

import java.rmi.*;

import javax.ejb.*;

import com.wiley.compBooks.EJwithUML.Base.ApplicationExceptions.*;

/**

* The RecordTimeWorkflow allows client objects to record their time

* RecordTimeWorkflowHome is the remote interface through which Æ clients find

* and create the underlying session beans

*/

<<SessionHome>>

RecordTimeWorkflowHome

+ create(username : String) : RecordTimeWorkflow

RecordTimeWorkflowSessionBean 29

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

TỪ KHÓA LIÊN QUAN