Project

General

Profile

Download (3.18 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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
package eu.etaxonomy.cdm.ext.geo;
10

    
11
import java.util.HashSet;
12
import java.util.Set;
13

    
14
import javax.xml.stream.XMLStreamException;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.stereotype.Component;
19

    
20
import eu.etaxonomy.cdm.api.service.ITermService;
21
import eu.etaxonomy.cdm.model.common.Annotation;
22
import eu.etaxonomy.cdm.model.common.AnnotationType;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.location.NamedArea;
25

    
26
/**
27
 * This class implements {@link IGeoServiceAreaMapping}. The mapping
28
 * is stored as a technical annotation of the area.
29
 * The area is saved while the mapping is set.
30
 * @author a.mueller
31
 \* @since 15.08.2011
32
 *
33
 */
34
@Component
35
public class GeoServiceAreaAnnotatedMapping implements IGeoServiceAreaMapping {
36
    @SuppressWarnings("unused")
37
    private static final Logger logger = Logger.getLogger(GeoServiceAreaAnnotatedMapping.class);
38

    
39
    @Autowired
40
    private ITermService termService;
41

    
42
    @Override
43
    public GeoServiceArea valueOf(NamedArea area) {
44
        for (Annotation annotation : area.getAnnotations()){
45
            if (AnnotationType.TECHNICAL().equals(annotation.getAnnotationType())){
46
                GeoServiceArea areas = GeoServiceArea.valueOf(annotation.getText());
47
                return areas;
48
            }
49
        }
50

    
51
        return null;
52
    }
53

    
54

    
55
    @Override
56
    public void set(NamedArea area, GeoServiceArea geoServiceArea) {
57
        String xml = null;
58
        try {
59
            xml = geoServiceArea.toXml();
60
        } catch (XMLStreamException e) {
61
            throw new RuntimeException(e);
62
        }
63
        Annotation annotation =null;
64
        for (Annotation existingAnnotation : area.getAnnotations()){
65
            if (AnnotationType.TECHNICAL().equals(existingAnnotation.getAnnotationType())){
66
                if (GeoServiceArea.isAreaMapping(existingAnnotation.getText())){
67
                    //FIXME test mapping type. There may be a mapping for each map service
68
                    annotation = existingAnnotation;
69
                }
70
            }
71
        }
72
        if (annotation == null){
73
            AnnotationType type = AnnotationType.TECHNICAL();
74
            annotation = Annotation.NewInstance(xml, type, Language.DEFAULT());
75
        }
76
        area.addAnnotation(annotation);
77

    
78
        termService.saveOrUpdate(area);
79

    
80
    }
81

    
82
    @Override
83
    public void clear(NamedArea area){
84

    
85
        Set<Annotation> removeCandidates = new HashSet<Annotation>();
86
        for (Annotation annotation : area.getAnnotations()){
87
            if (AnnotationType.TECHNICAL().equals(annotation.getAnnotationType())){
88
                if (GeoServiceArea.isAreaMapping(annotation.getText())){
89
                    removeCandidates.add(annotation);
90
                }
91
            }
92
        }
93

    
94
        for(Annotation  remove : removeCandidates){
95
            area.removeAnnotation(remove);
96
        }
97
        termService.saveOrUpdate(area);
98
    }
99

    
100

    
101
}
(9-9/14)