1
|
package eu.etaxonomy.xper;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
import java.util.Set;
|
7
|
import java.util.UUID;
|
8
|
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.transaction.TransactionStatus;
|
11
|
|
12
|
import fr_jussieu_snv_lis.Xper;
|
13
|
import fr_jussieu_snv_lis.base.BaseObjectResource;
|
14
|
import fr_jussieu_snv_lis.base.Individual;
|
15
|
import fr_jussieu_snv_lis.base.Mode;
|
16
|
import fr_jussieu_snv_lis.base.Variable;
|
17
|
import fr_jussieu_snv_lis.base.XPResource;
|
18
|
import fr_jussieu_snv_lis.utils.Utils;
|
19
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
20
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
21
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
23
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
24
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
25
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
26
|
import eu.etaxonomy.cdm.model.description.State;
|
27
|
import eu.etaxonomy.cdm.model.description.StateData;
|
28
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
29
|
import eu.etaxonomy.cdm.model.description.WorkingSet;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
32
|
|
33
|
public class AdaptaterCdmXper {
|
34
|
private static final Logger logger = Logger.getLogger(AdaptaterCdmXper.class);
|
35
|
|
36
|
public AdaptaterCdmXper() {
|
37
|
|
38
|
}
|
39
|
|
40
|
// Load the featureTree with the UUID
|
41
|
public void loadFeatures() {
|
42
|
UUID featureTreeUUID = UUID.fromString("43ab1efd-fa15-419a-8cd6-05477e4b37bc");
|
43
|
List<String> featureTreeInit = Arrays.asList(new String[]{"root.children.feature.representations"});
|
44
|
|
45
|
TransactionStatus tx = Xper.getCdmApplicationController().startTransaction();
|
46
|
FeatureTree featureTree = Xper.getCdmApplicationController().getFeatureTreeService().load(featureTreeUUID, featureTreeInit);
|
47
|
if (featureTree != null) {
|
48
|
loadFeatureNode(featureTree.getRoot(), -1);
|
49
|
}else{
|
50
|
logger.warn("Feature tree " + featureTreeUUID.toString() + " not found");
|
51
|
}
|
52
|
Xper.getCdmApplicationController().commitTransaction(tx);
|
53
|
}
|
54
|
|
55
|
// Recursive methode to load FeatureNode and all its children
|
56
|
public void loadFeatureNode(FeatureNode featureNode, int indiceParent){
|
57
|
List<FeatureNode> featureList = featureNode.getChildren();
|
58
|
for(FeatureNode child : featureList){
|
59
|
boolean alreadyExist = false;
|
60
|
Variable variable = new Variable(child.getFeature().getLabel());
|
61
|
variable.setUuid(child.getFeature().getUuid());
|
62
|
List<Variable> vars = Utils.currentBase.getVariables();
|
63
|
for(Variable var : vars){
|
64
|
if(var.getName().equals(variable.getName()))
|
65
|
alreadyExist = true;
|
66
|
}
|
67
|
|
68
|
if(!alreadyExist && (child.getFeature().isSupportsCategoricalData() || child.getFeature().isSupportsQuantitativeData())){
|
69
|
|
70
|
Utils.currentBase.addVariable(variable);
|
71
|
|
72
|
if(child.getFeature().isSupportsCategoricalData()){
|
73
|
// Add states to the character
|
74
|
Set<TermVocabulary<State>> termVocabularySet = child.getFeature().getSupportedCategoricalEnumerations();
|
75
|
for(TermVocabulary<State> termVocabulary : termVocabularySet){
|
76
|
for(State sate : termVocabulary.getTerms()){
|
77
|
Mode mode = new Mode(sate.getLabel());
|
78
|
mode.setUuid(sate.getUuid());
|
79
|
variable.addMode(mode);
|
80
|
}
|
81
|
}
|
82
|
}else if (child.getFeature().isSupportsQuantitativeData()) {
|
83
|
// Specify the character type (numerical)
|
84
|
variable.setType(Utils.numType);
|
85
|
}
|
86
|
|
87
|
if(indiceParent != -1 && Utils.currentBase.getVariableAt(indiceParent) != null){
|
88
|
variable.addMother(((Variable)Utils.currentBase.getVariableAt(indiceParent -1)));
|
89
|
}
|
90
|
|
91
|
loadFeatureNode(child, variable.getIndexInt());
|
92
|
}else{
|
93
|
loadFeatureNode(child, indiceParent);
|
94
|
}
|
95
|
}
|
96
|
}
|
97
|
|
98
|
// Load all the taxa and 1 description
|
99
|
public void loadTaxaAndDescription() {
|
100
|
TransactionStatus tx = Xper.getCdmApplicationController().startTransaction();
|
101
|
List<TaxonBase> taxonList = Xper.getCdmApplicationController().getTaxonService().list(Taxon.class , null, null, null, null);
|
102
|
for(TaxonBase taxonBase : taxonList){
|
103
|
if (Utils.currentBase != null) {
|
104
|
Individual individual = new Individual(taxonBase.getName().toString());
|
105
|
individual.setUuid(taxonBase.getUuid());
|
106
|
|
107
|
// Add a image to the taxon
|
108
|
BaseObjectResource bor = new BaseObjectResource(new XPResource("http://www.cheloniophilie.com/Images/Photos/Chelonia-mydas/tortue-marine.JPG"));
|
109
|
individual.addResource(bor);
|
110
|
|
111
|
// Add an empty description
|
112
|
List<Variable> vars = Utils.currentBase.getVariables();
|
113
|
for(Variable var : vars){
|
114
|
individual.addMatrix(var, new ArrayList<Mode>());
|
115
|
}
|
116
|
loadDescription(individual, taxonBase);
|
117
|
Utils.currentBase.addIndividual(individual);
|
118
|
}
|
119
|
}
|
120
|
Xper.getCdmApplicationController().commitTransaction(tx);
|
121
|
}
|
122
|
|
123
|
// Load the first taxonDescription
|
124
|
public void loadDescription(Individual individual, TaxonBase taxonBase) {
|
125
|
|
126
|
Pager<TaxonDescription> taxonDescriptionPager = Xper.getCdmApplicationController().getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));
|
127
|
List<TaxonDescription> taxonDescriptionList = taxonDescriptionPager.getRecords();
|
128
|
TaxonDescription taxonDescription = taxonDescriptionList.get(0);
|
129
|
Set<DescriptionElementBase> DescriptionElementBaseList = taxonDescription.getElements();
|
130
|
for(DescriptionElementBase descriptionElementBase : DescriptionElementBaseList){
|
131
|
if(descriptionElementBase instanceof CategoricalData){
|
132
|
// find the xper variable corresponding
|
133
|
Variable variable = null;
|
134
|
List<Variable> vars = Utils.currentBase.getVariables();
|
135
|
for(Variable var : vars){
|
136
|
if(var.getUuid().equals(((CategoricalData)descriptionElementBase).getFeature().getUuid())){
|
137
|
variable = var;
|
138
|
}
|
139
|
}
|
140
|
if(variable != null){
|
141
|
// create a list of xper Mode corresponding
|
142
|
List<Mode> modesList = variable.getModes();
|
143
|
List<StateData> stateList = ((CategoricalData)descriptionElementBase).getStates();
|
144
|
for(StateData state : stateList){
|
145
|
for(Mode mode : modesList){
|
146
|
if(state.getState().getUuid().equals(mode.getUuid())){
|
147
|
// Add state to the Description
|
148
|
individual.addModeMatrix(variable, mode);
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
}else if(descriptionElementBase instanceof QuantitativeData){
|
154
|
// find the xper variable corresponding
|
155
|
Variable variable = null;
|
156
|
List<Variable> vars = Utils.currentBase.getVariables();
|
157
|
for(Variable var : vars){
|
158
|
if(var.getUuid().equals(((QuantitativeData)descriptionElementBase).getFeature().getUuid())){
|
159
|
variable = var;
|
160
|
}
|
161
|
}
|
162
|
if(variable != null){
|
163
|
fr_jussieu_snv_lis.base.QuantitativeData qdXper = new fr_jussieu_snv_lis.base.QuantitativeData();
|
164
|
QuantitativeData qdCDM = ((QuantitativeData)descriptionElementBase);
|
165
|
|
166
|
if(qdCDM.getMax() != null)
|
167
|
qdXper.setMax(new Double(qdCDM.getMax()));
|
168
|
if(qdCDM.getMin() != null)
|
169
|
qdXper.setMin(new Double(qdCDM.getMin()));
|
170
|
if(qdCDM.getTypicalLowerBoundary() != null)
|
171
|
qdXper.setUmethLower(new Double(qdCDM.getTypicalLowerBoundary()));
|
172
|
if(qdCDM.getTypicalUpperBoundary() != null)
|
173
|
qdXper.setUmethUpper(new Double(qdCDM.getTypicalUpperBoundary()));
|
174
|
|
175
|
// Does not work
|
176
|
//qdXper.setMean(new Double(qdCDM.getAverage()));
|
177
|
//qdXper.setSd(new Double(qdCDM.getStandardDeviation()));
|
178
|
//qdXper.setNSample(new Double(qdCDM.getSampleSize()));
|
179
|
|
180
|
individual.addNumMatrix(variable, qdXper);
|
181
|
}
|
182
|
|
183
|
}
|
184
|
}
|
185
|
}
|
186
|
|
187
|
// Create a workingSet if not exist
|
188
|
public void createWorkingSet(){
|
189
|
|
190
|
if(Xper.getCdmApplicationController().getWorkingSetService().list(WorkingSet.class, null, null, null, null).size() <= 0){
|
191
|
WorkingSet ws = WorkingSet.NewInstance();
|
192
|
|
193
|
UUID featureTreeUUID = UUID.fromString("47eda782-89c7-4c69-9295-e4052ebe16c6");
|
194
|
List<String> featureTreeInit = Arrays.asList(new String[]{"root.children.feature.representations"});
|
195
|
|
196
|
FeatureTree featureTree = Xper.getCdmApplicationController().getFeatureTreeService().load(featureTreeUUID, featureTreeInit);
|
197
|
ws.setDescriptiveSystem(featureTree);
|
198
|
|
199
|
List<TaxonBase> taxonList = Xper.getCdmApplicationController().getTaxonService().list(Taxon.class , null, null, null, null);
|
200
|
for(TaxonBase taxonBase : taxonList){
|
201
|
Pager<TaxonDescription> taxonDescriptionPager = Xper.getCdmApplicationController().getDescriptionService().getTaxonDescriptions((Taxon)taxonBase, null, null, null, 0, Arrays.asList(new String[]{"elements.states", "elements.feature"} ));
|
202
|
List<TaxonDescription> taxonDescriptionList = taxonDescriptionPager.getRecords();
|
203
|
TaxonDescription taxonDescription = taxonDescriptionList.get(0);
|
204
|
ws.addDescription(taxonDescription);
|
205
|
System.out.println(taxonDescription.getUuid());
|
206
|
}
|
207
|
|
208
|
Xper.getCdmApplicationController().getWorkingSetService().save(ws);
|
209
|
}
|
210
|
}
|
211
|
}
|