001 /**
002 * Copyright 2005-2014 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 */
016 package org.kuali.rice.kim.document;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.apache.log4j.Logger;
020 import org.hibernate.annotations.Fetch;
021 import org.hibernate.annotations.FetchMode;
022 import org.hibernate.annotations.Type;
023 import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
024 import org.kuali.rice.kim.api.KimConstants;
025 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026 import org.kuali.rice.kim.api.type.KimAttributeField;
027 import org.kuali.rice.kim.api.type.KimType;
028 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
029 import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
030 import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
031 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
032 import org.kuali.rice.krad.service.SequenceAccessorService;
033 import org.springframework.util.AutoPopulatingList;
034
035 import javax.persistence.AttributeOverride;
036 import javax.persistence.AttributeOverrides;
037 import javax.persistence.CascadeType;
038 import javax.persistence.Column;
039 import javax.persistence.Entity;
040 import javax.persistence.FetchType;
041 import javax.persistence.JoinColumn;
042 import javax.persistence.OneToMany;
043 import javax.persistence.Table;
044 import javax.persistence.Transient;
045 import java.util.ArrayList;
046 import java.util.HashMap;
047 import java.util.List;
048 import java.util.Map;
049
050
051 /**
052 * This is a description of what this class does - bhargavp don't forget to fill this in.
053 *
054 * @author Kuali Rice Team (rice.collab@kuali.org)
055 *
056 */
057 @Entity
058 @AttributeOverrides({
059 @AttributeOverride(name="documentNumber",column=@Column(name="FDOC_NBR"))
060 })
061 @Table(name="KRIM_GRP_DOCUMENT_T")
062 public class IdentityManagementGroupDocument extends IdentityManagementTypeAttributeTransactionalDocument {
063 private static final Logger LOG = Logger.getLogger(IdentityManagementGroupDocument.class);
064
065 private static final long serialVersionUID = 1L;
066
067 // principal data
068 @Column(name="GRP_ID")
069 protected String groupId;
070 @Column(name="KIM_TYP_ID")
071 protected String groupTypeId;
072 @Transient
073 protected String groupTypeName;
074 @Column(name="GRP_NMSPC")
075 protected String groupNamespace;
076 @Column(name="GRP_NM")
077 protected String groupName;
078 @Column(name="GRP_DESC")
079 protected String groupDescription;
080 @Type(type="yes_no")
081 @Column(name="ACTV_IND")
082 protected boolean active = true;
083
084 @Transient
085 protected boolean editing;
086
087 @OneToMany(targetEntity = GroupDocumentMember.class, fetch = FetchType.EAGER, cascade=CascadeType.ALL)
088 @Fetch(value = FetchMode.SELECT)
089 @JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
090 private List<GroupDocumentMember> members = new AutoPopulatingList(GroupDocumentMember.class);
091 @OneToMany(targetEntity = GroupDocumentQualifier.class, fetch = FetchType.EAGER, cascade=CascadeType.ALL)
092 @Fetch(value = FetchMode.SELECT)
093 @JoinColumn(name="FDOC_NBR", insertable = false, updatable = false)
094 private List<GroupDocumentQualifier> qualifiers = new AutoPopulatingList(GroupDocumentQualifier.class);
095
096 public IdentityManagementGroupDocument() {
097 }
098
099 /**
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 }