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.common.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
	private TableViewer viewer;
101

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

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

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

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

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

    
156
		viewer = tableViewer;
157

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

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

    
165
	}
166

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

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

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

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

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

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

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

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

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

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

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

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

    
236
	                        return Status.CANCEL_STATUS;
237
	                    }
238
	                }
239

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

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

    
250
				return Status.OK_STATUS;
251

    
252

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

    
257
		currentJob.schedule();
258
	}
259

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

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

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

    
328
	private List<CdmBase> sortReferencing(){
329

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

    
340
        }
341

    
342
	    return null;
343
	}
344

    
345
	class ReferencingClassComparator implements Comparator<CdmBase>   {
346

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

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

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

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

    
374
        				viewer.refresh();
375

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

    
388

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

    
398
         });
399
	 }
400

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

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

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

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

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

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

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

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

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

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

    
485
}
    (1-1/1)