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 @ 727

History | View | Annotate | Download (6.44 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 List<TopologyRuleAction> actions;
60

    
61
    protected AbstractTopologyRule(
62
            TopologyPlan plan,
63
            TopologyRuleFactory factory,
64
            double tolerance,
65
            String dataSet1,
66
            String dataSet2
67
    ) {
68
        this.plan = plan;
69
        this.factory = factory;
70
        this.tolerance = tolerance;
71
        this.dataSet1 = dataSet1;
72
        this.dataSet2 = dataSet2;
73
        this.actions = new ArrayList<>();
74
    }
75

    
76
    protected AbstractTopologyRule(
77
            TopologyPlan plan,
78
            TopologyRuleFactory factory,
79
            double tolerance,
80
            String dataSet1
81
    ) {
82
        this(plan, factory, tolerance, dataSet1, null);
83
    }
84

    
85
    protected TopologyPlan getPlan() {
86
        return this.plan;
87
    }
88

    
89
    @Override
90
    public TopologyRuleFactory getFactory() {
91
        return this.factory;
92
    }
93

    
94
    @Override
95
    public String getName() {
96
        return this.getFactory().getName();
97
    }
98

    
99
    @Override
100
    public String getId() {
101
        return this.getFactory().getId();
102
    }
103

    
104
    protected final void addAction(TopologyRuleAction action) {
105
        this.actions.add(action);
106
    }
107
    
108
    @Override
109
    public boolean equals(Object obj) {
110
        if( !(obj instanceof TopologyRule) ) {
111
            return false;
112
        }
113
        AbstractTopologyRule other = (AbstractTopologyRule)obj;
114
        if( !StringUtils.equals(this.getName(), other.getName()) ) {
115
            return false;
116
        }
117
        if( !StringUtils.equals(this.dataSet1, other.dataSet1) ) {
118
            return false;
119
        }
120
        if( !StringUtils.equals(this.dataSet2, other.dataSet2) ) {
121
            return false;
122
        }
123
        // Ojo con la comparacion de doubles
124
//        if( this.tolerance != other.tolerance ) {
125
//            return false;
126
//        }
127
        return true;
128
    }
129
    
130
    @Override
131
    public String toString() {
132
        return this.getName();
133
    }
134

    
135
    @Override
136
    public TopologyDataSet getDataSet1() {
137
        return this.getPlan().getDataSet(dataSet1);
138
    }
139

    
140
    @Override
141
    public TopologyDataSet getDataSet2() {
142
        return this.getPlan().getDataSet(dataSet2);
143
    }
144

    
145
    @Override
146
    public double getTolerance() {
147
        return this.tolerance;
148
    }
149

    
150
    @Override
151
    public List<TopologyRuleAction> getActions() {
152
        return Collections.unmodifiableList(actions);
153
    }
154

    
155
    @Override
156
    public TopologyRuleAction getAction(String id) {
157
        for (TopologyRuleAction action : actions) {
158
            if (StringUtils.equalsIgnoreCase(id, action.getId())) {
159
                return action;
160
            }
161
        }
162
        return null;
163
    }
164

    
165
    @Override
166
    public long getSteps() {
167
        return this.getDataSet1().getSize();
168
    }
169

    
170
    @Override
171
    public void execute(final SimpleTaskStatus taskStatus, final TopologyReport report) {
172
        try {
173
            this.getDataSet1().accept(new Visitor() {
174
                @Override
175
                public void visit(final Object o1) throws VisitCanceledException, BaseException {
176
                    if( taskStatus.isCancellationRequested() ) {
177
                        throw new VisitCanceledException();
178
                    }
179
                    taskStatus.incrementCurrentValue();
180
                    try {
181
                        check(taskStatus, report, (Feature) o1);
182
                    } catch (Exception ex) {
183
                        throw new RuntimeException(ex);
184
                    }
185
                }
186
            });
187
        } catch(VisitCanceledException ex) {
188
            // return;
189
        }
190
    }
191

    
192
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature) throws Exception {
193
        
194
    }
195

    
196
    @Override
197
    public JSONObject toJSON() {
198
        JSONObject me = new JSONObject();
199
        me.put("tolerance", this.tolerance);
200
        me.put("dataSet1", this.dataSet1);
201
        me.put("dataSet2", this.dataSet2);
202
        
203
        return me;
204
    }
205

    
206
    @Override
207
    public void fromJSON(String json) {
208
        this.fromJSON(new JSONObject(json));
209
    }
210
    
211
    @Override
212
    public void fromJSON(JSONObject json) {
213
//        this.report = null;
214

    
215
        if( json.has("tolerance") ) {
216
            this.tolerance = json.getDouble("tolerance");
217
        }
218
        if( json.has("dataSet1") ) {
219
            this.dataSet1 = json.getString("dataSet1");
220
        }
221
        if( json.has("dataSet2") ) {
222
            this.dataSet2 = json.getString("dataSet2");
223
        }
224
    }
225

    
226
}