Project

General

Profile

Download (25.4 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
    public static final String IS_LOADING = "Loading ...";
119
//    public static final String CANCELED = "Canceled ...";
120

    
121
    private static final List<ReferencingObjectDto> EMPTY_LIST = Arrays.asList();  //immutable empty list
122
    private static final RefObjectDtoComparator COMPARATOR = new RefObjectDtoComparator();
123

    
124
    private Label contentDescription;
125

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

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

    
144
		tableViewer.setCellModifier(new ICellModifier() {
145

    
146
            @Override
147
            public void modify(Object element, String property, Object value) {}
148

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

    
164
            @Override
165
            public boolean canModify(Object element, String property) {
166
                return true;
167
            }
168
        });
169

    
170
		viewer = tableViewer;
171

    
172
        // Propagate selection from viewer
173
        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
174
        viewer.addSelectionChangedListener(selectionChangedListener);
175

    
176
        //create context menu
177
        menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.referencingobjectsview");
178
	}
179

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

    
189
        CellEditor[] editors = new CellEditor[titles.length];
190

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

    
202
		viewer.setCellEditors(editors);
203
		viewer.setColumnProperties(titles);
204
	}
205

    
206

    
207
    private class ItemDto {
208
        String typeName;
209
        String itemLabel;
210
        UUID itemUuid;
211
        Integer itemId;
212
        Class<? extends CdmBase> itemClass;
213

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

    
221
	private class UpdateRefObjectsJob extends Job{
222

    
223
	    final private ItemDto item;
224

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

    
233
        @Override
234
        protected IStatus run(final IProgressMonitor monitor) {
235

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

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

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

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

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

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

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

    
289
            return Status.OK_STATUS;
290
        }
291
	}
292

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

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

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

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

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

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

    
350
    private void mergeInitializedData(ReferencingObjectDto to, ReferencingObjectDto from) {
351
        to.setTitleCache(from.getTitleCache());
352
        to.setOpenInTarget(from.getOpenInTarget());
353
        to.setReferencedEntity(from.getReferencedEntity());
354
    }
355

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

    
400
        	if (referencedObject != null){
401
        		setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjectDtos(referencedObject);
402
        		return setOfReferencingObjects;
403
        	}
404

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

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

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

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

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

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

    
455

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

    
465
    @Override
466
    protected boolean showEmptyIfNoActiveEditor(){
467
        return false;
468
    }
469

    
470
    @Override
471
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
472
        if(activePart==thisPart){
473
            return;
474
        }
475

    
476
        IStructuredSelection structuredSelection = createSelection(selection);
477
        if(structuredSelection!=null){
478
        	showViewer(structuredSelection, activePart, viewer);
479
        }
480
	}
481

    
482
	@Override
483
	public void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
484
	    handleNewSelection(selection.getFirstElement());
485
	}
486

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

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

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

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

    
593
	@PreDestroy
594
	public void dispose() {
595
	    //no conversation in this view
596
	}
597

    
598
	@Override
599
	public void changed(Object object) {
600
		// no editing in this view
601
	}
602

    
603
	@Override
604
	public boolean onComplete() {
605
		return false;
606
	}
607

    
608
	@Override
609
	protected String getViewName() {
610
		return "Referencing Objects";
611
	}
612
}
    (1-1/1)