View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.bo.ui;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
20  import org.kuali.rice.kim.api.type.KimAttributeField;
21  import org.kuali.rice.kim.bo.impl.KimAttributes;
22  import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
23  import org.kuali.rice.kim.framework.type.KimTypeService;
24  import org.kuali.rice.kim.impl.role.RoleBo;
25  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
26  import org.kuali.rice.kim.impl.type.KimTypeBo;
27  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
28  import org.springframework.util.AutoPopulatingList;
29  import org.springframework.util.StringUtils;
30  
31  import javax.persistence.CascadeType;
32  import javax.persistence.Column;
33  import javax.persistence.Entity;
34  import javax.persistence.FetchType;
35  import javax.persistence.Id;
36  import javax.persistence.IdClass;
37  import javax.persistence.JoinColumn;
38  import javax.persistence.JoinColumns;
39  import javax.persistence.ManyToMany;
40  import javax.persistence.OneToMany;
41  import javax.persistence.Table;
42  import javax.persistence.Transient;
43  import javax.persistence.UniqueConstraint;
44  import java.util.ArrayList;
45  import java.util.Collections;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * This is a description of what this class does - shyu don't forget to fill this in. 
52   * 
53   * @author Kuali Rice Team (rice.collab@kuali.org)
54   *
55   */
56  
57  @Entity
58  @IdClass(org.kuali.rice.kim.bo.ui.PersonDocumentRoleId.class)
59  @Table(name="KRIM_PND_ROLE_MT",uniqueConstraints=@UniqueConstraint(columnNames={"FDOC_NBR", "ROLE_ID"}))
60  public class PersonDocumentRole extends KimDocumentBoActivatableEditableBase {
61      private static final Logger LOG = Logger.getLogger(PersonDocumentRole.class);
62  	private static final long serialVersionUID = 4908044213007222739L;
63  	@Id
64  	@Column(name="ROLE_ID")
65  	protected String roleId;
66  	@Column(name="KIM_TYP_ID")
67  	protected String kimTypeId;
68  	@Column(name="ROLE_NM")
69  	protected String roleName;
70  	@Transient
71  	protected RoleBo roleBo;
72  	@Column(name="NMSPC_CD")
73  	protected String namespaceCode;
74  	@Transient
75  	protected KimTypeBo kimRoleType;
76  	@Transient
77  	protected List<? extends KimAttributes> attributes;
78  	@Transient
79  	protected transient List<KimAttributeField> definitions;
80  	@Transient
81  	protected transient Map<String,Object> attributeEntry;
82  	@OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
83      @JoinColumns({
84  		@JoinColumn(name="ROLE_ID",insertable=false,updatable=false),
85  		@JoinColumn(name="FDOC_NBR", insertable=false, updatable=false)
86  	})
87  	protected List<KimDocumentRoleMember> rolePrncpls;
88  	@Transient
89      protected KimDocumentRoleMember newRolePrncpl;
90  	//currently mapped as manyToMany even though it is technically a OneToMany
91  	//The reason for this is it is linked with a partial Composite-id, which technically can't 
92  	//guarantee uniqueness.  
93  	@ManyToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
94      @JoinColumn(name="ROLE_ID",insertable=false,updatable=false)
95      //@JoinColumns({
96  	//	@JoinColumn(name="ROLE_ID",insertable=false,updatable=false),
97  	//	@JoinColumn(name="FDOC_NBR", insertable=false, updatable=false, table="KRIM_PERSON_DOCUMENT_T")
98  	//})
99  	protected List<RoleResponsibilityBo> assignedResponsibilities = new AutoPopulatingList(RoleResponsibilityBo.class);
100 
101 	@Transient
102     protected boolean isEditable = true;
103     
104 	public PersonDocumentRole() {
105 		attributes = new ArrayList<KimAttributes>();	
106 		rolePrncpls = new ArrayList<KimDocumentRoleMember>();	
107 		attributeEntry = new HashMap<String,Object>();
108 	}
109 	
110 	public String getRoleId() {
111 		return this.roleId;
112 	}
113 
114 	public void setRoleId(String roleId) {
115 		this.roleId = roleId;
116 	}
117 
118 	public String getKimTypeId() {
119 		return this.kimTypeId;
120 	}
121 
122 	public void setKimTypeId(String kimTypeId) {
123 		this.kimTypeId = kimTypeId;
124 	}
125 
126 	public String getRoleName() {
127 		return this.roleName;
128 	}
129 
130 	public void setRoleName(String roleName) {
131 		this.roleName = roleName;
132 	}
133 
134 	public List<? extends KimAttributes> getAttributes() {
135 		return this.attributes;
136 	}
137 
138 	public void setAttributes(List<? extends KimAttributes> attributes) {
139 		this.attributes = attributes;
140 	}
141 
142 	public KimTypeBo getKimRoleType() {
143 		if ( kimRoleType == null && StringUtils.hasText(kimTypeId)) {
144 			kimRoleType = KimTypeBo.from(KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId));
145 		}
146 		return kimRoleType;
147 	}
148 
149     public Map<String, KimAttributeField> getDefinitionsKeyedByAttributeName() {
150         final Map<String, KimAttributeField> map = new HashMap<String, KimAttributeField>();
151         for (KimAttributeField field : getDefinitions()) {
152             map.put(field.getAttributeField().getName(), field);
153         }
154         return map;
155     }
156 
157 	public List<KimAttributeField> getDefinitions() {
158 		if (definitions == null || definitions.isEmpty()) {
159 	        KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(KimTypeBo.to(
160                     this.getKimRoleType()));
161 	        //it is possible that the the roleTypeService is coming from a remote application 
162 	        // and therefore it can't be guarenteed that it is up and working, so using a try/catch to catch this possibility.
163 	        try {
164     	        if ( kimTypeService != null ) {
165     	        	definitions = kimTypeService.getAttributeDefinitions(getKimTypeId());
166     	        } else {
167     	        	definitions = Collections.emptyList();
168     	        }
169     		} catch (Exception ex) {
170                 LOG.warn("Not able to retrieve KimTypeService from remote system for KIM Role Type: " + this.getKimRoleType(), ex);
171             }
172 		}
173 		
174 		return definitions;
175 	}
176 
177 	public void setDefinitions(List<KimAttributeField> definitions) {
178 		this.definitions = definitions;
179 	}
180 
181 	public Map<String,Object> getAttributeEntry() {
182 		if (attributeEntry == null || attributeEntry.isEmpty()) {
183 			attributeEntry = KIMServiceLocatorInternal.getUiDocumentService().getAttributeEntries(getDefinitions());
184 		}
185 		
186 		return this.attributeEntry;
187 	}
188 
189 	public void setAttributeEntry(Map<String,Object> attributeEntry) {
190 		this.attributeEntry = attributeEntry;
191 	}
192 
193 	public List<KimDocumentRoleMember> getRolePrncpls() {
194 		return this.rolePrncpls;
195 	}
196 
197 	public void setRolePrncpls(List<KimDocumentRoleMember> rolePrncpls) {
198 		this.rolePrncpls = rolePrncpls;
199 	}
200 
201 	public KimDocumentRoleMember getNewRolePrncpl() {
202 		return this.newRolePrncpl;
203 	}
204 
205 	public void setNewRolePrncpl(KimDocumentRoleMember newRolePrncpl) {
206 		this.newRolePrncpl = newRolePrncpl;
207 	}
208 
209 	public String getNamespaceCode() {
210 		return this.namespaceCode;
211 	}
212 
213 	public void setNamespaceCode(String namespaceCode) {
214 		this.namespaceCode = namespaceCode;
215 	}
216 
217 	public List<RoleResponsibilityBo> getAssignedResponsibilities() {
218 		return this.assignedResponsibilities;
219 	}
220 
221 	public void setAssignedResponsibilities(
222 			List<RoleResponsibilityBo> assignedResponsibilities) {
223 		this.assignedResponsibilities = assignedResponsibilities;
224 	}
225 
226 	/**
227 	 * @return the roleBo
228 	 */
229 	public RoleBo getRoleBo() {
230 		return this.roleBo;
231 	}
232 
233 	/**
234 	 * @param roleBo the roleBo to set
235 	 */
236 	public void setRoleBo(RoleBo roleBo) {
237 		this.roleBo = roleBo;
238 	}
239 
240 	/**
241 	 * @return the isEditable
242 	 */
243 	public boolean isEditable() {
244 		return this.isEditable;
245 	}
246 
247 	/**
248 	 * @param isEditable the isEditable to set
249 	 */
250 	public void setEditable(boolean isEditable) {
251 		this.isEditable = isEditable;
252 	}
253 
254 }