package com.gpc.tams.sync.customer.databuilder; import com.gpc.tams.model.customer.domain.CustomerRecord; import org.apache.commons.lang.StringUtils; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.gpc.tams.common.Base; import com.gpc.tams.repository.common.DistributionCenter; import com.gpc.tams.repository.common.StoreProfile; import com.gpc.tams.repository.customer.CustomerChangeQueue; import com.gpc.tams.repository.employeemanagement.Employee; import com.gpc.tams.sync.customer.constants.CustomerChangeQueueAction; import com.gpc.tams.sync.customer.constants.CustomerDomainTable; public abstract class AbstractCustomerDomainDataBuilder extends Base { protected static final String YYYY_MM_DD_HH_MM_SS_PATTERN = "yyyy-MM-dd HH:mm:ss"; protected static final String YYYY_MM_DD_HH_MM_SS_SSS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; protected static final String YYYY_MM_DD_PATTERN = "yyyy-MM-dd"; protected StoreProfile storeProfile = null; protected DistributionCenter distributionCenter = null; protected CustomerChangeQueue customerChangeQueue = null; protected String customerChangeQueueTriggerSource = null; protected String customerChangeQueueAction = null; protected String customerChangeQueueDeletedValue = null; protected boolean isCustomerScoreCardValidation = false; public void initialize(final CustomerChangeQueue customerChangeQueue, final StoreProfile storeProfile, final DistributionCenter distributionCenter) { final String methodSpec = "[CustomerSync] AbstractCustomerDomainDataBuilder.initialize - "; context.logDebug(methodSpec); this.storeProfile = storeProfile; this.distributionCenter = distributionCenter; this.customerChangeQueue = customerChangeQueue; if (customerChangeQueue != null) { this.customerChangeQueueTriggerSource = customerChangeQueue.getTriggerSource(); this.customerChangeQueueAction = customerChangeQueue.getAction(); this.customerChangeQueueDeletedValue = customerChangeQueue.getDeletedValue(); } } @Transactional(propagation = Propagation.REQUIRED) protected Employee findEmployeeByEmployeeId(final Integer modifiedByEmployeeId) { final String methodSpec = "[CustomerSync] AbstractCustomerDomainDataBuilder.findEmployeeByEmployeeId - "; context.logDebug(methodSpec); Employee employee = null; if (modifiedByEmployeeId != null && modifiedByEmployeeId != -1 && storeProfile != null) { try { employee = employeeDAO.findById(modifiedByEmployeeId.intValue(), storeProfile.getLocation()); } catch (Exception exception) { context.logError(methodSpec + "Error occured while retrieving Employee Details " + exception); } } return employee; } protected boolean doesDeletedRecordExists(final String triggerSource) { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(triggerSource) && StringUtils.isNotEmpty(customerChangeQueueAction) && customerChangeQueueAction.equalsIgnoreCase("D") && StringUtils.isNotEmpty(customerChangeQueueDeletedValue) && !isCustomerScoreCardValidation(); } protected boolean doesUpdatedRecordExists(final String triggerSource) { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(triggerSource) && StringUtils.isNotEmpty(customerChangeQueueAction) && (customerChangeQueueAction.equalsIgnoreCase("U") || customerChangeQueueAction.equalsIgnoreCase("I")); } protected String getCustomerChangeQueueActionCode(final String triggerSource) { String operationCode = CustomerChangeQueueAction.NO_CHANGE; if (doesUpdatedRecordExists(triggerSource)) { operationCode = customerChangeQueueAction .equalsIgnoreCase(CustomerChangeQueueAction.INSERT) ? CustomerChangeQueueAction.CREATE : CustomerChangeQueueAction.UPDATE; } return operationCode; } protected String getOperationCode(final String triggerSource, final int tableRecordId) { String operationCode = CustomerChangeQueueAction.NO_CHANGE; if (doesUpdatedRecordExists(triggerSource) && isCustomerChangeQueueRecord(tableRecordId)) { operationCode = customerChangeQueueAction .equalsIgnoreCase(CustomerChangeQueueAction.INSERT) ? CustomerChangeQueueAction.CREATE : CustomerChangeQueueAction.UPDATE; } return operationCode; } protected boolean isCustomerAccountRenumbered() { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(CustomerDomainTable.CUSTOMER) && StringUtils.isNotEmpty(customerChangeQueueAction) && customerChangeQueueAction.equalsIgnoreCase(CustomerChangeQueueAction.UPDATE) && StringUtils.isNotEmpty(customerChangeQueueDeletedValue); } protected boolean isCustomerTableRecordDeleted() { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(CustomerDomainTable.CUSTOMER) && StringUtils.isNotEmpty(customerChangeQueueAction) && customerChangeQueueAction.equalsIgnoreCase(CustomerChangeQueueAction.DELETE) && StringUtils.isNotEmpty(customerChangeQueueDeletedValue); } protected boolean isStoreNumberOrDcChange() { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(CustomerDomainTable.STORE_PROFILE); } protected boolean isMainStoreARNumberChange() { return StringUtils.isNotEmpty(customerChangeQueueTriggerSource) && customerChangeQueueTriggerSource.equalsIgnoreCase(CustomerDomainTable.AR_PROFILE); } protected boolean isCustomerChangeQueueRecord(final int tableRecordId) { return StringUtils.isNotEmpty(customerChangeQueueDeletedValue) && tableRecordId == convertStringToInt(customerChangeQueueDeletedValue); } public boolean isCustomerScoreCardValidation() { return isCustomerScoreCardValidation; } public void setCustomerScoreCardValidation(boolean isCustomerScoreCardValidation) { this.isCustomerScoreCardValidation = isCustomerScoreCardValidation; } protected int convertStringToInt(final String inputValue) { final String methodSpec = "CustomerDomainDataBuilder.convertStringToInt - "; context.logDebug(methodSpec + "Input Value = " + inputValue); int outputValue = -1; if (StringUtils.isNotEmpty(inputValue)) { try { outputValue = Integer.parseInt(inputValue); } catch (Exception exception) { context.logError(methodSpec + " Input value = " + inputValue + " is not numeric = " + exception); } } context.logDebug(methodSpec + "Output Value = " + inputValue); return outputValue; } protected String isFlagChecked(final boolean flagStatus) { return flagStatus ? "Y" : "N"; } protected String isFlagChecked(final Integer flagStatus) { return flagStatus != null && (flagStatus.intValue() == 1 || flagStatus.intValue() == 11) ? "Y" : "N"; } protected Integer convertToInt(final String inputValue) { final String methodSpec = "[CustomerSync] CustomerDomainDataBuilder.convertToInt - "; context.logDebug(methodSpec); Integer outputValue = null; if (StringUtils.isNotEmpty(inputValue)) { try { outputValue = Integer.valueOf(inputValue); } catch (NumberFormatException exception) { context.logError(methodSpec + "Error occured while converting input string to int " + exception); } } return outputValue; } public void setCustomerChangeQueue(CustomerRecord customerRecord) { if (customerRecord != null) { this.customerChangeQueueTriggerSource = customerRecord.getTriggerSource(); this.customerChangeQueueAction = customerRecord.getAction(); this.customerChangeQueueDeletedValue = customerRecord.getDeletedValue(); } } }