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

History | View | Annotate | Download (7.8 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.dynobject.DynClass;
32
import org.gvsig.tools.dynobject.DynField;
33
import org.gvsig.tools.dynobject.DynObject;
34
import org.gvsig.tools.exception.BaseException;
35
import org.gvsig.tools.task.SimpleTaskStatus;
36
import org.gvsig.tools.visitor.VisitCanceledException;
37
import org.gvsig.tools.visitor.Visitor;
38
import org.gvsig.topology.lib.api.TopologyDataSet;
39
import org.gvsig.topology.lib.api.TopologyPlan;
40
import org.gvsig.topology.lib.api.TopologyReport;
41
import org.gvsig.topology.lib.api.TopologyRule;
42
import org.gvsig.topology.lib.api.TopologyRuleAction;
43
import org.gvsig.topology.lib.api.TopologyRuleFactory;
44
import org.json.JSONObject;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47

    
48
/**
49
 *
50
 * @author jjdelcerro
51
 */
52
public abstract class AbstractTopologyRule implements TopologyRule {
53

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

    
62
    protected List<TopologyRuleAction> actions;
63
    private DynObject parameters;
64

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

    
80
    protected AbstractTopologyRule(
81
            TopologyPlan plan,
82
            TopologyRuleFactory factory,
83
            double tolerance,
84
            String dataSet1
85
    ) {
86
        this(plan, factory, tolerance, dataSet1, null);
87
    }
88

    
89
    protected TopologyPlan getPlan() {
90
        return this.plan;
91
    }
92

    
93
    @Override
94
    public TopologyRuleFactory getFactory() {
95
        return this.factory;
96
    }
97

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

    
103
    @Override
104
    public String getId() {
105
        return this.getFactory().getId();
106
    }
107

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

    
139
    @Override
140
    public TopologyDataSet getDataSet1() {
141
        return this.getPlan().getDataSet(dataSet1);
142
    }
143

    
144
    @Override
145
    public TopologyDataSet getDataSet2() {
146
        return this.getPlan().getDataSet(dataSet2);
147
    }
148

    
149
    @Override
150
    public double getTolerance() {
151
        return this.tolerance;
152
    }
153

    
154
    @Override
155
    public List<TopologyRuleAction> getActions() {
156
        return Collections.unmodifiableList(actions);
157
    }
158

    
159
    @Override
160
    public TopologyRuleAction getAction(String id) {
161
        for (TopologyRuleAction action : actions) {
162
            if (StringUtils.equalsIgnoreCase(id, action.getId())) {
163
                return action;
164
            }
165
        }
166
        return null;
167
    }
168

    
169
    @Override
170
    public long getSteps() {
171
        return this.getDataSet1().getSize();
172
    }
173

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

    
196
    protected void check(SimpleTaskStatus taskStatus, TopologyReport report, Feature feature) throws Exception {
197
        
198
    }
199

    
200
    @Override
201
    public JSONObject toJSON() {
202
        JSONObject me = new JSONObject();
203
        me.put("tolerance", this.tolerance);
204
        me.put("dataSet1", this.dataSet1);
205
        me.put("dataSet2", this.dataSet2);
206
        
207
        DynObject params = this.getParameters();
208
        if(params!= null) {
209
            JSONObject jsonParams = new JSONObject();
210
            DynClass parametersDefinition = params.getDynClass();
211
            for (DynField dynField : parametersDefinition.getDynFields()) {
212
                jsonParams.put(dynField.getName(), params.getDynValue(dynField.getName()));
213
            }
214
            me.put("params", jsonParams);
215
        }
216
        return me;
217
    }
218

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

    
228
        if( json.has("tolerance") ) {
229
            this.tolerance = json.getDouble("tolerance");
230
        }
231
        if( json.has("dataSet1") ) {
232
            this.dataSet1 = json.getString("dataSet1");
233
        }
234
        if( json.has("dataSet2") ) {
235
            this.dataSet2 = json.getString("dataSet2");
236
        }
237
        if( json.has("params") ) {
238
            JSONObject jsonParams = json.getJSONObject("params");
239
            DynObject params = this.getFactory().createRuleParameters();
240
            DynClass parametersDefinition = params.getDynClass();
241
            for (DynField dynField : parametersDefinition.getDynFields()) {
242
                String paramName = dynField.getName();
243
                if (jsonParams.has(paramName)){
244
                    params.setDynValue(paramName, jsonParams.getString(paramName));
245
                }
246
            }
247
            this.setParameters(params);
248
        }
249
    }
250

    
251
    @Override
252
    public void setParameters(DynObject parameters) {
253
        this.parameters=parameters;
254
        
255
    }
256

    
257
    @Override
258
    public DynObject getParameters() {
259
        return this.parameters;
260
    }
261

    
262
}