1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.cdm.app.eflora;
|
5
|
|
6
|
import java.net.URI;
|
7
|
import java.util.Collection;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.HashSet;
|
10
|
import java.util.List;
|
11
|
import java.util.Map;
|
12
|
import java.util.Set;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
18
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
19
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
20
|
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
|
21
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
|
22
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
23
|
import eu.etaxonomy.cdm.io.common.events.LoggingIoObserver;
|
24
|
import eu.etaxonomy.cdm.io.markup.FeatureSorter;
|
25
|
import eu.etaxonomy.cdm.io.markup.FeatureSorterInfo;
|
26
|
import eu.etaxonomy.cdm.io.markup.MarkupImportConfigurator;
|
27
|
import eu.etaxonomy.cdm.io.markup.MarkupImportState;
|
28
|
import eu.etaxonomy.cdm.model.description.Feature;
|
29
|
import eu.etaxonomy.cdm.model.description.FeatureNode;
|
30
|
import eu.etaxonomy.cdm.model.description.FeatureTree;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
*
|
35
|
*/
|
36
|
public class EfloraActivatorBase {
|
37
|
private static final Logger logger = Logger.getLogger(EfloraActivatorBase.class);
|
38
|
|
39
|
protected MarkupImportConfigurator config;
|
40
|
protected CdmDefaultImport<MarkupImportConfigurator> myImport;
|
41
|
protected IIoObserver observer = new LoggingIoObserver();
|
42
|
protected Set<IIoObserver> observerList = new HashSet<IIoObserver>();
|
43
|
|
44
|
protected MarkupImportConfigurator doImport(URI source, ICdmDataSource cdmDestination, CHECK check, boolean h2ForCheck){
|
45
|
observerList.add(observer);
|
46
|
if (h2ForCheck && cdmDestination.getDatabaseType().equals(CdmDestinations.localH2().getDatabaseType())){
|
47
|
check = CHECK.CHECK_ONLY;
|
48
|
}
|
49
|
config = MarkupImportConfigurator.NewInstance(source, cdmDestination);
|
50
|
config.setObservers(observerList);
|
51
|
config.setCheck(check);
|
52
|
|
53
|
myImport = new CdmDefaultImport<MarkupImportConfigurator>();
|
54
|
|
55
|
return config;
|
56
|
}
|
57
|
|
58
|
protected FeatureTree makeAutomatedFeatureTree(ICdmRepository app,
|
59
|
MarkupImportState state, UUID featureTreeUuid, String featureTreeTitle){
|
60
|
System.out.println("Start creating automated Feature Tree");
|
61
|
FeatureTree tree = FeatureTree.NewInstance(featureTreeUuid);
|
62
|
tree.setTitleCache(featureTreeTitle, true);
|
63
|
FeatureNode root = tree.getRoot();
|
64
|
|
65
|
ITermService termService = app.getTermService();
|
66
|
FeatureSorter sorter = new FeatureSorter();
|
67
|
FeatureNode descriptionNode = null;
|
68
|
|
69
|
//general features
|
70
|
Map<String, List<FeatureSorterInfo>> generalList = state.getGeneralFeatureSorterListMap();
|
71
|
List<UUID> uuidList = sorter.getSortOrder(generalList);
|
72
|
Map<UUID, Feature> map = makeUuidMap(uuidList, termService);
|
73
|
for (UUID key : uuidList){
|
74
|
Feature feature = map.get(key);
|
75
|
FeatureNode node = FeatureNode.NewInstance(feature);
|
76
|
root.addChild(node);
|
77
|
if (feature.equals(Feature.DESCRIPTION())){
|
78
|
descriptionNode = node;
|
79
|
}
|
80
|
}
|
81
|
FeatureNode newNode = FeatureNode.NewInstance(Feature.CITATION());
|
82
|
root.addChild(newNode);
|
83
|
|
84
|
|
85
|
//description features
|
86
|
if (descriptionNode != null){
|
87
|
Map<String, List<FeatureSorterInfo>> charList = state.getCharFeatureSorterListMap();
|
88
|
uuidList = sorter.getSortOrder(charList);
|
89
|
map = makeUuidMap(uuidList, termService);
|
90
|
for (UUID key : uuidList){
|
91
|
Feature feature = map.get(key);
|
92
|
descriptionNode.addChild(FeatureNode.NewInstance(feature));
|
93
|
}
|
94
|
}else{
|
95
|
logger.warn("No description node found. Could not create feature nodes for description features.");
|
96
|
}
|
97
|
|
98
|
//save tree
|
99
|
app.getFeatureTreeService().saveOrUpdate(tree);
|
100
|
|
101
|
System.out.println("End creating automated Feature Tree");
|
102
|
|
103
|
return tree;
|
104
|
}
|
105
|
|
106
|
private Map<UUID,Feature> makeUuidMap(Collection<UUID> uuids, ITermService termService){
|
107
|
HashSet<UUID> uuidSet = new HashSet<UUID>();
|
108
|
uuidSet.addAll(uuids);
|
109
|
List<Feature> featureSet = (List)termService.find(uuidSet);
|
110
|
|
111
|
Map<UUID,Feature> result = new HashMap<UUID, Feature>();
|
112
|
for (Feature feature : featureSet){
|
113
|
result.put(feature.getUuid(), feature);
|
114
|
}
|
115
|
return result;
|
116
|
}
|
117
|
|
118
|
|
119
|
/**
|
120
|
* @param markupConfig
|
121
|
* @param myImport
|
122
|
*/
|
123
|
protected void executeVolume(URI source, boolean include) {
|
124
|
if (include){
|
125
|
System.out.println("\nStart import from ("+ source.toString() + ") ...");
|
126
|
config.setSource(source);
|
127
|
myImport.invoke(config);
|
128
|
System.out.println("End import from ("+ source.toString() + ")...");
|
129
|
}
|
130
|
}
|
131
|
}
|