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