Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleCRMUIRewardSettlementRunDefinitionBean.java
linenumberstrue
collapsetrue
@Stateless
@LocalBean
public class CRMUIRewardSettlementRunDefinitionBean extends CRMUIProcessRunDefinitionBean {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String PROCESS_RUN_LOG_ENTRIES_EXIST	= "PROCESS_RUN_LOG_ENTRIES_EXIST";
	private static final String IS_PROCESS_RUN_DEF_LATEST_VERSION = "IS_PROCESS_RUN_DEF_LATEST_VERSION";
	private static final String LATEST_PROCESS_RUN_DEF_ID = "LATEST_PROCESS_RUN_DEF_ID";
	private static final String PROCESS	="REWARD_SETTLEMENT_PROCESSING";
	private static final String MODULE	="REWARDS";
	@EJB private CRMBOProcessRunDefinitionBean processRunDefinitionBean;
	@EJB private RewardSettlementProcessRunDefinitionXMLUtilBean rewardSettlementProcessRunDefinitionXMLUtilBean;
	@EJB private CRMBOProcessRunLogBean processRunLogBean;
	@EJB private CRMBORewardSchemeBean rewardSchemeBean;
	@EJB private CRMValidatorBean validatorBean;
	@EJB private CRMBORewardDefinitionBean rewardDefinitionBean;
	@EJB private RewardDefinitionXMLUtilBean rewardDefinitionXMLUtilBean;
	/**
	 * @see CRMUIProcessRunDefinitionBean#CRMUIProcessRunDefinitionBean()
	 */
	public CRMUIRewardSettlementRunDefinitionBean() {
		//do nothing
	}
	/**
	 * Loads a reward settlement run definition form.
	 * 
	 * @param id - the reward settlement run definition id to load
	 * @return the loaded form
	 * @throws Exception
	 */
	public RewardSettlementRunDefinition loadForm(String id) throws Exception {
		//Load the process run definition from db
		CRMDOProcessRunDefinition definition = (CRMDOProcessRunDefinition)processRunDefinitionBean.load(id);
		
		//Set process run definition additional information
		definition	 = processRunDefinitionBean.loadAdditionalInformation(definition);
 
		//Create new RewardSettlementRunDefinition instance and set process run definition object
		RewardSettlementRunDefinition rewardSettlementRunDefinition = new RewardSettlementRunDefinition();
		rewardSettlementRunDefinition.setProcessRunDefinition(definition);
 
		//Load criteria input and scheduling settings from XML 
		rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.loadFromXML(rewardSettlementRunDefinition);
		//Initialise criteria
		rewardSettlementRunDefinition = rewardSettlementProcessRunDefinitionXMLUtilBean.initialize(rewardSettlementRunDefinition);
		
		//Set aliases
		if (processRunLogBean.exists(rewardSettlementRunDefinition.getProcessRunDefinition()))
		{
			rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "1");
		}
		else
		{
			rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "0");
		}
		String latestVersionID = getLatestVersionID(rewardSettlementRunDefinition.getProcessRunDefinition());
		if (latestVersionID==null || ( latestVersionID!=null && latestVersionID.equals(id)))
		{
			rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
			rewardSettlementRunDefinition.setAlias(LATEST_PROCESS_RUN_DEF_ID, latestVersionID);
		}
		else
		{
			rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "0");
			rewardSettlementRunDefinition.setAlias(LATEST_PROCESS_RUN_DEF_ID, latestVersionID);
		}
	
		//Set mandatory fields
		rewardSettlementRunDefinition.setMandatoryFields();

		return rewardSettlementRunDefinition;
	}
	/**
	 * Creates a rewards settlement run definition.
	 * 
	 * @return a rewards settlement run definition
	 * @throws Exception
	 */
	public RewardSettlementRunDefinition createButton() throws Exception {
		RewardSettlementRunDefinition rewardSettlementRunDefinition = new RewardSettlementRunDefinition();
		
		//Initialise attributes
		rewardSettlementRunDefinition.setProcessRunDefinition((CRMDOProcessRunDefinition)processRunDefinitionBean.construct());
		rewardSettlementRunDefinition.setSchedulerTask(new SchedulerTask());
		rewardSettlementRunDefinition.setSchemeCriteria(new HashSet<RewardSchemeCriterion>());
		
 
		//Set process = process id as defined in modules metadata file
		rewardSettlementRunDefinition.getProcessRunDefinition().setProcess(PROCESS);
 
		//Set module = module id as defined in modules metadata file
		rewardSettlementRunDefinition.getProcessRunDefinition().setModule(MODULE);
 
		//Set type default value
		rewardSettlementRunDefinition.setRewardSettlementType(RewardSettlementType.BOTH);
 
		//Set the definition's mandatory fields
		rewardSettlementRunDefinition.setMandatoryFields();

		return rewardSettlementRunDefinition;
	}
	/**
	 * Saves a rewards settlement run definition.
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to save
	 * @return the saved rewards settlement run definition
	 * @throws Exception
	 */
	public RewardSettlementRunDefinition saveButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
		//Set and validate the definition's mandatory fields
		rewardSettlementRunDefinition.setMandatoryFields();
		validateMandatoryFields(rewardSettlementRunDefinition);
		
		//Convert criteria, input settings and scheduling settings to XML and set them to process run definition data object. 
		rewardSettlementRunDefinition			= rewardSettlementProcessRunDefinitionXMLUtilBean.constructXML(rewardSettlementRunDefinition);

		CRMDOProcessRunDefinition processRunDefinition	= rewardSettlementRunDefinition.getProcessRunDefinition();
 
		//Validate process run definition
		if(processRunDefinition.getCode()==null)
		{
			processRunDefinition.setCode(processRunDefinitionBean.getNextSequenceNumber(SequenceNumber.PROCESSRUNDEFINITIONCODES));
		}
		validateCriteria(rewardSettlementRunDefinition);
		validateActionInput(rewardSettlementRunDefinition);
		validateSchedulingSettings(rewardSettlementRunDefinition);
		
		CRMDORewardDefinition rewardDefinition= (CRMDORewardDefinition)rewardDefinitionBean.loadActiveDefinition();
		rewardDefinition = rewardDefinitionXMLUtilBean.initialize(rewardDefinition);
		if(rewardDefinition.getSettlementAutomation()==null || rewardDefinition.getSettlementAutomation().getInvoiceType()==null || rewardDefinition.getSettlementAutomation().getInvoiceProduct() == null)
		{
			throw new CannotExecuteSettlementNoConfigurationInRewardDefinitionException(getCRMSession());
		}
		
		processRunDefinitionBean.validateAndSave( rewardSettlementRunDefinition.getProcessRunDefinition() );
		String latestVersionID = getLatestVersionID(rewardSettlementRunDefinition.getProcessRunDefinition());
		if (latestVersionID==null || ( latestVersionID!=null && latestVersionID.equals(rewardSettlementRunDefinition.getProcessRunDefinition().getId())))
		{
			rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
		}
		else
		{
			rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "0");
		}
		return rewardSettlementRunDefinition;
	}
	/**
	 * Validate the criteria of the rewards settlement run definition
	 * 
	 * @param notificationRunDefinition - the rewards settlement run definition to validate the criteria of
	 * @throws Exception
	 */
	protected void validateCriteria(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception{
		if (ServiceUtil.isInitialized(rewardSettlementRunDefinition.getSchemeCriteria()) )
		{
			validatorBean.validateUniqueRecord(
					"Scheme", 
					new ArrayList<CRMDO>(rewardSettlementRunDefinition.getSchemeCriteria()), 
					new String[]{"scheme.name"}, 
					new String[]{"Name"});
		}
	}
	/**
	 * Validate the scheduling settings of the rewards settlement run definition
	 * 
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to validate the scheduling settings of
	 * @throws Exception
	 */
	protected void validateSchedulingSettings(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception{
	}
	/**
	 * Loads a rewardrewards settlement run definition form.
	 * 
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to load
	 * @return the loaded form
	 * @throws Exception
	 * @throws CannotModifyException
	 */
	public RewardSettlementRunDefinition editButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception, CannotModifyException {
		CRMDOProcessRunDefinition definition = (CRMDOProcessRunDefinition)processRunDefinitionBean.load(rewardSettlementRunDefinition.getProcessRunDefinition().getId());
		if(definition==null)
		{
			definition = processRunDefinitionBean.loadLatest(rewardSettlementRunDefinition.getProcessRunDefinition().getCode());
		}
		rewardSettlementRunDefinition = loadForm(definition.getId());
		if(rewardSettlementRunDefinition.getAlias(PROCESS_RUN_LOG_ENTRIES_EXIST).equals("1"))
		{
			rewardSettlementRunDefinition =  amendButton(rewardSettlementRunDefinition);
		}
		return rewardSettlementRunDefinition;
	}
	/**
	 * Amend a rewardrewards settlement run definition.
	 * 
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to amend
	 * @return the amended rewards settlement run definition
	 * @throws Exception
	 * @throws CannotModifyException
	 */
	public RewardSettlementRunDefinition amendButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
		CRMDOProcessRunDefinition processRunDefinition		= rewardSettlementRunDefinition.getProcessRunDefinition();
		CRMDOProcessRunDefinition newProcessRunDefinition	= (CRMDOProcessRunDefinition) processRunDefinition.clone();
		newProcessRunDefinition.setVersion(new Integer(processRunDefinition.getVersion().intValue()+1));
		rewardSettlementRunDefinition.setProcessRunDefinition(newProcessRunDefinition);
		rewardSettlementRunDefinition.setAlias(PROCESS_RUN_LOG_ENTRIES_EXIST, "0" );
		rewardSettlementRunDefinition.setAlias(IS_PROCESS_RUN_DEF_LATEST_VERSION, "1");
		return rewardSettlementRunDefinition;
	}
	/**
	 * Submit a rewards rewardsettlement run definition.
	 * 
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to submit
	 * @return the submitted form
	 * @throws Exception
	 * @throws CannotModifyException
	 */
	public RewardSettlementRunDefinition submitButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
		CRMDORewardDefinition rewardDefinition= (CRMDORewardDefinition)rewardDefinitionBean.loadActiveDefinition();
		rewardDefinition = rewardDefinitionXMLUtilBean.initialize(rewardDefinition);
		if(rewardDefinition.getSettlementAutomation()==null || rewardDefinition.getSettlementAutomation().getInvoiceType()==null || rewardDefinition.getSettlementAutomation().getInvoiceProduct() == null)
		{
			throw new CannotExecuteSettlementNoConfigurationInRewardDefinitionException(getCRMSession());
		}
		
		//Create a process run log object and set the process run definition
		CRMDOProcessRunLog processRunLog	= (CRMDOProcessRunLog) processRunLogBean.construct(rewardSettlementRunDefinition.getProcessRunDefinition());
		//submit the process run log for execution
		submit(processRunLog);
		return rewardSettlementRunDefinition;
	}
	
	/**
	 * Validate the action input of the rewards settlement run definition
	 * 
	 * @param definition - the rewards settlement run definition to validate the action input of
	 * @throws Exception
	 */
	protected void validateActionInput(RewardSettlementRunDefinition definition) throws Exception{
		if (definition.getRewardSettlementType()==null)
		{
			throw new MandatoryFieldException(getCRMSession(),"key_reward_settlement_type");
		}
	}
	
	/**
	 * Delete a rewards settlement run definition.
	 * 
	 * @param rewardSettlementRunDefinition - the rewards settlement run definition to delete
	 * @return the deleted form
	 * @throws Exception
	 * @throws CannotModifyException
	 */
	public RewardSettlementRunDefinition deleteButton(RewardSettlementRunDefinition rewardSettlementRunDefinition) throws Exception {
		processRunDefinitionBean.validateAndDelete(rewardSettlementRunDefinition.getProcessRunDefinition());
		return rewardSettlementRunDefinition;
	}
	/**
	 * Adds a list of reward schemes to the rewards settlement definition run.
	 *  
	 * @param entityIDs - the ids of the reward schemes
	 * @return the list of reward schemes
	 * @throws Exception
	 */
	public ArrayList<CRMDO> addRewardSchemeCriterionButton(ArrayList<String> entityIDs) throws Exception {
		ArrayList<CRMDO> dtos = new ArrayList<CRMDO>();
		if (entityIDs!=null && entityIDs.size() > 0)
		{
			for (int i=0; i<entityIDs.size(); i++)
			{
				CRMDORewardScheme scheme = (CRMDORewardScheme)rewardSchemeBean.load(entityIDs.get(i));	
				RewardSchemeCriterion schemeCriterion=new RewardSchemeCriterion();
				schemeCriterion.setScheme(scheme);
				dtos.add(schemeCriterion);
			}
		}
		return dtos;
	}
	/**
	 * Removes a list of reward scheme criteria from a rewards settlement run definition.
	 * 
	 * @param dtoList - a list of reward scheme criteria to delete
	 * @return the updated list
	 * @throws Exception
	 */
	public ArrayList<CRMDO> removeRewardSchemeCriterionButton(ArrayList<CRMDO> dtoList) throws Exception {
		return setDeleted(dtoList);
	}
	/**
	 * Loads the rewards settlement type options.
	 * 
	 * @return the rewards settlement type options
	 * @throws Exception
	 */
	public LookupBuilder getRewardSettlementTypeOptions() throws Exception {
		return getRewardSettlementTypeOptions(null);
	}
	/**
	 * Loads the rewards settlement type options.
	 * 
	 * @param emptyValue - an empty value for a rewards settlement type
	 * @return the rewards settlement type options
	 * @throws Exception
	 */
	public LookupBuilder getRewardSettlementTypeOptions(String emptyValue) throws Exception {
		LookupBuilder builder = new LookupBuilder();
		if (emptyValue!=null)
		{
			builder.build("", emptyValue);
		}
		RewardSettlementType[] rewardSettlementTypes = RewardSettlementType.values();
		for (int i=0; i<rewardSettlementTypes.length; i++)
		{
			RewardSettlementType rewardSettlementType = rewardSettlementTypes[i];
			builder.build(rewardSettlementType.toString(), rewardSettlementType.getLabel());
		}
		return builder;
	}
}

...