001/**
002 * Copyright 2005-2016 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.kim.bo.ui;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import javax.persistence.CascadeType;
025import javax.persistence.Column;
026import javax.persistence.Entity;
027import javax.persistence.Id;
028import javax.persistence.JoinColumn;
029import javax.persistence.JoinColumns;
030import javax.persistence.ManyToMany;
031import javax.persistence.OneToMany;
032import javax.persistence.Table;
033import javax.persistence.Transient;
034
035import org.apache.log4j.Logger;
036import org.eclipse.persistence.annotations.JoinFetch;
037import org.eclipse.persistence.annotations.JoinFetchType;
038import org.kuali.rice.kim.api.services.KimApiServiceLocator;
039import org.kuali.rice.kim.api.type.KimAttributeField;
040import org.kuali.rice.kim.bo.impl.KimAttributes;
041import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
042import org.kuali.rice.kim.framework.type.KimTypeService;
043import org.kuali.rice.kim.impl.role.RoleBo;
044import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
045import org.kuali.rice.kim.impl.type.KimTypeBo;
046import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
047import org.springframework.util.AutoPopulatingList;
048import org.springframework.util.StringUtils;
049
050/**
051 * This is a description of what this class does - shyu don't forget to fill this in. 
052 * 
053 * @author Kuali Rice Team (rice.collab@kuali.org)
054 *
055 */
056@Entity
057@Table(name = "KRIM_PND_ROLE_MT")
058public class PersonDocumentRole extends KimDocumentBoActivatableEditableBase {
059    private static final Logger LOG = Logger.getLogger(PersonDocumentRole.class);
060    private static final long serialVersionUID = 4908044213007222739L;
061
062    @Id
063    @Column(name = "ROLE_ID")
064    protected String roleId;
065
066    @Column(name = "KIM_TYP_ID")
067    protected String kimTypeId;
068
069    @Column(name = "ROLE_NM")
070    protected String roleName;
071
072    @Transient
073    protected RoleBo roleBo;
074
075    @Column(name = "NMSPC_CD")
076    protected String namespaceCode;
077
078    @Transient
079    protected KimTypeBo kimRoleType;
080
081    @Transient
082    protected List<? extends KimAttributes> attributes;
083
084    @Transient
085    protected transient List<KimAttributeField> definitions;
086
087    @Transient
088    protected transient Map<String, Object> attributeEntry;
089
090    @JoinFetch(value= JoinFetchType.OUTER)
091    @OneToMany(targetEntity = KimDocumentRoleMember.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
092    @JoinColumns({ 
093        @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false), 
094        @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID", insertable = false, updatable = false) })
095    protected List<KimDocumentRoleMember> rolePrncpls;
096
097    @Transient
098    protected KimDocumentRoleMember newRolePrncpl;
099
100    @Transient
101    protected boolean isEditable = true;
102
103    public PersonDocumentRole() {
104        attributes = new ArrayList<KimAttributes>();
105        rolePrncpls = new ArrayList<KimDocumentRoleMember>();
106        attributeEntry = new HashMap<String, Object>();
107    }
108
109    public String getRoleId() {
110        return this.roleId;
111    }
112
113    public void setRoleId(String roleId) {
114        this.roleId = roleId;
115    }
116
117    public String getKimTypeId() {
118        return this.kimTypeId;
119    }
120
121    public void setKimTypeId(String kimTypeId) {
122        this.kimTypeId = kimTypeId;
123    }
124
125    public String getRoleName() {
126        return this.roleName;
127    }
128
129    public void setRoleName(String roleName) {
130        this.roleName = roleName;
131    }
132
133    public List<? extends KimAttributes> getAttributes() {
134        return this.attributes;
135    }
136
137    public void setAttributes(List<? extends KimAttributes> attributes) {
138        this.attributes = attributes;
139    }
140
141    public KimTypeBo getKimRoleType() {
142        if (kimRoleType == null && StringUtils.hasText(kimTypeId)) {
143            kimRoleType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
144        }
145        return kimRoleType;
146    }
147
148    public Map<String, KimAttributeField> getDefinitionsKeyedByAttributeName() {
149        final Map<String, KimAttributeField> map = new HashMap<String, KimAttributeField>();
150        for (KimAttributeField field : getDefinitions()) {
151            map.put(field.getAttributeField().getName(), field);
152        }
153        return map;
154    }
155
156    public List<KimAttributeField> getDefinitions() {
157        if (definitions == null || definitions.isEmpty()) {
158            KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(KimTypeBo.to(this.getKimRoleType()));
159            //it is possible that the the roleTypeService is coming from a remote application                        
160            // and therefore it can't be guarenteed that it is up and working, so using a try/catch to catch this possibility.                       
161            try {
162                if (kimTypeService != null) {
163                    definitions = kimTypeService.getAttributeDefinitions(getKimTypeId());
164                } else {
165                    definitions = Collections.emptyList();
166                }
167            } catch (Exception ex) {
168                LOG.warn("Not able to retrieve KimTypeService from remote system for KIM Role Type: " + this.getKimRoleType(), ex);
169            }
170        }
171        return definitions;
172    }
173
174    public void setDefinitions(List<KimAttributeField> definitions) {
175        this.definitions = definitions;
176    }
177
178    public Map<String, Object> getAttributeEntry() {
179        if (attributeEntry == null || attributeEntry.isEmpty()) {
180            attributeEntry = KIMServiceLocatorInternal.getUiDocumentService().getAttributeEntries(getDefinitions());
181        }
182        return this.attributeEntry;
183    }
184
185    public void setAttributeEntry(Map<String, Object> attributeEntry) {
186        this.attributeEntry = attributeEntry;
187    }
188
189    public List<KimDocumentRoleMember> getRolePrncpls() {
190        return this.rolePrncpls;
191    }
192
193    public void setRolePrncpls(List<KimDocumentRoleMember> rolePrncpls) {
194        this.rolePrncpls = rolePrncpls;
195    }
196
197    public KimDocumentRoleMember getNewRolePrncpl() {
198        return this.newRolePrncpl;
199    }
200
201    public void setNewRolePrncpl(KimDocumentRoleMember newRolePrncpl) {
202        this.newRolePrncpl = newRolePrncpl;
203    }
204
205    public String getNamespaceCode() {
206        return this.namespaceCode;
207    }
208
209    public void setNamespaceCode(String namespaceCode) {
210        this.namespaceCode = namespaceCode;
211    }
212
213    /**
214         * @return the roleBo
215         */
216    public RoleBo getRoleBo() {
217        return this.roleBo;
218    }
219
220    /**
221         * @param roleBo the roleBo to set
222         */
223    public void setRoleBo(RoleBo roleBo) {
224        this.roleBo = roleBo;
225    }
226
227    /**
228         * @return the isEditable
229         */
230    public boolean isEditable() {
231        return this.isEditable;
232    }
233
234    /**
235         * @param isEditable the isEditable to set
236         */
237    public void setEditable(boolean isEditable) {
238        this.isEditable = isEditable;
239    }
240}