Project

General

Profile

Download (7.85 KB) Statistics
| Branch: | Tag: | Revision:
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 void 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
				group.addGrantedAuthority(cdmAuthority.asNewGrantedAuthority());
190
				group.removeGrantedAuthority(grantedAuthorityI);
191
				dirtyMarkerEditor.changed(grantedAuthorityI);				
192
			} catch(Exception e) {
193
				// Not a CDM Authority
194
				//e.printStackTrace();
195
			}
196
		}		
197
	}
198
	
199
	/**
200
	 * @return list of existing cdm authority objects
201
	 */
202
	public List<GrantedAuthorityImpl> getCdmAuthorities() {
203
		return cdmAuthorities;
204
	}
205
	
206
	/**
207
	 * @return list of newly added cdm authority objects
208
	 */
209
	public List<GrantedAuthorityImpl> getNewCdmAuthorities() {
210
		return newCdmAuthorities;
211
	}
212
	
213

    
214
	/**
215
	 * Empty the list of newly create autrity objects
216
	 */
217
	public void clearNewCdmAuthorities() {
218
		newCdmAuthorities.clear();
219
	}
220

    
221
	
222
	/**
223
	 * 
224
	 * 
225
	 */
226
	public void save() {
227
		clearNewCdmAuthorities();		
228
		refresh();
229
		
230
	}	
231
	
232
	/* (non-Javadoc)
233
	 * @see org.eclipse.jface.viewers.Viewer#getControl()
234
	 */
235
	@Override
236
	public Control getControl() {
237
		return cdmAuthorityComposite;
238
	}
239

    
240
	/* (non-Javadoc)
241
	 * @see org.eclipse.jface.viewers.Viewer#getSelection()
242
	 */
243
	@Override
244
	public ISelection getSelection() {
245
		// TODO Auto-generated method stub
246
		return null;
247
	}
248

    
249
	/* (non-Javadoc)
250
	 * @see org.eclipse.jface.viewers.Viewer#refresh()
251
	 */
252
	@Override
253
	public void refresh() {
254
		updateAuthorities();
255
		cdmAuthorityComposite.refresh();
256
		
257
	}
258

    
259
	/* (non-Javadoc)
260
	 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
261
	 */
262
	@Override
263
	public void setSelection(ISelection selection, boolean reveal) {
264
		// TODO Auto-generated method stub
265
		
266
	}
267
}
(3-3/5)