Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / topology / AbstractTopologyRule.java @ 24685

History | View | Annotate | Download (8.96 KB)

1
/*
2
 * Created on 07-sep-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: 
47
 * $Log: 
48
 *
49
 */
50
package org.gvsig.topology;
51

    
52
import java.awt.geom.Rectangle2D;
53
import java.net.URL;
54
import java.util.ArrayList;
55
import java.util.List;
56
import java.util.Locale;
57

    
58
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
59
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
60
import com.iver.cit.gvsig.fmap.core.IFeature;
61
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
62
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
63
import com.iver.utiles.XMLEntity;
64
import com.iver.utiles.swing.threads.CancellableProgressTask;
65

    
66
/**
67
 * Default implementation of ITopologyRule
68
 * 
69
 * @author azabala
70
 * 
71
 */
72
public abstract class AbstractTopologyRule implements IOneLyrRule  {
73
        
74
        /**
75
         * We need a reference to topology to get a layer from its name
76
         * in setXML method.
77
         */
78
        protected Topology topology;
79
        
80
 
81
        protected FLyrVect originLyr;
82
        
83
        /**
84
         * Container for the topology errors detected.
85
         */
86
        protected ITopologyErrorContainer errorContainer;
87
        
88
        
89
        /**
90
         * Container for all those error fixes that dont need user
91
         * interaction (so they could be invoked in a batch process).
92
         * The first fix of the list is the default fix
93
         */
94
        protected static List<ITopologyErrorFix> automaticErrorFixes = new ArrayList<ITopologyErrorFix>();
95
        
96
        /**
97
         * Unique identifier of the rule to distinct it of the rest of rules
98
         * of a topology
99
         */
100
        private int ruleId;
101
        
102
        /**
103
         * Constructor.
104
         * 
105
         *  @param originLyr
106
         *            layer which features we are checking
107
         * 
108
         * @param topology reference to the topology that owns this rule.
109
         */
110
        public AbstractTopologyRule(Topology topology, FLyrVect originLyr){
111
                this.topology = topology;
112
                this.originLyr = originLyr;
113
        }
114
        
115
        /**
116
         * Constructor without topology param
117
         * 
118
         * @param originLyr
119
         */
120
        public AbstractTopologyRule(FLyrVect originLyr){
121
                this.originLyr = originLyr;
122
        }
123
        
124
        public AbstractTopologyRule(){
125
                //default constructor. Needed for persistence
126
        }
127
         
128
        public void setOriginLyr(FLyrVect originLyr) {
129
                this.originLyr = originLyr;
130
        }
131
         
132
        public FLyrVect getOriginLyr() {
133
                return originLyr;
134
        }
135
         
136
        
137
        public void setTopologyErrorContainer(ITopologyErrorContainer errorContainer){
138
                this.errorContainer = errorContainer;
139
        }
140
        
141
        public ITopologyErrorContainer getTopologyErrorContainer(){
142
                return errorContainer;
143
        }
144

    
145
        public abstract String getName();
146
        
147
        
148
        public  URL  getDescription(){
149
                Locale locale = Locale.getDefault();
150
                String localeStr = locale.getLanguage();
151
                String urlStr = "docs/"+
152
                                                getClass().getName() +
153
                                                "/description_%lang%.html";
154
                String localizedUrl = urlStr.replaceAll("%lang%", localeStr);
155
                
156
                URL url = AbstractTopologyRule.class.getResource(localizedUrl);
157
                if(url == null){
158
                        // for languages used in Spain, fallback to Spanish if their translation is not available
159
                        if (localeStr.equals("ca")||localeStr.equals("gl")||localeStr.equals("eu")||localeStr.equals("va")) {
160
                                localeStr = "es";
161
                                localizedUrl = urlStr.replaceAll("%lang", localeStr);
162
                                url = AbstractTopologyRule.class.getResource(localizedUrl);
163
                                if(url != null)
164
                                        return url;
165
                        }
166
                        // as a last resort, fallback to English
167
                        localeStr = "en";
168
                        localizedUrl = urlStr.replaceAll("%lang", localeStr);
169
                        url = AbstractTopologyRule.class.getResource(localizedUrl);        
170
                }                        
171
                return url;
172
     }
173
                
174
        /**
175
         * Checks if the rule's parameters (sourceLyr, destinationLyr) verify rule
176
         * preconditions (geometry type, etc.)
177
         */
178
        public abstract void checkPreconditions() throws TopologyRuleDefinitionException ;
179
         
180
        
181
        public void checkRule(){
182
                this.checkRule((CancellableProgressTask)null);
183
        }
184
        
185
        public void checkRule(Rectangle2D rect){
186
                checkRule(null, rect);
187
        }
188
        
189
        
190
        public void checkRule(CancellableProgressTask progressMonitor){
191
                try {
192
                        // when we dont pass field names iterator only iterates over
193
                        // geometries
194
                        IFeatureIterator featureIterator = originLyr.getSource().getFeatureIterator();
195
                        while(featureIterator.hasNext()){
196
                                IFeature feature = featureIterator.next();
197
                                if(progressMonitor != null){
198
                                        if(progressMonitor.isCanceled()/*
199
                                                                                                         * ||
200
                                                                                                         * progressMonitor.isFinished()
201
                                                                                                         */){
202
                                                // TODO Maybe we could show progress info of rule
203
                                                // checking.
204
                                                // example: feature 1 of N...etc
205
                                                return;
206
                                        }
207
                                   }
208
                                validateFeature(feature);
209
                        }
210
                        this.ruleChecked();
211
                } catch (ExpansionFileReadException e) {
212
                        e.printStackTrace();
213
                } catch (ReadDriverException e) {
214
                        e.printStackTrace();
215
                }        
216
        }
217
         
218
        public  void checkRule(CancellableProgressTask progressMonitor, Rectangle2D rect){
219
                try {
220
                        IFeatureIterator iterator = originLyr.getSource().getFeatureIterator(rect, null, null, true);
221
                        while(iterator.hasNext()){
222
                                IFeature feature = iterator.next();
223
                                if(progressMonitor != null){
224
                                        if(progressMonitor.isCanceled()/*
225
                                                                                                         * ||
226
                                                                                                         * progressMonitor.isFinished()
227
                                                                                                         */){
228
                                                // TODO Maybe we could show progress info of rule
229
                                                // checking.
230
                                                // example: feature 1 of N...etc
231
                                                return;
232
                                        }
233
                                   }
234
                                validateFeature(feature);
235
                        }//while
236
                        ruleChecked();
237
                } catch (ExpansionFileReadException e) {
238
                        // TODO Auto-generated catch block
239
                        e.printStackTrace();
240
                } catch (ReadDriverException e) {
241
                        // TODO Auto-generated catch block
242
                        e.printStackTrace();
243
                }        
244
        }
245
        
246
        /**
247
         * This method must be overwrited by all of those subclases which uses temporal results,
248
         * cach?s, etc. 
249
         */
250
        public void ruleChecked(){};
251
        
252
        public abstract void validateFeature(IFeature feature);
253
        
254
        
255
        public void addTopologyError(TopologyError topologyError){
256
                this.errorContainer.addTopologyError(topologyError);
257
        }
258
        
259
        /*
260
         * Implementation of IPersistence
261
         */
262
        public String getClassName(){
263
                return this.getClass().getName();
264
        }
265
           
266
        public XMLEntity getXMLEntity(){
267
                XMLEntity xml = new XMLEntity();
268
                xml.putProperty("className", getClassName());
269
                xml.putProperty("originLayerName", this.originLyr.getName());
270
                xml.putProperty("ruleId", this.ruleId);
271
                if(this instanceof IRuleWithClusterTolerance){
272
                        double clusterTolerance = ((IRuleWithClusterTolerance)this).getClusterTolerance();
273
                        xml.putProperty("clusterTolerance", clusterTolerance);
274
                }
275
                return xml;
276
        }
277
            
278
        public void setXMLEntity(XMLEntity xml){
279
                String originLayerName = "";
280
                if (xml.contains("originLayerName")) {
281
                        originLayerName = xml.getStringProperty("originLayerName");
282
                        this.originLyr = (FLyrVect) topology.getLayer(originLayerName);
283
                }//if
284
                
285
                if(xml.contains("ruleId")){
286
                        ruleId = xml.getIntProperty("ruleId");
287
                }
288
                
289
                if(xml.contains("clusterTolerance")){
290
                        double clusterTolerance = xml.getDoubleProperty("clusterTolerance");
291
                        if(this instanceof IRuleWithClusterTolerance)
292
                                ((IRuleWithClusterTolerance)this).setClusterTolerance(clusterTolerance);
293
                }
294
        }
295
        
296
    public void setId(int ruleId){
297
            this.ruleId = ruleId;
298
    }
299
        
300
        public int getId(){
301
                return ruleId;
302
        }
303

    
304
        public Topology getTopology() {
305
                return topology;
306
        }
307

    
308
        public void setTopology(Topology topology) {
309
                this.topology = topology;
310
        }
311
        
312
        public boolean equals(Object o){
313
                if(!o.getClass().equals(this.getClass()))
314
                        return false;
315
                AbstractTopologyRule oRule = (AbstractTopologyRule)o;
316
                if(!oRule.originLyr.equals(this.originLyr))
317
                        return false;
318
                if(this instanceof ITwoLyrRule){
319
                        if(! (o instanceof ITwoLyrRule))
320
                                return false;
321
                        
322
                        ITwoLyrRule thisRule = (ITwoLyrRule)this;
323
                        ITwoLyrRule oTwoRule = (ITwoLyrRule)oRule;
324
                        if(! thisRule.getDestinationLyr().equals(oTwoRule.getDestinationLyr()))
325
                                return false;
326
                }
327
                return true;
328
        }
329
        
330
        public int hashCode(){
331
                return 1;
332
        }
333
        
334
        
335
        
336
        public ITopologyErrorFix getDefaultFix(){
337
                ITopologyErrorFix solution = null;
338
                if(automaticErrorFixes.size() > 0)
339
                        solution = automaticErrorFixes.get(0);
340
                return solution;
341
        }
342

    
343
        public  List<ITopologyErrorFix> getAutomaticErrorFixes() {
344
                return automaticErrorFixes;
345
        }
346

    
347
}
348