Subject: about struts
Posted By: kishore.dyn Post Date: 2/15/2006 2:06:33 AM

can we write our own methods in Action class.
give me example.

k.ram kishore
Reply By: longjava Reply Date: 2/15/2006 2:23:01 PM
What do you mean by writing you own method in the Action class??

The Action class is the class, which provided by Struts framework, that your action class should be derived from. And typically you need to override the execution() method of the Action class to provide your own business logic. In your own action class you can define any methods that you like just any Java class that you're typically write.

Here is an example of how to derive your own action class from the Struts's Action class:

public class MyAction extends Action
{
   public ActionForward execute( ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response ) throws Exception
   {
      String forwardTo = "success";
      if ( !perform_bussiness_logic() )
      {
          forwardTo = "failure";
      }

      return mapping.findForward( forwardTo );
   }

   private boolean perform_bussiness_logic()
   {
      boolean status = true;
      // perform bussiness logic
      // .....
      return status;
   }
}

Hope this help.

Reply By: kishore.dyn Reply Date: 2/16/2006 2:25:18 AM
public ActionForward execute( ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response )
this is fine my query is:

who creates the references for these?

ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,



k.ram kishore
Reply By: longjava Reply Date: 2/16/2006 4:33:45 AM
The ActionMapping and ActionForm objects are created by RequestProcessor object in the process() method. The HttpServletRequest and HttpServletResponse objects are passed in from ActionServlet object.

Btw, the RequestProcessor object is the one that actually call the execute() method that you own action class provides.

Reply By: sachin.tathod Reply Date: 8/8/2006 1:28:57 AM
Hi Using this code u can solve ur problem

package com.passkey.resdesk.web.action.event;

import com.passkey.core.datavo.common.AcceptedCardListVO;
import com.passkey.core.datavo.common.AcceptedCardVO;
import com.passkey.core.datavo.common.AcceptedCardVOType;
import com.passkey.core.datavo.common.AccessPrivilegeVO;
import com.passkey.core.datavo.common.CommerceCVO;
import com.passkey.core.datavo.common.CommerceProcessingVOType;
import com.passkey.core.datavo.common.CommerceVOType;
import com.passkey.core.datavo.common.EventCommerceListVOType;
import com.passkey.core.datavo.common.EventCommerceVO;
import com.passkey.core.datavo.common.EventCommerceVOType;
import com.passkey.core.datavo.common.GroupCommerceListVOType;
import com.passkey.core.datavo.common.GroupCommerceVO;
import com.passkey.core.datavo.common.GroupCommerceVOType;
import com.passkey.core.datavo.common.ObjectFactory;
import com.passkey.core.datavo.common.PKComponentRequestVO;
import com.passkey.core.framework.infraservice.exception.ExceptionService;
import com.passkey.core.framework.infraservice.logging.LoggingService;
import com.passkey.core.framework.infraservice.logging.PasskeyLogger;
import com.passkey.core.framework.ui.session.ResdeskSessionManager;
import com.passkey.core.framework.ui.template.TemplateConstants;
import com.passkey.core.util.ConstantDeclareI;
import com.passkey.core.util.DateValidator;
import com.passkey.core.util.PasskeyConstants;
import com.passkey.core.util.PasskeyUtils;
import com.passkey.core.util.PropertyFileReader;

import com.passkey.resdesk.web.action.ResdeskPageAction;
import com.passkey.resdesk.web.action.ResdeskXAction;
import com.passkey.resdesk.web.actionform.event.CommerceForm;
import com.passkey.resdesk.web.delegate.PKResdeskXDelegate;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.xml.bind.JAXBException;


public class CommerceAction extends DispatchAction {
    LoggingService logService = LoggingService.getInstance();
    PasskeyLogger logger = logService.getLogger(this.getClass().getName());
    private SimpleDateFormat formatterSmalldate = new SimpleDateFormat(ConstantDeclareI.SMALL_DATE);

    public ActionForward load(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        PKResdeskXDelegate pkResdeskXDelegate = new PKResdeskXDelegate();
        PKComponentRequestVO requestVO = null;
        CommerceCVO commerceCVO = null;
        List eventCommercelist = new ArrayList();
        List groupCommercelist = new ArrayList();
        List cancellationfee = new ArrayList();
        logger.entering();

        if (ResdeskPageAction.checkAndTouchLogin(request, response,
                    PasskeyUtils.getResDeskAppID().intValue())) {
            return null; // session has expired
        }

        ResdeskSessionManager session = ResdeskPageAction.getResdeskSessionManager(request);
        requestVO = session.getPKComponentRequestVO();
        requestVO.setComponentTypeId(PKComponentRequestVO.COMMERCE);
        commerceCVO = (CommerceCVO) pkResdeskXDelegate.load(requestVO);

        AccessPrivilegeVO accessPriviligeVO = pkResdeskXDelegate.getAccessPrivileges(requestVO);
        session.putSessionData(ConstantDeclareI.ACCESSPREVILEGE,
            accessPriviligeVO);

        populateForm(form, commerceCVO);

        EventCommerceVOType eventCommerceVOType = null;
        GroupCommerceVOType groupCommerceVOType =null;
        groupCommercelist = commerceCVO.getGroupCommerceListVO()
                                       .getGroupCommerceVO();
        eventCommercelist = commerceCVO.getEventCommerceListVO().getEventCommerceVO();

        for (int i = 0; i < groupCommercelist.size(); i++) {
            groupCommerceVOType = (GroupCommerceVOType) groupCommercelist.get(i);
            cancellationfee.add(groupCommerceVOType);
        }
        //added for displaying Last Date to Charge.
        for (int i = 0; i < eventCommercelist.size(); i++) {
            eventCommerceVOType = (EventCommerceVOType)eventCommercelist.get(i);
            if(eventCommerceVOType.getFeeTypeID()==PasskeyConstants.ROOM_CXL_FEE){
                request.setAttribute("eventCommerceVOType", eventCommerceVOType);
            }
        }
        request.setAttribute(ConstantDeclareI.CANCELLATIONFEE, cancellationfee);
        ResdeskXAction.setBreadCrumbs(session,
            TemplateConstants.COMMERCE_CANCELLATION_POLICY);
        request.setAttribute("DISP_PAGE", "CancellationPolicy");
        requestVO.setComponentTypeId(PKComponentRequestVO.GROUP_TYPE);

        List attendeeTypeUICVOList = pkResdeskXDelegate.listAttendeeTypesByEvent(requestVO);

        //added to check  no of attendee types should be atleast one.
        if (attendeeTypeUICVOList.size() != 0) {
            session.putSessionData("NoOfAttendees",
                new Integer(attendeeTypeUICVOList.size()));
        }else{
            session.putSessionData("NoOfAttendees",new Integer(0));
        }

        return mapping.findForward("nxtpage");
    }

    public ActionForward submit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ObjectFactory objFactory = new ObjectFactory();
        PKResdeskXDelegate pkResdeskXDelegate = new PKResdeskXDelegate();
        PKComponentRequestVO requestVO = null;
        AcceptedCardListVO accList = null;
        AcceptedCardVO acceptedCardVO = null;
        CommerceForm commerceForm = (CommerceForm) form;

        try {
            accList = new ObjectFactory().createAcceptedCardListVO();
        } catch (JAXBException e) {
            ExceptionService.handleException(requestVO, e, logger);
        }

        acceptedCardVO = new ObjectFactory().createAcceptedCardVO();

        if (commerceForm.getAmex()) {
            acceptedCardVO = new ObjectFactory().createAcceptedCardVO();
            acceptedCardVO.setName("American Express");
            acceptedCardVO.setCardTypeID(102);
            accList.getAcceptedCard().add(acceptedCardVO);
        }

        if (commerceForm.getDinersClub()) {
            acceptedCardVO = new ObjectFactory().createAcceptedCardVO();
            acceptedCardVO.setName("Diners Club");
            acceptedCardVO.setCardTypeID(104);
            accList.getAcceptedCard().add(acceptedCardVO);
        }

        if (commerceForm.getDiscover()) {
            acceptedCardVO = new ObjectFactory().createAcceptedCardVO();
            acceptedCardVO.setName("Discover");
            acceptedCardVO.setCardTypeID(103);
            accList.getAcceptedCard().add(acceptedCardVO);
        }

        if (commerceForm.getMasterCard()) {
            acceptedCardVO = new ObjectFactory().createAcceptedCardVO();
            acceptedCardVO.setName("MasterCard");
            acceptedCardVO.setCardTypeID(101);
            accList.getAcceptedCard().add(acceptedCardVO);
        }

        if (commerceForm.getVisa()) {
            acceptedCardVO = new ObjectFactory().createAcceptedCardVO();
            acceptedCardVO.setName("VISA");
            acceptedCardVO.setCardTypeID(100);
            accList.getAcceptedCard().add(acceptedCardVO);
        }

        CommerceCVO commerceCVO = null;
        commerceCVO = objFactory.createCommerceCVO();
        commerceCVO.setAcceptedCardListVO(accList);
        commerceCVO.setCancellationInstructions(commerceForm.getInstructions());

        ResdeskSessionManager session = ResdeskPageAction.getResdeskSessionManager(request);
        requestVO = session.getPKComponentRequestVO();
        requestVO.setComponentTypeId(PKComponentRequestVO.COMMERCE);
        pkResdeskXDelegate.save(requestVO, commerceCVO);

        return mapping.findForward("submit");
    }

    private void populateForm(ActionForm form, CommerceCVO commerceCVO) {
        CommerceForm commerceForm = (CommerceForm) form;
       
        List acceptedCards = new ArrayList();
        acceptedCards = commerceCVO.getAcceptedCardListVO().getAcceptedCard();
        AcceptedCardVOType cardVOType = null;
            for (int i = 0; i < acceptedCards.size(); i++) {
            cardVOType = (AcceptedCardVOType) acceptedCards.get(i);

            if (cardVOType.getName().equalsIgnoreCase(ConstantDeclareI.GL_AMERICAN_EXPRESS)) {
                commerceForm.setAmex(true);
            }

            if (cardVOType.getName().equalsIgnoreCase(ConstantDeclareI.GL_DINERS_CLUB)) {
                commerceForm.setDinersClub(true);
            }

            if (cardVOType.getName().equalsIgnoreCase(ConstantDeclareI.GL_DISCOVER)) {
                commerceForm.setDiscover(true);
            }

            if (cardVOType.getName().equalsIgnoreCase(ConstantDeclareI.GL_VISA)) {
                commerceForm.setVisa(true);
            }

            if (cardVOType.getName().equalsIgnoreCase(ConstantDeclareI.GL_MASTERCARD)) {
                commerceForm.setMasterCard(true);
            }
        }

        commerceForm.setInstructions(commerceCVO.getCancellationInstructions());
       }

    public ActionForward saveCommerce(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ObjectFactory objFactory = new ObjectFactory();
        SimpleDateFormat formatterSmalldate = new SimpleDateFormat(ConstantDeclareI.SMALL_DATE);
        PKResdeskXDelegate pkResdeskXDelegate = new PKResdeskXDelegate();
        PKComponentRequestVO requestVO = null;
        CommerceForm commerceForm = (CommerceForm) form;
        EventCommerceVO eventCommerceVO = null;
        GroupCommerceVO groupCommerceVO =null;
        EventCommerceListVOType eventCommerceListVO = null;
        GroupCommerceListVOType GroupCommerceListVO =null;
        CommerceProcessingVOType CommerceProcessingVO =null;
        EventCommerceVOType eventCommerceVOType = null;
        CommerceCVO commerceCVO = null;
        List eventCommercelist = new ArrayList();
        ActionErrors errors = new ActionErrors();
        //added for session expired.
        if (ResdeskPageAction.checkAndTouchLogin(request, response,
                PasskeyUtils.getResDeskAppID().intValue())) {
        return null; // session has expired
        }
        try {
            eventCommerceListVO = new ObjectFactory().createEventCommerceListVOType();
            GroupCommerceListVO =new ObjectFactory().createGroupCommerceListVOType();
            CommerceProcessingVO =new ObjectFactory().createCommerceProcessingVOType();
        } catch (JAXBException e) {
            ExceptionService.handleException(requestVO, e, logger);
        }
        ResdeskSessionManager session = ResdeskPageAction.getResdeskSessionManager(request);
        requestVO = session.getPKComponentRequestVO();
        requestVO.setComponentTypeId(PKComponentRequestVO.COMMERCE);
        eventCommerceVO = new ObjectFactory().createEventCommerceVO();
        groupCommerceVO =new ObjectFactory().createGroupCommerceVO();
       
        commerceCVO = (CommerceCVO) pkResdeskXDelegate.load(requestVO);
        eventCommercelist = commerceCVO.getEventCommerceListVO().getEventCommerceVO();
        for (int i = 0; i < eventCommercelist.size(); i++) {
            eventCommerceVOType = (EventCommerceVOType)eventCommercelist.get(i);
            if(eventCommerceVOType.getFeeTypeID()==PasskeyConstants.ROOM_CXL_FEE){
                 groupCommerceVO.setCancellationDate(eventCommerceVOType.getCancellationDate());
                 eventCommerceVO.setCancellationDate(eventCommerceVOType.getCancellationDate());
                 commerceForm.setCclastDate(formatterSmalldate.format(new Date(
                         eventCommerceVOType.getCancellationDate().getTimeInMillis())));
            }
        }
        errors = commerceForm.validate(mapping, request);

        if ((errors != null) && !errors.isEmpty()) {
            saveErrors(request, errors);
            request.setAttribute("StartDateError",new Boolean (true));
            return (mapping.findForward("success"));
        }
        groupCommerceVO.setFeeValue(Float.parseFloat(commerceForm.getAmount()));
        groupCommerceVO.setProcessingFeeDate(commerceForm.getEventCommerceVO()
                .getProcessingFeeDate());
        //groupCommerceVO.setCancellationDate(commerceForm.getEventCommerceVO()
          //      .getCancellationDate());
        groupCommerceVO.setFeeTypeID(PasskeyConstants.ROOM_CXL_FEE);
        eventCommerceVO.setFeeTypeID(PasskeyConstants.ROOM_CXL_FEE);
        CommerceProcessingVO.setMerchantAccountName(PropertyFileReader.getPasskeyProperty(
                "STR_MER_ACC_NAME"));
        CommerceProcessingVO.setMerchantID(PropertyFileReader.getPasskeyProperty(
                "MERCHANT_ID"));
        CommerceProcessingVO.setProcessorName(PropertyFileReader.getPasskeyProperty(
                "STR_PROC_NAME"));
        CommerceProcessingVO.setTerminalID(PropertyFileReader.getPasskeyProperty(
                "STR_TERMINAL_ID"));
        CommerceProcessingVO.setVendorCode(PropertyFileReader.getPasskeyProperty(
                "STR_VENDOR_NAME"));
        CommerceProcessingVO.setOwnerShortName(PropertyFileReader.getPasskeyProperty(
                "STR_OWNER_SHORT_NAME"));
        CommerceProcessingVO.setOwnerDivisionName(PropertyFileReader.getPasskeyProperty(
                "STR_OWNER_DIVISION_NAME"));
        GroupCommerceListVO.getGroupCommerceVO().add(groupCommerceVO);
        eventCommerceListVO.getEventCommerceVO().add(eventCommerceVO);
        commerceCVO = objFactory.createCommerceCVO();
        commerceCVO.setGroupCommerceListVO(GroupCommerceListVO);
        commerceCVO.setEventCommerceListVO(eventCommerceListVO);
        commerceCVO.setProcessingVO(CommerceProcessingVO);

        //TODO set the old date in the requestVO
        if(!(("").equalsIgnoreCase(commerceForm.getOldstartDate()))){
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateValidator.isValid(commerceForm.getOldstartDate(),Locale.US));
            requestVO.setCustomPayload(cal);
        }
        pkResdeskXDelegate.save(requestVO, commerceCVO);
        return mapping.findForward("success");
    }

    public ActionForward removeCommerce(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            ObjectFactory objFactory = new ObjectFactory();
           
            PKResdeskXDelegate pkResdeskXDelegate = new PKResdeskXDelegate();
            PKComponentRequestVO requestVO = null;
            CommerceForm commerceForm = (CommerceForm) form;
            CommerceVOType commerceVO =null;

            try {
                commerceVO =new ObjectFactory().createCommerceVOType();
            } catch (JAXBException e) {
                ExceptionService.handleException(requestVO, e, logger);
            }

            ResdeskSessionManager session = ResdeskPageAction.getResdeskSessionManager(request);
            requestVO = session.getPKComponentRequestVO();
            requestVO.setComponentTypeId(PKComponentRequestVO.COMMERCE);
            commerceVO.setFeeValue(Float.parseFloat(request.getParameter("Value")));

            Calendar cal =Calendar.getInstance();
        cal.setTime(formatterSmalldate.parse((request.getParameter("ProcDate")
                                                     .toString())));
            commerceVO.setProcessingFeeDate(cal) ;  

            Calendar endCal =Calendar.getInstance();
        endCal.setTime(formatterSmalldate.parse((request.getParameter("EndDate")
                                                        .toString())));
            commerceVO.setCancellationDate(endCal);
            commerceVO.setFeeTypeID(PasskeyConstants.ROOM_CXL_FEE);
            pkResdeskXDelegate.remove(requestVO,commerceVO);

            return mapping.findForward("success");
        }
    public ActionForward saveLastDateToCharge(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        ObjectFactory objFactory = new ObjectFactory();
        PKResdeskXDelegate pkResdeskXDelegate = new PKResdeskXDelegate();
        PKComponentRequestVO requestVO = null;
        CommerceForm commerceForm = (CommerceForm) form;
        EventCommerceVO eventCommerceVO = null;
        EventCommerceListVOType eventCommerceListVO = null;
        CommerceProcessingVOType CommerceProcessingVO =null;
        GroupCommerceVO groupCommerceVO =null;
        GroupCommerceListVOType GroupCommerceListVO =null;
        GroupCommerceVOType groupCommerceVOType =null;
        List groupCommercelist = new ArrayList();
        CommerceCVO commerceCVO = null;
        ActionErrors errors = new ActionErrors();
        //added for session expired.
        if (ResdeskPageAction.checkAndTouchLogin(request, response,
                PasskeyUtils.getResDeskAppID().intValue())) {
            return null; // session has expired
        }
        try {
            eventCommerceListVO = new ObjectFactory().createEventCommerceListVOType();
            GroupCommerceListVO =new ObjectFactory().createGroupCommerceListVOType();
            CommerceProcessingVO =new ObjectFactory().createCommerceProcessingVOType();
        } catch (JAXBException e) {
            ExceptionService.handleException(requestVO, e, logger);
        }
        
        ResdeskSessionManager session = ResdeskPageAction.getResdeskSessionManager(request);
        requestVO = session.getPKComponentRequestVO();
         errors = commerceForm.validateLastDate(mapping, request);
         if ((errors != null) && !errors.isEmpty()) {
             saveErrors(request, errors);
             request.setAttribute("LastDateError",new Boolean (true));
             return (mapping.findForward("success"));
         }
         
        requestVO.setComponentTypeId(PKComponentRequestVO.COMMERCE);
        eventCommerceVO = new ObjectFactory().createEventCommerceVO();
        eventCommerceVO.setFeeTypeID(PasskeyConstants.ROOM_CXL_FEE);
        //TODO set Last Date To Charge
        if(!(("").equalsIgnoreCase(commerceForm.getCclastDate()))){
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateValidator.isValid(commerceForm.getCclastDate(),Locale.US));
            eventCommerceVO.setCancellationDate(cal);
        }
         commerceCVO = (CommerceCVO) pkResdeskXDelegate.load(requestVO);
         groupCommercelist = commerceCVO.getGroupCommerceListVO().getGroupCommerceVO();
         for (int i = 0; i < groupCommercelist.size(); i++) {
             groupCommerceVOType = (GroupCommerceVOType)groupCommercelist.get(i);
             if(groupCommerceVOType.getFeeTypeID()==PasskeyConstants.ROOM_CXL_FEE){
                  commerceForm.setStartDate(formatterSmalldate.format(new Date(
                          groupCommerceVOType.getProcessingFeeDate().getTimeInMillis())));
                          errors = commerceForm.CommerceBusinessValidation(mapping, request);
                 if ((errors != null) && !errors.isEmpty()) {
                    saveErrors(request, errors);
                    request.setAttribute("LastDateError",new Boolean (true));
                    return (mapping.findForward("success"));
                }
                 
             }
         }
        eventCommerceListVO.getEventCommerceVO().add(eventCommerceVO);
        CommerceProcessingVO.setMerchantAccountName(PropertyFileReader.getPasskeyProperty(
        "STR_MER_ACC_NAME"));
        CommerceProcessingVO.setMerchantID(PropertyFileReader.getPasskeyProperty(
        "MERCHANT_ID"));
        CommerceProcessingVO.setProcessorName(PropertyFileReader.getPasskeyProperty(
        "STR_PROC_NAME"));
        CommerceProcessingVO.setTerminalID(PropertyFileReader.getPasskeyProperty(
        "STR_TERMINAL_ID"));
        CommerceProcessingVO.setVendorCode(PropertyFileReader.getPasskeyProperty(
        "STR_VENDOR_NAME"));
        CommerceProcessingVO.setOwnerShortName(PropertyFileReader.getPasskeyProperty(
        "STR_OWNER_SHORT_NAME"));
        CommerceProcessingVO.setOwnerDivisionName(PropertyFileReader.getPasskeyProperty(
        "STR_OWNER_DIVISION_NAME"));

        commerceCVO = objFactory.createCommerceCVO();
        commerceCVO.setEventCommerceListVO(eventCommerceListVO);
        commerceCVO.setProcessingVO(CommerceProcessingVO);
        pkResdeskXDelegate.save(requestVO, commerceCVO);
        return mapping.findForward("success");
    }
    
    
}


Thanks and Regards,
SACHIN S.TATHOD
Patni Computer System Ltd.
Magarpatta City, Cyber City,
Tower 3,Level I & II,
Hadapsar,Pune - 411028
Mobile No:- +91-9881239401
Email ID:- sachin.tathod@patni.com

Go to topic 48117

Return to index page 209
Return to index page 208
Return to index page 207
Return to index page 206
Return to index page 205
Return to index page 204
Return to index page 203
Return to index page 202
Return to index page 201
Return to index page 200