View Javadoc

1   /**
2    * Copyright 2005-2012 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.document;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.hibernate.annotations.Fetch;
21  import org.hibernate.annotations.FetchMode;
22  import org.hibernate.annotations.Type;
23  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
24  import org.kuali.rice.kim.api.KimConstants;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.kim.api.type.KimAttributeField;
27  import org.kuali.rice.kim.api.type.KimType;
28  import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
29  import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
30  import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
31  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
32  import org.kuali.rice.krad.service.SequenceAccessorService;
33  import org.springframework.util.AutoPopulatingList;
34  
35  import javax.persistence.AttributeOverride;
36  import javax.persistence.AttributeOverrides;
37  import javax.persistence.CascadeType;
38  import javax.persistence.Column;
39  import javax.persistence.Entity;
40  import javax.persistence.FetchType;
41  import javax.persistence.JoinColumn;
42  import javax.persistence.OneToMany;
43  import javax.persistence.Table;
44  import javax.persistence.Transient;
45  import java.util.ArrayList;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  
51  /**
52   * This is a description of what this class does - bhargavp don't forget to fill this in.
53   *
54   * @author Kuali Rice Team (rice.collab@kuali.org)
55   *
56   */
57  @Entity
58  @AttributeOverrides({
59  	@AttributeOverride(name="documentNumber",column=@Column(name="FDOC_NBR"))
60  })
61  @Table(name="KRIM_GRP_DOCUMENT_T")
62  public class IdentityManagementGroupDocument extends IdentityManagementTypeAttributeTransactionalDocument {
63  	private static final Logger LOG = Logger.getLogger(IdentityManagementGroupDocument.class);
64  	
65  	private static final long serialVersionUID = 1L;
66  	
67  	// principal data
68  	@Column(name="GRP_ID")
69  	protected String groupId;
70  	@Column(name="KIM_TYP_ID")
71  	protected String groupTypeId;
72  	@Transient
73  	protected String groupTypeName;
74  	@Column(name="GRP_NMSPC")
75  	protected String groupNamespace;
76  	@Column(name="GRP_NM")
77  	protected String groupName;
78  	@Column(name="GRP_DESC")
79  	protected String groupDescription;
80  	@Type(type="yes_no")
81  	@Column(name="ACTV_IND")
82  	protected boolean active = true;
83  
84  	@Transient
85  	protected boolean editing;
86  
87  	@OneToMany(targetEntity = GroupDocumentMember.class, fetch = FetchType.EAGER, cascade=CascadeType.ALL)
88  	@Fetch(value = FetchMode.SELECT)
89  	@JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
90  	private List<GroupDocumentMember> members = new AutoPopulatingList(GroupDocumentMember.class);
91  	@OneToMany(targetEntity = GroupDocumentQualifier.class, fetch = FetchType.EAGER, cascade=CascadeType.ALL)
92  	@Fetch(value = FetchMode.SELECT)
93  	@JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
94  	private List<GroupDocumentQualifier> qualifiers = new AutoPopulatingList(GroupDocumentQualifier.class);
95  
96  	public IdentityManagementGroupDocument() {
97  	}
98  	
99  	/**
100 	 * @return the active
101 	 */
102 	public boolean isActive() {
103 		return this.active;
104 	}
105 
106 	/**
107 	 * @param active the active to set
108 	 */
109 	public void setActive(boolean active) {
110 		this.active = active;
111 	}
112 
113 	/**
114 	 * @param groupId the groupId to set
115 	 */
116 	public void setRoleId(String groupId) {
117 		this.groupId = groupId;
118 	}
119 
120 	/**
121 	 * @param member the members to set
122 	 */
123 	public void addMember(GroupDocumentMember member) {
124        	getMembers().add(member);
125 	}
126 
127 	/**
128 	 * @return the kimType
129 	 */
130 	public KimType getKimType() {
131         if (getGroupTypeId() != null) {
132 		    return KimApiServiceLocator.getKimTypeInfoService().getKimType(getGroupTypeId());
133         }
134         return null;
135 	}
136 	
137 	public GroupDocumentMember getBlankMember() {
138 		return new GroupDocumentMember();
139 	}
140 
141 	/**
142 	 * @see org.kuali.rice.krad.document.DocumentBase#doRouteStatusChange(org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange)
143 	 */
144 	@Override
145 	public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
146 		super.doRouteStatusChange(statusChangeEvent);
147 		if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
148 			KIMServiceLocatorInternal.getUiDocumentService().saveGroup(this);
149 		}
150 	}
151 
152 	@Override
153 	public void prepareForSave(){
154 		String groupId;
155 		if(StringUtils.isBlank(getGroupId())){
156 			SequenceAccessorService sas = getSequenceAccessorService();
157 			Long nextSeq = sas.getNextAvailableSequenceNumber(
158 					"KRIM_GRP_ID_S", this.getClass());
159 			groupId = nextSeq.toString();
160 			setGroupId(groupId);
161 		} else{
162 			groupId = getGroupId();
163 		}
164 		if(getMembers()!=null){
165 			String groupMemberId;
166 			for(GroupDocumentMember member: getMembers()){
167 				member.setGroupId(getGroupId());
168 				if(StringUtils.isBlank(member.getGroupMemberId())){
169 					SequenceAccessorService sas = getSequenceAccessorService();
170 					Long nextSeq = sas.getNextAvailableSequenceNumber(
171 							"KRIM_GRP_MBR_ID_S", this.getClass());
172 					groupMemberId = nextSeq.toString();
173 					member.setGroupMemberId(groupMemberId);
174 				}
175 				if (StringUtils.isBlank(member.getDocumentNumber())) {
176 					member.setDocumentNumber(getDocumentNumber());
177 				}
178  			}
179 		}
180 		int index = 0;
181 		// this needs to be checked - are all qualifiers present?
182 		if(getDefinitions()!=null){
183 			for(KimAttributeField key : getDefinitions()) {
184 				if ( getQualifiers().size() > index ) {
185 					GroupDocumentQualifier qualifier = getQualifiers().get(index);
186 					qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
187 					qualifier.setKimTypId(getKimType().getId());
188 					qualifier.setGroupId(groupId);
189 				}
190 				index++;
191 	        }
192 		}
193 	}
194 
195 	public void initializeDocumentForNewGroup() {
196 		if(StringUtils.isBlank(this.groupId)){
197 			SequenceAccessorService sas = getSequenceAccessorService();
198 			Long nextSeq = sas.getNextAvailableSequenceNumber(
199 					KimConstants.SequenceNames.KRIM_GROUP_ID_S, this.getClass());
200 			this.groupId = nextSeq.toString();
201 		}
202 		if(StringUtils.isBlank(this.groupTypeId)) {
203 			this.groupTypeId = "1";
204 		}
205 	}
206 	
207 	public String getGroupId(){
208 //		if(StringUtils.isBlank(this.groupId)){
209 //			initializeDocumentForNewGroup();
210 //		}
211 		return groupId;
212 	}
213 
214 	/**
215 	 * @param groupId the groupId to set
216 	 */
217 	public void setGroupId(String groupId) {
218 		this.groupId = groupId;
219 	}
220 
221 	/**
222 	 * @return the groupName
223 	 */
224 	public String getGroupName() {
225 		return this.groupName;
226 	}
227 
228 	/**
229 	 * @param groupName the groupName to set
230 	 */
231 	public void setGroupName(String groupName) {
232 		this.groupName = groupName;
233 	}
234 
235 	public String getGroupDescription() {
236 		return this.groupDescription;
237 	}
238 
239 	public void setGroupDescription(String groupDescription) {
240 		this.groupDescription = groupDescription;
241 	}
242 	
243 	/**
244 	 * @return the groupNamespace
245 	 */
246 	public String getGroupNamespace() {
247 		return this.groupNamespace;
248 	}
249 
250 	/**
251 	 * @param groupNamespace the groupNamespace to set
252 	 */
253 	public void setGroupNamespace(String groupNamespace) {
254 		this.groupNamespace = groupNamespace;
255 	}
256 
257 	/**
258 	 * @return the groupTypeId
259 	 */
260 	public String getGroupTypeId() {
261 		return this.groupTypeId;
262 	}
263 
264 	/**
265 	 * @param groupTypeId the groupTypeId to set
266 	 */
267 	public void setGroupTypeId(String groupTypeId) {
268 		this.groupTypeId = groupTypeId;
269 	}
270 
271 	/**
272 	 * @return the groupTypeName
273 	 */
274 	public String getGroupTypeName() {
275 		return this.groupTypeName;
276 	}
277 
278 	/**
279 	 * @param groupTypeName the groupTypeName to set
280 	 */
281 	public void setGroupTypeName(String groupTypeName) {
282 		this.groupTypeName = groupTypeName;
283 	}
284 
285 	/**
286 	 * @return the members
287 	 */
288 	public List<GroupDocumentMember> getMembers() {
289 		return this.members;
290 	}
291 
292 	/**
293 	 * @param members the members to set
294 	 */
295 	public void setMembers(List<GroupDocumentMember> members) {
296 		this.members = members;
297 	}
298 
299 	/**
300 	 * @return the qualifiers
301 	 */
302 	public List<GroupDocumentQualifier> getQualifiers() {
303 		return this.qualifiers;
304 	}
305 
306 	/**
307 	 * @param qualifiers the qualifiers to set
308 	 */
309 	public void setQualifiers(List<GroupDocumentQualifier> qualifiers) {
310 		this.qualifiers = qualifiers;
311 	}
312 
313 	public GroupDocumentQualifier getQualifier(String kimAttributeDefnId) {
314 		for(GroupDocumentQualifier qualifier: qualifiers){
315 			if(qualifier.getKimAttrDefnId().equals(kimAttributeDefnId))
316 				return qualifier;
317 		}
318 		return null;
319 	}
320 
321 	public Map<String, String> getQualifiersAsAttributes() {
322         Map<String, String> attributes = new HashMap<String, String>();
323 		for(GroupDocumentQualifier qualifier: qualifiers){
324 			if ( qualifier.getKimAttribute() != null ) {
325 				attributes.put(qualifier.getKimAttribute().getAttributeName(), qualifier.getAttrVal());
326 			} else {
327 				LOG.warn( "Unknown attribute ID on group: " + qualifier.getKimAttrDefnId() + " / value=" + qualifier.getAttrVal());
328 				attributes.put("Unknown Attribute ID: " + qualifier.getKimAttrDefnId(), qualifier.getAttrVal());
329 			}
330 		}
331 		return attributes;
332 	}
333 	
334 	public void setDefinitions(List<KimAttributeField> definitions) {
335 		super.setDefinitions(definitions);
336 		if(getQualifiers()==null || getQualifiers().size()<1){
337 			GroupDocumentQualifier qualifier;
338 			setQualifiers(new ArrayList<GroupDocumentQualifier>());
339 			if(getDefinitions()!=null){
340 				for(KimAttributeField key : getDefinitions()) {
341 					qualifier = new GroupDocumentQualifier();
342 		        	qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
343 		        	getQualifiers().add(qualifier);
344 		        }
345 			}
346 		}
347 	}
348 
349 	/**
350 	 * @return the editing
351 	 */
352 	public boolean isEditing() {
353 		return this.editing;
354 	}
355 
356 	/**
357 	 * @param editing the editing to set
358 	 */
359 	public void setEditing(boolean editing) {
360 		this.editing = editing;
361 	}
362 
363 	public void setKimType(KimType kimType) {
364 		super.setKimType(kimType);
365 		if (kimType != null){
366 			setGroupTypeId(kimType.getId());
367 			setGroupTypeName(kimType.getName());
368 		}
369 	}
370 }