Revision a30452ad
Added by Andreas Müller about 3 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/molecular/DnaSample.java | ||
---|---|---|
36 | 36 |
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit; |
37 | 37 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
38 | 38 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType; |
39 |
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
|
|
39 |
import eu.etaxonomy.cdm.strategy.cache.occurrence.DnaSampleDefaultCacheStrategy;
|
|
40 | 40 |
|
41 | 41 |
/** |
42 | 42 |
* A DNA Sample is the extracted DNA of a given tissue sample. It may be stored in |
... | ... | |
105 | 105 |
|
106 | 106 |
protected DnaSample() { //protected for Javassist, otherwise private |
107 | 107 |
super(SpecimenOrObservationType.DnaSample); |
108 |
this.cacheStrategy = new IdentifiableEntityDefaultCacheStrategy<DerivedUnit>();
|
|
108 |
this.cacheStrategy = new DnaSampleDefaultCacheStrategy();
|
|
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
//************ GETTER / SETTER **********************************/ |
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/cache/occurrence/DnaSampleDefaultCacheStrategy.java | ||
---|---|---|
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 |
package eu.etaxonomy.cdm.strategy.cache.occurrence; |
|
10 |
|
|
11 |
import java.util.UUID; |
|
12 |
|
|
13 |
import org.apache.log4j.Logger; |
|
14 |
|
|
15 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
16 |
import eu.etaxonomy.cdm.model.common.IdentifiableSource; |
|
17 |
import eu.etaxonomy.cdm.model.common.Language; |
|
18 |
import eu.etaxonomy.cdm.model.molecular.DnaSample; |
|
19 |
import eu.etaxonomy.cdm.model.molecular.Sequence; |
|
20 |
|
|
21 |
/** |
|
22 |
* A default cache strategy for {@link DnaSample}. |
|
23 |
* |
|
24 |
* TODO This is a <b>preliminary</b> implementation to have at least one default cache strategy. |
|
25 |
* It will need improvement later on. |
|
26 |
* |
|
27 |
* #5575 |
|
28 |
* |
|
29 |
* Also see DerivedUnitFacadeCacheStrategy in Service Layer. |
|
30 |
* |
|
31 |
* @author a.mueller |
|
32 |
* @since 09.01.2021 |
|
33 |
*/ |
|
34 |
public class DnaSampleDefaultCacheStrategy |
|
35 |
extends OccurrenceCacheStrategyBase<DnaSample>{ |
|
36 |
|
|
37 |
private static final long serialVersionUID = -7166834437359037691L; |
|
38 |
|
|
39 |
@SuppressWarnings("unused") |
|
40 |
private static final Logger logger = Logger.getLogger(DnaSampleDefaultCacheStrategy.class); |
|
41 |
|
|
42 |
private static final UUID uuid = UUID.fromString("48e72fbc-9714-4df6-b031-ba361fbf3f59"); |
|
43 |
|
|
44 |
@Override |
|
45 |
protected UUID getUuid() { |
|
46 |
return uuid; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
protected String doGetTitleCache(DnaSample dnaSample) { |
|
51 |
String result = getCollectionAndAccession(dnaSample); |
|
52 |
//TODO still very unclear what to use, currently it is best practice to create |
|
53 |
//a protected titleCache when creating the DnaSample so the application |
|
54 |
//decides what title to give |
|
55 |
if (isBlank(result)){ |
|
56 |
if (!dnaSample.getIdentifiers().isEmpty()){ |
|
57 |
result = dnaSample.getIdentifiers().get(0).getIdentifier(); |
|
58 |
} |
|
59 |
} |
|
60 |
if (isBlank(result)){ |
|
61 |
if (!dnaSample.getDefinition().isEmpty()){ |
|
62 |
Language key = dnaSample.getDefinition().keySet().iterator().next(); |
|
63 |
result = truncate(dnaSample.getDefinition().get(key).getText(), 50); |
|
64 |
} |
|
65 |
} |
|
66 |
if (isBlank(result)){ |
|
67 |
if (!dnaSample.getSequences().isEmpty()){ |
|
68 |
Sequence seq = dnaSample.getSequences().iterator().next(); |
|
69 |
if (seq != null){ |
|
70 |
result = seq.getSequenceString(); |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
if (isBlank(result)){ |
|
75 |
if (!dnaSample.getSources().isEmpty()){ |
|
76 |
for (IdentifiableSource source : dnaSample.getSources()){ |
|
77 |
if (isNotBlank(source.getIdInSource())){ |
|
78 |
result = CdmUtils.concat(":", source.getIdNamespace(), source.getIdInSource()); |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
} |
|
84 |
|
|
85 |
return result; |
|
86 |
} |
|
87 |
} |
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/cache/occurrence/OccurrenceCacheStrategyBase.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2021 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 |
package eu.etaxonomy.cdm.strategy.cache.occurrence; |
|
10 |
|
|
11 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
12 |
import eu.etaxonomy.cdm.model.occurrence.Collection; |
|
13 |
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit; |
|
14 |
import eu.etaxonomy.cdm.strategy.StrategyBase; |
|
15 |
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy; |
|
16 |
|
|
17 |
/** |
|
18 |
* @author a.mueller |
|
19 |
* @since 09.01.2021 |
|
20 |
*/ |
|
21 |
public abstract class OccurrenceCacheStrategyBase<T extends DerivedUnit> |
|
22 |
extends StrategyBase |
|
23 |
implements IIdentifiableEntityCacheStrategy<T>{ |
|
24 |
|
|
25 |
private static final long serialVersionUID = -6044178882022038807L; |
|
26 |
|
|
27 |
@Override |
|
28 |
public String getTitleCache(T specimen) { |
|
29 |
if (specimen == null){ |
|
30 |
return null; |
|
31 |
} |
|
32 |
String result = doGetTitleCache(specimen); |
|
33 |
if (isBlank(result)){ |
|
34 |
result = specimen.toString(); |
|
35 |
} |
|
36 |
return result; |
|
37 |
} |
|
38 |
|
|
39 |
protected abstract String doGetTitleCache(T specimen); |
|
40 |
|
|
41 |
protected String getCollectionAndAccession(T specimen){ |
|
42 |
String result = null; |
|
43 |
if (specimen.getCollection() != null){ |
|
44 |
Collection collection = specimen.getCollection(); |
|
45 |
if (isNotBlank(collection.getCode())){ |
|
46 |
result = collection.getCode(); |
|
47 |
} |
|
48 |
} |
|
49 |
result = CdmUtils.concat(" ", result, CdmUtils.Ne(specimen.getAccessionNumber())); |
|
50 |
return result; |
|
51 |
} |
|
52 |
|
|
53 |
protected String truncate(String str, int length){ |
|
54 |
if (str == null){ |
|
55 |
return null; |
|
56 |
}else{ |
|
57 |
if (str.length() > length){ |
|
58 |
if (length <3){ |
|
59 |
return str.substring(0, str.length()); |
|
60 |
}else{ |
|
61 |
return str.substring(0, str.length()-3)+"..."; |
|
62 |
} |
|
63 |
}else{ |
|
64 |
return str; |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
cdmlib-model/src/test/java/eu/etaxonomy/cdm/strategy/cache/occurrence/DnaSampleDefaultCacheStrategyTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2021 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 |
package eu.etaxonomy.cdm.strategy.cache.occurrence; |
|
10 |
|
|
11 |
import java.util.UUID; |
|
12 |
|
|
13 |
import org.junit.Assert; |
|
14 |
import org.junit.Test; |
|
15 |
|
|
16 |
import eu.etaxonomy.cdm.model.common.Identifier; |
|
17 |
import eu.etaxonomy.cdm.model.common.Language; |
|
18 |
import eu.etaxonomy.cdm.model.molecular.DnaSample; |
|
19 |
import eu.etaxonomy.cdm.model.occurrence.Collection; |
|
20 |
import eu.etaxonomy.cdm.model.term.DefinedTerm; |
|
21 |
import eu.etaxonomy.cdm.test.TermTestBase; |
|
22 |
|
|
23 |
/** |
|
24 |
* @author a.mueller |
|
25 |
* @since 09.01.2021 |
|
26 |
*/ |
|
27 |
public class DnaSampleDefaultCacheStrategyTest extends TermTestBase { |
|
28 |
|
|
29 |
@Test |
|
30 |
public void test() { |
|
31 |
DnaSample specimen = DnaSample.NewInstance(); |
|
32 |
specimen.setUuid(UUID.fromString("b5fa679f-12a1-4f47-906a-28b41c90f019")); |
|
33 |
DnaSampleDefaultCacheStrategy strategy = new DnaSampleDefaultCacheStrategy(); |
|
34 |
|
|
35 |
Assert.assertEquals("DnaSample#0<b5fa679f-12a1-4f47-906a-28b41c90f019>", strategy.getTitleCache(specimen)); |
|
36 |
|
|
37 |
Collection collection = Collection.NewInstance(); |
|
38 |
collection.setCode("B"); |
|
39 |
specimen.setCollection(collection); |
|
40 |
Assert.assertEquals("B", strategy.getTitleCache(specimen)); |
|
41 |
|
|
42 |
specimen.setAccessionNumber("123"); |
|
43 |
Assert.assertEquals("B 123", strategy.getTitleCache(specimen)); |
|
44 |
|
|
45 |
Identifier<DnaSample> identifier = Identifier.NewInstance(specimen, "id3", DefinedTerm.IDENTIFIER_NAME_IPNI()); |
|
46 |
Assert.assertEquals("B 123", strategy.getTitleCache(specimen)); |
|
47 |
specimen.setCollection(null); |
|
48 |
specimen.setAccessionNumber(null); |
|
49 |
Assert.assertEquals("id3", strategy.getTitleCache(specimen)); |
|
50 |
|
|
51 |
specimen.putDefinition(Language.ENGLISH(), "sample definition"); |
|
52 |
Assert.assertEquals("id3", strategy.getTitleCache(specimen)); |
|
53 |
specimen.removeIdentifier(identifier); |
|
54 |
Assert.assertEquals("sample definition", strategy.getTitleCache(specimen)); |
|
55 |
|
|
56 |
specimen.addImportSource("123", "dna", null, null); |
|
57 |
Assert.assertEquals("sample definition", strategy.getTitleCache(specimen)); |
|
58 |
specimen.removeDefinition(Language.ENGLISH()); |
|
59 |
Assert.assertEquals("dna:123", strategy.getTitleCache(specimen)); |
|
60 |
|
|
61 |
Assert.assertNull(strategy.getTitleCache(null)); |
|
62 |
} |
|
63 |
} |
Also available in: Unified diff
ref #5575 add first version of DnaSample cache strategy