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.purap.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.purap.PurapConstants;
020    import org.kuali.kfs.module.purap.PurapKeyConstants;
021    import org.kuali.kfs.module.purap.PurapPropertyConstants;
022    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
023    import org.kuali.kfs.sys.KFSConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.kfs.sys.document.validation.GenericValidation;
026    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
027    import org.kuali.rice.kim.bo.Person;
028    import org.kuali.rice.kim.service.PersonService;
029    import org.kuali.rice.kns.util.GlobalVariables;
030    import org.kuali.rice.kns.util.MessageMap;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    
033    public class PurchaseOrderAssignedUserValidation extends GenericValidation {
034        private PersonService<Person> personService;
035        
036        /**
037         * Validation to check that the assigned user exists in the system.
038         * @return boolean false if the assigned user doesn't exist in the system.
039         */
040        public boolean validate(AttributedDocumentEvent event) {
041            PurchaseOrderDocument poDocument = (PurchaseOrderDocument)event.getDocument();
042            MessageMap errorMap = GlobalVariables.getMessageMap();
043            errorMap.clearErrorPath();
044            errorMap.addToErrorPath(PurapConstants.DETAIL_TAB_ERRORS);
045            boolean valid = true;
046    
047            // assigned user is not a required field
048            String principalName = poDocument.getAssignedUserPrincipalName();
049            if (StringUtils.isEmpty(principalName))
050                return true;
051            
052            // check to see if the person exists in the database
053            //if (ObjectUtils.isNull(personService.getPersonByPrincipalName(principalName))) {
054            // the following if is equivalent to the above if, since the name->ID conversion is done when the PO form is submit
055            // so if ID is null that means no person is found by the name
056            if (ObjectUtils.isNull(poDocument.getAssignedUserPrincipalId())) {            
057                valid = false;
058                errorMap.putError(PurapPropertyConstants.ASSIGNED_USER_PRINCIPAL_NAME, PurapKeyConstants.ERROR_NONEXIST_ASSIGNED_USER);
059            }
060    
061            errorMap.clearErrorPath();
062            return valid;
063        }
064    
065        /**
066         * @return Returns the personService.
067         */
068        protected PersonService<Person> getPersonService() {
069            if(personService==null)
070                personService = SpringContext.getBean(PersonService.class);
071            return personService;
072        }
073    
074    }