001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.kim.document;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.persistence.CascadeType;
024import javax.persistence.Column;
025import javax.persistence.Convert;
026import javax.persistence.Entity;
027import javax.persistence.JoinColumn;
028import javax.persistence.OneToMany;
029import javax.persistence.Table;
030import javax.persistence.Transient;
031
032import org.apache.commons.lang.StringUtils;
033import org.apache.log4j.Logger;
034import org.eclipse.persistence.annotations.JoinFetch;
035import org.eclipse.persistence.annotations.JoinFetchType;
036import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
037import org.kuali.rice.kim.api.KimConstants;
038import org.kuali.rice.kim.api.services.KimApiServiceLocator;
039import org.kuali.rice.kim.api.type.KimAttributeField;
040import org.kuali.rice.kim.api.type.KimType;
041import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
042import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
043import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
044import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
045import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
046import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
047import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
048import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
049import org.springframework.util.AutoPopulatingList;
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@Table(name = "KRIM_GRP_DOCUMENT_T")
059public class IdentityManagementGroupDocument extends IdentityManagementTypeAttributeTransactionalDocument {
060    private static final Logger LOG = Logger.getLogger(IdentityManagementGroupDocument.class);
061    private static final long serialVersionUID = 1L;
062
063    // principal data                       
064    @Column(name = "GRP_ID")
065    protected String groupId;
066
067    @Column(name = "KIM_TYP_ID")
068    protected String groupTypeId;
069
070    @Transient
071    protected String groupTypeName;
072
073    @Column(name = "GRP_NMSPC")
074    protected String groupNamespace;
075
076    @Column(name = "GRP_NM")
077    protected String groupName;
078
079    @Column(name = "GRP_DESC")
080    protected String groupDescription;
081
082    //@Type(type="yes_no")                       
083    @Column(name = "ACTV_IND")
084    @Convert(converter = BooleanYNConverter.class)
085    protected boolean active = true;
086
087    @Transient
088    protected boolean editing;
089
090    @JoinFetch(value= JoinFetchType.OUTER)
091    @OneToMany(targetEntity = GroupDocumentMember.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
092    @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
093    private List<GroupDocumentMember> members = new AutoPopulatingList<GroupDocumentMember>(GroupDocumentMember.class);
094
095    @JoinFetch(value= JoinFetchType.OUTER)
096    @OneToMany(targetEntity = GroupDocumentQualifier.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
097    @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
098    private List<GroupDocumentQualifier> qualifiers = new AutoPopulatingList<GroupDocumentQualifier>(GroupDocumentQualifier.class);
099
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}