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