Project

General

Profile

Download (8.55 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.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.permission.CRUD;
30
import eu.etaxonomy.cdm.model.permission.GrantedAuthorityImpl;
31
import eu.etaxonomy.cdm.model.permission.Group;
32
import eu.etaxonomy.cdm.model.term.TermBase;
33
import eu.etaxonomy.cdm.persistence.permission.CdmAuthority;
34
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
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
//FIXME:move warning dialogs to the table class
44
public class CdmAuthorityCompositeViewer extends ContentViewer {
45

    
46
	private IDirtyMarkable dirtyMarkerEditor;
47
	private CdmAuthorityComposite cdmAuthorityComposite;
48
	private Group group;
49
	private boolean isDirty = false;
50
	/**
51
	 * List which contains already existing authority objects.
52
	 */
53
	private List<GrantedAuthorityImpl> cdmAuthorities;
54
	/**
55
	 * List which contains newly added (unsaved) authority objects.
56
	 * Used to make sure that new authority objects are always added
57
	 * to the top of the table.
58
	 */
59
	private List<GrantedAuthorityImpl> newCdmAuthorities;
60

    
61

    
62
	/**
63
	 * Creates a viewer with a {@link CdmAuthorityComposite} table, {@link Group} object as input
64
	 *
65
	 * @param composite parent of generated {@link CdmAuthorityComposite}
66
	 * @param dirtyMarkerEditor provider to mark as dirty
67
	 * @param group input data object
68
	 */
69
	public CdmAuthorityCompositeViewer(Composite composite,
70
			IDirtyMarkable dirtyMarkerEditor,
71
			Group group) {
72
		this.dirtyMarkerEditor = dirtyMarkerEditor;
73
		this.group = group;
74
		newCdmAuthorities = new ArrayList<GrantedAuthorityImpl>();
75
		updateAuthorities();
76

    
77
		ScrolledComposite sc = new ScrolledComposite(composite, SWT.H_SCROLL | SWT.V_SCROLL);
78
		this.cdmAuthorityComposite = new CdmAuthorityComposite(sc, SWT.H_SCROLL | SWT.V_SCROLL, this);
79
		sc.setContent(cdmAuthorityComposite);
80
		sc.setExpandHorizontal(true);
81
		sc.setExpandVertical(true);
82
	}
83

    
84
	/**
85
	 * Gets the {@link CdmAuthorityComposite}
86
	 *
87
	 * @return {@link CdmAuthorityComposite} generated by this viewer
88
	 */
89
	public CdmAuthorityComposite getCdmAuthorityComposite() {
90
		return cdmAuthorityComposite;
91
	}
92

    
93

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

    
105
		Iterator<GrantedAuthority> itr = grantedAuthorities.iterator();
106
		while (itr.hasNext()) {
107
			GrantedAuthority grantedAuthority = itr.next();
108
			try {
109

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

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

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

    
169
	/**
170
	 * Removes the given {@link GrantedAuthorityImpl} object from the list of
171
	 * granted authorities belonging to the group attached to this viewer.
172
	 *
173
	 * @param gai {@link GrantedAuthorityImpl} object to remove
174
	 */
175
	public void removeCdmAuthority(GrantedAuthorityImpl gai) {
176

    
177
		if(cdmAuthorities.contains(gai)) {
178
			cdmAuthorities.remove(gai);
179
			isDirty = true;
180
		} else {
181
			newCdmAuthorities.remove(gai);
182
			if(newCdmAuthorities.isEmpty() && !isDirty) {
183
				isDirty = false;
184
			}
185
		}
186
		group.removeGrantedAuthority(gai);
187
		dirtyMarkerEditor.changed(group);
188
		cdmAuthorityComposite.refresh();
189
	}
190

    
191
	/**
192
	 * Updates an existing {@link GrantedAuthorityImpl} object using a {@link CdmAuthority} object.
193
	 *
194
	 * @param grantedAuthorityI to update.
195
	 * @param cdmAuthority to use in updating the granted authority object.
196
	 */
197
	public GrantedAuthorityImpl updateGrantedAuthority(GrantedAuthorityImpl grantedAuthorityI, CdmAuthority cdmAuthority) {
198
		if(grantedAuthorityI.getAuthority().equals(cdmAuthority.getAuthority())) {
199

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

    
220
	/**
221
	 * @return list of existing cdm authority objects
222
	 */
223
	public List<GrantedAuthorityImpl> getCdmAuthorities() {
224
		return cdmAuthorities;
225
	}
226

    
227
	/**
228
	 * @return list of newly added cdm authority objects
229
	 */
230
	public List<GrantedAuthorityImpl> getNewCdmAuthorities() {
231
		return newCdmAuthorities;
232
	}
233

    
234

    
235
	/**
236
	 * Empty the list of newly create autrity objects
237
	 */
238
	public void clearNewCdmAuthorities() {
239
		newCdmAuthorities.clear();
240
	}
241

    
242

    
243
	/**
244
	 *
245
	 *
246
	 */
247
	public void save() {
248
		clearNewCdmAuthorities();
249
		refresh();
250

    
251
	}
252

    
253
	/* (non-Javadoc)
254
	 * @see org.eclipse.jface.viewers.Viewer#getControl()
255
	 */
256
	@Override
257
	public Control getControl() {
258
		return cdmAuthorityComposite;
259
	}
260

    
261
	/* (non-Javadoc)
262
	 * @see org.eclipse.jface.viewers.Viewer#getSelection()
263
	 */
264
	@Override
265
	public ISelection getSelection() {
266
		// TODO Auto-generated method stub
267
		return null;
268
	}
269

    
270
	/* (non-Javadoc)
271
	 * @see org.eclipse.jface.viewers.Viewer#refresh()
272
	 */
273
	@Override
274
	public void refresh() {
275
		updateAuthorities();
276
		cdmAuthorityComposite.refresh();
277

    
278
	}
279

    
280
	/* (non-Javadoc)
281
	 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
282
	 */
283
	@Override
284
	public void setSelection(ISelection selection, boolean reveal) {
285
		// TODO Auto-generated method stub
286

    
287
	}
288
}
(3-3/5)