001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.module.ar.document.validation.impl;
017
018 import org.kuali.kfs.module.ar.ArConstants;
019 import org.kuali.kfs.module.ar.ArKeyConstants;
020 import org.kuali.kfs.module.ar.ArPropertyConstants;
021 import org.kuali.kfs.module.ar.businessobject.Customer;
022 import org.kuali.kfs.module.ar.document.service.CustomerService;
023 import org.kuali.kfs.sys.context.SpringContext;
024 import org.kuali.rice.kns.document.Document;
025 import org.kuali.rice.kns.document.MaintenanceDocument;
026 import org.kuali.rice.kns.rules.PromptBeforeValidationBase;
027 import org.kuali.rice.kns.service.KualiConfigurationService;
028 import org.kuali.rice.kns.util.GlobalVariables;
029 import org.kuali.rice.kns.util.ObjectUtils;
030
031 public class CustomerPreRules extends PromptBeforeValidationBase {
032
033 /**
034 * @see org.kuali.rice.kns.rules.PromptBeforeValidationBase#doRules(org.kuali.rice.kns.document.Document)
035 */
036 @Override
037 public boolean doPrompts(Document document) {
038 boolean preRulesOK = true;
039 preRulesOK &= conditionallyAskQuestion(document);
040 return preRulesOK;
041 }
042
043 /**
044 * This method checks if there is another customer with the same name and generates yes/no question
045 *
046 * @param document the maintenance document
047 * @return
048 */
049 protected boolean conditionallyAskQuestion(Document document) {
050 MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
051 Customer newCostomer = (Customer) maintenanceDocument.getNewMaintainableObject().getBusinessObject();
052 boolean shouldAskQuestion = maintenanceDocument.isNew() && checkIfOtherCustomerSameName(newCostomer);
053
054 if (shouldAskQuestion) {
055 String questionText = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(ArKeyConstants.CustomerConstants.MESSAGE_CUSTOMER_WITH_SAME_NAME_EXISTS);
056 boolean confirm = super.askOrAnalyzeYesNoQuestion(ArKeyConstants.CustomerConstants.GENERATE_CUSTOMER_QUESTION_ID, questionText);
057 if (!confirm) {
058 super.abortRulesCheck();
059 }
060 }
061 return true;
062 }
063
064 /**
065 * This method checks if a customer with the same name already exists
066 *
067 * @param newCustomer
068 * @return true if exists, false otherwise
069 */
070 public boolean checkIfOtherCustomerSameName(Customer newCustomer) {
071 boolean exists = false;
072 Customer customer = SpringContext.getBean(CustomerService.class).getCustomerByName(newCustomer.getCustomerName());
073 if (ObjectUtils.isNotNull(customer)) {
074 exists = true;
075 GlobalVariables.getMessageList().add(ArKeyConstants.CustomerConstants.MESSAGE_CUSTOMER_WITH_SAME_NAME_EXISTS);
076 }
077 return exists;
078 }
079
080 }