Saturday, October 17, 2015

OIM 11G R2 PS2 (11.1.2.2.X) Submit Buttion Action Listener Sample Code

In this post , I will be sharing some sample code related to submit action listener that gets invoked every time you hit the submit button on the request page.

---------------------------------------------------------------------------------------------------------------------






package deepak.dubey.com;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.el.MethodExpression;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

public class CustomReqBean {
    public CustomReqBean() {
        super();
    }
    
    
    private UIComponent startDateID;
    private UIComponent endDateID;
     
    public void setStartDateID(UIComponent startDateID) {
        this.startDateID = startDateID;
    }
     
    public UIComponent getStartDateID() {
        return startDateID;
    }
     
    public void setEndDateID(UIComponent endDateID) {
        this.endDateID = endDateID;
    }
     
    public UIComponent getEndDateID() {
        return endDateID;
    }
    
    private static final String START_DATE_END_DATE_VALIDATION_MSG = "Start Date - End Date interval cannot exceed 180 days for Contractors.";
        private static final String START_DATE_AFTER_END_DATE_VALIDATION_MSG = "Start Date cannot be before Today's Date.";
     
        private static final String USER_TYPE_ATTRIBUTE = "usr_emp_type__c";
        private static final String START_DATE_ATTRIBUTE = "usr_start_date__c";
        private static final String END_DATE_ATTRIBUTE = "usr_end_date__c";
    private static final String STATUS_ATTRIBUTE = "usr_status";
        
        
        
    public void submitButtonActionListener(ActionEvent e){
        int integer = 0;
        try{
            String cs3 = FacesUtils.getValueFromELExpression("#{backingBeanScope.catReqBean.cartItemSize}", String.class);
            integer = Integer.parseInt(cs3);
            
        }catch(Exception e1){
            e1.printStackTrace();
        }
        
        String edateStr = "";
        try{
            edateStr = FacesUtils.getValueFromELExpression("#{bindings.executionDate}", String.class);
            
        }catch(Exception e2){
            e2.printStackTrace();
        }
        
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String todaysString = sdf.format(d);
        if(integer > 1){
            FacesMessage fm = new FacesMessage();
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            fm.setSummary("More than 1 role");
            FacesUtils.showFacesMessage(fm);
        }else if(!(edateStr.equalsIgnoreCase(todaysString))){
            FacesMessage fm = new FacesMessage();
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            fm.setSummary("Effective Date == today date");
            FacesUtils.showFacesMessage(fm);
        }else{
            MethodExpression originalActionListener = FacesUtils.getMethodExpressionFromEL("#{backingBeanScope.cartReqBean.submitActionListener"
                                                                                           null,new Class[]{ActionEvent.class});
            originalActionListener.invoke(FacesUtils.getELContext(), new Object[]{e});
        }
            
    }
        public void validator(FacesContext facesContext, UIComponent uiComponent, Object object) {
            if (uiComponent.equals(startDateID)) {
                // get value of End Date through binding
                oracle.jbo.domain.Date jboEndDate = FacesUtils.getAttributeBindingValue(END_DATE_ATTRIBUTE, oracle.jbo.domain.Date.class);
                // only validate if both Start Date and End Date are set
                if (jboEndDate != null) {
                    // value of Start Date is passed to validator
                    Date startDate = ((oracle.jbo.domain.Date)object).getValue();
                    Date endDate = jboEndDate.getValue();
                    validateStartDateEndDate(facesContext, uiComponent, startDate, endDate);
                }
            } 
            
//            else if (uiComponent.equals(endDateID)) {
//                // get value of Start Date through binding
//                oracle.jbo.domain.Date jboStartDate = FacesUtils.getAttributeBindingValue(START_DATE_ATTRIBUTE, oracle.jbo.domain.Date.class);
//                // only validate if both Start Date and End Date are set
//                if (jboStartDate != null) {
//                    Date startDate = jboStartDate.getValue();
//                    // value of End Date is passed to validator
//                    Date endDate = ((oracle.jbo.domain.Date)object).getValue();
//                    validateStartDateEndDate(facesContext, uiComponent, startDate, endDate);
//                }
//            }
        }
     
        private void validateStartDateEndDate(FacesContext facesContext, UIComponent uiComponent, Date startDate, Date endDate) {
            Date startDatePlus180Days = new Date(startDate.getTime() + 180L * 24 * 60 * 60 * 1000);
            java.util.Date todaysDate = new Date();
            
            if (!("Active".equalsIgnoreCase(FacesUtils.getListBindingValue(STATUS_ATTRIBUTE, String.class)))){
                if(startDate.before(todaysDate)){
                    facesContext.addMessage(uiComponent.getClientId(facesContext),
                                            new FacesMessage(FacesMessage.SEVERITY_ERROR, START_DATE_AFTER_END_DATE_VALIDATION_MSG, null));
                    
                    
                    
                }
            }else if("Active".equalsIgnoreCase(FacesUtils.getListBindingValue(STATUS_ATTRIBUTE, String.class))){
                facesContext.addMessage(uiComponent.getClientId(facesContext),
                                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Start Date cannot be modified for Active Users"null));
            }else  {
                // re-render -- in case there was an error message in queue for any of the two components it will be released
                FacesUtils.partialRender(startDateID);
                FacesUtils.partialRender(endDateID);
            }
   
            
            
//            if (startDate.after(endDate)) {
//                // queue error message for the component which is being validated (either Start Date or End Date)
//                facesContext.addMessage(uiComponent.getClientId(facesContext),
//                                        new FacesMessage(FacesMessage.SEVERITY_ERROR, START_DATE_AFTER_END_DATE_VALIDATION_MSG, null));
//            } else if (isContractorUserTypeSelected() && startDatePlus180Days.before(endDate)) {
//                // queue error message for the component which is being validated (either Start Date or End Date)
//                facesContext.addMessage(uiComponent.getClientId(facesContext),
//                                        new FacesMessage(FacesMessage.SEVERITY_ERROR, START_DATE_END_DATE_VALIDATION_MSG, null));
//            } else {
//                // re-render -- in case there was an error message in queue for any of the two components it will be released
//                FacesUtils.partialRender(startDateID);
//                FacesUtils.partialRender(endDateID);
//            }
        }
     
        public boolean isContractorUserTypeSelected() {
            // return true if value of "usr_emp_type__c" binding attribute equals to "Contractor"
            // "usr_emp_type__c" binding attribute is used to display value of User Type in the User Type drop-down
            return "Contractor".equals(FacesUtils.getListBindingValue(USER_TYPE_ATTRIBUTE, String.class));
        }
}