View Javadoc

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