Project

General

Profile

« Previous | Next » 

Revision 58e6cb8f

Added by Andreas Müller about 3 years ago

ref #9541 new referencing objects implementation in TaxEditor

View differences:

eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/referencingobjects/e4/ReferencingObjectsViewE4.java
10 10
package eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.e4;
11 11

  
12 12
import java.util.ArrayList;
13
import java.util.Arrays;
13 14
import java.util.Collections;
14 15
import java.util.Comparator;
16
import java.util.HashMap;
17
import java.util.HashSet;
15 18
import java.util.List;
19
import java.util.Map;
16 20
import java.util.Set;
17 21
import java.util.UUID;
18 22

  
......
42 46
import org.eclipse.swt.widgets.Display;
43 47
import org.eclipse.swt.widgets.Label;
44 48
import org.eclipse.swt.widgets.Table;
45
import org.springframework.remoting.RemoteAccessException;
46 49

  
47 50
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
48 51
import eu.etaxonomy.cdm.api.service.IAgentService;
......
59 62
import eu.etaxonomy.cdm.api.service.ITermService;
60 63
import eu.etaxonomy.cdm.api.service.IUserService;
61 64
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
65
import eu.etaxonomy.cdm.common.CdmUtils;
62 66
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
63 67
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
64 68
import eu.etaxonomy.cdm.model.common.CdmBase;
......
79 83
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
80 84
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
81 85
import eu.etaxonomy.cdm.model.term.TermBase;
86
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
82 87
import eu.etaxonomy.cdm.persistence.dto.TermDto;
88
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
83 89
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsContentProvider;
84 90
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsLabelProvider;
85 91
import eu.etaxonomy.taxeditor.editor.IReferencingObjectsView;
......
94 100
 */
95 101
public class ReferencingObjectsViewE4 extends AbstractCdmEditorPartE4 implements IReferencingObjectsView{
96 102

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

  
105
    public static final String IS_LOADING = "Loading ...";
106
//    public static final String CANCELED = "Canceled ...";
99 107

  
100 108
    private Label contentDescription;
109
    private String description;
101 110

  
102
	private String referencedObjectTitleCache;
111
//	private String referencedObjectTitleCache;
103 112
	private ConversationHolder conversation;
104 113
	private UUID actualUuid;
105
	List<CdmBase> referencingObjects = null;
106
	Set<CdmBase> referencingObjectsSet = null;
107
	IProgressMonitor actualMonitor = null;
114
	private List<ReferencingObjectDto> referencingObjects = null;
115
	private Set<ReferencingObjectDto> referencingObjectsSet = null;
116
	private IProgressMonitor actualMonitor = null;
108 117
	private Job currentJob = null;
109 118

  
110 119
    @PostConstruct
......
200 209
	        while(!actualMonitor.isCanceled()){
201 210
	            actualMonitor.setCanceled(true);
202 211
	        }
203

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

  
214
		currentJob = new Job("Update Referencing Objects for " + entityUUID) {
206 215

  
207 216
			@Override
208 217
			protected IStatus run(final IProgressMonitor monitor) {
......
210 219
				actualUuid = entityUUID;
211 220

  
212 221
				monitor.worked(2);
213
				referencingObjects = new ArrayList<>();
222
				ReferencingObjectDto loadDto = new ReferencingObjectDto();
223
				loadDto.setTitleCache(IS_LOADING);
224
				referencingObjects = Arrays.asList(loadDto);
225
				updateView();
214 226
				if(monitor.isCanceled()) {
215
				    actualUuid = null;
227
//				    actualUuid = null;
216 228
                    return Status.CANCEL_STATUS;
217 229
                }
218 230
				if (actualMonitor != null){
......
222 234
				actualMonitor = monitor;
223 235
				if (entityUUID != null){
224 236
				    monitor.subTask("Load data from server");
225
	                referencingObjectsSet = getReferencingObjects(entityUUID, objectClass);
226

  
237
				    monitor.worked(1);
238
	                Set<ReferencingObjectDto> refObjectsFromServer = loadReferencingObjects(entityUUID, objectClass);
239
	                monitor.worked(25);
227 240
	                if(monitor.isCanceled()) {
228
	                    actualUuid = null;
229

  
241
//	                  actualUuid = null;
230 242
	                    return Status.CANCEL_STATUS;
231 243
	                }
232
	                monitor.worked(33);
233
                    monitor.subTask("Sort");
234
	                referencingObjects = sortReferencing();
235
	                if (referencingObjects == null){
236
	                    if (monitor.isCanceled()){
237
	                        actualUuid = null;
238

  
239
	                        return Status.CANCEL_STATUS;
240
	                    }
244
	                referencingObjectsSet = refObjectsFromServer;
245

  
246
	                monitor.subTask("Show without description");
247
	                monitor.worked(1);//maybe this helps to update the subtask label
248

  
249
	                //TODO have a status line instead
250
	                updateDescriptionLabel(description + "(n="+referencingObjectsSet.size()+")"); //
251

  
252

  
253
	                List<ReferencingObjectDto> localDtos = sortToList(referencingObjectsSet, monitor);
254
	                monitor.worked(10);
255
	                if(monitor.isCanceled()) {
256
//	                    actualUuid = null;
257
	                    return Status.CANCEL_STATUS;
241 258
	                }
259
	                referencingObjects = localDtos;
260
	                updateView();
261

  
262
	                monitor.subTask("Initialize");
263
	                monitor.worked(1);//maybe this helps to update the subtask label
264
                    initializeDtos(localDtos, monitor, 50);
265
                    if (monitor.isCanceled()){
266
//                        actualUuid = null;
267
                        //TODO use status line instead
268
//                        referencingObjects = Arrays.asList(CANCELED);
269
                        updateView();
270
//                        monitor.done();
271
                        return Status.CANCEL_STATUS;
272
                    }
273

  
274
                    //not necessary anymore, we sort by type and
275
                    //id as this can be done immediately
276
//	                monitor.subTask("Sort");
277
//                    referencingObjects = sortReferencing();
278
//                    monitor.worked(5);
279
//	                if (referencingObjects == null){
280
//	                    if (monitor.isCanceled()){
281
////	                        actualUuid = null;
282
//	                        return Status.CANCEL_STATUS;
283
//	                    }
284
//	                }
242 285

  
243 286
	                if(monitor.isCanceled()) {
244 287
	                    return Status.CANCEL_STATUS;
245 288
	                }
246
	                monitor.worked(35);
247 289
	                monitor.subTask("Update View");
248 290

  
249 291
	                updateView();
250
	                monitor.worked(30);
292
	                monitor.worked(5);
251 293
				}
252 294
				monitor.done();
253 295
				actualMonitor = null;
254 296

  
255 297
				return Status.OK_STATUS;
256 298
			}
299

  
257 300
		};
258 301
		currentJob.setUser(true);
259 302

  
260 303
		currentJob.schedule();
261 304
	}
262 305

  
263
	private Set<CdmBase> getReferencingObjects(UUID entity, Class objectClass) {
264
		CdmBase referencedObject = null;
306
    private List<ReferencingObjectDto> sortToList(Set<ReferencingObjectDto> referencingObjectsSet, IProgressMonitor monitor) {
307
        List<ReferencingObjectDto> result = new ArrayList<>(referencingObjectsSet);
308
        //TODO make singleton
309
        Collections.sort(result, new RefObjectDtoComparator());
310
        return result;
311
    }
312

  
313
    protected boolean initializeDtos(List<ReferencingObjectDto> localDtos, IProgressMonitor monitor, int work) {
314
        IProgressMonitor subMonitor = AbstractUtility.getSubProgressMonitor(monitor, work);
315
        subMonitor.beginTask("Initialize DTOs", localDtos.size());
316

  
317
        int i = 100 - 20;  //the first run should only include 20 records
318

  
319
        Set<ReferencingObjectDto> toInitialize = new HashSet<>();
320
        for (ReferencingObjectDto dto : localDtos){
321
            toInitialize.add(dto);
322

  
323
//            dto.setTitleCache(ReferencingObjectFormatter.format(dto.getReferencedEntity(), CdmStore.getDefaultLanguage() ));
324
            if (monitor.isCanceled()){
325
                return false;
326
            }
327
            subMonitor.worked(1);
328
            if (++i == 100){
329
                initBulk(toInitialize);
330
                updateView();
331
                toInitialize = new HashSet<>();
332
                i = 0;
333
            }
334
        }
335
        initBulk(toInitialize);
336
        return true;
337
    }
338

  
339
    private void initBulk(Set<ReferencingObjectDto> toInitialize) {
340
        Set<ReferencingObjectDto> initialized = CdmStore.getCommonService().initializeReferencingObjectDtos(toInitialize, true, true, true, CdmStore.getDefaultLanguage());
341
        Map<UUID,ReferencingObjectDto> map = new HashMap<>();
342
        initialized.forEach(i->map.put(i.getUuid(), i));
343
        toInitialize.forEach(dto->merge(dto, map.get(dto.getUuid())));
344
        updateView();
345

  
346
    }
347

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

  
354
    private Set<ReferencingObjectDto> loadReferencingObjects(UUID entity, Class objectClass) {
355
		//TODO why do we need to load the referenced object here
356
        CdmBase referencedObject = null;
265 357
        try {
266 358
        	if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
267 359
	        	referencedObject = CdmStore.getService(IAgentService.class).load(entity);
......
302 394
                referencedObject = CdmStore.getCommonService().find(objectClass, entity);
303 395
            }
304 396
	        	//referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
305
        	Set<CdmBase> setOfReferencingObjects = null;
397
        	Set<ReferencingObjectDto> setOfReferencingObjects = null;
306 398

  
307 399
        	if (referencedObject != null){
308
        	    if(referencedObject.isInstanceOf(IdentifiableEntity.class)){
309
        	        referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedObject, IdentifiableEntity.class)).getTitleCache();
310
        	    }
311
        	    else if(referencedObject.isInstanceOf(DescriptionElementBase.class)){
312
        	        referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
313
        	    }
314
        	    else if (referencedObject.isInstanceOf(User.class)){
315
        	        referencedObjectTitleCache = ((User)referencedObject).getUsername();
316
        	    }else{
317
        	        referencedObjectTitleCache = null;
318
        	    }
319
        		setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjects(referencedObject);
400
//        	    if(referencedObject.isInstanceOf(IdentifiableEntity.class)){
401
//        	        referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedObject, IdentifiableEntity.class)).getTitleCache();
402
//        	    }
403
//        	    else if(referencedObject.isInstanceOf(DescriptionElementBase.class)){
404
//        	        referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
405
//        	    }
406
//        	    else if (referencedObject.isInstanceOf(User.class)){
407
//        	        referencedObjectTitleCache = ((User)referencedObject).getUsername();
408
//        	    }else{
409
//        	        referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
410
//        	    }
411
        		setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjectDtos(referencedObject);
320 412
        		return setOfReferencingObjects;
321 413
        	}
322 414

  
323 415
        } catch (Exception e) {
324 416
            logger.debug("Error retrieving referencing objects", e);
325 417
            e.printStackTrace();
326
            updateDescription("The referencing objects view could not be loaded completely. Some Problems occurred.");
418
            updateDescriptionLabel("The referencing objects view could not be loaded completely. Some problems occurred: " + e.getMessage());
327 419
        }
328 420
		return null;
329 421
	}
330 422

  
331
	private List<CdmBase> sortReferencing(){
423
	class RefObjectDtoComparator implements Comparator<ReferencingObjectDto>{
332 424

  
333
	    if (referencingObjectsSet != null){
334
            List<CdmBase> referencingObjects = new ArrayList<>(referencingObjectsSet);
335
            try{
336
                Collections.sort(referencingObjects, new ReferencingClassComparator());
337
            }catch (RemoteAccessException e){
338
                logger.debug(e.getStackTrace());
339
                return null;
425
        @Override
426
        public int compare(ReferencingObjectDto dto1, ReferencingObjectDto dto2) {
427
            int result = dto1.getType().getSimpleName().compareToIgnoreCase(dto2.getType().getSimpleName());
428
            if (result == 0) {
429
                result = Integer.compare(dto1.getId(),dto2.getId());
340 430
            }
341
            return referencingObjects;
342

  
431
            return result;
343 432
        }
344

  
345
	    return null;
346
	}
347

  
348
	class ReferencingClassComparator implements Comparator<CdmBase>   {
349

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

  
359
				result = string1.compareToIgnoreCase(string2);
360
				if (result == 0){
361
				    return o1.getUuid().compareTo(o2.getUuid());
362
				}
363
			}
364
			return result;
365
		}
366 433
	}
367 434

  
368 435
	private void updateView() {
369
	    Display.getDefault().asyncExec(new Runnable() {
370
            @Override
371
            public void run() {
372

  
373
        		if (viewer != null && !viewer.getControl().isDisposed()){
374
        			try{
375
        				viewer.setInput(referencingObjects);
376

  
377
        				viewer.refresh();
378

  
379
        				//enable/disable table
380
        				viewer.getControl().setEnabled(referencingObjects!=null);
381
        			}catch(Exception e){
382
        			    e.printStackTrace();
383
        			    logger.debug(e.getStackTrace());
384
        			    updateDescription("The referencing objects view could not be loaded completely. Some Problems occurred.");
385
        			}
386
        		}
436
	    Display.getDefault().asyncExec(()->{
437
	        if (viewer != null && !viewer.getControl().isDisposed()){
438
                try{
439
                    viewer.setInput(referencingObjects);
440
                    viewer.refresh();
441

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

  
391 453

  
392
	private void updateDescription(final String description){
393
	    Display.getDefault().asyncExec(new Runnable() {
394
            @Override
395
            public void run() {
454
	private void updateDescriptionLabel(final String description){
455
	    Display.getDefault().asyncExec(()->{
396 456
                if(contentDescription!=null && !contentDescription.isDisposed()) {
397 457
                    contentDescription.setText(description.replace("&", "&&"));
398 458
                }
399 459
            }
400
         });
460
         );
401 461
	 }
402 462

  
403 463
    @Override
......
420 480
		Object firstElement = selection.getFirstElement();
421 481
		if (firstElement instanceof TermDto){
422 482
		   TermDto termDto = (TermDto) firstElement;
423
		   updateDescription("'"+termDto.getRepresentation_L10n() + "' is referenced by:");
483
		   description = "'"+termDto.getRepresentation_L10n() + "' is referenced by:";
484
		   updateDescriptionLabel(description);
424 485
		   updateReferencingObjects(termDto.getUuid(), TermBase.class );
425 486
		   return;
426 487
		}
......
435 496
		    if (referencedCdmObject.getUuid() == actualUuid){
436 497
		        return;
437 498
		    }
499
		    String referencedObjectTitleCache = null;
438 500
	        if(referencedCdmObject.isInstanceOf(IdentifiableEntity.class)){
439 501
                referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedCdmObject, IdentifiableEntity.class)).getTitleCache();
440 502
            }
......
446 508
            }else if (referencedCdmObject.isInstanceOf(TaxonNode.class)){
447 509
                referencedObjectTitleCache = "TaxonNode of "+(HibernateProxyHelper.deproxy(referencedCdmObject, TaxonNode.class)).getTaxon().getTitleCache();
448 510
            }
449
		    if (referencedObjectTitleCache != null){
450
		        updateDescription(referencedCdmObject.getUserFriendlyTypeName() + " '" + referencedObjectTitleCache + "' is referenced by:");
451
		    } else{
452
		        updateDescription("");
453
		    }
511
	        if (CdmUtils.isBlank(referencedObjectTitleCache)){
512
	            referencedObjectTitleCache = "#"+referencedCdmObject.getId();
513
	        }
514
	        description = referencedCdmObject.getUserFriendlyTypeName() + " '" + referencedObjectTitleCache + "' is referenced by:";
515
	        updateDescriptionLabel(description);
454 516

  
455 517
		    updateReferencingObjects(referencedCdmObject.getUuid(),firstElement.getClass() );
456 518

  
519
		}else if  (firstElement instanceof UuidAndTitleCache<?>){
520
		    UuidAndTitleCache<?> element = (UuidAndTitleCache<?>) firstElement;
521
		    description = element.getType().getSimpleName() + " '" + element.getTitleCache() + "' is referenced by:";
522
	        updateDescriptionLabel(description);
523
            updateReferencingObjects(element.getUuid(),element.getType());
457 524
		}
458 525
		else if (firstElement != null){
459 526
		    updateView();
460
            updateDescription("");
527
            description = "undefined:";
528
		    updateDescriptionLabel(description);
461 529
		}
462 530
	}
463 531

  

Also available in: Unified diff