View Javadoc
1   /**
2    * Copyright 2005-2015 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.Comparator;
20  import java.util.List;
21  
22  import javax.persistence.CascadeType;
23  import javax.persistence.Column;
24  import javax.persistence.Convert;
25  import javax.persistence.Entity;
26  import javax.persistence.JoinColumn;
27  import javax.persistence.OneToMany;
28  import javax.persistence.Table;
29  import javax.persistence.Transient;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.eclipse.persistence.annotations.JoinFetch;
33  import org.eclipse.persistence.annotations.JoinFetchType;
34  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  import org.kuali.rice.kim.api.type.KimAttributeField;
39  import org.kuali.rice.kim.api.type.KimType;
40  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
41  import org.kuali.rice.kim.bo.ui.KimDocumentRolePermission;
42  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
43  import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
44  import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibilityAction;
45  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegation;
46  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
47  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
48  import org.kuali.rice.kim.impl.responsibility.ResponsibilityInternalService;
49  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
50  import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
51  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
52  import org.kuali.rice.kim.web.struts.form.IdentityManagementRoleDocumentForm;
53  import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
54  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
55  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
56  import org.springframework.util.AutoPopulatingList;
57  
58  /**
59   * This is a description of what this class does - bhargavp don't forget to fill this in.
60   *
61   * @author Kuali Rice Team (rice.collab@kuali.org)
62   *
63   */
64  @Entity
65  @Table(name = "KRIM_ROLE_DOCUMENT_T")
66  public class IdentityManagementRoleDocument extends IdentityManagementTypeAttributeTransactionalDocument {
67  
68      private static final long serialVersionUID = 1L;
69  
70      // principal data
71      @Column(name = "ROLE_ID")
72      protected String roleId;
73  
74      @Column(name = "ROLE_TYP_ID")
75      protected String roleTypeId;
76  
77      @Transient
78      protected String roleTypeName;
79  
80      @Column(name = "ROLE_NMSPC")
81      protected String roleNamespace = "";
82  
83      @Column(name = "ROLE_NM")
84      protected String roleName = "";
85  
86      @Column(name = "DESC_TXT")
87      protected String roleDescription = "";
88  
89      @Column(name = "ACTV_IND")
90      @Convert(converter = BooleanYNConverter.class)
91      protected boolean active = true;
92  
93      @Transient
94      protected boolean editing;
95  
96      @JoinFetch(value= JoinFetchType.OUTER)
97      @OneToMany(targetEntity = KimDocumentRolePermission.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
98      @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
99      protected List<KimDocumentRolePermission> permissions = new AutoPopulatingList<KimDocumentRolePermission>(KimDocumentRolePermission.class);
100 
101     @JoinFetch(value= JoinFetchType.OUTER)
102     @OneToMany(targetEntity = KimDocumentRoleResponsibility.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
103     @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
104     protected List<KimDocumentRoleResponsibility> responsibilities = new AutoPopulatingList<KimDocumentRoleResponsibility>(KimDocumentRoleResponsibility.class);
105 
106     @JoinFetch(value= JoinFetchType.OUTER)
107     @OneToMany(targetEntity = KimDocumentRoleMember.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
108     @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
109     protected List<KimDocumentRoleMember> modifiedMembers = new AutoPopulatingList<KimDocumentRoleMember>(KimDocumentRoleMember.class);
110 
111     @Transient
112     private List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList<RoleDocumentDelegationMember>(RoleDocumentDelegationMember.class);
113 
114     @JoinFetch(value= JoinFetchType.OUTER)
115     @OneToMany(targetEntity = RoleDocumentDelegation.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
116     @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false)
117     private List<RoleDocumentDelegation> delegations = new AutoPopulatingList<RoleDocumentDelegation>(RoleDocumentDelegation.class);
118 
119     @Transient
120     protected List<KimDocumentRoleMember> searchResultMembers = new ArrayList<KimDocumentRoleMember>();
121 
122     @Transient
123     protected List<KimDocumentRoleMember> members = new ArrayList<KimDocumentRoleMember>();
124 
125     @Transient
126     private transient ResponsibilityService responsibilityService;
127 
128     @Transient
129     private transient ResponsibilityInternalService responsibilityInternalService;
130 
131     public IdentityManagementRoleDocument() {
132     }
133 
134     /**
135 	 * @return the active
136 	 */
137     public boolean isActive() {
138         return this.active;
139     }
140 
141     /**
142 	 * @param active the active to set
143 	 */
144     public void setActive(boolean active) {
145         this.active = active;
146     }
147 
148     /**
149 	 * @param roleId the roleId to set
150 	 */
151     public void setRoleId(String roleId) {
152         this.roleId = roleId;
153     }
154 
155     /**
156 	 * @return the roleName
157 	 */
158     public String getRoleName() {
159         return this.roleName;
160     }
161 
162     /**
163 	 * @param roleName the roleName to set
164 	 */
165     public void setRoleName(String roleName) {
166         this.roleName = roleName;
167     }
168 
169     /**
170 	 * @return the roleDescription
171 	 */
172     public String getRoleDescription() {
173         return this.roleDescription;
174     }
175 
176     /**
177 	 * @param roleDescription the roleDescription to set
178 	 */
179     public void setRoleDescription(String roleDescription) {
180         this.roleDescription = roleDescription;
181     }
182 
183     /**
184 	 * @return the roleNamespace
185 	 */
186     public String getRoleNamespace() {
187         return this.roleNamespace;
188     }
189 
190     /**
191 	 * @param roleNamespace the roleNamespace to set
192 	 */
193     public void setRoleNamespace(String roleNamespace) {
194         this.roleNamespace = roleNamespace;
195     }
196 
197     /**
198 	 * @return the roleTypeId
199 	 */
200     public String getRoleTypeId() {
201         return this.roleTypeId;
202     }
203 
204     /**
205 	 * @param roleTypeId the roleTypeId to set
206 	 */
207     public void setRoleTypeId(String roleTypeId) {
208         this.roleTypeId = roleTypeId;
209     }
210 
211     /**
212 	 * @return the roleTypeName
213 	 */
214     public String getRoleTypeName() {
215         if (roleTypeName == null) {
216             if (kimType != null) {
217                 roleTypeName = kimType.getName();
218             } else if (roleTypeId != null) {
219                 setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(roleTypeId));
220                 if (kimType != null) {
221                     roleTypeName = kimType.getName();
222                 }
223             }
224         }
225         return this.roleTypeName;
226     }
227 
228     /**
229 	 * @param roleTypeName the roleTypeName to set
230 	 */
231     public void setRoleTypeName(String roleTypeName) {
232         this.roleTypeName = roleTypeName;
233     }
234 
235     /**
236 	 * @return the delegationMembers
237 	 */
238     @Override
239     public List<RoleDocumentDelegationMember> getDelegationMembers() {
240         return this.delegationMembers;
241     }
242 
243     /**
244 	 * @param delegationMembers the delegationMembers to set
245 	 */
246     @Override
247     public void setDelegationMembers(List<RoleDocumentDelegationMember> delegationMembers) {
248         this.delegationMembers = delegationMembers;
249     }
250 
251     /**
252 	 * @return the permissions
253 	 */
254     public List<KimDocumentRolePermission> getPermissions() {
255         return this.permissions;
256     }
257 
258     /**
259 	 * @param permissions the permissions to set
260 	 */
261     public void setPermissions(List<KimDocumentRolePermission> permissions) {
262         this.permissions = permissions;
263     }
264 
265     /**
266 	 * @return the responsibilities
267 	 */
268     public List<KimDocumentRoleResponsibility> getResponsibilities() {
269         return this.responsibilities;
270     }
271 
272     /**
273 	 * @param responsibilities the responsibilities to set
274 	 */
275     public void setResponsibilities(List<KimDocumentRoleResponsibility> responsibilities) {
276         this.responsibilities = responsibilities;
277     }
278 
279     /**
280 	 * @return the members
281 	 */
282     public List<KimDocumentRoleMember> getMembers() {
283         return this.members;
284     }
285 
286     public enum RoleMemberMetaDataType implements Comparator<KimDocumentRoleMember> {
287 
288         MEMBER_ID("memberId"), MEMBER_NAME("memberName"), FULL_MEMBER_NAME("memberFullName");
289 
290         private final String attributeName;
291 
292         RoleMemberMetaDataType(String anAttributeName) {
293             this.attributeName = anAttributeName;
294         }
295 
296         public String getAttributeName() {
297             return attributeName;
298         }
299 
300         @Override
301         public int compare(KimDocumentRoleMember m1, KimDocumentRoleMember m2) {
302             if (m1 == null && m2 == null) {
303                 return 0;
304             } else if (m1 == null) {
305                 return -1;
306             } else if (m2 == null) {
307                 return 1;
308             }
309             if (this.getAttributeName().equals(MEMBER_ID.getAttributeName())) {
310                 return m1.getMemberId().compareToIgnoreCase(m2.getMemberId());
311             } else if (this.getAttributeName().equals(FULL_MEMBER_NAME.getAttributeName())) {
312                 return m1.getMemberFullName().compareToIgnoreCase(m2.getMemberFullName());
313             }
314             return m1.getMemberName().compareToIgnoreCase(m2.getMemberName());
315         }
316     }
317 
318     public void setMemberMetaDataTypeToSort(Integer columnNumber) {
319         switch(columnNumber) {
320             case 1:
321                 this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_ID;
322                 break;
323             case 2:
324                 this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
325                 break;
326             case 3:
327                 this.memberMetaDataType = RoleMemberMetaDataType.FULL_MEMBER_NAME;
328                 break;
329             default:
330                 this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
331                 break;
332         }
333     }
334 
335     @Transient
336     protected RoleMemberMetaDataType memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
337 
338     public RoleMemberMetaDataType getMemberMetaDataType() {
339         return memberMetaDataType;
340     }
341 
342     public void setMemberMetaDataType(RoleMemberMetaDataType memberMetaDataType) {
343         this.memberMetaDataType = memberMetaDataType;
344     }
345 
346     /**
347 	 * @return the members
348 	 */
349     public KimDocumentRoleMember getMember(String roleMemberId) {
350         if (StringUtils.isEmpty(roleMemberId)) {
351             return null;
352         }
353         for (KimDocumentRoleMember roleMember : getMembers()) {
354             if (roleMemberId.equals(roleMember.getRoleMemberId())) {
355                 return roleMember;
356             }
357         }
358         return null;
359     }
360 
361     /**
362      * @param members the members to set
363      */
364     public void setMembers(List<KimDocumentRoleMember> members) {
365         this.members = members;
366     }
367 
368     /**
369      * @return the modifiedMembers
370      */
371     public List<KimDocumentRoleMember> getModifiedMembers() {
372         return this.modifiedMembers;
373     }
374 
375     /**
376      * @param modifiedMembers the modifiedMembers to set
377      */
378     public void setModifiedMembers(List<KimDocumentRoleMember> modifiedMembers) {
379         this.modifiedMembers = modifiedMembers;
380     }
381 
382     /**
383      * @return the searchResultMembers
384      */
385     public List<KimDocumentRoleMember> getSearchResultMembers() {
386         return this.searchResultMembers;
387     }
388 
389     /**
390      * @param searchResultMembers the searchResultMembers to set
391      */
392     public void setSearchResultMembers(List<KimDocumentRoleMember> searchResultMembers) {
393         this.searchResultMembers = searchResultMembers;
394     }
395 
396     public void addResponsibility(KimDocumentRoleResponsibility roleResponsibility) {
397         if (!getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResponsibility.getResponsibilityId())) {
398             roleResponsibility.getRoleRspActions().add(getNewRespAction(roleResponsibility));
399         }
400         getResponsibilities().add(roleResponsibility);
401     }
402 
403     protected KimDocumentRoleResponsibilityAction getNewRespAction(KimDocumentRoleResponsibility roleResponsibility) {
404         KimDocumentRoleResponsibilityAction roleRspAction = new KimDocumentRoleResponsibilityAction();
405         roleRspAction.setKimResponsibility(roleResponsibility.getKimResponsibility());
406         roleRspAction.setRoleResponsibilityId(roleResponsibility.getRoleResponsibilityId());
407         return roleRspAction;
408     }
409 
410     public void addDelegationMember(RoleDocumentDelegationMember newDelegationMember) {
411         getDelegationMembers().add(newDelegationMember);
412     }
413 
414     public void addMember(KimDocumentRoleMember member) {
415         DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_MBR_ID_S);
416         member.setRoleMemberId(incrementer.nextStringValue());
417         setupMemberRspActions(member);
418         getModifiedMembers().add(member);
419     }
420 
421     public KimDocumentRoleMember getBlankMember() {
422         KimDocumentRoleMember member = new KimDocumentRoleMember();
423         KimDocumentRoleQualifier qualifier;
424         if (getDefinitions() != null) {
425             for (KimAttributeField key : getDefinitions()) {
426                 qualifier = new KimDocumentRoleQualifier();
427                 qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
428                 member.getQualifiers().add(qualifier);
429             }
430         }
431         setupMemberRspActions(member);
432         return member;
433     }
434 
435     public RoleDocumentDelegationMember getBlankDelegationMember() {
436         RoleDocumentDelegationMember member = new RoleDocumentDelegationMember();
437         RoleDocumentDelegationMemberQualifier qualifier;
438         if (getDefinitions() != null) {
439             for (KimAttributeField key : getDefinitions()) {
440                 qualifier = new RoleDocumentDelegationMemberQualifier();
441                 setAttrDefnIdForDelMemberQualifier(qualifier, key);
442                 member.getQualifiers().add(qualifier);
443             }
444         }
445         return member;
446     }
447 
448     public void setupMemberRspActions(KimDocumentRoleMember member) {
449         member.getRoleRspActions().clear();
450         for (KimDocumentRoleResponsibility roleResp : getResponsibilities()) {
451             if (getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResp.getResponsibilityId())) {
452                 KimDocumentRoleResponsibilityAction action = new KimDocumentRoleResponsibilityAction();
453                 action.setRoleResponsibilityId("*");
454                 action.setRoleMemberId(member.getRoleMemberId());
455                 member.getRoleRspActions().add(action);
456                 break;
457             }
458         }
459     }
460 
461     public void updateMembers(IdentityManagementRoleDocumentForm roleDocumentForm) {
462         for (KimDocumentRoleMember member : roleDocumentForm.getRoleDocument().getMembers()) {
463             roleDocumentForm.getRoleDocument().setupMemberRspActions(member);
464         }
465     }
466 
467     public void updateMembers(KimDocumentRoleResponsibility newResponsibility) {
468         for (KimDocumentRoleMember member : getMembers()) {
469             setupMemberRspActions(newResponsibility, member);
470         }
471     }
472 
473     public void setupMemberRspActions(KimDocumentRoleResponsibility roleResp, KimDocumentRoleMember member) {
474         if ((member.getRoleRspActions() == null || member.getRoleRspActions().size() < 1) && getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResp.getResponsibilityId())) {
475             KimDocumentRoleResponsibilityAction action = new KimDocumentRoleResponsibilityAction();
476             action.setRoleResponsibilityId("*");
477             action.setRoleMemberId(member.getRoleMemberId());
478             if (member.getRoleRspActions() == null) {
479                 member.setRoleRspActions(new ArrayList<KimDocumentRoleResponsibilityAction>());
480             }
481             member.getRoleRspActions().add(action);
482         }
483     }
484 
485     protected void setAttrDefnIdForDelMemberQualifier(RoleDocumentDelegationMemberQualifier qualifier, KimAttributeField definition) {
486         qualifier.setKimAttrDefnId(definition.getId());
487     }
488 
489     /**
490      * @see org.kuali.rice.krad.document.DocumentBase#doRouteStatusChange(org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange)
491      */
492     @Override
493     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
494         super.doRouteStatusChange(statusChangeEvent);
495         if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
496             KIMServiceLocatorInternal.getUiDocumentService().saveRole(this);
497         }
498     }
499 
500     public void initializeDocumentForNewRole() {
501         if (StringUtils.isBlank(this.roleId)) {
502             DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_ID_S);
503             this.roleId = incrementer.nextStringValue();
504         }
505         if (StringUtils.isBlank(this.roleTypeId)) {
506             this.roleTypeId = "1";
507         }
508     }
509 
510     public String getRoleId() {
511         if (StringUtils.isBlank(this.roleId)) {
512             initializeDocumentForNewRole();
513         }
514         return roleId;
515     }
516 
517     @Override
518     public void prepareForSave() {
519         String roleId;
520         if (StringUtils.isBlank(getRoleId())) {
521             DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_ID_S);
522             roleId = incrementer.nextStringValue();
523             setRoleId(roleId);
524         } else {
525             roleId = getRoleId();
526         }
527         if (getPermissions() != null) {
528             for (KimDocumentRolePermission permission : getPermissions()) {
529                 permission.setRoleId(roleId);
530                 permission.setDocumentNumber(getDocumentNumber());
531                 if (StringUtils.isBlank(permission.getRolePermissionId())) {
532                     DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_PERM_ID_S);
533                     permission.setRolePermissionId(incrementer.nextStringValue());
534                 }
535             }
536         }
537         if (getResponsibilities() != null) {
538             for (KimDocumentRoleResponsibility responsibility : getResponsibilities()) {
539                 String nextRoleResponsibilityId = null;
540 
541                 if (StringUtils.isBlank(responsibility.getRoleResponsibilityId())) {
542                     DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_RSP_ID_S);
543                     nextRoleResponsibilityId = incrementer.nextStringValue();
544                     responsibility.setRoleResponsibilityId(nextRoleResponsibilityId);
545                 } else{
546                     responsibility.setDocumentNumber(getDocumentNumber());
547                     responsibility.setVersionNumber(null);
548                 }
549 
550                 responsibility.setRoleId(roleId);
551                 if (!getResponsibilityInternalService().areActionsAtAssignmentLevelById(responsibility.getResponsibilityId())) {
552                     if (StringUtils.isBlank(responsibility.getRoleRspActions().get(0).getRoleResponsibilityActionId())) {
553                         DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_RSP_ACTN_ID_S);
554                         responsibility.getRoleRspActions().get(0).setRoleResponsibilityActionId(incrementer.nextStringValue());
555                     }
556 
557                     if (StringUtils.isBlank(responsibility.getRoleRspActions().get(0).getRoleResponsibilityId())) {
558                         if (StringUtils.isBlank(nextRoleResponsibilityId)) {
559                             responsibility.getRoleRspActions().get(0).setRoleResponsibilityId(responsibility.getRoleResponsibilityId());
560                         } else {
561                             responsibility.getRoleRspActions().get(0).setRoleResponsibilityId(nextRoleResponsibilityId);
562                         }
563                     }
564 
565                     responsibility.getRoleRspActions().get(0).setRoleMemberId("*");
566                     responsibility.getRoleRspActions().get(0).setDocumentNumber(getDocumentNumber());
567                 }
568             }
569         }
570         if (getModifiedMembers() != null) {
571             for (KimDocumentRoleMember member : getModifiedMembers()) {
572                 member.setDocumentNumber(getDocumentNumber());
573                 member.setRoleId(roleId);
574                 if (StringUtils.isBlank(member.getRoleMemberId())) {
575                     DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_MBR_ID_S);
576                     member.setRoleMemberId(incrementer.nextStringValue());
577                 }
578                 for (KimDocumentRoleQualifier qualifier : member.getQualifiers()) {
579                     qualifier.setDocumentNumber(getDocumentNumber());
580                     qualifier.setRoleMemberId(member.getRoleMemberId());
581                     qualifier.setKimTypId(getKimType().getId());
582                 }
583                 for (KimDocumentRoleResponsibilityAction roleRespAction : member.getRoleRspActions()) {
584                     if (StringUtils.isBlank(roleRespAction.getRoleResponsibilityActionId())) {
585                         DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_ROLE_RSP_ACTN_ID_S);
586                         roleRespAction.setRoleResponsibilityActionId(incrementer.nextStringValue());
587                         roleRespAction.setDocumentNumber(getDocumentNumber());
588                     }
589                     roleRespAction.setRoleMemberId(member.getRoleMemberId());
590                     roleRespAction.setDocumentNumber(getDocumentNumber());
591                     if (!StringUtils.equals(roleRespAction.getRoleResponsibilityId(), "*")) {
592                         for (KimDocumentRoleResponsibility responsibility : getResponsibilities()) {
593                             if (StringUtils.equals(roleRespAction.getKimResponsibility().getId(), responsibility.getResponsibilityId())) {
594                                 roleRespAction.setRoleResponsibilityId(responsibility.getRoleResponsibilityId());
595                             }
596                         }
597                     }
598                     // This will cause a problem with JPA
599 //                    if (roleRespAction.getVersionNumber() == null) {
600 //                        roleRespAction.setVersionNumber(new Long(1));
601 //                    }
602                 }
603             }
604         }
605         if (getDelegationMembers() != null) {
606             for (RoleDocumentDelegationMember delegationMember : getDelegationMembers()) {
607                 delegationMember.setDocumentNumber(getDocumentNumber());
608                 addDelegationMemberToDelegation(delegationMember);
609             }
610             for (RoleDocumentDelegation delegation : getDelegations()) {
611                 delegation.setDocumentNumber(getDocumentNumber());
612                 delegation.setKimTypeId(getKimType().getId());
613                 List<RoleDocumentDelegationMember> membersToRemove = new AutoPopulatingList<RoleDocumentDelegationMember>(RoleDocumentDelegationMember.class);
614                 for (RoleDocumentDelegationMember member : delegation.getMembers()) {
615                     if (delegation.getDelegationId().equals(member.getDelegationId()) && delegation.getDelegationTypeCode().equals(member.getDelegationTypeCode())) {
616                         for (RoleDocumentDelegationMemberQualifier qualifier : member.getQualifiers()) {
617                             qualifier.setKimTypId(getKimType().getId());
618                             qualifier.setDocumentNumber(getDocumentNumber());
619                         }
620                     } else {
621                         membersToRemove.add(member);
622                     }
623                 }
624                 if (!membersToRemove.isEmpty()) {
625                     for (RoleDocumentDelegationMember member : membersToRemove) {
626                         delegation.getMembers().remove(member);
627                     }
628                 }
629                 delegation.setRoleId(roleId);
630             }
631         }
632     }
633 
634     public ResponsibilityService getResponsibilityService() {
635         if (responsibilityService == null) {
636             responsibilityService = KimApiServiceLocator.getResponsibilityService();
637         }
638         return responsibilityService;
639     }
640 
641     public ResponsibilityInternalService getResponsibilityInternalService() {
642         if (responsibilityInternalService == null) {
643             responsibilityInternalService = KimImplServiceLocator.getResponsibilityInternalService();
644         }
645         return responsibilityInternalService;
646     }
647 
648     /**
649 	 * @return the editing
650 	 */
651     public boolean isEditing() {
652         return this.editing;
653     }
654 
655     /**
656 	 * @param editing the editing to set
657 	 */
658     public void setEditing(boolean editing) {
659         this.editing = editing;
660     }
661 
662     /**
663 	 * @return the delegations
664 	 */
665     @Override
666     public List<RoleDocumentDelegation> getDelegations() {
667         return this.delegations;
668     }
669 
670     /**
671 	 * @param delegations the delegations to set
672 	 */
673     @Override
674     public void setDelegations(List<RoleDocumentDelegation> delegations) {
675         this.delegations = delegations;
676     }
677 
678     @Override
679     public void setKimType(KimType kimType) {
680         super.setKimType(kimType);
681         if (kimType != null) {
682             setRoleTypeId(kimType.getId());
683             setRoleTypeName(kimType.getName());
684         }
685     }
686 }