Statistics
| Revision:

gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.lib / org.gvsig.topology.lib.api / src / main / java / org / gvsig / topology / lib / spi / AbstractTopologyRuleFactory.java @ 886

History | View | Annotate | Download (4.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.topology.lib.spi;
25

    
26
import java.net.URL;
27
import java.util.List;
28
import org.gvsig.fmap.geom.GeometryLocator;
29
import org.gvsig.fmap.geom.GeometryManager;
30
import org.gvsig.topology.lib.api.TopologyDataSet;
31
import org.gvsig.topology.lib.api.TopologyRuleFactory;
32
import org.json.JSONObject;
33
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35

    
36
/**
37
 *
38
 * @author jjdelcerro
39
 */
40
public abstract class AbstractTopologyRuleFactory implements TopologyRuleFactory {
41
    
42
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTopologyRuleFactory.class);
43
    
44
    private final String id;
45
    protected String name;
46
    protected String description;
47
    private final List<Integer> geometryTypeDataSet1;
48
    private final List<Integer> geometryTypeDataSet2;
49

    
50
    protected AbstractTopologyRuleFactory(
51
            String id,
52
            String name,
53
            String description,
54
            List<Integer> geometryTypeDataSet1,
55
            List<Integer> geometryTypeDataSet2
56
        ) {
57
        this.id = id;
58
        this.name = name;
59
        this.description = description;
60
        this.geometryTypeDataSet1 = geometryTypeDataSet1;
61
        this.geometryTypeDataSet2 = geometryTypeDataSet2;
62
        this.load_from_resource();
63
    }
64

    
65
    protected AbstractTopologyRuleFactory(
66
            String id,
67
            String name,
68
            String description,
69
            List<Integer> geometryTypeDataSet1
70
        ) {
71
        this(id, name, description, geometryTypeDataSet1, null);
72
    }
73

    
74
    @Override
75
    public String toString() {
76
        return this.name;
77
    }
78
    
79
    @Override
80
    public String getId() {
81
        return this.id;
82
    }
83

    
84
    @Override
85
    public String getName() {
86
        return this.name;
87
    }
88

    
89
    @Override
90
    public String getDescription() {
91
        return this.description;
92
    }
93

    
94
    @Override
95
    public List<Integer> getGeometryTypeDataSet1() {
96
        return this.geometryTypeDataSet1;
97
    }
98

    
99
    @Override
100
    public List<Integer> getGeometryTypeDataSet2() {
101
        return this.geometryTypeDataSet2;
102
    }
103

    
104
    @Override
105
    public boolean hasSecondaryDataSet() {
106
        return this.geometryTypeDataSet2 != null && !this.geometryTypeDataSet2.isEmpty();
107
    }
108

    
109
    @Override
110
    public boolean canApplyToDataSet(TopologyDataSet dataSet) {
111
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
112
        for (Integer geometryType : geometryTypeDataSet1) {
113
            boolean canApply = geomManager.isSubtype(geometryType, dataSet.getGeometryType());
114
            if( canApply ) {
115
                return true;
116
            }
117
        }        
118
        return false;
119
    }
120

    
121
    @Override
122
    public boolean canApplyToSecondaryDataSet(TopologyDataSet dataSet) {
123
        if( !this.hasSecondaryDataSet() ) {
124
            return false;
125
        }
126
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
127
        for (Integer geometryType : geometryTypeDataSet2) {
128
            boolean canApply = geomManager.isSubtype(geometryType, dataSet.getGeometryType() );
129
            if( canApply ) {
130
                return true;
131
            }
132
        }        
133
        return false;
134
    }
135

    
136
    private void load_from_resource() {
137
        URL url = RuleResourceLoaderUtils.getRuleURL(this.id);
138
        JSONObject json = RuleResourceLoaderUtils.getRule(url);
139
        load_from_resource(url, json);
140
    }
141
    
142
    protected void load_from_resource(URL jsonUrl, JSONObject json) {
143
        if( json == null ) {
144
            return;
145
        }
146
        if( json.has("name") ) {
147
            this.name = json.getString("name");
148
        }
149
        if( json.has("description") ) {
150
            this.description = RuleResourceLoaderUtils.getDescription(jsonUrl, json);
151
        }
152
    }
153
}