001    /**
002     * Copyright 2005-2013 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.rice.krad.bo;
017    
018    import javax.persistence.Entity;
019    import javax.persistence.Table;
020    import javax.persistence.Transient;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.rice.kim.api.identity.Person;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    
026    /**
027     * Ad Hoc Route Person Business Object
028     *
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    //@IdClass(AdHocRoutePersonId.class)
032    @Entity
033    @Table(name = "KRNS_ADHOC_RTE_ACTN_RECIP_T")
034    public class AdHocRoutePerson extends AdHocRouteRecipient {
035        private static final long serialVersionUID = 1L;
036    
037        @Transient
038        private transient Person person;
039    
040        public AdHocRoutePerson() {
041            setType(PERSON_TYPE);
042    
043            try {
044                person = (Person) KimApiServiceLocator.getPersonService().getPersonImplementationClass().newInstance();
045            } catch (Exception e) {
046                throw new RuntimeException(e);
047            }
048        }
049    
050        @Override
051        public void setType(Integer type) {
052            if (!PERSON_TYPE.equals(type)) {
053                throw new IllegalArgumentException("cannot change type to " + type);
054            }
055            super.setType(type);
056        }
057    
058        @Override
059        public void setId(String id) {
060            super.setId(id);
061    
062            if (StringUtils.isNotBlank(id)) {
063                person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(id);
064                setPerson(person);
065            }
066        }
067    
068        @Override
069        public void setName(String name) {
070            super.setName(name);
071    
072            if (StringUtils.isNotBlank(name) && getId() != null &&
073                    ((person != null) && !StringUtils.equals(person.getName(), name))) {
074                person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(getId());
075                setPerson(person);
076            }
077        }
078    
079        public Person getPerson() {
080            if ((person == null) || !StringUtils.equals(person.getPrincipalName(), getId())) {
081                person = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(getId());
082    
083                if (person == null) {
084                    try {
085                        person = (Person) KimApiServiceLocator.getPersonService().getPersonImplementationClass()
086                                .newInstance();
087                    } catch (Exception e) {
088                        throw new RuntimeException(e);
089                    }
090                }
091            }
092    
093            return person;
094        }
095    
096        public void setPerson(Person person) {
097            this.person = person;
098            if (person != null) {
099                this.id = person.getPrincipalName();
100                this.name = person.getName();
101            }
102        }
103    }
104