View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.vnd.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase;
20  import org.kuali.ole.sys.OLEConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.vnd.VendorConstants;
23  import org.kuali.ole.vnd.VendorKeyConstants;
24  import org.kuali.ole.vnd.VendorUtils;
25  import org.kuali.ole.vnd.businessobject.VendorDetail;
26  import org.kuali.ole.vnd.businessobject.VendorType;
27  import org.kuali.ole.vnd.document.service.VendorService;
28  import org.kuali.rice.core.api.datetime.DateTimeService;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  /**
35   * Business Prerules applicable to VendorDetail documents. These PreRules checks for the VendorDetail that needs to occur while
36   * still in the Struts processing. This includes setting the vendorName field using the values from vendorLastName and
37   * vendorFirstName, and could be used for many other purposes.
38   */
39  public class VendorPreRules extends MaintenancePreRulesBase {
40  
41      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorPreRules.class);
42  
43      protected VendorDetail newVendorDetail;
44      protected String personId;
45  
46      public VendorPreRules() {
47      }
48  
49      /**
50       * Returns the Universal User Id of the current logged-in user
51       * 
52       * @return String the PersonId
53       */
54  
55      public String getPersonId() {
56          if (ObjectUtils.isNull(personId)) {
57              this.personId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
58          }
59          return this.personId;
60      }
61  
62      /**
63       * Sets up a convenience object and few other vendor attributes
64       * 
65       * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
66       */
67      @Override
68      protected boolean doCustomPreRules(MaintenanceDocument document) {
69          setupConvenienceObjects(document);
70          setVendorNamesAndIndicator(document);
71          setVendorRestriction(document);
72          if (StringUtils.isBlank(question) || (question.equals(VendorConstants.CHANGE_TO_PARENT_QUESTION_ID))) {
73              detectAndConfirmChangeToParent(document);
74          }
75          
76          //check to page review ONLY if it is a new vendor
77          if (newVendorDetail.getVendorHeaderGeneratedIdentifier() == null &&
78                  newVendorDetail.getVendorDetailAssignedIdentifier() == null) {
79              displayReview(document);
80          }
81          return true;
82      }
83  
84      /**
85       * Sets the convenience objects like newVendorDetail and oldVendorDetail, so you have short and easy handles to the new and old
86       * objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all
87       * sub-objects from the DB by their primary keys, if available.
88       * 
89       * @param document - the maintenanceDocument being evaluated
90       */
91      protected void setupConvenienceObjects(MaintenanceDocument document) {
92          // setup newAccount convenience objects, make sure all possible sub-objects are populated
93          newVendorDetail = (VendorDetail) document.getNewMaintainableObject().getBusinessObject();
94      }
95  
96      /**
97       * Sets the vendorFirstLastNameIndicator to true if the first name and last name fields were filled in but the vendorName field
98       * is blank and it sets the vendorFirstLastNameIndicator to false if the vendorName field is filled in and the first name and
99       * last name fields were both blank.
100      * 
101      * @param document - the maintenanceDocument being evaluated
102      */
103     protected void setVendorNamesAndIndicator(MaintenanceDocument document) {
104         if (StringUtils.isBlank(newVendorDetail.getVendorName()) && !StringUtils.isBlank(newVendorDetail.getVendorFirstName()) && !StringUtils.isBlank(newVendorDetail.getVendorLastName())) {
105 
106             newVendorDetail.setVendorFirstLastNameIndicator(true);
107             newVendorDetail.setVendorFirstName(removeDelimiter(newVendorDetail.getVendorFirstName()));
108             newVendorDetail.setVendorLastName(removeDelimiter(newVendorDetail.getVendorLastName()));
109 
110         }
111         else if (!StringUtils.isBlank(newVendorDetail.getVendorName()) && StringUtils.isBlank(newVendorDetail.getVendorFirstName()) && StringUtils.isBlank(newVendorDetail.getVendorLastName())) {
112             newVendorDetail.setVendorFirstLastNameIndicator(false);
113         }
114     }
115 
116     /**
117      * Sets the vendorRestrictedDate and vendorRestrictedPersonIdentifier if the vendor restriction has changed from No to Yes.
118      * 
119      * @param document - the maintenanceDocument being evaluated
120      */
121     protected void setVendorRestriction(MaintenanceDocument document) {
122         VendorDetail oldVendorDetail = (VendorDetail) document.getOldMaintainableObject().getBusinessObject();
123         Boolean oldVendorRestrictedIndicator = null;
124         if (ObjectUtils.isNotNull(oldVendorDetail)) {
125             oldVendorRestrictedIndicator = oldVendorDetail.getVendorRestrictedIndicator();
126         }
127         // If the Vendor Restricted Indicator will change, change the date and person id appropriately.
128         if ((ObjectUtils.isNull(oldVendorRestrictedIndicator) || (!oldVendorRestrictedIndicator)) && ObjectUtils.isNotNull(newVendorDetail.getVendorRestrictedIndicator()) && newVendorDetail.getVendorRestrictedIndicator()) {
129             // Indicator changed from (null or false) to true.
130             newVendorDetail.setVendorRestrictedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
131             newVendorDetail.setVendorRestrictedPersonIdentifier(getPersonId());
132         }
133         else if (ObjectUtils.isNotNull(oldVendorRestrictedIndicator) && oldVendorRestrictedIndicator && ObjectUtils.isNotNull(newVendorDetail.getVendorRestrictedIndicator()) && (!newVendorDetail.getVendorRestrictedIndicator())) {
134             // Indicator changed from true to false.
135             newVendorDetail.setVendorRestrictedDate(null);
136             newVendorDetail.setVendorRestrictedPersonIdentifier(null);
137         }
138 
139     }
140 
141     /**
142      * This is a helper method to remove all the delimiters from the vendor name
143      * 
144      * @param str the original vendorName
145      * @return result String the vendorName after the delimiters have been removed
146      */
147     protected String removeDelimiter(String str) {
148         String result = str.replaceAll(VendorConstants.NAME_DELIM, OLEConstants.BLANK_SPACE);
149         return result;
150     }
151 
152 
153     /**
154      * Displays a review if indicated by the vendor type and the associated text from that type
155      * 
156      * @param document - vendordetail document
157      */
158     public void displayReview(MaintenanceDocument document) {
159         VendorDetail vendorDetail = (VendorDetail) document.getDocumentBusinessObject();
160 
161         VendorType vendorType = vendorDetail.getVendorHeader().getVendorType();
162 
163         if (vendorType == null) {
164             vendorType = new VendorType();
165             vendorType.setVendorTypeCode(vendorDetail.getVendorHeader().getVendorTypeCode());
166             vendorType = (VendorType) SpringContext.getBean(BusinessObjectService.class).retrieve(vendorType);
167         }
168         if (vendorType != null && vendorType.isVendorShowReviewIndicator()) {
169             String questionText = vendorType.getVendorReviewText();
170 
171             //
172             // Only recognize the review request if the question text isn't null.
173             // Why preview something that doesn't exist?
174             //
175             if (questionText != null) {
176 
177                 if (vendorDetail.getVendorName() != null) {
178                     questionText = questionText.replace("{0}", vendorDetail.getVendorName());
179                 }
180                 else {
181                     questionText = questionText.replace("{0}", "(not entered)");
182                 }
183                 questionText = questionText.replace("{1}", document.getDocumentNumber());
184                 Boolean proceed = super.askOrAnalyzeYesNoQuestion(VendorConstants.ACKNOWLEDGE_NEW_VENDOR_INFO, questionText);
185 
186                 if (!proceed) {
187                     abortRulesCheck();
188                 }
189             }
190         }
191     }
192     
193    /**
194      * This method displays a review if indicated by the vendor type and the associated text from that type This method screens the
195      * current document for changes from division vendor to parent vendor. If the document does contain such a change, the question
196      * framework is invoked to obtain the user's confirmation for the change. If confirmation is obtained, a note is added to the
197      * old parent vendor. Indicators are set appropriately.
198      * 
199      * @param document The vendor-change-containing MaintenanceDocument under examination
200      */
201     protected void detectAndConfirmChangeToParent(MaintenanceDocument document) {
202         boolean proceed = true;
203         VendorDetail oldVendorDetail = (VendorDetail) document.getOldMaintainableObject().getBusinessObject();
204         boolean oldVendorIsParent = oldVendorDetail.isVendorParentIndicator();
205         boolean newVendorIsParent = newVendorDetail.isVendorParentIndicator();
206         if (!oldVendorIsParent && newVendorIsParent) {
207             // A change to division is being tried. Obtain confirmation.
208             VendorDetail oldParentVendor = SpringContext.getBean(VendorService.class).getParentVendor(oldVendorDetail.getVendorHeaderGeneratedIdentifier());
209             String oldParentVendorName = oldParentVendor.getVendorName();
210             String oldParentVendorNumber = oldParentVendor.getVendorNumber();
211             proceed = askOrAnalyzeYesNoQuestion(VendorConstants.CHANGE_TO_PARENT_QUESTION_ID, VendorUtils.buildMessageText(VendorKeyConstants.CONFIRM_VENDOR_CHANGE_TO_PARENT, oldVendorDetail.getVendorName() + "  (" + oldVendorDetail.getVendorNumber() + ")", oldParentVendorName + " (" + oldParentVendorNumber + ")"));
212             if (proceed) {
213                 newVendorDetail.setVendorParentIndicator(true);
214             }
215             else {
216                 newVendorDetail.setVendorParentIndicator(false);
217             }
218         }
219         if (!proceed) {
220             abortRulesCheck();
221         }
222     }
223 
224 }
225