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