1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.jaxb;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import javax.xml.bind.Marshaller;
26 import javax.xml.bind.UnmarshalException;
27 import javax.xml.bind.Unmarshaller;
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.XmlTransient;
32 import javax.xml.bind.annotation.XmlType;
33
34 import org.kuali.rice.core.api.criteria.QueryByCriteria;
35 import org.kuali.rice.core.util.jaxb.RiceXmlExportList;
36 import org.kuali.rice.core.util.jaxb.RiceXmlImportList;
37 import org.kuali.rice.core.util.jaxb.RiceXmlListAdditionListener;
38 import org.kuali.rice.core.util.jaxb.RiceXmlListGetterListener;
39 import org.kuali.rice.kim.api.role.RoleMember;
40 import org.kuali.rice.kim.api.role.RoleMemberContract;
41 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42
43 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
44
45
46
47
48
49
50
51 @XmlTransient
52 public abstract class RoleMembersXmlDTO<T extends RoleMemberXmlDTO> implements RiceXmlListAdditionListener<T>, Serializable {
53
54 private static final long serialVersionUID = 1L;
55
56 public abstract List<T> getRoleMembers();
57
58 public abstract void setRoleMembers(List<T> roleMembers);
59
60 void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
61 setRoleMembers(new RiceXmlImportList<T>(this));
62 }
63
64 void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
65 setRoleMembers(null);
66 }
67
68
69
70
71
72
73
74
75 @XmlAccessorType(XmlAccessType.FIELD)
76 @XmlType(name="StandaloneRoleMembersType", propOrder={"roleMembers"})
77 public static class OutsideOfRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.OutsideOfRole> {
78
79 private static final long serialVersionUID = 1L;
80
81 @XmlElement(name="roleMember")
82 private List<RoleMemberXmlDTO.OutsideOfRole> roleMembers;
83
84
85
86
87 @Override
88 public List<RoleMemberXmlDTO.OutsideOfRole> getRoleMembers() {
89 return this.roleMembers;
90 }
91
92
93
94
95 @Override
96 public void setRoleMembers(List<RoleMemberXmlDTO.OutsideOfRole> roleMembers) {
97 this.roleMembers = roleMembers;
98 }
99
100
101
102
103 @Override
104 public void newItemAdded(RoleMemberXmlDTO.OutsideOfRole item) {
105 try {
106 RoleXmlUtil.validateAndPersistNewRoleMember(item);
107 } catch (UnmarshalException e) {
108 throw new RuntimeException(e);
109 }
110 }
111 }
112
113
114
115
116
117
118
119
120 @XmlAccessorType(XmlAccessType.FIELD)
121 @XmlType(name="RoleMembersType", propOrder={"roleMembers"})
122 public static class WithinRole extends RoleMembersXmlDTO<RoleMemberXmlDTO.WithinRole>
123 implements RiceXmlListGetterListener<RoleMemberXmlDTO.WithinRole,String> {
124
125 private static final long serialVersionUID = 1L;
126
127 @XmlElement(name="roleMember")
128 private List<RoleMemberXmlDTO.WithinRole> roleMembers;
129
130 @XmlTransient
131 private String roleId;
132
133 @XmlTransient
134 private Set<String> existingRoleMemberIds;
135
136 public WithinRole() {}
137
138 public WithinRole(String roleId) {
139 this.roleId = roleId;
140 }
141
142
143
144
145 @Override
146 public List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> getRoleMembers() {
147 return this.roleMembers;
148 }
149
150
151
152
153 @Override
154 public void setRoleMembers(List<org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole> roleMembers) {
155 this.roleMembers = roleMembers;
156 }
157
158
159
160
161 public String getRoleId() {
162 return this.roleId;
163 }
164
165
166
167
168 @Override
169 void beforeUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
170 if (parent instanceof RoleXmlDTO) {
171
172 RoleXmlDTO parentRole = (RoleXmlDTO) parent;
173 if (!parentRole.isAlreadyPersisted()) {
174 RoleXmlUtil.validateAndPersistNewRole(parentRole);
175 }
176 roleId = parentRole.getRoleId();
177 }
178 existingRoleMemberIds = new HashSet<String>();
179 super.beforeUnmarshal(unmarshaller, parent);
180 }
181
182
183
184
185
186
187 @Override
188 void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws UnmarshalException {
189 super.afterUnmarshal(unmarshaller, parent);
190 if (parent instanceof RoleXmlDTO) {
191 ((RoleXmlDTO)parent).setExistingRoleMemberIds(existingRoleMemberIds);
192 }
193 existingRoleMemberIds = null;
194 }
195
196
197
198
199 @Override
200 public void newItemAdded(org.kuali.rice.kim.impl.jaxb.RoleMemberXmlDTO.WithinRole item) {
201
202 try {
203 existingRoleMemberIds.add(RoleXmlUtil.validateAndPersistNewRoleMember(item));
204 } catch (UnmarshalException e) {
205 throw new RuntimeException(e);
206 }
207 }
208
209 void beforeMarshal(Marshaller marshaller) {
210 List<RoleMember> tempMembers = KimApiServiceLocator.getRoleService().findRoleMembers(
211 QueryByCriteria.Builder.fromPredicates(equal("roleId", roleId))).getResults();
212 if (tempMembers != null && !tempMembers.isEmpty()) {
213 List<String> roleMemberIds = new ArrayList<String>();
214
215 for (RoleMemberContract tempMember : tempMembers) {
216 if (tempMember.isActive(null)) {
217 roleMemberIds.add(tempMember.getId());
218 }
219 }
220
221 if (!roleMemberIds.isEmpty()) {
222 setRoleMembers(new RiceXmlExportList<RoleMemberXmlDTO.WithinRole,String>(roleMemberIds, this));
223 }
224 }
225 }
226
227 void afterMarshal(Marshaller marshaller) {
228 setRoleMembers(null);
229 }
230
231
232
233
234 @Override
235 public RoleMemberXmlDTO.WithinRole gettingNextItem(String nextItem, int index) {
236 return new RoleMemberXmlDTO.WithinRole(KimApiServiceLocator.getRoleService().findRoleMembers(QueryByCriteria.Builder.fromPredicates(equal("roleMemberId", nextItem))).getResults().get(0), false);
237 }
238 }
239 }