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