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