Project

General

Profile

Download (2.82 KB) Statistics
| Branch: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.cdm.app.eflora;
5

    
6
import java.util.Collection;
7
import java.util.HashMap;
8
import java.util.HashSet;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.UUID;
12

    
13
import org.apache.log4j.Logger;
14

    
15
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
16
import eu.etaxonomy.cdm.api.service.ITermService;
17
import eu.etaxonomy.cdm.io.markup.FeatureSorter;
18
import eu.etaxonomy.cdm.io.markup.FeatureSorterInfo;
19
import eu.etaxonomy.cdm.io.markup.MarkupImportState;
20
import eu.etaxonomy.cdm.model.description.Feature;
21
import eu.etaxonomy.cdm.model.description.FeatureNode;
22
import eu.etaxonomy.cdm.model.description.FeatureTree;
23

    
24
/**
25
 * @author a.mueller
26
 *
27
 */
28
public class EfloraActivatorBase {
29
	private static final Logger logger = Logger.getLogger(EfloraActivatorBase.class);
30

    
31
	
32
	protected FeatureTree makeAutomatedFeatureTree(ICdmApplicationConfiguration app, 
33
			MarkupImportState state, UUID featureTreeUuid, String featureTreeTitle){
34
		FeatureTree tree = FeatureTree.NewInstance(featureTreeUuid);
35
		tree.setTitleCache(featureTreeTitle, true);
36
		FeatureNode root = tree.getRoot();
37
		
38
		ITermService termService = app.getTermService();
39
		FeatureSorter sorter = new FeatureSorter();
40
		FeatureNode descriptionNode = null;
41
		
42
		//general features
43
		Map<String, List<FeatureSorterInfo>> generalList = state.getGeneralFeatureSorterListMap();
44
		List<UUID> uuidList = sorter.getSortOrder(generalList);
45
		Map<UUID, Feature> map = makeUuidMap(uuidList, termService);
46
		for (UUID key : uuidList){
47
			Feature feature = map.get(key);
48
			FeatureNode node = FeatureNode.NewInstance(feature);
49
			root.addChild(node);
50
			if (feature.equals(Feature.DESCRIPTION())){
51
				descriptionNode = node;
52
			}
53
		}
54
		FeatureNode newNode = FeatureNode.NewInstance(Feature.CITATION());
55
		root.addChild(newNode);
56
		
57
		
58
		//description features
59
		if (descriptionNode != null){
60
			Map<String, List<FeatureSorterInfo>> charList = state.getCharFeatureSorterListMap();
61
			uuidList = sorter.getSortOrder(charList);
62
			map = makeUuidMap(uuidList, termService);
63
			for (UUID key : uuidList){
64
				Feature feature = map.get(key);
65
				descriptionNode.addChild(FeatureNode.NewInstance(feature));
66
			}
67
		}else{
68
			logger.warn("No description node found. Could not create feature nodes for description features.");
69
		}
70

    
71
		//save tree
72
		app.getFeatureTreeService().saveOrUpdate(tree);
73
		
74
		return tree;
75
	}
76
	
77
	private Map<UUID,Feature> makeUuidMap(Collection<UUID> uuids, ITermService termService){
78
		HashSet<UUID> uuidSet = new HashSet<UUID>();
79
		uuidSet.addAll(uuids);
80
		List<Feature> featureSet = (List)termService.find(uuidSet);
81
		
82
		Map<UUID,Feature> result = new HashMap<UUID, Feature>();
83
		for (Feature feature : featureSet){
84
			result.put(feature.getUuid(), feature);
85
		}
86
		return result;
87
	}
88
}
(7-7/12)