📄 recordtimeworkflowbean.java
字号:
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();
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
System.out.println("there is no timecard marked current.");
// timecard = createTimecard(timecard);
createTimecard(timecard);
timecard = user.getCurrentTimecard();
}
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(),
timeEntry.getHours(), timeEntry.getDate());
tcDTO.addEntry(timeEntryDTO);
}
System.out.println(tcDTO);
return tcDTO;
}
catch(InvalidDataException e)
{
throw new DataCreateException(Origin.TIMECARD_WORKFLOW, e,
"falied to retrieve current timecard.");
}
}
/** Marks the current timecard as closed and create a new one. */
public void submitTimecard() throws DataCreateException,
FatalApplicationException, RemoteException
{
System.out.println("in submitTimecard().");
TimecardLocal currentTimecard = user.getCurrentTimecard();
createTimecard(currentTimecard);
System.out.println("done submitTimecard().");
}
/** 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(),
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);
}
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 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.");
}
}
/**
* 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 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
{
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())
{
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;
//makes the current timecard to be not current, and makes the new one to
//be current.
if(user.getCurrentTimecard()!= null)
{
user.getCurrentTimecard().setIsCurrent(false);
System.out.println("previous timecard id = " +
user.getCurrentTimecard().getPrimaryKey()
+ "with isCurrent = " + user.getCurrentTimecard().getIsCurrent());
}
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);
System.out.println("nextTimecard id = "+
nextTimecard.getPrimaryKey() + "with isCurrent = " +
nextTimecard.getIsCurrent());
user.setCurrentTimecard(nextTimecard);
System.out.println("done creating a new Timecard with id = " +
nextTimecard.getPrimaryKey() + "with isCurrent = " +
nextTimecard.getIsCurrent());
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.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -