root/trunk/cdmlib/cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/location/TdwgArea.java

Revision 11934, 6.5 kB (checked in by k.luther, 13 months ago)

clone methods

  • Property svn:keywords set to Id
Line 
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
10package eu.etaxonomy.cdm.model.location;
11
12import java.io.IOException;
13import java.io.InputStream;
14import java.util.HashMap;
15import java.util.Map;
16import java.util.UUID;
17
18import javax.persistence.Entity;
19import javax.xml.bind.annotation.XmlAccessType;
20import javax.xml.bind.annotation.XmlAccessorType;
21import javax.xml.bind.annotation.XmlRootElement;
22import javax.xml.bind.annotation.XmlType;
23
24import org.apache.log4j.Logger;
25import org.hibernate.envers.Audited;
26import org.hibernate.search.annotations.Indexed;
27import org.jdom.Element;
28import org.jdom.Namespace;
29
30import eu.etaxonomy.cdm.common.CdmUtils;
31import eu.etaxonomy.cdm.common.XmlHelp;
32import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
33import eu.etaxonomy.cdm.model.common.Language;
34import eu.etaxonomy.cdm.model.common.Representation;
35import eu.etaxonomy.cdm.model.common.TermVocabulary;
36
37/**
38 * @author a.mueller
39 * @created 15.07.2008
40 * @version 1.0
41 */
42@XmlAccessorType(XmlAccessType.FIELD)
43@XmlType(name = "TdwgArea")
44@XmlRootElement(name = "TdwgArea")
45@Entity
46@Indexed(index = "eu.etaxonomy.cdm.model.common.DefinedTermBase")
47@Audited
48public class TdwgArea extends NamedArea implements Cloneable{
49        private static final long serialVersionUID = 4662215686356109015L;
50        private static final Logger logger = Logger.getLogger(TdwgArea.class);
51       
52        public static final UUID uuidTdwgAreaVocabulary = UUID.fromString("1fb40504-d1d7-44b0-9731-374fbe6cac77");
53
54       
55        private static Map<String, UUID> abbrevMap = null;
56        private static Map<String, UUID> labelMap = null;
57       
58        protected static Map<UUID, TdwgArea> termMap = null;
59       
60
61       
62//************************** METHODS ********************************
63       
64        /* (non-Javadoc)
65         * @see eu.etaxonomy.cdm.model.common.DefinedTermBase#resetTerms()
66         */
67        @Override
68        public void resetTerms(){
69                termMap = null;
70                labelMap = null;
71                abbrevMap = null;
72        }
73
74
75        protected static TdwgArea getTermByUuid(UUID uuid){
76                if (termMap == null){
77                        DefaultTermInitializer vocabularyStore = new DefaultTermInitializer();
78                        vocabularyStore.initialize();
79                }
80                return (TdwgArea)termMap.get(uuid);
81        }
82       
83        /**
84         * FIXME This class should really be refactored into an interface and service implementation,
85         * relying on TermVocabularyDao / service (Ben)
86         * @param tdwgAbbreviation
87         * @return
88         */
89        public static NamedArea getAreaByTdwgAbbreviation(String tdwgAbbreviation){
90                if (abbrevMap == null){
91                        initMaps();
92                }
93                UUID uuid = abbrevMap.get(tdwgAbbreviation);
94                if (uuid == null){
95                        logger.info("Unknown TDWG area: " + CdmUtils.Nz(tdwgAbbreviation));
96                        return null;
97                }
98                return TdwgArea.getTermByUuid(uuid);
99        }
100       
101        /**
102         * FIXME This class should really be refactored into an interface and service implementation,
103         * relying on TermVocabularyDao / service (Ben)
104         * @param tdwgLabel
105         * @return
106         */
107        public static NamedArea getAreaByTdwgLabel(String tdwgLabel){
108                if (labelMap == null){
109                        initMaps();
110                }
111                tdwgLabel = tdwgLabel.toLowerCase();
112                UUID uuid = labelMap.get(tdwgLabel);
113                if (uuid == null){
114                        logger.info("Unknown TDWG area: " + CdmUtils.Nz(tdwgLabel));
115                        return null;
116                }
117                return TdwgArea.getTermByUuid(uuid);
118        }
119       
120        public static boolean isTdwgAreaLabel(String label){
121                label = (label == null? null : label.toLowerCase());
122                if (labelMap.containsKey(label)){
123                        return true;
124                }else{
125                        return false;
126                }
127        }
128       
129        public static boolean isTdwgAreaAbbreviation(String abbrev){
130                if (abbrevMap.containsKey(abbrev)){
131                        return true;
132                }else{
133                        return false;
134                }
135        }
136       
137
138        /* (non-Javadoc)
139         * @see eu.etaxonomy.cdm.model.location.NamedArea#setDefaultTerms(eu.etaxonomy.cdm.model.common.TermVocabulary)
140         */
141//      @Override
142//      protected void setDefaultTerms(TermVocabulary<NamedArea> termVocabulary) {
143//              Set<NamedArea> terms = termVocabulary.getTerms();
144//              for (NamedArea term : terms){
145//                      addTdwgArea(term);
146//              }
147//      }
148       
149        @Override
150        protected void setDefaultTerms(TermVocabulary<NamedArea> termVocabulary) {
151                termMap = new HashMap<UUID, TdwgArea>();
152                for (NamedArea term : termVocabulary.getTerms()){
153                        termMap.put(term.getUuid(), (TdwgArea)term);  //TODO casting
154                        addTdwgArea(term);
155                }
156        }
157       
158       
159        protected static void addTdwgArea(NamedArea area){
160                if (area == null){
161                        logger.warn("tdwg area is null");
162                        return;
163                }
164                Language lang = Language.DEFAULT();
165                Representation representation = area.getRepresentation(lang);
166                String tdwgAbbrevLabel = representation.getAbbreviatedLabel();
167                String tdwgLabel = representation.getLabel().toLowerCase();
168                if (tdwgAbbrevLabel == null){
169                        logger.warn("tdwgLabel = null");
170                        return;
171                }
172                //init map
173                if (abbrevMap == null){
174                        abbrevMap = new HashMap<String, UUID>();
175                }
176                if (labelMap == null){
177                        labelMap = new HashMap<String, UUID>();
178                }
179                //add to map
180                abbrevMap.put(tdwgAbbrevLabel, area.getUuid());
181                labelMap.put(tdwgLabel, area.getUuid());
182                //add type
183                area.setType(NamedAreaType.ADMINISTRATION_AREA());
184                //add level
185                if (tdwgAbbrevLabel.trim().length()== 1){
186                        area.setLevel(NamedAreaLevel.TDWG_LEVEL1());
187                }else if (tdwgAbbrevLabel.trim().length()== 2){
188                        area.setLevel(NamedAreaLevel.TDWG_LEVEL2());
189                }else if (tdwgAbbrevLabel.trim().length()== 3){
190                        area.setLevel(NamedAreaLevel.TDWG_LEVEL3());
191                }else if (tdwgAbbrevLabel.trim().length()== 6){
192                        area.setLevel(NamedAreaLevel.TDWG_LEVEL4());
193                }else {
194                        logger.warn("Unknown TDWG Level " + tdwgAbbrevLabel + "! Unvalid string length (" +  tdwgAbbrevLabel.length() +")");
195                }       
196        }
197       
198        private static void initMaps(){
199                labelMap = new HashMap<String, UUID>();
200                abbrevMap = new HashMap<String, UUID>();
201        }
202       
203
204//********************* OLD ******************************/
205       
206       
207        private static NamedArea getNamedAreaByTdwgLabel(String tdwgLabel){
208                if (tdwgLabel == null){
209                        return null;
210                }
211                InputStream file;
212                try {
213                        file = CdmUtils.getReadableResourceStream("");
214                } catch (IOException e) {
215                        logger.error(e);
216                        e.printStackTrace();
217                        return null;
218                }
219                Element root = XmlHelp.getRoot(file, "RDF");
220                Namespace nsRdf = root.getNamespace("rdf");
221                XmlHelp.getFirstAttributedChild(root, "", "ID", tdwgLabel.trim());
222               
223                //Filter filter = ;
224                //root.getDescendants(filter);
225                return null;
226        }       
227       
228}
Note: See TracBrowser for help on using the browser.