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