Project

General

Profile

Revision 5d2a77b2

ID5d2a77b2afa1dde13d861f4001ae30bac8443cd6
Parent 40a73ef7
Child bfa30d38

Added by Andreas Müller about 2 years ago

ref #8030 Move cache updating to model classes, no tests yet

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Person.java
370 370
		this.lifespan = lifespan;
371 371
	}
372 372

  
373
    @Override
374
    public boolean updateCaches(){
375
        boolean result = false;
376
        result |= super.updateCaches();
377
        if (this.nomenclaturalTitle == null){
378
            this.nomenclaturalTitle = this.getTitleCache();
379
            if ( this.nomenclaturalTitle != null ){
380
                 result = true;
381
            }
382
         }
383

  
384
         return result;
385
     }
386

  
373 387
//*********************** CLONE ********************************************************/
374 388

  
375 389
	/**
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/agent/Team.java
342 342

  
343 343

  
344 344
    @Override
345
    public boolean hasUnprotectedCache(){
346
        return super.hasUnprotectedCache()
347
                || !this.protectedNomenclaturalTitleCache
348
                || !this.protectedCollectorTitleCache;
349
    }
345
    public boolean updateCaches(){
346
        boolean result = super.updateCaches();
347
        if (this.protectedNomenclaturalTitleCache == false){
348
            String oldNomTitleCache = this.nomenclaturalTitle;
349
            this.protectedNomenclaturalTitleCache = false;
350

  
351
            String newAbbrevTitleCache = cacheStrategy.getTitleCache(this);
352

  
353
            if ( oldNomTitleCache == null   || ! oldNomTitleCache.equals(newAbbrevTitleCache) ){
354
                 this.setNomenclaturalTitle(null, false);
355
                 String newCache = this.getNomenclaturalTitle();
356

  
357
                 if (newCache == null){
358
                     logger.warn("New nomTitleCache should never be null");
359
                 }
360
                 if (oldNomTitleCache == null){
361
                     logger.info("Old nomTitleCache should never be null");
362
                 }
363
                 result = true;
364
             }
365
         }
366
        return result;
367
     }
350 368

  
351 369
//*********************** CLONE ********************************************************/
352 370

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/IdentifiableEntity.java
267 267
        return this.titleCache == null || "".equals(this.titleCache);
268 268
    }
269 269

  
270
    public boolean updateCaches(){
271
        if (this.protectedTitleCache == false){
272
            String oldTitleCache = this.titleCache;
273

  
274
            String newTitleCache = cacheStrategy.getTitleCache(this);
275

  
276
            if ( oldTitleCache == null   || ! oldTitleCache.equals(newTitleCache) ){
277
                this.setTitleCache(null, false);
278
                String newCache = this.getTitleCache();
279

  
280
                if (newCache == null){
281
                    logger.warn("newCache should never be null");
282
                }
283
                if (oldTitleCache == null){
284
                    logger.info("oldTitleCache should never be null");
285
                }
286
                return true;
287
            }
288
        }
289
        return false;
290
    }
291

  
270 292
    /**
271
     * Returns true if any of the caches is not protected. Needs to be overriden
272
     * by subclass if other caches exist.
293
     * Updates the caches with the given cache strategy
294
     * @param entityCacheStrategy
295
     * @return <code>true</code> if some cache was updated, <code>false</code> otherwise
273 296
     */
274
    public boolean hasUnprotectedCache(){
275
        return !this.protectedTitleCache;
297
    public boolean updateCaches(S entityCacheStrategy){
298
        S oldCacheStrategy = this.getCacheStrategy();
299
        this.cacheStrategy = entityCacheStrategy != null? entityCacheStrategy : this.getCacheStrategy();
300
        boolean result = this.updateCaches();
301
        this.cacheStrategy = oldCacheStrategy;
302
        return result;
276 303
    }
277 304

  
278 305
//**************************************************************************************
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/TaxonName.java
822 822
        }
823 823
    }
824 824

  
825
    @Override
826
    public boolean hasUnprotectedCache(){
827
        return super.hasUnprotectedCache()
828
                || !this.protectedNameCache
829
                || !this.protectedAuthorshipCache
830
                || !this.protectedFullTitleCache;
831
    }
832

  
833 825
// ****************** GETTER / SETTER ****************************/
834 826

  
835 827
    @Override
......
1542 1534

  
1543 1535

  
1544 1536

  
1545
    /**
1546
     * Updates the authorship cache if any changes appeared in the authors nomenclatural caches.
1547
     * Deletes the titleCache and the fullTitleCache if not protected and if any change has happened
1548
     * @return
1549
     */
1550
    private void updateAuthorshipCache() {
1551
        //updates the authorship cache if necessary and via the listener updates all higher caches
1552
        if (protectedAuthorshipCache == false){
1553
            String oldCache = this.authorshipCache;
1554
            String newCache = this.getAuthorshipCache();
1555
            if ( (oldCache == null && newCache != null)  ||  CdmUtils.nullSafeEqual(oldCache,newCache)){
1556
                this.setAuthorshipCache(this.getAuthorshipCache(), false);
1557
            }
1558
        }
1559
    }
1537

  
1560 1538

  
1561 1539

  
1562 1540
    /**
......
3593 3571
        return nameType.isViral();
3594 3572
    }
3595 3573

  
3574
// *********************** CACHES ***************************************************/
3575

  
3576

  
3577
    @Override
3578
    public boolean updateCaches() {
3579
        boolean result = updateAuthorshipCache();
3580
        result |= updateNameCache();
3581
        result |= super.updateCaches();
3582
        result |= updateFullTitleCache();
3583
        return result;
3584
    }
3585

  
3586
    /**
3587
     * Updates the authorship cache if any changes appeared in the authors nomenclatural caches.
3588
     * Deletes the titleCache and the fullTitleCache if not protected and if any change has happened.
3589
     * @return <code>true</code> if something changed
3590
     */
3591
    private boolean updateAuthorshipCache() {
3592
        //updates the authorship cache if necessary and via the listener updates all higher caches
3593
        if (protectedAuthorshipCache == false){
3594
            String oldCache = this.authorshipCache;
3595
            String newCache = this.getAuthorshipCache();
3596
            if (!CdmUtils.nullSafeEqual(oldCache, newCache)){
3597
                this.setAuthorshipCache(this.getAuthorshipCache(), false);
3598
                return true;
3599
            }
3600
        }
3601
        return false;
3602
    }
3603

  
3604
    /**
3605
     * @return
3606
     */
3607
    private boolean updateNameCache() {
3608
        //updates the name cache if necessary and via the listener updates all higher caches
3609
        if (protectedNameCache == false){
3610
            String oldCache = this.nameCache;
3611
            String newCache = this.getNameCache();
3612
            if (!CdmUtils.nullSafeEqual(oldCache, newCache)){
3613
                this.setNameCache(this.getNameCache(), false);
3614
                return true;
3615
            }
3616
        }
3617
        return false;
3618
    }
3619

  
3620

  
3621
    /**
3622
     * @return
3623
     */
3624
    private boolean updateFullTitleCache() {
3625
        if (protectedFullTitleCache == false){
3626
            String oldCache = this.fullTitleCache;
3627
            String newCache = this.getFullTitleCache();
3628
            if (!CdmUtils.nullSafeEqual(oldCache, newCache)){
3629
                this.setFullTitleCache(this.getFullTitleCache(), false);
3630
                return true;
3631
            }
3632
        }
3633
        return false;
3634
    }
3635

  
3596 3636

  
3597 3637
//*********************** CLONE ********************************************************/
3598 3638

  
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/occurrence/SpecimenOrObservationBase.java
275 275
        return identityCache;
276 276
    }
277 277
    /**
278
     * @param identityCache the identityCache to set
278
     * @Deprecated For special use only.
279
     * Use {@link #setIdentityCache(String, boolean)} instead
279 280
     */
281
    @Deprecated
280 282
    public void setIdentityCache(String identityCache) {
281 283
        this.identityCache = identityCache;
282 284
    }
283 285

  
286
    public void setIdentityCache(String identityCache, boolean isProtected) {
287
        this.protectedIdentityCache = isProtected;
288
        setIdentityCache(identityCache);
289
    }
290

  
284 291
    /**
285 292
     * @return the protectedIdentityCache
286 293
     */
......
582 589
    }
583 590

  
584 591
    @Override
585
    public boolean hasUnprotectedCache(){
586
        return super.hasUnprotectedCache()
587
                || !this.protectedIdentityCache;
592
    public boolean updateCaches(){
593
        boolean result = super.updateCaches();
594
//        if (this.protectedIdentityCache == false){
595
//            String oldIdentityCache = this.identityCache;
596
//
597
//            String newIdentityCache = cacheStrategy.getIdentityCache(this);
598
//
599
//            if ( oldIdentityCache == null   || ! oldIdentityCache.equals(newIdentityCache) ){
600
//                 this.setIdentityCache(null, false);
601
//                 String newCache = this.getIdentityCache();
602
//
603
//                 if (newCache == null){
604
//                     logger.warn("New identityCache should never be null");
605
//                 }
606
//                 if (oldIdentityCache == null){
607
//                     logger.info("Old abbrevTitleCache should never be null");
608
//                 }
609
//                 result = true;
610
//             }
611
//         }
612
        return result;
588 613
    }
589 614

  
590 615
//******************** CLONE **********************************************/
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/reference/Reference.java
1158 1158
		this.cacheStrategy = referenceCacheStrategy;
1159 1159
	}
1160 1160

  
1161
   @Override
1162
   public boolean updateCaches(){
1163
       //TODO shouldn't this be moved to the cache strategy?
1164
       if (this.equals(this.getInReference())){
1165
           String message = "-- invalid inreference (self-referencing) --";
1166
           String oldTitleCache = this.titleCache;
1167
           this.titleCache = message;
1168
           return !message.equals(oldTitleCache);
1169
       }
1170
       boolean result = super.updateCaches();
1171
       if (this.protectedAbbrevTitleCache == false){
1172
           String oldAbbrevTitleCache = this.abbrevTitleCache;
1173

  
1174
           String newAbbrevTitleCache = cacheStrategy.getFullAbbrevTitleString(this);
1175

  
1176
           if ( oldAbbrevTitleCache == null   || ! oldAbbrevTitleCache.equals(newAbbrevTitleCache) ){
1177
                this.setAbbrevTitleCache(null, false);
1178
                String newCache = this.getAbbrevTitleCache();
1179

  
1180
                if (newCache == null){
1181
                    logger.warn("New abbrevCache should never be null");
1182
                }
1183
                if (oldAbbrevTitleCache == null){
1184
                    logger.info("Old abbrevTitleCache should never be null");
1185
                }
1186
                result = true;
1187
            }
1188
        }
1189
        return result;
1190
    }
1161 1191

  
1162 1192

  
1163 1193
//*********************** CLONE ********************************************************/
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IdentifiableServiceBase.java
15 15
import java.util.HashSet;
16 16
import java.util.List;
17 17
import java.util.Map;
18
import java.util.Objects;
19 18
import java.util.Set;
20 19
import java.util.UUID;
21 20

  
......
31 30
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
32 31
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
33 32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
34
import eu.etaxonomy.cdm.model.agent.Person;
35
import eu.etaxonomy.cdm.model.agent.Team;
36 33
import eu.etaxonomy.cdm.model.common.CdmBase;
37 34
import eu.etaxonomy.cdm.model.common.DefinedTerm;
38 35
import eu.etaxonomy.cdm.model.common.ISourceable;
......
41 38
import eu.etaxonomy.cdm.model.common.LSID;
42 39
import eu.etaxonomy.cdm.model.common.MarkerType;
43 40
import eu.etaxonomy.cdm.model.media.Rights;
44
import eu.etaxonomy.cdm.model.name.INonViralName;
45
import eu.etaxonomy.cdm.model.name.ITaxonName;
46
import eu.etaxonomy.cdm.model.name.TaxonName;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
49 41
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
50 42
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
51 43
import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
......
293 285

  
294 286
			List<T> entitiesToUpdate = new ArrayList<>();
295 287
			for (T entity : list){
296
				HibernateProxyHelper.deproxy(entity, clazz);
297
				if (entity.hasUnprotectedCache()){
298
				    // always execute for TeamOrPersonBase to allow updating the nomenclaturalTitle
299
					updateTitleCacheForSingleEntity(cacheStrategy, entitiesToUpdate, entity);
300
				}
288
				entity = HibernateProxyHelper.deproxy(entity);
289
			    if (entity.updateCaches(cacheStrategy)){
290
			        countUpdated++;
291
			    }
301 292
				worked++;
302 293
			}
303
			for (T entity: entitiesToUpdate){
304
				if (entity.getTitleCache() != null){
305
					//System.err.println(entity.getTitleCache());
306
				}else{
307
				    //System.err.println("no titleCache" + ((TaxonName)entity).getNameCache());
308
				}
309
			}
310
			saveOrUpdate(entitiesToUpdate);
294

  
311 295
			monitor.worked(list.size());
312 296
			if (monitor.isCanceled()){
313 297
				monitor.done();
......
342 326
		return oldAutoInitializers;
343 327
	}
344 328

  
345
	/**
346
	 * @param cacheStrategy
347
	 * @param entitiesToUpdate
348
	 * @param entity
349
	 */
350
	@SuppressWarnings("unchecked")
351
	private void updateTitleCacheForSingleEntity(
352
			IIdentifiableEntityCacheStrategy<T> cacheStrategy,
353
			List<T> entitiesToUpdate,
354
			T entity) {
355

  
356
		//assert (entity.isProtectedTitleCache() == false );
357
	    entity = CdmBase.deproxy(entity);
358

  
359
		//exclude recursive inreferences
360
		if (entity.isInstanceOf(Reference.class)){
361
			Reference ref = (Reference)entity;
362
			if (ref.getInReference() != null && ref.getInReference().equals(ref)){
363
				return;
364
			}
365
		}
366

  
367
		//define the correct cache strategy
368
		IIdentifiableEntityCacheStrategy entityCacheStrategy = cacheStrategy;
369
		if (entityCacheStrategy == null){
370
			entityCacheStrategy = entity.getCacheStrategy();
371
			//FIXME find out why the wrong cache strategy is loaded here, see #1876
372
			if (entity instanceof Reference){
373
				entityCacheStrategy = ReferenceFactory.newReference(((Reference)entity).getType()).getCacheStrategy();
374
			}
375
		}
376

  
377

  
378
		//old titleCache
379
		entity.setProtectedTitleCache(true);
380

  
381
		String oldTitleCache = entity.getTitleCache();
382
		entity.setTitleCache(oldTitleCache, false);   //before we had entity.setProtectedTitleCache(false) but this deleted the titleCache itself
383
		entity.setCacheStrategy(entityCacheStrategy);
384
		//NonViralNames and Reference have more caches //TODO handle in NameService
385
		String oldNameCache = null;
386
		String oldFullTitleCache = null;
387
		String oldAbbrevTitleCache = null;
388
        String oldNomenclaturalTitle = null;
389
		if (entity instanceof TaxonName ){
390
		    if (((TaxonName) entity).isNonViral()) {
391
                try{
392
                	INonViralName nvn = (INonViralName) entity;
393
                	if (!nvn.isProtectedNameCache()){
394
                	    nvn.setProtectedNameCache(true);
395
                		oldNameCache = nvn.getNameCache();
396
                		nvn.setProtectedNameCache(false);
397
                	}
398
                	if (!nvn.isProtectedFullTitleCache()){
399
                	    nvn.setProtectedFullTitleCache(true);
400
                		oldFullTitleCache = nvn.getFullTitleCache();
401
                		nvn.setProtectedFullTitleCache(false);
402
                	}
403
                }catch(ClassCastException e){
404
                	System.out.println("entity: " + entity.getTitleCache());
405
                }
406
            }
407
		}else if (entity instanceof Reference){
408
			Reference ref = (Reference) entity;
409
			if (!ref.isProtectedAbbrevTitleCache()){
410
				ref.setProtectedAbbrevTitleCache(true);
411
				oldAbbrevTitleCache = ref.getAbbrevTitleCache();
412
				ref.setProtectedAbbrevTitleCache(false);
413
			}
414
		} else if (entity instanceof Team){
415
            Team team = (Team) entity;
416
            if(!team.isProtectedNomenclaturalTitleCache()){
417
                team.setProtectedNomenclaturalTitleCache(true);
418
                oldNomenclaturalTitle = team.getNomenclaturalTitle();
419
                team.setProtectedNomenclaturalTitleCache(false);
420
            }
421
        } else if (entity instanceof Person){
422
            // Person has no flag for protecting the nomenclaturalTitle
423
            Person person = (Person) entity;
424
            oldNomenclaturalTitle = person.getNomenclaturalTitle();
425
        }
426
		setOtherCachesNull(entity);
427
		String newTitleCache= null;
428
		INonViralName nvn = null; //TODO find better solution
429
		try{
430
			if (entity instanceof TaxonName){
431
				nvn = (ITaxonName) entity;
432
				newTitleCache = entityCacheStrategy.getTitleCache(nvn);
433
			} else{
434
				 newTitleCache = entityCacheStrategy.getTitleCache(entity);
435
			}
436
		}catch (ClassCastException e){
437
			nvn = HibernateProxyHelper.deproxy(entity, TaxonName.class);
438
			newTitleCache = entityCacheStrategy.getTitleCache(nvn);
439
		}
440

  
441
		if ( oldTitleCache == null   || ! oldTitleCache.equals(newTitleCache) ){
442
			entity.setTitleCache(null, false);
443
			String newCache = entity.getTitleCache();
444

  
445
			if (newCache == null){
446
				logger.warn("newCache should never be null");
447
			}
448
			if (oldTitleCache == null){
449
				logger.info("oldTitleCache should never be null");
450
			}
451
			if (nvn != null){
452
				nvn.getNameCache();
453
				nvn.getFullTitleCache();
454
			}
455
			if (entity instanceof Reference){
456
				Reference ref = (Reference) entity;
457
				ref.getAbbrevTitleCache();
458
			}
459
			entitiesToUpdate.add(entity);
460
		}else if (nvn != null){
461
			String newNameCache = nvn.getNameCache();
462
			String newFullTitleCache = nvn.getFullTitleCache();
463
			if ((oldNameCache == null && !nvn.isProtectedNameCache()) || (oldNameCache != null && !oldNameCache.equals(newNameCache))){
464
				entitiesToUpdate.add(entity);
465
			}else if ((oldFullTitleCache == null && !nvn.isProtectedFullTitleCache()) || (oldFullTitleCache != null && !oldFullTitleCache.equals(newFullTitleCache))){
466
				entitiesToUpdate.add(entity);
467
			}
468
		}else if (entity instanceof Reference){
469
			Reference ref = (Reference) entity;
470
			String newAbbrevTitleCache = ref.getAbbrevTitleCache();
471
			if ( (oldAbbrevTitleCache == null && !ref.isProtectedAbbrevTitleCache() ) || (oldAbbrevTitleCache != null && !oldAbbrevTitleCache.equals(newAbbrevTitleCache))){
472
				entitiesToUpdate.add(entity);
473
			}
474
		}else if (entity instanceof Team){
475
		    Team team = (Team) entity;
476
            String newNomenclaturalTitle = team.getNomenclaturalTitle();
477
            if ( (oldNomenclaturalTitle == null && !team.isProtectedNomenclaturalTitleCache() ) || !Objects.equals(oldNomenclaturalTitle, newNomenclaturalTitle)){
478
                entitiesToUpdate.add(entity);
479
            }
480
        }else if (entity instanceof Person){
481
            Person person = (Person) entity;
482
            String newNomenclaturalTitle = person.getNomenclaturalTitle();
483
            if (!Objects.equals(oldNomenclaturalTitle, newNomenclaturalTitle)){
484
                entitiesToUpdate.add(entity);
485
            }
486
        }
487
	}
488

  
489

  
490
	/**
491
	 * Needs override if not only the title cache should be set to null to
492
	 * generate the correct new title cache
493
	 */
494
	protected void setOtherCachesNull(T entity) {
495
		return;
496
	}
497

  
498 329
	private class DeduplicateState{
499 330
		String lastTitleCache;
500 331
		Integer pageSize = 50;
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/NameServiceImpl.java
847 847

  
848 848

  
849 849
    @Override
850
    protected void setOtherCachesNull(TaxonName name) {
851
         if (! name.isProtectedNameCache()){
852
             name.setNameCache(null, false);
853
        }
854
        if (! name.isProtectedAuthorshipCache()){
855
            name.setAuthorshipCache(null, false);
856
        }
857
        if (! name.isProtectedFullTitleCache()){
858
            name.setFullTitleCache(null, false);
859
        }
860

  
861
    }
862

  
863
    @Override
864 850
    public List<TaggedText> getTaggedName(UUID uuid) {
865 851
        TaxonName taxonName = dao.load(uuid);
866 852
        List<TaggedText> taggedName = taxonName.getTaggedName();
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/ReferenceServiceImpl.java
53 53
        super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
54 54
    }
55 55

  
56

  
57
    @Override
58
    protected void setOtherCachesNull(Reference ref) {
59
        if (! ref.isProtectedAbbrevTitleCache()){
60
            ref.setAbbrevTitleCache(null, false);
61
        }
62
    }
63

  
64

  
65 56
    @Override
66 57
    @Autowired
67 58
    protected void setDao(IReferenceDao dao) {

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)