View Javadoc
1   /*
2    * Copyright 2009 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.module.purap.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapKeyConstants;
21  import org.kuali.ole.module.purap.PurapPropertyConstants;
22  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.sys.document.validation.GenericValidation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.kim.api.identity.PersonService;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.MessageMap;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  public class PurchaseOrderAssignedUserValidation extends GenericValidation {
32      private PersonService personService;
33  
34      /**
35       * Validation to check that the assigned user exists in the system.
36       *
37       * @return boolean false if the assigned user doesn't exist in the system.
38       */
39      public boolean validate(AttributedDocumentEvent event) {
40          PurchaseOrderDocument poDocument = (PurchaseOrderDocument) event.getDocument();
41          MessageMap errorMap = GlobalVariables.getMessageMap();
42          errorMap.clearErrorPath();
43          errorMap.addToErrorPath(PurapConstants.DETAIL_TAB_ERRORS);
44          boolean valid = true;
45  
46          // assigned user is not a required field
47          String principalName = poDocument.getAssignedUserPrincipalName();
48          if (StringUtils.isEmpty(principalName))
49              return true;
50  
51          // check to see if the person exists in the database
52          //if (ObjectUtils.isNull(personService.getPersonByPrincipalName(principalName))) {
53          // the following if is equivalent to the above if, since the name->ID conversion is done when the PO form is submit
54          // so if ID is null that means no person is found by the name
55          if (ObjectUtils.isNull(poDocument.getAssignedUserPrincipalId())) {
56              valid = false;
57              errorMap.putError(PurapPropertyConstants.ASSIGNED_USER_PRINCIPAL_NAME, PurapKeyConstants.ERROR_NONEXIST_ASSIGNED_USER);
58          }
59  
60          errorMap.clearErrorPath();
61          return valid;
62      }
63  
64      /**
65       * @return Returns the personService.
66       */
67      protected PersonService getPersonService() {
68          if (personService == null)
69              personService = SpringContext.getBean(PersonService.class);
70          return personService;
71      }
72  
73  }