View Javadoc
1   /**
2    * Copyright 2005-2014 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.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.persistence.CascadeType;
24  import javax.persistence.Column;
25  import javax.persistence.Convert;
26  import javax.persistence.Entity;
27  import javax.persistence.JoinColumn;
28  import javax.persistence.OneToMany;
29  import javax.persistence.Table;
30  import javax.persistence.Transient;
31  
32  import org.apache.commons.lang.StringUtils;
33  import org.apache.log4j.Logger;
34  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
37  import org.kuali.rice.kim.api.type.KimAttributeField;
38  import org.kuali.rice.kim.api.type.KimType;
39  import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
40  import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
41  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
42  import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
43  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
44  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
45  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
46  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
47  import org.springframework.util.AutoPopulatingList;
48  
49  /**
50   * This is a description of what this class does - bhargavp don't forget to fill this in.
51   *
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   *
54   */
55  @Entity
56  @Table(name = "KRIM_GRP_DOCUMENT_T")
57  public class IdentityManagementGroupDocument extends IdentityManagementTypeAttributeTransactionalDocument {
58      private static final Logger LOG = Logger.getLogger(IdentityManagementGroupDocument.class);
59      private static final long serialVersionUID = 1L;
60  
61      // principal data                       
62      @Column(name = "GRP_ID")
63      protected String groupId;
64  
65      @Column(name = "KIM_TYP_ID")
66      protected String groupTypeId;
67  
68      @Transient
69      protected String groupTypeName;
70  
71      @Column(name = "GRP_NMSPC")
72      protected String groupNamespace;
73  
74      @Column(name = "GRP_NM")
75      protected String groupName;
76  
77      @Column(name = "GRP_DESC")
78      protected String groupDescription;
79  
80      //@Type(type="yes_no")                       
81      @Column(name = "ACTV_IND")
82      @Convert(converter = BooleanYNConverter.class)
83      protected boolean active = true;
84  
85      @Transient
86      protected boolean editing;
87  
88      @OneToMany(targetEntity = GroupDocumentMember.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
89      @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
90      private List<GroupDocumentMember> members = new AutoPopulatingList<GroupDocumentMember>(GroupDocumentMember.class);
91  
92      @OneToMany(targetEntity = GroupDocumentQualifier.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
93      @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
94      private List<GroupDocumentQualifier> qualifiers = new AutoPopulatingList<GroupDocumentQualifier>(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             DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_GROUP_ID_S);
157             groupId = incrementer.nextStringValue();
158             setGroupId(groupId);
159         } else {
160             groupId = getGroupId();
161         }
162         if (getMembers() != null) {
163             for (GroupDocumentMember member : getMembers()) {
164                 member.setGroupId(getGroupId());
165                 if (StringUtils.isBlank(member.getGroupMemberId())) {
166                     DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), "KRIM_GRP_MBR_ID_S");
167                     member.setGroupMemberId(incrementer.nextStringValue());
168                 }
169                 if (StringUtils.isBlank(member.getDocumentNumber())) {
170                     member.setDocumentNumber(getDocumentNumber());
171                 }
172             }
173         }
174         int index = 0;
175         // this needs to be checked - are all qualifiers present?                       
176         if (getDefinitions() != null) {
177             for (KimAttributeField key : getDefinitions()) {
178                 if (getQualifiers().size() > index) {
179                     GroupDocumentQualifier qualifier = getQualifiers().get(index);
180                     qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
181                     qualifier.setKimTypId(getKimType().getId());
182                     qualifier.setGroupId(groupId);
183                 }
184                 index++;
185             }
186         }
187     }
188 
189     public void initializeDocumentForNewGroup() {
190         if (StringUtils.isBlank(this.groupId)) {
191             DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_GROUP_ID_S);
192             this.groupId = incrementer.nextStringValue();
193         }
194         if (StringUtils.isBlank(this.groupTypeId)) {
195             this.groupTypeId = "1";
196         }
197     }
198 
199     public String getGroupId() {
200         //		if(StringUtils.isBlank(this.groupId)){                       
201         //			initializeDocumentForNewGroup();                       
202         //		}                       
203         return groupId;
204     }
205 
206     /**
207 	 * @param groupId the groupId to set
208 	 */
209     public void setGroupId(String groupId) {
210         this.groupId = groupId;
211     }
212 
213     /**
214 	 * @return the groupName
215 	 */
216     public String getGroupName() {
217         return this.groupName;
218     }
219 
220     /**
221 	 * @param groupName the groupName to set
222 	 */
223     public void setGroupName(String groupName) {
224         this.groupName = groupName;
225     }
226 
227     public String getGroupDescription() {
228         return this.groupDescription;
229     }
230 
231     public void setGroupDescription(String groupDescription) {
232         this.groupDescription = groupDescription;
233     }
234 
235     /**
236 	 * @return the groupNamespace
237 	 */
238     public String getGroupNamespace() {
239         return this.groupNamespace;
240     }
241 
242     /**
243 	 * @param groupNamespace the groupNamespace to set
244 	 */
245     public void setGroupNamespace(String groupNamespace) {
246         this.groupNamespace = groupNamespace;
247     }
248 
249     /**
250 	 * @return the groupTypeId
251 	 */
252     public String getGroupTypeId() {
253         return this.groupTypeId;
254     }
255 
256     /**
257 	 * @param groupTypeId the groupTypeId to set
258 	 */
259     public void setGroupTypeId(String groupTypeId) {
260         this.groupTypeId = groupTypeId;
261     }
262 
263     /**
264 	 * @return the groupTypeName
265 	 */
266     public String getGroupTypeName() {
267         return this.groupTypeName;
268     }
269 
270     /**
271 	 * @param groupTypeName the groupTypeName to set
272 	 */
273     public void setGroupTypeName(String groupTypeName) {
274         this.groupTypeName = groupTypeName;
275     }
276 
277     /**
278 	 * @return the members
279 	 */
280     public List<GroupDocumentMember> getMembers() {
281         return this.members;
282     }
283 
284     /**
285 	 * @param members the members to set
286 	 */
287     public void setMembers(List<GroupDocumentMember> members) {
288         this.members = members;
289     }
290 
291     /**
292 	 * @return the qualifiers
293 	 */
294     public List<GroupDocumentQualifier> getQualifiers() {
295         return this.qualifiers;
296     }
297 
298     /**
299 	 * @param qualifiers the qualifiers to set
300 	 */
301     public void setQualifiers(List<GroupDocumentQualifier> qualifiers) {
302         this.qualifiers = qualifiers;
303     }
304 
305     public GroupDocumentQualifier getQualifier(String kimAttributeDefnId) {
306         for (GroupDocumentQualifier qualifier : qualifiers) {
307             if (qualifier.getKimAttrDefnId().equals(kimAttributeDefnId))
308                 return qualifier;
309         }
310         return null;
311     }
312 
313     public Map<String, String> getQualifiersAsAttributes() {
314         Map<String, String> attributes = new HashMap<String, String>();
315         for (GroupDocumentQualifier qualifier : qualifiers) {
316             if (qualifier.getKimAttribute() != null) {
317                 attributes.put(qualifier.getKimAttribute().getAttributeName(), qualifier.getAttrVal());
318             } else {
319                 LOG.warn("Unknown attribute ID on group: " + qualifier.getKimAttrDefnId() + " / value=" + qualifier.getAttrVal());
320                 attributes.put("Unknown Attribute ID: " + qualifier.getKimAttrDefnId(), qualifier.getAttrVal());
321             }
322         }
323         return attributes;
324     }
325 
326     public void setDefinitions(List<KimAttributeField> definitions) {
327         super.setDefinitions(definitions);
328         if (getQualifiers() == null || getQualifiers().size() < 1) {
329             GroupDocumentQualifier qualifier;
330             setQualifiers(new ArrayList<GroupDocumentQualifier>());
331             if (getDefinitions() != null) {
332                 for (KimAttributeField key : getDefinitions()) {
333                     qualifier = new GroupDocumentQualifier();
334                     qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
335                     getQualifiers().add(qualifier);
336                 }
337             }
338         }
339     }
340 
341     public boolean isEditing() {
342         return this.editing;
343     }
344 
345     public void setEditing(boolean editing) {
346         this.editing = editing;
347     }
348 
349     public void setKimType(KimType kimType) {
350         super.setKimType(kimType);
351         if (kimType != null) {
352             setGroupTypeId(kimType.getId());
353             setGroupTypeName(kimType.getName());
354         }
355     }
356 }