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 / AbstractTopologyRule.java @ 712

History | View | Annotate | Download (6.62 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.util.ArrayList;
27
import java.util.Collections;
28
import java.util.List;
29
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.fmap.dal.feature.Feature;
31
import org.gvsig.tools.exception.BaseException;
32
import org.gvsig.tools.task.SimpleTaskStatus;
33
import org.gvsig.tools.visitor.VisitCanceledException;
34
import org.gvsig.tools.visitor.Visitor;
35
import org.gvsig.topology.lib.api.TopologyDataSet;
36
import org.gvsig.topology.lib.api.TopologyPlan;
37
import org.gvsig.topology.lib.api.TopologyReport;
38
import org.gvsig.topology.lib.api.TopologyRule;
39
import org.gvsig.topology.lib.api.TopologyRuleAction;
40
import org.gvsig.topology.lib.api.TopologyRuleFactory;
41
import org.json.JSONObject;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44

    
45
/**
46
 *
47
 * @author jjdelcerro
48
 */
49
public abstract class AbstractTopologyRule implements TopologyRule {
50

    
51
    protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractTopologyRule.class);
52
    
53
    private final TopologyPlan plan;
54
    private final TopologyRuleFactory factory;
55
    private double tolerance;
56
    private String dataSet1;
57
    private String dataSet2;
58

    
59
//    protected TopologyReport report;
60
    protected List<TopologyRuleAction> actions;
61

    
62
    protected AbstractTopologyRule(
63
            TopologyPlan plan,
64
            TopologyRuleFactory factory
65
    ) {
66
        this.plan = plan;
67
        this.factory = factory;
68

    
69
        this.tolerance = plan.getTolerance();
70
        this.dataSet1 = null;
71
        this.dataSet2 = null;
72
//        this.report = null;
73
        this.actions = new ArrayList<>();
74
    }
75
    
76
    protected AbstractTopologyRule(
77
            TopologyPlan plan,
78
            TopologyRuleFactory factory,
79
            double tolerance,
80
            String dataSet1,
81
            String dataSet2
82
    ) {
83
        this(plan,factory);
84
        this.tolerance = tolerance;
85
        this.dataSet1 = dataSet1;
86
        this.dataSet2 = dataSet2;
87
    }
88

    
89
    protected AbstractTopologyRule(
90
            TopologyPlan plan,
91
            TopologyRuleFactory factory,
92
            double tolerance,
93
            String dataSet1
94
    ) {
95
        this(plan, factory, tolerance, dataSet1, null);
96
    }
97

    
98
    protected TopologyPlan getPlan() {
99
        return this.plan;
100
    }
101

    
102
    @Override
103
    public TopologyRuleFactory getFactory() {
104
        return this.factory;
105
    }
106

    
107
    @Override
108
    public String getName() {
109
        return this.getFactory().getName();
110
    }
111

    
112
    @Override
113
    public String getId() {
114
        return this.getFactory().getId();
115
    }
116

    
117
    
118
    @Override
119
    public boolean equals(Object obj) {
120
        if( !(obj instanceof TopologyRule) ) {
121
            return false;
122
        }
123
        AbstractTopologyRule other = (AbstractTopologyRule)obj;
124
        if( !StringUtils.equals(this.getName(), other.getName()) ) {
125
            return false;
126
        }
127
        if( !StringUtils.equals(this.dataSet1, other.dataSet1) ) {
128
            return false;
129
        }
130
        if( !StringUtils.equals(this.dataSet2, other.dataSet2) ) {
131
            return false;
132
        }
133
        // Ojo con la comparacion de doubles
134
//        if( this.tolerance != other.tolerance ) {
135
//            return false;
136
//        }
137
        return true;
138
    }
139
    
140
    @Override
141
    public String toString() {
142
        return this.getName();
143
    }
144

    
145
    @Override
146
    public TopologyDataSet getDataSet1() {
147
        return this.getPlan().getDataSet(dataSet1);
148
    }
149

    
150
    @Override
151
    public TopologyDataSet getDataSet2() {
152
        return this.getPlan().getDataSet(dataSet2);
153
    }
154

    
155
    @Override
156
    public double getTolerance() {
157
        return this.tolerance;
158
    }
159

    
160
    @Override
161
    public List<TopologyRuleAction> getActions() {
162
        return Collections.unmodifiableList(actions);
163
    }
164

    
165
    @Override
166
    public TopologyRuleAction getAction(String id) {
167
        for (TopologyRuleAction action : actions) {
168
            if (StringUtils.equalsIgnoreCase(id, action.getId())) {
169
                return action;
170
            }
171
        }
172
        return null;
173
    }
174

    
175
    @Override
176
    public long getSteps() {
177
        return this.getDataSet1().getSize();
178
    }
179

    
180
    @Override
181
    public void execute(final SimpleTaskStatus taskStatus, final TopologyReport report) {
182
        this.getDataSet1().accept(new Visitor() {
183
            @Override
184
            public void visit(final Object o1) throws VisitCanceledException, BaseException {
185
                taskStatus.incrementCurrentValue();
186
                try {
187
                    if (getDataSet2() == null) {
188
                        check(report, (Feature) o1);
189
                    } else {
190
                        check(report, (Feature) o1, getDataSet2());
191
                    }
192
                } catch (Exception ex) {
193
                    // ??????
194
                }
195
            }
196
        });
197
    }
198

    
199
    protected void check(TopologyReport report, Feature feature) throws Exception {
200
        
201
    }
202

    
203
    protected  void check(TopologyReport report, Feature feature1, TopologyDataSet dataSet2 ) throws Exception {
204
        
205
    }
206

    
207
    @Override
208
    public JSONObject toJSON() {
209
        JSONObject me = new JSONObject();
210
        me.put("tolerance", this.tolerance);
211
        me.put("dataSet1", this.dataSet1);
212
        me.put("dataSet2", this.dataSet2);
213
        
214
        return me;
215
    }
216

    
217
    @Override
218
    public void fromJSON(String json) {
219
        this.fromJSON(new JSONObject(json));
220
    }
221
    
222
    @Override
223
    public void fromJSON(JSONObject json) {
224
//        this.report = null;
225

    
226
        if( json.has("tolerance") ) {
227
            this.tolerance = json.getDouble("tolerance");
228
        }
229
        if( json.has("dataSet1") ) {
230
            this.dataSet1 = json.getString("dataSet1");
231
        }
232
        if( json.has("dataSet2") ) {
233
            this.dataSet2 = json.getString("dataSet2");
234
        }
235
    }
236

    
237
}