2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.group
.grantedauthority
;
12 import java
.util
.ArrayList
;
13 import java
.util
.EnumSet
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
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
;
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
;
36 * Viewer class for the {@link CdmAuthorityComposite} used in the editing of CDM Authorities.
39 * @created Mar 28, 2013
50 //FIXME:move warning dialogs to the table class
51 public class CdmAuthorityCompositeViewer
extends ContentViewer
{
53 private IDirtyMarkableSelectionProvider dirtyMarkerEditor
;
54 private CdmAuthorityComposite cdmAuthorityComposite
;
57 * List which contains already existing authority objects.
59 private List
<GrantedAuthorityImpl
> cdmAuthorities
;
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.
65 private List
<GrantedAuthorityImpl
> newCdmAuthorities
;
69 * Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input
71 * @param composite parent of generated {@link CdmAuthorityComposite}
72 * @param dirtyMarkerEditor provider to mark as dirty
73 * @param group input data object
75 public CdmAuthorityCompositeViewer(Composite composite
,
76 IDirtyMarkableSelectionProvider dirtyMarkerEditor
,
78 this.dirtyMarkerEditor
= dirtyMarkerEditor
;
80 newCdmAuthorities
= new ArrayList
<GrantedAuthorityImpl
>();
82 this.cdmAuthorityComposite
= new CdmAuthorityComposite(composite
, SWT
.NONE
, this);
86 * Gets the {@link CdmAuthorityComposite}
88 * @return {@link CdmAuthorityComposite} generated by this viewer
90 public CdmAuthorityComposite
getCdmAuthorityComposite() {
91 return cdmAuthorityComposite
;
96 * Retrieves the {@link GrantedAuthorityImpl} objects of the group attached to this viewer
97 * creates a sub-list of only {@link CdmAuthority} objects.
99 * @return list of {@link CdmAuthority} objects belonging to the input group
101 public List
<GrantedAuthorityImpl
> updateAuthorities() {
102 // get all granted authorities
103 Set
<GrantedAuthority
> grantedAuthorities
= group
.getGrantedAuthorities();
104 cdmAuthorities
= new ArrayList
<GrantedAuthorityImpl
>();
106 Iterator
<GrantedAuthority
> itr
= grantedAuthorities
.iterator();
107 while (itr
.hasNext()) {
108 GrantedAuthority grantedAuthority
= itr
.next();
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
);
117 } catch (Exception e
) {
118 // not a CdmAuthority
119 //e.printStackTrace();
122 return cdmAuthorities
;
126 * Adds a new {@link CdmAuthority} to the group attached to this viewer
127 * based on the input {@link CdmBase} object.
129 * @param cb {@link CdmBase} object used to construct the cdm authority
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
;
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");
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");
149 group
.addGrantedAuthority(gai
);
150 newCdmAuthorities
.add(gai
);
151 dirtyMarkerEditor
.changed(group
);
152 cdmAuthorityComposite
.refresh();
157 * Removes the given {@link GrantedAuthorityImpl} object from the list of
158 * granted authorities belonging to the group attached to this viewer.
160 * @param gai {@link GrantedAuthorityImpl} object to remove
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();
170 StoreUtil
.warningDialog("Unknown CDM Authority", this, "Chosen CDM Authority does not exist in current group");
175 * Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
177 * @param grantedAuthorityI to update.
178 * @param cdmAuthority to use in updating the granted authority object.
180 public GrantedAuthorityImpl
updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI
, CdmAuthority cdmAuthority
) {
181 if(grantedAuthorityI
.getAuthority().equals(cdmAuthority
.getAuthority())) {
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
);
194 } catch(Exception e
) {
195 // Not a CDM Authority
196 //e.printStackTrace();
199 return grantedAuthorityI
;
203 * @return list of existing cdm authority objects
205 public List
<GrantedAuthorityImpl
> getCdmAuthorities() {
206 return cdmAuthorities
;
210 * @return list of newly added cdm authority objects
212 public List
<GrantedAuthorityImpl
> getNewCdmAuthorities() {
213 return newCdmAuthorities
;
218 * Empty the list of newly create autrity objects
220 public void clearNewCdmAuthorities() {
221 newCdmAuthorities
.clear();
230 clearNewCdmAuthorities();
236 * @see org.eclipse.jface.viewers.Viewer#getControl()
239 public Control
getControl() {
240 return cdmAuthorityComposite
;
244 * @see org.eclipse.jface.viewers.Viewer#getSelection()
247 public ISelection
getSelection() {
248 // TODO Auto-generated method stub
253 * @see org.eclipse.jface.viewers.Viewer#refresh()
256 public void refresh() {
258 cdmAuthorityComposite
.refresh();
263 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
266 public void setSelection(ISelection selection
, boolean reveal
) {
267 // TODO Auto-generated method stub