LoginAction.java
package vincent.actions;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action{
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
//can logon?
boolean isValidate = false;
//get the LoginForm attribute
String userName = ((LoginForm)form).getUsername();
String passWord = ((LoginForm)form).getPassword();
//validate
if(userName.equals("Admin") && passWord.equals("Admin")){
isValidate = true;
}
else if(userName.equals("Customer") && passWord.equals("Customer")){
isValidate = true;
}
//add to ActionErrors
if(isValidate == false){
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionError("error.login.failed"));
this.saveErrors(request,errors);
return mapping.findForward("Logon");
}
//get the session
HttpSession session = request.getSession();
session.setAttribute("userName",userName);
session.setAttribute("passWord",passWord);
session.setAttribute("LoginForm",(LoginForm)form);//Put it in the session cause the jsp may use it
return (mapping.findForward("View"));
}
}
LoginForm.java
package vincent.actions;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class LoginForm extends ValidatorForm{
//declare the fields of the form here
private String username = null;
private String password = null;
public void setUsername(String username){
this.username = username;
}
public void setPassword(String password){
this.password = password;
}
public String getUsername(){
return (this.username);
}
public String getPassword(){
return (this.password);
}
/**
* This is the validation
* It must not be omitted.It MUST not be implemented cause the ValidationPlugin will do that.
*/
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
return null;
}
}
ViewAction.java
package vincent.actions;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import vincent.CMP.ProductLocal;
import vincent.SLSBean.*;
import vincent.util.PageControl;
public final class ViewAction extends Action{
//cache the accessManagerHome here
AccessManagerHome accessManagerHome = null;
ArrayList productList = null;
ArrayList array = new ArrayList();
/**
* Initializing the accessManagerHome
*
*/
public void initAccessManager(){
System.out.println("Loading AccessManager in the servlet");
try {
accessManagerHome = getHome();
} catch (NamingException e) {
//Have to add something later
e.printStackTrace();
}
}
/**
* Get the AccessManagerHome
* @return
* @throws NamingException
*/
public AccessManagerHome getHome() throws NamingException {
Object result = getContext().lookup(AccessManagerHome.JNDI_NAME);
return (AccessManagerHome)PortableRemoteObject.narrow(result,AccessManagerHome.class);
}
/**
* Get the Context for JNDI
* @return
* @throws NamingException
*/
public InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();
props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
// This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password");
InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public void setArrayList(ArrayList arrayList,PageControl pageControl){
int startRow = pageControl.getPageStartRow();
int endRow = pageControl.getPageEndRow();
Iterator iterator = arrayList.iterator();
for(int i=1;i<startRow;i++){
iterator.next();
}
for(int i=startRow;i<=endRow;i++){
ProductLocal element = (ProductLocal) iterator.next();
ProductForm productform = new ProductForm();
//add the info to productform
productform.setId(element.getID().toString());
//test whether it can add
productform.setName(element.getName());
productform.setRetailPrice(element.getRetailPrice().toString());
productform.setWholeSalePrice(element.getWholeSalePrice().toString());
productform.setQuantity(element.getQuantity().toString());
productform.setDescription(element.getDescription());
productform.setDiscountPrice(element.getDiscountPrice().toString());
//add it to the list
array.add(productform);
}
}
/**
* perform the main function here
*/
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException{
//At first I get the session
HttpSession session = request.getSession();
PageControl pageControl = (PageControl) session.getAttribute("pageControl");
//get the username and password from the session.
//if the IE has been restarted,return to the first index page
String userName = (String) session.getAttribute("userName");
String passWord = (String) session.getAttribute("passWord");
//Validate here
/*if(userName == null || passWord == null)
{
return (mapping.findForward("Logon"));
}*/
/*else if(!userName.equals("Admin") || !userName.equals("Customer"))
{
return (mapping.findForward("Logon"));
}
*/
//get the accessManagerHome initialized
initAccessManager();
//reset your array first
array = new ArrayList();
try {
accessManagerHome.create().calculateDiscount((float)0.75);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
productList = accessManagerHome.create().getAllRecords();
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (CreateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//get the pages information from request
String pageInfo = request.getParameter("pageInfo");
//if null create the new PageControl
if(pageInfo == null){
pageControl = new PageControl(productList.size());
session.setAttribute("pageControl",pageControl);
}
else if(pageInfo.equals("next")){
pageControl.setCurrentPage(pageControl.getNextPage());
}
else if(pageInfo.equals("previous")){
pageControl.setCurrentPage(pageControl.getPreviousPage());
}
//transfer the productlist to the array
//in array the element in "ProductForm"
setArrayList(productList,pageControl);
//add the PageForm info
PageForm pageForm = new PageForm();
String hasNext = null;
if(pageControl.hasNext){
hasNext = "yes";
}
else{
hasNext = "no";
}
String hasPrevious = null;
if(pageControl.hasPrevious){
hasPrevious = "yes";
}
else{
hasPrevious = "no";
}
pageForm.setHasNext(hasNext);
pageForm.setHasPrevious(hasPrevious);
//add it to the session
request.setAttribute("array",array);
session.setAttribute("PageForm",pageForm);
//redirect to ProductList.jsp with the same request(I will verify later)
return mapping.findForward("ProductList");
}
}