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