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