Project

General

Profile

Download (3.19 KB) Statistics
| Branch: | Tag: | Revision:
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 java.util.HashSet;
13
import java.util.Set;
14

    
15
import javax.xml.stream.XMLStreamException;
16

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

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

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

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

    
79
        termService.saveOrUpdate(area);
80

    
81
    }
82

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

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

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

    
101

    
102
}
(7-7/12)