1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.ui.group.grantedauthority;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.EnumSet;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.eclipse.jface.viewers.ContentViewer;
|
19
|
import org.eclipse.jface.viewers.ISelection;
|
20
|
import org.eclipse.swt.SWT;
|
21
|
import org.eclipse.swt.custom.ScrolledComposite;
|
22
|
import org.eclipse.swt.widgets.Composite;
|
23
|
import org.eclipse.swt.widgets.Control;
|
24
|
import org.springframework.security.core.GrantedAuthority;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
|
28
|
import eu.etaxonomy.cdm.model.common.Group;
|
29
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
30
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
31
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
32
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
33
|
|
34
|
|
35
|
|
36
|
/**
|
37
|
* Viewer class for the {@link CdmAuthorityComposite} used in the editing of CDM Authorities.
|
38
|
*
|
39
|
* @author cmathew
|
40
|
* @created Mar 28, 2013
|
41
|
*
|
42
|
*/
|
43
|
|
44
|
|
45
|
//FIXME:move warning dialogs to the table class
|
46
|
public class CdmAuthorityCompositeViewer extends ContentViewer {
|
47
|
|
48
|
private IDirtyMarkable dirtyMarkerEditor;
|
49
|
private CdmAuthorityComposite cdmAuthorityComposite;
|
50
|
private Group group;
|
51
|
private boolean isDirty = false;
|
52
|
/**
|
53
|
* List which contains already existing authority objects.
|
54
|
*/
|
55
|
private List<GrantedAuthorityImpl> cdmAuthorities;
|
56
|
/**
|
57
|
* List which contains newly added (unsaved) authority objects.
|
58
|
* Used to make sure that new authority objects are always added
|
59
|
* to the top of the table.
|
60
|
*/
|
61
|
private List<GrantedAuthorityImpl> newCdmAuthorities;
|
62
|
|
63
|
|
64
|
/**
|
65
|
* Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input
|
66
|
*
|
67
|
* @param composite parent of generated {@link CdmAuthorityComposite}
|
68
|
* @param dirtyMarkerEditor provider to mark as dirty
|
69
|
* @param group input data object
|
70
|
*/
|
71
|
public CdmAuthorityCompositeViewer(Composite composite,
|
72
|
IDirtyMarkable dirtyMarkerEditor,
|
73
|
Group group) {
|
74
|
this.dirtyMarkerEditor = dirtyMarkerEditor;
|
75
|
this.group = group;
|
76
|
newCdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
|
77
|
updateAuthorities();
|
78
|
|
79
|
ScrolledComposite sc = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
|
80
|
this.cdmAuthorityComposite = new CdmAuthorityComposite(sc, SWT.H_SCROLL | SWT.V_SCROLL, this);
|
81
|
sc.setContent(cdmAuthorityComposite);
|
82
|
sc.setExpandHorizontal(true);
|
83
|
sc.setExpandVertical(true);
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Gets the {@link CdmAuthorityComposite}
|
88
|
*
|
89
|
* @return {@link CdmAuthorityComposite} generated by this viewer
|
90
|
*/
|
91
|
public CdmAuthorityComposite getCdmAuthorityComposite() {
|
92
|
return cdmAuthorityComposite;
|
93
|
}
|
94
|
|
95
|
|
96
|
/**
|
97
|
* Retrieves the {@link GrantedAuthorityImpl} objects of the group attached to this viewer
|
98
|
* creates a sub-list of only {@link CdmAuthority} objects.
|
99
|
*
|
100
|
* @return list of {@link CdmAuthority} objects belonging to the input group
|
101
|
*/
|
102
|
public List<GrantedAuthorityImpl> updateAuthorities() {
|
103
|
// get all granted authorities
|
104
|
Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
|
105
|
cdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
|
106
|
|
107
|
Iterator<GrantedAuthority> itr = grantedAuthorities.iterator();
|
108
|
while (itr.hasNext()) {
|
109
|
GrantedAuthority grantedAuthority = itr.next();
|
110
|
try {
|
111
|
|
112
|
if(grantedAuthority != null && grantedAuthority instanceof GrantedAuthorityImpl) {
|
113
|
// create a cdm authority from a granted authority
|
114
|
// this could fail in which case an exception is thrown
|
115
|
CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthority);
|
116
|
cdmAuthorities.add((GrantedAuthorityImpl)grantedAuthority);
|
117
|
}
|
118
|
} catch (Exception e) {
|
119
|
// not a CdmAuthority
|
120
|
//e.printStackTrace();
|
121
|
}
|
122
|
}
|
123
|
return cdmAuthorities;
|
124
|
}
|
125
|
|
126
|
public boolean isDirty() {
|
127
|
return isDirty;
|
128
|
}
|
129
|
/**
|
130
|
* Adds a new {@link CdmAuthority} to the group attached to this viewer
|
131
|
* based on the input {@link CdmBase} object.
|
132
|
*
|
133
|
* @param cb {@link CdmBase} object used to construct the cdm authority
|
134
|
*/
|
135
|
public void addCdmAuthority(CdmBase cb) {
|
136
|
// Default permission is to READ
|
137
|
EnumSet<CRUD> defaultOperation = EnumSet.noneOf(CRUD.class);
|
138
|
defaultOperation.add(CRUD.READ);
|
139
|
CdmAuthority cdma = new CdmAuthority(cb, defaultOperation);
|
140
|
GrantedAuthorityImpl gai;
|
141
|
|
142
|
try {
|
143
|
// create a granted authority from a cdm authority
|
144
|
gai = cdma.asNewGrantedAuthority();
|
145
|
} catch (Exception e) {
|
146
|
MessagingUtils.warningDialog("Parsing Error", this, "Could not parse authority string");
|
147
|
return;
|
148
|
}
|
149
|
//FIXME : this contains call will allow users to add authorities which differ only in CRUD operation choice.
|
150
|
// need to have a comparator which only checks permission class and uuid
|
151
|
if(cdmAuthorities.contains(gai) || newCdmAuthorities.contains(gai)) {
|
152
|
MessagingUtils.warningDialog("Duplicate CDM Authority", this, "Chosen CDM Authority is already attached to current group");
|
153
|
} else {
|
154
|
group.addGrantedAuthority(gai);
|
155
|
newCdmAuthorities.add(gai);
|
156
|
isDirty = true;
|
157
|
dirtyMarkerEditor.changed(group);
|
158
|
cdmAuthorityComposite.refresh();
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Removes the given {@link GrantedAuthorityImpl} object from the list of
|
164
|
* granted authorities belonging to the group attached to this viewer.
|
165
|
*
|
166
|
* @param gai {@link GrantedAuthorityImpl} object to remove
|
167
|
*/
|
168
|
public void removeCdmAuthority(GrantedAuthorityImpl gai) {
|
169
|
|
170
|
if(cdmAuthorities.contains(gai)) {
|
171
|
cdmAuthorities.remove(gai);
|
172
|
isDirty = true;
|
173
|
} else {
|
174
|
newCdmAuthorities.remove(gai);
|
175
|
if(newCdmAuthorities.isEmpty() && !isDirty) {
|
176
|
isDirty = false;
|
177
|
}
|
178
|
}
|
179
|
group.removeGrantedAuthority(gai);
|
180
|
dirtyMarkerEditor.changed(group);
|
181
|
cdmAuthorityComposite.refresh();
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
|
186
|
*
|
187
|
* @param grantedAuthorityI to update.
|
188
|
* @param cdmAuthority to use in updating the granted authority object.
|
189
|
*/
|
190
|
public GrantedAuthorityImpl updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI, CdmAuthority cdmAuthority) {
|
191
|
if(grantedAuthorityI.getAuthority().equals(cdmAuthority.getAuthority())) {
|
192
|
|
193
|
} else {
|
194
|
try {
|
195
|
// since granted authority is just a wrapper object around a string
|
196
|
// we can't really 'update' the granted authority.
|
197
|
// so we first add the new authority (hope no exception) is thrown
|
198
|
// and then remove the old one.
|
199
|
GrantedAuthorityImpl gai = cdmAuthority.asNewGrantedAuthority();
|
200
|
group.addGrantedAuthority(gai);
|
201
|
group.removeGrantedAuthority(grantedAuthorityI);
|
202
|
isDirty = true;
|
203
|
dirtyMarkerEditor.changed(grantedAuthorityI);
|
204
|
return gai;
|
205
|
} catch(Exception e) {
|
206
|
// Not a CDM Authority
|
207
|
//e.printStackTrace();
|
208
|
}
|
209
|
}
|
210
|
return grantedAuthorityI;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* @return list of existing cdm authority objects
|
215
|
*/
|
216
|
public List<GrantedAuthorityImpl> getCdmAuthorities() {
|
217
|
return cdmAuthorities;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* @return list of newly added cdm authority objects
|
222
|
*/
|
223
|
public List<GrantedAuthorityImpl> getNewCdmAuthorities() {
|
224
|
return newCdmAuthorities;
|
225
|
}
|
226
|
|
227
|
|
228
|
/**
|
229
|
* Empty the list of newly create autrity objects
|
230
|
*/
|
231
|
public void clearNewCdmAuthorities() {
|
232
|
newCdmAuthorities.clear();
|
233
|
}
|
234
|
|
235
|
|
236
|
/**
|
237
|
*
|
238
|
*
|
239
|
*/
|
240
|
public void save() {
|
241
|
clearNewCdmAuthorities();
|
242
|
refresh();
|
243
|
|
244
|
}
|
245
|
|
246
|
/* (non-Javadoc)
|
247
|
* @see org.eclipse.jface.viewers.Viewer#getControl()
|
248
|
*/
|
249
|
@Override
|
250
|
public Control getControl() {
|
251
|
return cdmAuthorityComposite;
|
252
|
}
|
253
|
|
254
|
/* (non-Javadoc)
|
255
|
* @see org.eclipse.jface.viewers.Viewer#getSelection()
|
256
|
*/
|
257
|
@Override
|
258
|
public ISelection getSelection() {
|
259
|
// TODO Auto-generated method stub
|
260
|
return null;
|
261
|
}
|
262
|
|
263
|
/* (non-Javadoc)
|
264
|
* @see org.eclipse.jface.viewers.Viewer#refresh()
|
265
|
*/
|
266
|
@Override
|
267
|
public void refresh() {
|
268
|
updateAuthorities();
|
269
|
cdmAuthorityComposite.refresh();
|
270
|
|
271
|
}
|
272
|
|
273
|
/* (non-Javadoc)
|
274
|
* @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
|
275
|
*/
|
276
|
@Override
|
277
|
public void setSelection(ISelection selection, boolean reveal) {
|
278
|
// TODO Auto-generated method stub
|
279
|
|
280
|
}
|
281
|
}
|