Project

General

Profile

Download (25.2 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.Arrays;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.UUID;
22

    
23
import javax.annotation.PostConstruct;
24
import javax.annotation.PreDestroy;
25

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

    
51
import eu.etaxonomy.cdm.api.service.IAgentService;
52
import eu.etaxonomy.cdm.api.service.IDescriptionService;
53
import eu.etaxonomy.cdm.api.service.IEventBaseService;
54
import eu.etaxonomy.cdm.api.service.IGroupService;
55
import eu.etaxonomy.cdm.api.service.IMediaService;
56
import eu.etaxonomy.cdm.api.service.INameService;
57
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
58
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
59
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
60
import eu.etaxonomy.cdm.api.service.IReferenceService;
61
import eu.etaxonomy.cdm.api.service.ITaxonService;
62
import eu.etaxonomy.cdm.api.service.ITermService;
63
import eu.etaxonomy.cdm.api.service.IUserService;
64
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
65
import eu.etaxonomy.cdm.common.CdmUtils;
66
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
67
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
68
import eu.etaxonomy.cdm.model.common.CdmBase;
69
import eu.etaxonomy.cdm.model.common.EventBase;
70
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
71
import eu.etaxonomy.cdm.model.description.DescriptionBase;
72
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
73
import eu.etaxonomy.cdm.model.description.PolytomousKey;
74
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
75
import eu.etaxonomy.cdm.model.media.Media;
76
import eu.etaxonomy.cdm.model.molecular.Sequence;
77
import eu.etaxonomy.cdm.model.name.TaxonName;
78
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
79
import eu.etaxonomy.cdm.model.permission.Group;
80
import eu.etaxonomy.cdm.model.permission.User;
81
import eu.etaxonomy.cdm.model.reference.Reference;
82
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
83
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
84
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
85
import eu.etaxonomy.cdm.model.term.TermBase;
86
import eu.etaxonomy.cdm.model.term.TermNode;
87
import eu.etaxonomy.cdm.model.term.TermTree;
88
import eu.etaxonomy.cdm.model.term.TermVocabulary;
89
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
90
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
91
import eu.etaxonomy.cdm.persistence.dto.TermDto;
92
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
93
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
94
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
95
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
96
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsContentProvider;
97
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsLabelProvider;
98
import eu.etaxonomy.taxeditor.editor.IReferencingObjectsView;
99
import eu.etaxonomy.taxeditor.model.AbstractUtility;
100
import eu.etaxonomy.taxeditor.model.DescriptionHelper;
101
import eu.etaxonomy.taxeditor.store.CdmStore;
102
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
103

    
104
/**
105
 * This view loads and presents referencing objects information asynchronously.
106
 *
107
 * Most of the task is done in UpdateRefObjectsJob.run()
108
 *
109
 * @author pplitzner
110
 * @author k.luther
111
 * @author a.mueller
112
 * @since Aug 16, 2017
113
 */
114
public class ReferencingObjectsViewE4 extends AbstractCdmEditorPartE4 implements IReferencingObjectsView{
115

    
116
    private static final Logger logger = Logger.getLogger(ReferencingObjectsViewE4.class);
117

    
118
    private static final List<ReferencingObjectDto> EMPTY_LIST = Arrays.asList();  //immutable empty list
119
    private static final RefObjectDtoComparator COMPARATOR = new RefObjectDtoComparator();
120

    
121
    private Label contentDescription;
122

    
123
	private volatile UUID actualUuid;  //volatile as it used used between threads but only written in the synchronized method
124
	private Job currentJob = null;  //this variable should only be accessed in synchronized updateToNewItem
125

    
126
    @PostConstruct
127
    public void create(Composite parent, EMenuService menuService) {
128
        if (!CdmStore.isActive()){
129
            return;
130
        }
131
        parent.setLayout(new GridLayout());
132
		contentDescription = new Label(parent, SWT.NONE);
133
		contentDescription.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
134
		TableViewer tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
135
				| SWT.V_SCROLL | SWT.FULL_SELECTION);
136
		createColumns(tableViewer);
137
		tableViewer.setContentProvider(new ReferencingObjectsContentProvider());
138
		tableViewer.setLabelProvider(new ReferencingObjectsLabelProvider());
139
		tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
140

    
141
		tableViewer.setCellModifier(new ICellModifier() {
142

    
143
            @Override
144
            public void modify(Object element, String property, Object value) {}
145

    
146
            @Override
147
            public Object getValue(Object element, String property) {
148
                ITableLabelProvider tableLabelProvider = null;
149
                if(tableViewer.getLabelProvider() instanceof ITableLabelProvider){
150
                    tableLabelProvider = (ITableLabelProvider) tableViewer.getLabelProvider();
151
                }
152
                Object[] columnProperties = tableViewer.getColumnProperties();
153
                for (int i=0;i<columnProperties.length;i++) {
154
                    if(columnProperties[i].equals(property) && tableLabelProvider!=null){
155
                        return tableLabelProvider.getColumnText(element, i);
156
                    }
157
                }
158
                return "";
159
            }
160

    
161
            @Override
162
            public boolean canModify(Object element, String property) {
163
                return true;
164
            }
165
        });
166

    
167
		viewer = tableViewer;
168

    
169
        // Propagate selection from viewer
170
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
171
        viewer.addSelectionChangedListener(selectionChangedListener);
172

    
173
        //create context menu
174
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.referencingobjectsview");
175
	}
176

    
177
	/**
178
	 * Create the columns for the table
179
	 * @param viewer
180
	 */
181
	private void createColumns(TableViewer viewer) {
182
		Table table = viewer.getTable();
183
		String[] titles = {"Class", "Description", "UUID", "Object ID" };
184
		int[] bounds = { 100, 200, 100, 70};
185

    
186
        CellEditor[] editors = new CellEditor[titles.length];
187

    
188
		for (int i = 0; i < titles.length; i++) {
189
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
190
			editors[i] = new TextCellEditor(table);
191
			column.getColumn().setText(titles[i]);
192
			column.getColumn().setWidth(bounds[i]);
193
			column.getColumn().setResizable(true);
194
			column.getColumn().setMoveable(true);
195
		}
196
		table.setHeaderVisible(true);
197
		table.setLinesVisible(true);
198

    
199
		viewer.setCellEditors(editors);
200
		viewer.setColumnProperties(titles);
201
	}
202

    
203

    
204
    private class ItemDto {
205
        String typeName;
206
        String itemLabel;
207
        UUID itemUuid;
208
        Integer itemId;
209
        Class<? extends CdmBase> itemClass;
210

    
211
        private String bestLabel(){
212
            return (StringUtils.isNotBlank(this.typeName)? this.typeName + " " : "")
213
                    + "'"+(StringUtils.isNotBlank(this.itemLabel)? this.itemLabel : this.itemUuid.toString()) +"'"
214
                    + (this.itemId != null? " (id=" + this.itemId+")" : "");
215
        }
216
    }
217

    
218
	private class UpdateRefObjectsJob extends Job{
219

    
220
	    final private ItemDto item;
221

    
222
        public UpdateRefObjectsJob(String name, ItemDto item) {
223
            super(name);
224
            this.item = item;
225
            if (item.itemUuid == null){
226
                throw new RuntimeException("Item uuid must always exist at this point");
227
            }
228
        }
229

    
230
        @Override
231
        protected IStatus run(final IProgressMonitor monitor) {
232

    
233
            monitor.beginTask("Calculating referencing objects for " + item.itemLabel, 100);
234

    
235
            //set to loading
236
            monitor.subTask("Load empty");
237
            monitor.worked(1);
238
            updateView("Loading " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
239
            if(monitor.isCanceled()) {
240
                return Status.CANCEL_STATUS;
241
            }
242

    
243
            //handle transient instance
244
            if (item.itemId != null && item.itemId.equals(0)){
245
                updateView("Not yet persisted: " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
246
                monitor.done();
247
                return Status.OK_STATUS;
248
            }
249
            monitor.worked(1);  //sum = 2
250

    
251
            //load uninitialized DTOs from server
252
            monitor.subTask("Load base data from server");
253
            Set<ReferencingObjectDto> refObjectsFromServer = loadReferencingObjects(item.itemUuid, item.itemClass);
254
            if (refObjectsFromServer == null){
255
                updateView("An error occurred when loading " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
256
                return Status.CANCEL_STATUS;  //TODO is this correct?, null can happen e.g. if server call throws exception
257
            }
258
            if(monitor.isCanceled()) {
259
                return Status.CANCEL_STATUS;
260
            }
261
            monitor.worked(10); //sum = 12
262

    
263
            //show count
264
            monitor.subTask("Show without description");
265
            int count = refObjectsFromServer.size();
266
            updateView("Loading " + count + " items for " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
267
            monitor.worked(1);  //sum = 13
268

    
269
            //sort
270
            List<ReferencingObjectDto> localDtos = sortToList(refObjectsFromServer);
271
            updateView("0/" + count + " items for " + item.bestLabel(), localDtos, item.itemUuid);
272
            if(monitor.isCanceled()) {
273
                return Status.CANCEL_STATUS;
274
            }
275
            monitor.worked(2);  //sum = 15
276

    
277
            //initialize
278
            monitor.subTask("Initialize");
279
            initializeDtos(localDtos, item, monitor, 83);  //is calling updateView itself; sum = 95
280
            if(monitor.isCanceled()) {
281
                return Status.CANCEL_STATUS;
282
            }
283
            monitor.worked(2);  //sum = 100 (just in case)
284
            monitor.done();
285

    
286
            return Status.OK_STATUS;
287
        }
288
	}
289

    
290
	private synchronized void updateToNewItem(final ItemDto item) {
291
		if (currentJob != null){
292
		    currentJob.cancel();
293
		}
294
		Job newJob = new UpdateRefObjectsJob("Update Referencing Objects for " + item.bestLabel(), item);
295
		newJob.setUser(true);
296
		actualUuid = item.itemUuid;
297
		currentJob = newJob;
298
		currentJob.schedule();
299
	}
300

    
301
    private List<ReferencingObjectDto> sortToList(Set<ReferencingObjectDto> referencingObjectsSet) {
302
        List<ReferencingObjectDto> result = new ArrayList<>(referencingObjectsSet);
303
        Collections.sort(result, COMPARATOR);
304
        return result;
305
    }
306

    
307
    protected boolean initializeDtos(List<ReferencingObjectDto> localDtos, ItemDto item, IProgressMonitor monitor, int work) {
308

    
309
        IProgressMonitor subMonitor = AbstractUtility.getSubProgressMonitor(monitor, work);
310
        subMonitor.beginTask("Initialize DTOs", localDtos.size());
311
        int i = 100 - 20;  //the first run should only include 20 records
312
        int initCount = 0;
313

    
314
        Set<ReferencingObjectDto> toInitialize = new HashSet<>();
315
        for (ReferencingObjectDto dto : localDtos){
316
            initCount++;
317
            toInitialize.add(dto);
318
            subMonitor.worked(1);
319
            if (++i == 100){
320
                if (monitor.isCanceled()){
321
                    return false;
322
                }
323
                initBulk(toInitialize, initCount, localDtos, item);
324
                //reset
325
                toInitialize = new HashSet<>();
326
                i = 0;
327
            }
328
        }
329
        //final bulk
330
        if (monitor.isCanceled()){
331
            return false;
332
        }
333
        initBulk(toInitialize, initCount, localDtos, item);
334
        return true;
335
    }
336

    
337
    private void initBulk(Set<ReferencingObjectDto> toInitialize, int initCount,
338
            List<ReferencingObjectDto> localDtos, ItemDto item) {
339
        Set<ReferencingObjectDto> initialized = CdmStore.getCommonService().initializeReferencingObjectDtos(toInitialize, true, true, true, CdmStore.getDefaultLanguage());
340
        Map<UUID,ReferencingObjectDto> map = new HashMap<>();
341
        initialized.forEach(i->map.put(i.getUuid(), i));
342
        toInitialize.forEach(dto->mergeInitializedData(dto, map.get(dto.getUuid())));
343
        String initStr = initCount < localDtos.size()? initCount + "/" + localDtos.size() + " items": "Items";
344
        updateView(initStr + " for " + item.bestLabel(), localDtos, item.itemUuid);
345
    }
346

    
347
    private void mergeInitializedData(ReferencingObjectDto to, ReferencingObjectDto from) {
348
        to.setTitleCache(from.getTitleCache());
349
        to.setOpenInTarget(from.getOpenInTarget());
350
        to.setReferencedEntity(from.getReferencedEntity());
351
    }
352

    
353
    private Set<ReferencingObjectDto> loadReferencingObjects(UUID entity, Class objectClass) {
354
		//TODO why do we need to load the referenced object here
355
        CdmBase referencedObject = null;
356
        try {
357
        	if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
358
	        	referencedObject = CdmStore.getService(IAgentService.class).load(entity);
359
        	} else if (objectClass.getSuperclass().equals(TaxonName.class)){
360
        		referencedObject = CdmStore.getService(INameService.class).load(entity);
361
        	} else if (objectClass.getSuperclass().equals(TaxonBase.class)){
362
        		referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
363
        	} else if (objectClass.equals(Reference.class)){
364
        		referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
365
        	} else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
366
        		referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
367
        	} else if (objectClass.isAssignableFrom(User.class)){
368
        		referencedObject = CdmStore.getService(IUserService.class).load(entity);
369
        	} else if (objectClass.isAssignableFrom(Group.class)){
370
        		referencedObject = CdmStore.getService(IGroupService.class).load(entity);
371
        	} else if (objectClass.isAssignableFrom(Media.class)){
372
        	    referencedObject = CdmStore.getService(IMediaService.class).load(entity);
373
        	} else if (DescriptionBase.class.isAssignableFrom(objectClass)){
374
        	    referencedObject = CdmStore.getService(IDescriptionService.class).load(entity);
375
        	} else if (DescriptionElementBase.class.isAssignableFrom(objectClass)){
376
                referencedObject = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(entity, null);
377
            } else if (objectClass.equals(Sequence.class)){
378
                referencedObject = CdmStore.getService(ISequenceService.class).load(entity, null);
379
            } else if (PolytomousKey.class.isAssignableFrom(objectClass)){
380
                referencedObject = CdmStore.getService(IPolytomousKeyService.class).load(entity, null);
381
            } else if (PolytomousKeyNode.class.isAssignableFrom(objectClass)){
382
                referencedObject = CdmStore.getService(IPolytomousKeyNodeService.class).load(entity, null);
383
            } else if (DefinedTermBase.class.isAssignableFrom(objectClass)){
384
                referencedObject = CdmStore.getService(ITermService.class).load(entity, null);
385
            } else if (EventBase.class.isAssignableFrom(objectClass)){
386
                referencedObject = CdmStore.getService(IEventBaseService.class).load(entity, null);
387
            } else if (User.class.isAssignableFrom(objectClass)){
388
                referencedObject = CdmStore.getService(IUserService.class).load(entity, null);
389
            } else if (Group.class.isAssignableFrom(objectClass)){
390
                referencedObject = CdmStore.getService(IGroupService.class).load(entity, null);
391
            }
392
            else if(CdmBase.class.isAssignableFrom(objectClass)){
393
                referencedObject = CdmStore.getCommonService().find(objectClass, entity);
394
            }
395
        	Set<ReferencingObjectDto> setOfReferencingObjects = null;
396

    
397
        	if (referencedObject != null){
398
        		setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjectDtos(referencedObject);
399
        		return setOfReferencingObjects;
400
        	}
401

    
402
        } catch (Exception e) {
403
            logger.debug("Error retrieving referencing objects", e);
404
            e.printStackTrace();
405
            updateDescriptionLabel("The referencing objects view could not be loaded completely. Some problems occurred: " + e.getMessage());
406
        }
407
		return null;
408
	}
409

    
410
	/**
411
	 * Compares the referencing object by type and id. Using "description" for
412
	 * comparation has been given up to avoid initialization before comparison.
413
	 */
414
	static class RefObjectDtoComparator implements Comparator<ReferencingObjectDto>{
415

    
416
        @Override
417
        public int compare(ReferencingObjectDto dto1, ReferencingObjectDto dto2) {
418
            int result = dto1.getType().getSimpleName().compareToIgnoreCase(dto2.getType().getSimpleName());
419
            if (result == 0) {
420
                result = Integer.compare(dto1.getId(),dto2.getId());
421
            }
422
            return result;
423
        }
424
	}
425

    
426
	//not sure if it needs to be synchronized, only the local variable actualUuid is read
427
	private void updateView(final String label,
428
	        final List<ReferencingObjectDto> referencingObjects, UUID itemUuid) {
429

    
430
	    if (this.actualUuid == itemUuid){
431
	        Display.getDefault().asyncExec(()->{
432
	            if (contentDescription != null && !contentDescription.isDisposed()){
433
	                contentDescription.setText(label);
434
	            }
435
	            if (viewer != null && !viewer.getControl().isDisposed()){
436
	                try{
437
	                    viewer.setInput(referencingObjects);
438
	                    viewer.refresh();
439

    
440
	                    //enable/disable table
441
	                    viewer.getControl().setEnabled(referencingObjects!=null);
442
	                }catch(Exception e){
443
	                    e.printStackTrace();
444
	                    logger.debug(e.getStackTrace());
445
	                    updateDescriptionLabel("The referencing objects view could not be updated completely. Some Problems occurred: " + e.getMessage());
446
	                }
447
	            }
448
	        });
449
	    }
450
	}
451

    
452

    
453
	private void updateDescriptionLabel(final String description){
454
	    Display.getDefault().asyncExec(()->{
455
                if(contentDescription!=null && !contentDescription.isDisposed()) {
456
                    contentDescription.setText(description.replace("&", "&&"));
457
                }
458
            }
459
         );
460
	 }
461

    
462
    @Override
463
    protected boolean showEmptyIfNoActiveEditor(){
464
        return false;
465
    }
466

    
467
    @Override
468
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
469
        if(activePart==thisPart){
470
            return;
471
        }
472

    
473
        IStructuredSelection structuredSelection = createSelection(selection);
474
        if(structuredSelection!=null){
475
        	showViewer(structuredSelection, activePart, viewer);
476
        }
477
	}
478

    
479
	@Override
480
	public void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
481
	    handleNewSelection(selection.getFirstElement());
482
	}
483

    
484
	//Note AM: this can probably be done better together with base class methods
485
	//         As I am not so familiar with this structure I only adapt it this way to be on the safe side
486
	@Override
487
    public void handleNewSelection(Object firstElement){
488
        ItemDto dto = makeItemDto(firstElement);
489
		if (dto.itemUuid == null || dto.itemClass == null || dto.itemUuid.equals(this.actualUuid)){
490
		    return;
491
		}
492
		updateToNewItem(dto);
493
	}
494

    
495
    /**
496
     * Transforms the selection into uniform format (ItemDto).
497
     * This method must be fast / should never require a server call
498
     * as it takes place before the update Job is started and therefore is not
499
     * asynchronous.
500
     */
501
    public ItemDto makeItemDto(Object firstElement) {
502

    
503
        ItemDto dto = new ItemDto();
504
        if(firstElement instanceof TreeNode){
505
            firstElement = ((TreeNode) firstElement).getValue();
506
        }
507
        if (firstElement instanceof TaxonNode && !((TaxonNode)firstElement).hasTaxon()){
508
            firstElement = ((TaxonNode)firstElement).getClassification();
509
        }
510

    
511
        if (firstElement instanceof AbstractTermDto){
512
           AbstractTermDto termDto = (AbstractTermDto) firstElement;
513
           dto.itemLabel = termDto.getTitleCache();
514
           dto.itemUuid= termDto.getUuid();
515
           dto.typeName = termDto.getTermType().getLabel();
516
           dto.itemId = null;   // id does not yet exist in TermDto
517
           if (termDto instanceof TermDto){
518
               dto.itemClass = TermBase.class;
519
           }else if(termDto instanceof TermTreeDto){
520
               dto.itemClass = TermTree.class;
521
               dto.typeName = dto.typeName + " Tree";
522
           }else if(termDto instanceof TermVocabularyDto){
523
               dto.itemClass = TermVocabulary.class;
524
               dto.typeName = dto.typeName + " Vocabulary";
525
           }else{
526
               //make it an unhandled selection
527
               dto.itemUuid = null;
528
           }
529
        }else if (firstElement instanceof TermNodeDto){
530
            TermNodeDto termNodeDto = (TermNodeDto) firstElement;
531
            dto.itemLabel = (termNodeDto.getTerm() != null? termNodeDto.getTerm().getTitleCache() : termNodeDto.getTreeIndex());
532
            dto.itemUuid= termNodeDto.getUuid();
533
            dto.typeName = termNodeDto.getType().getLabel() + " Node";
534
            dto.itemId = null;  //does not yet exist in TermDto
535
            dto.itemClass = TermNode.class;
536
        }else if(firstElement instanceof CdmBase){
537
		    CdmBase cdmBase = CdmBase.deproxy(firstElement, CdmBase.class);
538
		    String label = null;
539
	        if(cdmBase instanceof IdentifiableEntity){
540
                label = (HibernateProxyHelper.deproxy(cdmBase, IdentifiableEntity.class)).getTitleCache();
541
            }
542
            else if(cdmBase instanceof DescriptionElementBase){
543
                label = DescriptionHelper.getLabel(cdmBase);
544
            }
545
            else if (cdmBase instanceof User){
546
                label = ((User)cdmBase).getUsername();
547
            }else if (cdmBase instanceof TaxonNode){
548
                label = ((TaxonNode)cdmBase).getTaxon().getTitleCache();
549
            }
550
	        if (CdmUtils.isBlank(label)){
551
	            label = "#"+cdmBase.getId();
552
	        }
553
	        dto.typeName = cdmBase.getUserFriendlyTypeName();
554
	        dto.itemLabel = label;
555
	        dto.itemUuid= cdmBase.getUuid();
556
	        dto.itemClass = cdmBase.getClass();
557
	        dto.itemId = cdmBase.getId();
558
		}else if  (firstElement instanceof UuidAndTitleCache<?>){
559
		    UuidAndTitleCache<? extends CdmBase> element = (UuidAndTitleCache<? extends CdmBase>) firstElement;
560
		    dto.typeName = CdmUtils.userFriendlyClassName(element.getType());
561
		    dto.itemLabel = element.getTitleCache();
562
            if (CdmUtils.isBlank(dto.itemLabel)){
563
                dto.itemLabel = "id=" + element.getId();
564
            }
565
            dto.itemUuid= element.getUuid();
566
            dto.itemClass = element.getType();
567
            dto.itemId = element.getId();
568
		}else if (firstElement instanceof String){
569
            dto.typeName = "String";
570
            dto.itemLabel = firstElement.toString();
571
            dto.itemUuid= null;
572
            dto.itemClass = null;
573
            dto.itemId = null;
574
		}else if (firstElement != null){
575
		    dto.typeName = CdmUtils.userFriendlyClassName(firstElement.getClass());
576
		    dto.itemLabel = firstElement.toString();
577
		    dto.itemUuid= null;
578
		    dto.itemClass = null;
579
		    dto.itemId = null;
580
		}else{
581
		    dto.typeName = null;
582
		    dto.itemLabel = "no selection";
583
		    dto.itemUuid= null;
584
		    dto.itemClass = null;
585
		    dto.itemId = null;
586
		}
587
        return dto;
588
    }
589

    
590
	@PreDestroy
591
	public void dispose() {
592
	    //no conversation in this view
593
	}
594

    
595
	@Override
596
	public void changed(Object object) {
597
		// no editing in this view
598
	}
599

    
600
	@Override
601
	public boolean onComplete() {
602
		return false;
603
	}
604

    
605
	@Override
606
	protected String getViewName() {
607
		return "Referencing Objects";
608
	}
609
}
    (1-1/1)