Statistics
| Revision:

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

History | View | Annotate | Download (7.8 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.Locale;
55

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

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

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

    
280
        public Topology getTopology() {
281
                return topology;
282
        }
283

    
284
        public void setTopology(Topology topology) {
285
                this.topology = topology;
286
        }
287
        
288
        public boolean equals(Object o){
289
                if(!o.getClass().equals(this.getClass()))
290
                        return false;
291
                AbstractTopologyRule oRule = (AbstractTopologyRule)o;
292
                if(!oRule.originLyr.equals(this.originLyr))
293
                        return false;
294
                if(this instanceof ITwoLyrRule){
295
                        if(! (o instanceof ITwoLyrRule))
296
                                return false;
297
                        
298
                        ITwoLyrRule thisRule = (ITwoLyrRule)this;
299
                        ITwoLyrRule oTwoRule = (ITwoLyrRule)oRule;
300
                        if(! thisRule.getDestinationLyr().equals(oTwoRule.getDestinationLyr()))
301
                                return false;
302
                }
303
                return true;
304
        }
305
        
306
        public int hashCode(){
307
                return 1;
308
        }
309
}
310