1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.openpermis.editor.policy.presenter; |
11 | |
|
12 | |
import java.util.ArrayList; |
13 | |
import java.util.List; |
14 | |
|
15 | |
import org.jdesktop.observablecollections.ObservableCollections; |
16 | |
import org.jdesktop.observablecollections.ObservableList; |
17 | |
import org.jdesktop.observablecollections.ObservableListListener; |
18 | |
import org.slf4j.Logger; |
19 | |
import org.slf4j.LoggerFactory; |
20 | |
|
21 | |
import org.openpermis.editor.policy.beans.PropertyChange; |
22 | |
import org.openpermis.editor.policy.command.CollectionAddCommand; |
23 | |
import org.openpermis.editor.policy.command.CompositeCommand; |
24 | |
import org.openpermis.editor.policy.command.ObligationRemoveCommand; |
25 | |
import org.openpermis.editor.policy.command.PoolRemoveCommand; |
26 | |
import org.openpermis.editor.policy.command.TargetRemoveCommand; |
27 | |
import org.openpermis.editor.policy.gui.binding.ObservableListAdapter; |
28 | |
import org.openpermis.editor.policy.view.EditPartCommand; |
29 | |
import org.openpermis.policy.Predicate; |
30 | |
import org.openpermis.policy.Role; |
31 | |
import org.openpermis.policy.bean.ObligationBean; |
32 | |
import org.openpermis.policy.bean.RoleCollection; |
33 | |
import org.openpermis.policy.bean.TargetAccessRuleBean; |
34 | |
import org.openpermis.policy.bean.TargetBean; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class TargetAccessRulePresenter |
41 | |
extends PartPresenter<TargetAccessRuleBean> |
42 | |
{ |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | private static final Logger LOGGER = |
51 | |
LoggerFactory.getLogger(TargetAccessRulePresenter.class); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private final ObservableList<TargetBean> targets; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private final ObservableList<ObligationBean> obligations; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private final ObservableList<Role> roles; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private final ObservableListListener rolesUpdater; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private final ObservableListListener targetsUpdater; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private final ObservableListListener obligationsUpdater; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
private Predicate condition; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public TargetAccessRulePresenter ( |
106 | |
TargetAccessRuleBean model, PolicyContext context |
107 | |
) { |
108 | |
|
109 | 0 | super(model, context); |
110 | 0 | LOGGER.debug("Presenter for [{}].", model); |
111 | |
|
112 | 0 | this.targets = createTargetList(model); |
113 | 0 | this.targetsUpdater = new ObservableListAdapter<TargetBean>() { |
114 | |
@Override |
115 | 0 | protected void listChanged (ObservableList<TargetBean> list) { |
116 | 0 | updateTargetsAtModel(); |
117 | 0 | } |
118 | |
}; |
119 | 0 | this.targets.addObservableListListener(this.targetsUpdater); |
120 | |
|
121 | 0 | this.obligations = createObligationList(model); |
122 | 0 | this.obligationsUpdater = new ObservableListAdapter<ObligationBean>() { |
123 | |
@Override |
124 | 0 | protected void listChanged (ObservableList<ObligationBean> list) { |
125 | 0 | updateObligationsAtModel(); |
126 | 0 | } |
127 | |
}; |
128 | 0 | this.obligations.addObservableListListener(this.obligationsUpdater); |
129 | |
|
130 | 0 | this.roles = createRoleList(model); |
131 | 0 | this.rolesUpdater = new ObservableListAdapter<Role>() { |
132 | |
@Override |
133 | 0 | protected void listChanged (ObservableList<Role> list) { |
134 | 0 | updateRolesAtModel(); |
135 | 0 | } |
136 | |
}; |
137 | 0 | this.roles.addObservableListListener(this.rolesUpdater); |
138 | 0 | updateConditionAtPresenter(); |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public Predicate getCondition () { |
149 | 0 | return this.condition; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
private ObservableList<Role> createRoleList (TargetAccessRuleBean model) { |
159 | 0 | return createCollectionAtPresenter(model.getRoles()); |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
private ObservableList<TargetBean> createTargetList (TargetAccessRuleBean model) { |
169 | 0 | final List<TargetBean> result = new ArrayList<TargetBean>(); |
170 | 0 | for (TargetBean target : model.getTargets()) { |
171 | 0 | result.add(target); |
172 | |
} |
173 | 0 | return ObservableCollections.observableList(result); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
private ObservableList<ObligationBean> createObligationList (TargetAccessRuleBean model) { |
183 | 0 | final List<ObligationBean> result = new ArrayList<ObligationBean>(); |
184 | 0 | for (ObligationBean obligation : model.getObligations()) { |
185 | 0 | result.add(obligation); |
186 | |
} |
187 | 0 | return ObservableCollections.observableList(result); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private void updateRolesAtModel () { |
195 | 0 | LOGGER.debug("updateRolesAtModel"); |
196 | 0 | change("roles", RoleCollection.create(this.roles)); |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
private void updateTargetsAtModel () { |
204 | 0 | LOGGER.debug("updateRoles"); |
205 | 0 | change("targets", getPartBeanFactory().createTargetCollection(this.targets)); |
206 | 0 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private void updateObligationsAtModel () { |
213 | 0 | LOGGER.debug("updateObligations"); |
214 | 0 | change("obligations", getPartBeanFactory().createObligationCollection(this.obligations)); |
215 | 0 | } |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
private void updateRolesAtPresenter () { |
222 | 0 | LOGGER.debug("updateRolesAtPresenter"); |
223 | 0 | this.roles.removeObservableListListener(this.rolesUpdater); |
224 | 0 | updateAddedRolesAtPresenter(getModel().getRoles(), this.roles); |
225 | 0 | updateDeletedRolesAtPresenter(getModel().getRoles(), this.roles); |
226 | 0 | this.roles.addObservableListListener(this.rolesUpdater); |
227 | 0 | } |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public void updateConditionAtModel (final Predicate predicate) { |
234 | 0 | LOGGER.debug("updateCondition"); |
235 | 0 | change("condition", predicate); |
236 | 0 | } |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
private void updateConditionAtPresenter () { |
243 | 0 | LOGGER.debug("updateConditionAtPresenter"); |
244 | 0 | Predicate oldCondition = this.condition; |
245 | 0 | this.condition = getModel().getCondition(); |
246 | 0 | firePropertyChange("condition", oldCondition, this.condition); |
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private void updateAddedRolesAtPresenter ( |
253 | |
RoleCollection modelRoles, ObservableList<Role> list) { |
254 | 0 | for (Role role : modelRoles) { |
255 | 0 | if (!list.contains(role)) { |
256 | 0 | list.add(role); |
257 | |
} |
258 | |
} |
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
private void updateDeletedRolesAtPresenter (RoleCollection modelRoles, ObservableList<Role> list |
265 | |
) { |
266 | 0 | final List<Role> roleList = modelRoles.toList(); |
267 | 0 | for (Role role : list) { |
268 | 0 | if (!roleList.contains(role)) { |
269 | 0 | list.remove(role); |
270 | |
} |
271 | |
} |
272 | 0 | } |
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
private void updateTargetsAtPresenter () { |
279 | 0 | updateCollectionAtPresenter(getModel().getTargets(), this.targets, this.targetsUpdater); |
280 | 0 | } |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
private void updateObligationsAtPresenter () { |
287 | 0 | updateCollectionAtPresenter( |
288 | |
getModel().getObligations(), this.obligations, this.obligationsUpdater |
289 | |
); |
290 | 0 | } |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
public ObservableList<Role> getRoles () { |
298 | 0 | return this.roles; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public ObservableList<TargetBean> getTargets () { |
307 | 0 | return this.targets; |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public ObservableList<ObligationBean> getObligations () { |
316 | 0 | return this.obligations; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public void removeActiveTarget (TargetBean activeTarget) { |
324 | 0 | if (activeTarget != null) { |
325 | 0 | execute( |
326 | |
new CompositeCommand( |
327 | |
new TargetRemoveCommand(activeTarget), |
328 | |
new PoolRemoveCommand<TargetBean>(this.getTargetPool(), activeTarget) |
329 | |
) |
330 | |
); |
331 | |
} |
332 | 0 | } |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
public void removeActiveObligation (ObligationBean activeObligation) { |
339 | 0 | if (activeObligation != null) { |
340 | 0 | execute( |
341 | |
new CompositeCommand( |
342 | |
new ObligationRemoveCommand(activeObligation), |
343 | |
new PoolRemoveCommand<ObligationBean>( |
344 | |
this.getObligationPool(), activeObligation |
345 | |
) |
346 | |
) |
347 | |
); |
348 | |
} |
349 | 0 | } |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
public void addTarget (EditPartCommand<TargetBean> editPartCommand) { |
355 | 0 | final TargetBean newTarget = getContext().getPartBeanFactory().createTarget(null, null); |
356 | 0 | editPartCommand.setPart(newTarget); |
357 | 0 | execute( |
358 | |
new CompositeCommand( |
359 | |
new CollectionAddCommand<TargetBean>(getModel(), newTarget, "targets"), |
360 | |
editPartCommand |
361 | |
) |
362 | |
); |
363 | 0 | } |
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
public void addObligation (EditPartCommand<ObligationBean> editPartCommand) { |
369 | 0 | final ObligationBean newObligation = getContext().getPartBeanFactory().createObligation(""); |
370 | 0 | editPartCommand.setPart(newObligation); |
371 | 0 | execute( |
372 | |
new CompositeCommand( |
373 | |
new CollectionAddCommand<ObligationBean>(getModel(), newObligation, "obligations"), |
374 | |
editPartCommand |
375 | |
) |
376 | |
); |
377 | 0 | } |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
@PropertyChange(bean = TargetAccessRuleBean.class, property = "targets") |
386 | |
public void updateTargets () { |
387 | 0 | updateTargetsAtPresenter(); |
388 | 0 | } |
389 | |
|
390 | |
|
391 | |
|
392 | |
|
393 | |
|
394 | |
@PropertyChange(bean = TargetAccessRuleBean.class, property = "obligations") |
395 | |
public void updateObligations () { |
396 | 0 | updateObligationsAtPresenter(); |
397 | 0 | } |
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
@PropertyChange(bean = TargetAccessRuleBean.class, property = "roles") |
404 | |
public void updateRoles () { |
405 | 0 | LOGGER.debug("updateRoles"); |
406 | 0 | updateRolesAtPresenter(); |
407 | 0 | } |
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
@PropertyChange(bean = TargetAccessRuleBean.class, property = "condition") |
414 | |
public void updateCondition () { |
415 | 0 | LOGGER.debug("updateCondition"); |
416 | 0 | updateConditionAtPresenter(); |
417 | 0 | } |
418 | |
|
419 | |
} |