merging delete functionality into trunk
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / geo / GeoServiceAreaAnnotatedMapping.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.ext.geo;
11
12 import javax.xml.stream.XMLStreamException;
13
14 import org.apache.log4j.Logger;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Component;
17
18 import eu.etaxonomy.cdm.api.service.ITermService;
19 import eu.etaxonomy.cdm.model.common.Annotation;
20 import eu.etaxonomy.cdm.model.common.AnnotationType;
21 import eu.etaxonomy.cdm.model.common.Language;
22 import eu.etaxonomy.cdm.model.location.NamedArea;
23
24 /**
25 * This class implements {@link IGeoServiceAreaMapping}. The mapping
26 * is stored as a technical annotation of the area.
27 * The area is saved while the mapping is set.
28 * @author a.mueller
29 * @date 15.08.2011
30 *
31 */
32 @Component
33 public class GeoServiceAreaAnnotatedMapping implements IGeoServiceAreaMapping {
34 @SuppressWarnings("unused")
35 private static final Logger logger = Logger.getLogger(GeoServiceAreaAnnotatedMapping.class);
36
37 @Autowired
38 private ITermService termService;
39
40 /* (non-Javadoc)
41 * @see eu.etaxonomy.cdm.ext.geo.IGeoServiceAreaMapping#valueOf(eu.etaxonomy.cdm.model.location.NamedArea)
42 */
43 @Override
44 public GeoServiceArea valueOf(NamedArea area) {
45 for (Annotation annotation : area.getAnnotations()){
46 if (AnnotationType.TECHNICAL().equals(annotation.getAnnotationType())){
47 GeoServiceArea areas = GeoServiceArea.valueOf(annotation.getText());
48 return areas;
49 }
50 }
51
52 return null;
53 }
54
55
56
57 /* (non-Javadoc)
58 * @see eu.etaxonomy.cdm.ext.geo.IGeoServiceAreaMapping#set(eu.etaxonomy.cdm.model.location.NamedArea, eu.etaxonomy.cdm.ext.geo.GeoServiceArea)
59 */
60 @Override
61 public void set(NamedArea area, GeoServiceArea geoServiceArea) {
62 String xml = null;
63 try {
64 xml = geoServiceArea.toXml();
65 } catch (XMLStreamException e) {
66 throw new RuntimeException(e);
67 }
68 Annotation annotation =null;
69 for (Annotation existingAnnotation : area.getAnnotations()){
70 if (AnnotationType.TECHNICAL().equals(existingAnnotation.getAnnotationType())){
71 if (GeoServiceArea.isAreaMapping(existingAnnotation.getText())){
72 //FIXME test mapping type. There may be a mapping for each map service
73 annotation = existingAnnotation;
74 }
75 }
76 }
77 if (annotation == null){
78 AnnotationType type = AnnotationType.TECHNICAL();
79 annotation = Annotation.NewInstance(xml, type, Language.DEFAULT());
80 }
81 area.addAnnotation(annotation);
82
83 termService.saveOrUpdate(area);
84
85 }
86
87
88 }