Project

General

Profile

Download (18.3 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.bulkeditor.referencingobjects.e4;
11

    
12
import java.util.ArrayList;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import javax.annotation.PostConstruct;
20
import javax.annotation.PreDestroy;
21

    
22
import org.apache.log4j.Logger;
23
import org.eclipse.core.runtime.IProgressMonitor;
24
import org.eclipse.core.runtime.IStatus;
25
import org.eclipse.core.runtime.Status;
26
import org.eclipse.core.runtime.jobs.Job;
27
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
28
import org.eclipse.e4.ui.services.EMenuService;
29
import org.eclipse.jface.viewers.CellEditor;
30
import org.eclipse.jface.viewers.ICellModifier;
31
import org.eclipse.jface.viewers.IStructuredSelection;
32
import org.eclipse.jface.viewers.ITableLabelProvider;
33
import org.eclipse.jface.viewers.TableViewer;
34
import org.eclipse.jface.viewers.TableViewerColumn;
35
import org.eclipse.jface.viewers.TextCellEditor;
36
import org.eclipse.jface.viewers.TreeNode;
37
import org.eclipse.jface.viewers.Viewer;
38
import org.eclipse.swt.SWT;
39
import org.eclipse.swt.layout.GridData;
40
import org.eclipse.swt.layout.GridLayout;
41
import org.eclipse.swt.widgets.Composite;
42
import org.eclipse.swt.widgets.Display;
43
import org.eclipse.swt.widgets.Label;
44
import org.eclipse.swt.widgets.Table;
45
import org.springframework.remoting.RemoteAccessException;
46

    
47
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
48
import eu.etaxonomy.cdm.api.service.IAgentService;
49
import eu.etaxonomy.cdm.api.service.IDescriptionService;
50
import eu.etaxonomy.cdm.api.service.IEventBaseService;
51
import eu.etaxonomy.cdm.api.service.IGroupService;
52
import eu.etaxonomy.cdm.api.service.IMediaService;
53
import eu.etaxonomy.cdm.api.service.INameService;
54
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
55
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
56
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
57
import eu.etaxonomy.cdm.api.service.IReferenceService;
58
import eu.etaxonomy.cdm.api.service.ITaxonService;
59
import eu.etaxonomy.cdm.api.service.ITermService;
60
import eu.etaxonomy.cdm.api.service.IUserService;
61
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
62
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
63
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
64
import eu.etaxonomy.cdm.model.common.CdmBase;
65
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
66
import eu.etaxonomy.cdm.model.common.EventBase;
67
import eu.etaxonomy.cdm.model.common.Group;
68
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
69
import eu.etaxonomy.cdm.model.common.User;
70
import eu.etaxonomy.cdm.model.description.DescriptionBase;
71
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
72
import eu.etaxonomy.cdm.model.description.PolytomousKey;
73
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
74
import eu.etaxonomy.cdm.model.media.Media;
75
import eu.etaxonomy.cdm.model.molecular.Sequence;
76
import eu.etaxonomy.cdm.model.name.TaxonName;
77
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
78
import eu.etaxonomy.cdm.model.reference.Reference;
79
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
80
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
81
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsContentProvider;
82
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsLabelProvider;
83
import eu.etaxonomy.taxeditor.model.AbstractUtility;
84
import eu.etaxonomy.taxeditor.model.DescriptionHelper;
85
import eu.etaxonomy.taxeditor.store.CdmStore;
86
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
87

    
88
/**
89
 *
90
 * @author pplitzner
91
 * @since Aug 16, 2017
92
 *
93
 */
94
public class ReferencingObjectsViewE4 extends AbstractCdmEditorPartE4 {
95

    
96
    private static final Logger logger = Logger
97
			.getLogger(ReferencingObjectsViewE4.class);
98

    
99
    private Label contentDescription;
100

    
101
	private String referencedObjectTitleCache;
102
	private ConversationHolder conversation;
103
	private UUID actualUuid;
104
	List<CdmBase> referencingObjects = null;
105
	Set<CdmBase> referencingObjectsSet = null;
106
	IProgressMonitor actualMonitor = null;
107
	private Job currentJob = null;
108

    
109
    @PostConstruct
110
    public void create(Composite parent, EMenuService menuService) {
111
        if (CdmStore.isActive()){
112
            if(conversation == null){
113
                conversation = CdmStore.createConversation();
114
            }
115
        }
116
        else{
117
            return;
118
        }
119
        parent.setLayout(new GridLayout());
120
		contentDescription = new Label(parent, SWT.NONE);
121
		contentDescription.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
122
		TableViewer tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
123
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
124
		createColumns(tableViewer);
125
		tableViewer.setContentProvider(new ReferencingObjectsContentProvider());
126
		tableViewer.setLabelProvider(new ReferencingObjectsLabelProvider());
127
		tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128

    
129
		tableViewer.setCellModifier(new ICellModifier() {
130
            @Override
131
            public void modify(Object element, String property, Object value) {
132
            }
133

    
134
            @Override
135
            public Object getValue(Object element, String property) {
136
                ITableLabelProvider tableLabelProvider = null;
137
                if(tableViewer.getLabelProvider() instanceof ITableLabelProvider){
138
                    tableLabelProvider = (ITableLabelProvider) tableViewer.getLabelProvider();
139
                }
140
                Object[] columnProperties = tableViewer.getColumnProperties();
141
                for (int i=0;i<columnProperties.length;i++) {
142
                    if(columnProperties[i].equals(property) && tableLabelProvider!=null){
143
                        return tableLabelProvider.getColumnText(element, i);
144
                    }
145
                }
146
                return "";
147
            }
148

    
149
            @Override
150
            public boolean canModify(Object element, String property) {
151
                return true;
152
            }
153
        });
154

    
155
		viewer = tableViewer;
156

    
157
        // Propagate selection from viewer
158
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
159
        viewer.addSelectionChangedListener(selectionChangedListener);
160

    
161
        //create context menu
162
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.referencingobjectsview");
163

    
164
	}
165

    
166
	/**
167
	 * Create the columns for the table
168
	 * @param viewer
169
	 */
170
	private void createColumns(TableViewer viewer) {
171
		Table table = viewer.getTable();
172
		String[] titles = {"Class", "Description", "UUID", "Object ID" };
173
		int[] bounds = { 100, 200, 100, 70};
174

    
175
        CellEditor[] editors = new CellEditor[titles.length];
176

    
177
		for (int i = 0; i < titles.length; i++) {
178
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
179
			editors[i] = new TextCellEditor(table);
180
			column.getColumn().setText(titles[i]);
181
			column.getColumn().setWidth(bounds[i]);
182
			column.getColumn().setResizable(true);
183
			column.getColumn().setMoveable(true);
184
		}
185
		table.setHeaderVisible(true);
186
		table.setLinesVisible(true);
187

    
188
		viewer.setCellEditors(editors);
189
		viewer.setColumnProperties(titles);
190
	}
191

    
192
	public void updateReferencingObjects(final UUID entityUUID, final Class objectClass) {
193
	    if (actualUuid == entityUUID){
194
	        return ;
195
	    }
196

    
197
	    if(actualMonitor!=null && !actualMonitor.isCanceled()){
198
	        while(!actualMonitor.isCanceled()){
199
	            actualMonitor.setCanceled(true);
200
	        }
201

    
202
	    }
203
		currentJob = new Job("Update Referencing Objects") {
204

    
205
			@Override
206
			protected IStatus run(final IProgressMonitor monitor) {
207
				monitor.beginTask("Calculating referencing objects", 100);
208
				actualUuid = entityUUID;
209

    
210
				monitor.worked(5);
211
				referencingObjects = new ArrayList<>();
212
				if(monitor.isCanceled()) {
213
				    actualUuid = null;
214
                    return Status.CANCEL_STATUS;
215
                }
216
				if (actualMonitor != null){
217
				    actualMonitor.setCanceled(true);
218

    
219
				}
220
				actualMonitor = monitor;
221
				if (entityUUID != null){
222
	                referencingObjectsSet = getReferencingObjects(entityUUID, objectClass);
223

    
224
	                if(monitor.isCanceled()) {
225
	                    actualUuid = null;
226

    
227
	                    return Status.CANCEL_STATUS;
228
	                }
229
	                monitor.worked(30);
230
	                referencingObjects = sortReferencing();
231
	                if (referencingObjects == null){
232
	                    if (monitor.isCanceled()){
233
	                        actualUuid = null;
234

    
235
	                        return Status.CANCEL_STATUS;
236
	                    }
237
	                }
238

    
239
	                if(monitor.isCanceled()) {
240
	                    return Status.CANCEL_STATUS;
241
	                }
242
	                monitor.worked(80);
243

    
244
	                updateView();
245
				}
246
				monitor.done();
247
				actualMonitor = null;
248

    
249
				return Status.OK_STATUS;
250

    
251

    
252
			}
253
		};
254
		currentJob.setUser(true);
255

    
256
		currentJob.schedule();
257
	}
258

    
259
	private Set<CdmBase> getReferencingObjects(UUID entity, Class objectClass) {
260
		CdmBase referencedObject = null;
261
        try {
262
        	if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
263
	        	referencedObject = CdmStore.getService(IAgentService.class).load(entity);
264
        	} else if (objectClass.getSuperclass().equals(TaxonName.class)){
265
        		referencedObject = CdmStore.getService(INameService.class).load(entity);
266
        	} else if (objectClass.getSuperclass().equals(TaxonBase.class)){
267
        		referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
268
        	} else if (objectClass.equals(Reference.class)){
269
        		referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
270
        	} else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
271
        		referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
272
        	} else if (objectClass.isAssignableFrom(User.class)){
273
        		referencedObject = CdmStore.getService(IUserService.class).load(entity);
274
        	} else if (objectClass.isAssignableFrom(Group.class)){
275
        		referencedObject = CdmStore.getService(IGroupService.class).load(entity);
276
        	} else if (objectClass.isAssignableFrom(Media.class)){
277
        	    referencedObject = CdmStore.getService(IMediaService.class).load(entity);
278
        	} else if (DescriptionBase.class.isAssignableFrom(objectClass)){
279
        	    referencedObject = CdmStore.getService(IDescriptionService.class).load(entity);
280
        	} else if (DescriptionElementBase.class.isAssignableFrom(objectClass)){
281
                referencedObject = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(entity, null);
282
            } else if (objectClass.equals(Sequence.class)){
283
                referencedObject = CdmStore.getService(ISequenceService.class).load(entity, null);
284
            } else if (PolytomousKey.class.isAssignableFrom(objectClass)){
285
                referencedObject = CdmStore.getService(IPolytomousKeyService.class).load(entity, null);
286
            } else if (PolytomousKeyNode.class.isAssignableFrom(objectClass)){
287
                referencedObject = CdmStore.getService(IPolytomousKeyNodeService.class).load(entity, null);
288
            } else if (DefinedTermBase.class.isAssignableFrom(objectClass)){
289
                referencedObject = CdmStore.getService(ITermService.class).load(entity, null);
290
            } else if (EventBase.class.isAssignableFrom(objectClass)){
291
                referencedObject = CdmStore.getService(IEventBaseService.class).load(entity, null);
292
            } else if (User.class.isAssignableFrom(objectClass)){
293
                referencedObject = CdmStore.getService(IUserService.class).load(entity, null);
294
            } else if (Group.class.isAssignableFrom(objectClass)){
295
                referencedObject = CdmStore.getService(IGroupService.class).load(entity, null);
296
            }
297
            else if(CdmBase.class.isAssignableFrom(objectClass)){
298
                referencedObject = CdmStore.getCommonService().find(objectClass, entity);
299
            }
300
	        	//referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
301
        	Set<CdmBase> setOfReferencingObjects = null;
302

    
303
        	if (referencedObject != null){
304
        	    if(referencedObject.isInstanceOf(IdentifiableEntity.class)){
305
        	        referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedObject, IdentifiableEntity.class)).getTitleCache();
306
        	    }
307
        	    else if(referencedObject.isInstanceOf(DescriptionElementBase.class)){
308
        	        referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
309
        	    }
310
        	    else if (referencedObject.isInstanceOf(User.class)){
311
        	        referencedObjectTitleCache = ((User)referencedObject).getUsername();
312
        	    }else{
313
        	        referencedObjectTitleCache = null;
314
        	    }
315
        		setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjects(referencedObject);
316
        		return setOfReferencingObjects;
317
        	}
318

    
319
        } catch (Exception e) {
320
            logger.debug("Error retrieving referencing objects", e);
321
            e.printStackTrace();
322
            updateDescription("The referencing objects view could not be loaded completely. Some Problems occurred.");
323
        }
324
		return null;
325
	}
326

    
327
	private List<CdmBase> sortReferencing(){
328

    
329
	    if (referencingObjectsSet != null){
330
            List<CdmBase> referencingObjects = new ArrayList<CdmBase>(referencingObjectsSet);
331
            try{
332
                Collections.sort(referencingObjects, new ReferencingClassComparator());
333
            }catch (RemoteAccessException e){
334
                logger.debug(e.getStackTrace());
335
                return null;
336
            }
337
            return referencingObjects;
338

    
339
        }
340

    
341
	    return null;
342
	}
343

    
344
	class ReferencingClassComparator implements Comparator<CdmBase>   {
345

    
346
		@Override
347
        public int compare(CdmBase o1, CdmBase o2) {
348
			String string1 = o1.getClass().getSimpleName();
349
			String string2 = o2.getClass().getSimpleName();
350
			int result = string1.compareToIgnoreCase(string2);
351
			if (result == 0) {
352
				string1 = DescriptionHelper.getObjectDescription(o1);
353
				string2 = DescriptionHelper.getObjectDescription(o2);
354

    
355
				result = string1.compareToIgnoreCase(string2);
356
				if (result == 0){
357
				    return o1.getUuid().compareTo(o2.getUuid());
358
				}
359
			}
360
				return result;
361
		}
362
	}
363

    
364
	private void updateView() {
365
	    Display.getDefault().asyncExec(new Runnable() {
366
            @Override
367
            public void run() {
368

    
369
        		if (viewer != null && !viewer.getControl().isDisposed()){
370
        			try{
371
        				viewer.setInput(referencingObjects);
372

    
373
        				viewer.refresh();
374

    
375
        				//enable/disable table
376
        				viewer.getControl().setEnabled(referencingObjects!=null);
377
        			}catch(Exception e){
378
        			    e.printStackTrace();
379
        			    logger.debug(e.getStackTrace());
380
        			    updateDescription("The referencing objects view could not be loaded completely. Some Problems occurred.");
381
        			}
382
        		}
383
            }
384
            });
385
	}
386

    
387

    
388
	private void updateDescription(final String description){
389
	    Display.getDefault().asyncExec(new Runnable() {
390
            @Override
391
            public void run() {
392
                if(contentDescription!=null && !contentDescription.isDisposed()) {
393
                    contentDescription.setText(description.replace("&", "&&"));
394
                }
395
            }
396

    
397
         });
398
	 }
399

    
400
    @Override
401
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
402
        if(activePart==thisPart){
403
            return;
404
        }
405

    
406
        IStructuredSelection structuredSelection = createSelection(selection);
407
        if(structuredSelection!=null){
408
        	//referencedObjectTitleCache = null;
409
        	showViewer(structuredSelection, activePart, viewer);
410
        }
411
	}
412

    
413
	/** {@inheritDoc} */
414
	@Override
415
	public void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
416
	//	this.part = part;
417

    
418
		Object firstElement = selection.getFirstElement();
419
		if(firstElement instanceof TreeNode){
420
		    firstElement = ((TreeNode) firstElement).getValue();
421
		}
422
		if (firstElement instanceof TaxonNode && !((TaxonNode)firstElement).hasTaxon()){
423
			firstElement = ((TaxonNode)firstElement).getClassification();
424
		}
425
		if(firstElement instanceof CdmBase){
426
		    CdmBase referencedCdmObject = (CdmBase) firstElement;
427
		    if (referencedCdmObject.getUuid() == actualUuid){
428
		        return;
429
		    }
430
		    if (referencedCdmObject != null){
431
                if(referencedCdmObject.isInstanceOf(IdentifiableEntity.class)){
432
                    referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedCdmObject, IdentifiableEntity.class)).getTitleCache();
433
                }
434
                else if(referencedCdmObject.isInstanceOf(DescriptionElementBase.class)){
435
                    referencedObjectTitleCache = DescriptionHelper.getLabel(referencedCdmObject);
436
                }
437
                else if (referencedCdmObject.isInstanceOf(User.class)){
438
                    referencedObjectTitleCache = ((User)referencedCdmObject).getUsername();
439
                }else if (referencedCdmObject.isInstanceOf(TaxonNode.class)){
440
                    referencedObjectTitleCache = "TaxonNode of "+(HibernateProxyHelper.deproxy(referencedCdmObject, TaxonNode.class)).getTaxon().getTitleCache();
441
                }
442
		    }
443
		    if (referencedObjectTitleCache != null){
444
		        updateDescription(referencedCdmObject.getUserFriendlyTypeName() + " '" + referencedObjectTitleCache + "' is referenced by:");
445
		    } else{
446
		        updateDescription("");
447
		    }
448

    
449
		    updateReferencingObjects(referencedCdmObject.getUuid(),firstElement.getClass() );
450

    
451
		}
452
		else if (firstElement != null){
453
		    updateView();
454
            updateDescription("");
455
		}
456
	}
457

    
458
	/** {@inheritDoc} */
459
	@PreDestroy
460
	public void dispose() {
461
	    if(conversation!=null){
462
	        conversation.close();
463
	        conversation = null;
464
	    }
465
	}
466

    
467
	/** {@inheritDoc} */
468
	@Override
469
	public void changed(Object object) {
470
		// no editing in this view
471
	}
472

    
473
	/** {@inheritDoc} */
474
	@Override
475
	public boolean onComplete() {
476
		return false;
477
	}
478

    
479
	@Override
480
	protected String getViewName() {
481
		return "Referencing Objects";
482
	}
483

    
484
}
    (1-1/1)