Project

General

Profile

Download (8.18 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.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
		sc.setMinWidth(650);
85
		sc.setMinHeight(400);
86
	}
87

    
88
	/**
89
	 * Gets the {@link CdmAuthorityComposite}
90
	 *
91
	 * @return {@link CdmAuthorityComposite} generated by this viewer
92
	 */
93
	public CdmAuthorityComposite getCdmAuthorityComposite() {
94
		return cdmAuthorityComposite;
95
	}
96

    
97

    
98
	/**
99
	 * Retrieves the {@link GrantedAuthorityImpl} objects of the group attached to this viewer
100
	 * creates a sub-list of only {@link CdmAuthority} objects.
101
	 *
102
	 * @return list of {@link CdmAuthority} objects belonging to the input group
103
	 */
104
	public List<GrantedAuthorityImpl> updateAuthorities()  {
105
		// get all granted authorities
106
		Set<GrantedAuthority> grantedAuthorities = group.getGrantedAuthorities();
107
		cdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
108

    
109
		Iterator<GrantedAuthority> itr = grantedAuthorities.iterator();
110
		while (itr.hasNext()) {
111
			GrantedAuthority grantedAuthority = itr.next();
112
			try {
113

    
114
				if(grantedAuthority != null && grantedAuthority instanceof GrantedAuthorityImpl) {
115
					// create a cdm authority from a granted authority
116
					// this could fail in which case an exception is thrown
117
					CdmAuthority cdmAuthority = CdmAuthority.fromGrantedAuthority(grantedAuthority);
118
					cdmAuthorities.add((GrantedAuthorityImpl)grantedAuthority);
119
				}
120
			} catch (Exception e) {
121
				// not a CdmAuthority
122
				//e.printStackTrace();
123
			}
124
		}
125
		return cdmAuthorities;
126
	}
127

    
128
	public boolean isDirty() {
129
		return isDirty;
130
	}
131
	/**
132
	 * Adds a new {@link CdmAuthority} to the group attached to this viewer
133
	 * based on the input {@link CdmBase} object.
134
	 *
135
	 * @param cb {@link CdmBase} object used to construct the cdm authority
136
	 */
137
	public void addCdmAuthority(CdmBase cb) {
138
		// Default permission is to READ
139
		EnumSet<CRUD> defaultOperation = EnumSet.noneOf(CRUD.class);
140
		defaultOperation.add(CRUD.READ);
141
		CdmAuthority cdma = new CdmAuthority(cb, defaultOperation);
142
		GrantedAuthorityImpl gai;
143

    
144
		try {
145
			// create a granted authrity from a cdm authority
146
			gai = cdma.asNewGrantedAuthority();
147
		} catch (Exception e) {
148
			MessagingUtils.warningDialog("Parsing Error", this, "Could not parse authority string");
149
			return;
150
		}
151
		//FIXME : this contains call will allow users to add authorities which differ only in CRUD operation choice.
152
		//        need to have a comparator which only checks permission class and uuid
153
		if(cdmAuthorities.contains(gai) || newCdmAuthorities.contains(gai)) {
154
			MessagingUtils.warningDialog("Duplicate CDM Authority", this, "Chosen CDM Authority is already attached to current group");
155
		} else {
156
			group.addGrantedAuthority(gai);
157
			newCdmAuthorities.add(gai);
158
			isDirty = true;
159
			dirtyMarkerEditor.changed(group);
160
			cdmAuthorityComposite.refresh();
161
		}
162
	}
163

    
164
	/**
165
	 * Removes the given {@link GrantedAuthorityImpl} object from the list of
166
	 * granted authorities belonging to the group attached to this viewer.
167
	 *
168
	 * @param gai {@link GrantedAuthorityImpl} object to remove
169
	 */
170
	public void removeCdmAuthority(GrantedAuthorityImpl gai) {
171

    
172
		if(cdmAuthorities.contains(gai)) {
173
			cdmAuthorities.remove(gai);
174
			isDirty = true;
175
		} else {
176
			newCdmAuthorities.remove(gai);
177
			if(newCdmAuthorities.isEmpty() && !isDirty) {
178
				isDirty = false;
179
			}
180
		}
181
		group.removeGrantedAuthority(gai);
182
		dirtyMarkerEditor.changed(group);
183
		cdmAuthorityComposite.refresh();
184
	}
185

    
186
	/**
187
	 * Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
188
	 *
189
	 * @param grantedAuthorityI to update.
190
	 * @param cdmAuthority to use in updating the granted authority object.
191
	 */
192
	public GrantedAuthorityImpl updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI, CdmAuthority cdmAuthority) {
193
		if(grantedAuthorityI.getAuthority().equals(cdmAuthority.getAuthority())) {
194

    
195
		} else {
196
			try {
197
				// since granted authority is just a wrapper object around a string
198
				// we can't really 'update' the granted authority.
199
				// so we first add the new authority (hope no exception) is thrown
200
				// and then remove the old one.
201
				GrantedAuthorityImpl gai = cdmAuthority.asNewGrantedAuthority();
202
				group.addGrantedAuthority(gai);
203
				group.removeGrantedAuthority(grantedAuthorityI);
204
				isDirty = true;
205
				dirtyMarkerEditor.changed(grantedAuthorityI);
206
				return gai;
207
			} catch(Exception e) {
208
				// Not a CDM Authority
209
				//e.printStackTrace();
210
			}
211
		}
212
		return grantedAuthorityI;
213
	}
214

    
215
	/**
216
	 * @return list of existing cdm authority objects
217
	 */
218
	public List<GrantedAuthorityImpl> getCdmAuthorities() {
219
		return cdmAuthorities;
220
	}
221

    
222
	/**
223
	 * @return list of newly added cdm authority objects
224
	 */
225
	public List<GrantedAuthorityImpl> getNewCdmAuthorities() {
226
		return newCdmAuthorities;
227
	}
228

    
229

    
230
	/**
231
	 * Empty the list of newly create autrity objects
232
	 */
233
	public void clearNewCdmAuthorities() {
234
		newCdmAuthorities.clear();
235
	}
236

    
237

    
238
	/**
239
	 *
240
	 *
241
	 */
242
	public void save() {
243
		clearNewCdmAuthorities();
244
		refresh();
245

    
246
	}
247

    
248
	/* (non-Javadoc)
249
	 * @see org.eclipse.jface.viewers.Viewer#getControl()
250
	 */
251
	@Override
252
	public Control getControl() {
253
		return cdmAuthorityComposite;
254
	}
255

    
256
	/* (non-Javadoc)
257
	 * @see org.eclipse.jface.viewers.Viewer#getSelection()
258
	 */
259
	@Override
260
	public ISelection getSelection() {
261
		// TODO Auto-generated method stub
262
		return null;
263
	}
264

    
265
	/* (non-Javadoc)
266
	 * @see org.eclipse.jface.viewers.Viewer#refresh()
267
	 */
268
	@Override
269
	public void refresh() {
270
		updateAuthorities();
271
		cdmAuthorityComposite.refresh();
272

    
273
	}
274

    
275
	/* (non-Javadoc)
276
	 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
277
	 */
278
	@Override
279
	public void setSelection(ISelection selection, boolean reveal) {
280
		// TODO Auto-generated method stub
281

    
282
	}
283
}
(3-3/5)