Project

General

Profile

Download (5.85 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.model.location;
11

    
12
import java.io.IOException;
13
import java.io.InputStream;
14
import java.util.HashMap;
15
import java.util.Map;
16
import java.util.UUID;
17

    
18
import javax.persistence.Entity;
19
import javax.xml.bind.annotation.XmlAccessType;
20
import javax.xml.bind.annotation.XmlAccessorType;
21
import javax.xml.bind.annotation.XmlRootElement;
22
import javax.xml.bind.annotation.XmlType;
23

    
24
import org.apache.log4j.Logger;
25
import org.hibernate.envers.Audited;
26
import org.jdom.Element;
27
import org.jdom.Namespace;
28

    
29
import eu.etaxonomy.cdm.common.CdmUtils;
30
import eu.etaxonomy.cdm.common.XmlHelp;
31
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.common.Representation;
34
import eu.etaxonomy.cdm.model.common.TermVocabulary;
35

    
36
/**
37
 * @author a.mueller
38
 * @created 15.07.2008
39
 * @version 1.0
40
 */
41
@XmlAccessorType(XmlAccessType.FIELD)
42
@XmlType(name = "TdwgArea")
43
@XmlRootElement(name = "TdwgArea")
44
@Entity
45
@Audited
46
public class TdwgArea extends NamedArea {
47
	private static final long serialVersionUID = 4662215686356109015L;
48
	private static final Logger logger = Logger.getLogger(TdwgArea.class);
49

    
50
	
51
	private static Map<String, UUID> abbrevMap = null;
52
	private static Map<String, UUID> labelMap = null;
53
	
54
	protected static Map<UUID, TdwgArea> termMap = null;		
55

    
56
	protected static TdwgArea getTermByUuid(UUID uuid){
57
		if (termMap == null){
58
			DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
59
			vocabularyStore.initialize();
60
		}
61
		return (TdwgArea)termMap.get(uuid);
62
	}
63
	
64
	/**
65
	 * FIXME This class should really be refactored into an interface and service implementation,
66
	 * relying on TermVocabularyDao / service (Ben)
67
	 * @param tdwgAbbreviation
68
	 * @return
69
	 */
70
	public static NamedArea getAreaByTdwgAbbreviation(String tdwgAbbreviation){
71
		if (abbrevMap == null){
72
			initMaps();
73
		}
74
		UUID uuid = abbrevMap.get(tdwgAbbreviation);
75
		if (uuid == null){
76
			logger.warn("Unknown TDWG area: " + CdmUtils.Nz(tdwgAbbreviation));
77
			return null;
78
		}
79
		return TdwgArea.getTermByUuid(uuid);
80
	}
81
	
82
	/**
83
	 * FIXME This class should really be refactored into an interface and service implementation,
84
	 * relying on TermVocabularyDao / service (Ben)
85
	 * @param tdwgLabel
86
	 * @return
87
	 */
88
	public static NamedArea getAreaByTdwgLabel(String tdwgLabel){
89
		if (labelMap == null){
90
			initMaps();
91
		}
92
		UUID uuid = labelMap.get(tdwgLabel);
93
		if (uuid == null){
94
			logger.warn("Unknown TDWG area: " + CdmUtils.Nz(tdwgLabel));
95
			return null;
96
		}
97
		return TdwgArea.getTermByUuid(uuid);
98
	}
99
	
100
	public static boolean isTdwgAreaLabel(String label){
101
		if (labelMap.containsKey(label)){
102
			return true;
103
		}else{
104
			return false;
105
		}
106
	}
107
	
108
	public static boolean isTdwgAreaAbbreviation(String abbrev){
109
		if (abbrevMap.containsKey(abbrev)){
110
			return true;
111
		}else{
112
			return false;
113
		}
114
	}
115
	
116

    
117
	/* (non-Javadoc)
118
	 * @see eu.etaxonomy.cdm.model.location.NamedArea#setDefaultTerms(eu.etaxonomy.cdm.model.common.TermVocabulary)
119
	 */
120
//	@Override
121
//	protected void setDefaultTerms(TermVocabulary<NamedArea> termVocabulary) {
122
//		Set<NamedArea> terms = termVocabulary.getTerms();
123
//		for (NamedArea term : terms){
124
//			addTdwgArea(term);
125
//		}
126
//	}
127
	
128
	@Override
129
	protected void setDefaultTerms(TermVocabulary<NamedArea> termVocabulary) {
130
		termMap = new HashMap<UUID, TdwgArea>();
131
		for (NamedArea term : termVocabulary.getTerms()){
132
			termMap.put(term.getUuid(), (TdwgArea)term);  //TODO casting
133
			addTdwgArea((NamedArea)term);
134
		}
135
	}
136
	
137
	
138
	protected static void addTdwgArea(NamedArea area){
139
		if (area == null){
140
			logger.warn("tdwg area is null");
141
			return;
142
		}
143
		Language lang = Language.DEFAULT();
144
		Representation representation = area.getRepresentation(lang);
145
		String tdwgAbbrevLabel = representation.getAbbreviatedLabel();
146
		String tdwgLabel = representation.getLabel();
147
		if (tdwgAbbrevLabel == null){
148
			logger.warn("tdwgLabel = null");
149
			return;
150
		}
151
		//init map
152
		if (abbrevMap == null){
153
			abbrevMap = new HashMap<String, UUID>();
154
		}
155
		if (labelMap == null){
156
			labelMap = new HashMap<String, UUID>();
157
		}
158
		//add to map
159
		abbrevMap.put(tdwgAbbrevLabel, area.getUuid());
160
		labelMap.put(tdwgLabel, area.getUuid());
161
		//add type
162
		area.setType(NamedAreaType.ADMINISTRATION_AREA());
163
		//add level
164
		if (tdwgAbbrevLabel.trim().length()== 1){
165
			area.setLevel(NamedAreaLevel.TDWG_LEVEL1());
166
		}else if (tdwgAbbrevLabel.trim().length()== 2){
167
			area.setLevel(NamedAreaLevel.TDWG_LEVEL2());
168
		}else if (tdwgAbbrevLabel.trim().length()== 3){
169
			area.setLevel(NamedAreaLevel.TDWG_LEVEL3());
170
		}else if (tdwgAbbrevLabel.trim().length()== 6){
171
			area.setLevel(NamedAreaLevel.TDWG_LEVEL4());
172
		}else {
173
			logger.warn("Unknown TDWG Level " + tdwgAbbrevLabel + "! Unvalid string length (" +  tdwgAbbrevLabel.length() +")");
174
		}	
175
	}
176
	
177
	private static void initMaps(){
178
		labelMap = new HashMap<String, UUID>();
179
		abbrevMap = new HashMap<String, UUID>();
180
	}
181
	
182

    
183
//********************* OLD ******************************/
184
	
185
	
186
	private static NamedArea getNamedAreaByTdwgLabel(String tdwgLabel){
187
		if (tdwgLabel == null){
188
			return null;
189
		}
190
		InputStream file;
191
		try {
192
			file = CdmUtils.getReadableResourceStream("");
193
		} catch (IOException e) {
194
			logger.error(e);
195
			e.printStackTrace();
196
			return null;
197
		}
198
		Element root = XmlHelp.getRoot(file, "RDF");
199
		Namespace nsRdf = root.getNamespace("rdf");
200
		XmlHelp.getFirstAttributedChild(root, "", "ID", tdwgLabel.trim());
201
		
202
		//Filter filter = ;
203
		//root.getDescendants(filter);
204
		return null;
205
	}	
206
	
207
}
(7-7/10)