Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extJCRS / src / org / gvsig / crs / persistence / RecentTrsPersistence.java @ 20637

History | View | Annotate | Download (10.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha
34
*   Campus Universitario s/n
35
*   02071 Alabacete
36
*   Spain
37
*
38
*   +34 967 599 200
39
*/
40

    
41
package org.gvsig.crs.persistence;
42

    
43
import java.util.Arrays;
44
import java.util.Date;
45
import java.util.Iterator;
46
import java.util.Properties;
47
import java.util.Set;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.utiles.DateTime;
51
import com.iver.utiles.XMLEntity;
52

    
53
/**
54
 * This class is used to save a list of Transfomations (using the
55
 * Andami persistence model) to the plugins-persistence.xml file.
56
 * It has methods to create a set of TrData objects  from an
57
 * xml file. It can also save a set of TrData objects in an
58
 * xml file.
59
 *
60
 * @author Diego Guerrero Sevilla diego.guerrero@uclm.es
61
 */
62
public class RecentTrsPersistence {
63
        private XMLEntity xml = null;
64
        private XMLEntity recentsXml = null;
65
        private PluginServices ps = null;
66
        private static final String TR_RECENTS = "recentTransformations";
67
        private static final String TR_AUTHORITY = "authority";
68
        private static final String TR_NAME = "name";
69
        private static final String TR_CODE = "code";
70
        private static final String TR_CRS_SOURCE = "crsSource";
71
        private static final String TR_CRS_TARGET = "crsTarget";
72
        private static final String TR_DETAILS = "details";
73
        private static final String TR_DATE = "date";
74
        
75
        
76
        /**
77
         * Constructor
78
         */
79
        public RecentTrsPersistence(){
80
                ps = PluginServices.getPluginServices("org.gvsig.crs");
81
                xml = ps.getPersistentXML();
82
                for (int child = 0; child<xml.getChildrenCount();child++)
83
                        if (xml.getChild(child).getPropertyValue(0).equals(TR_RECENTS))
84
                                recentsXml = xml.getChild(child); 
85
                if (recentsXml == null){
86
                        XMLEntity xmlEntity = new XMLEntity();
87
                        xmlEntity.putProperty("groupName", TR_RECENTS);
88
                        xml.addChild(xmlEntity); 
89
                        recentsXml = xmlEntity;
90
                }
91
        }
92

    
93
        /**
94
         * This methos is used to save the information in an XML file
95
         */
96
        public void setPersistent(){
97
                ps.setPersistentXML(xml);
98
        }
99

    
100
        /**
101
         * This method saves an array of TrData using the Anadami
102
         * persistence model
103
         * @param tr
104
         * Array of trData to save.
105
         */
106
        public void setArrayOfTrData(TrData[] trs){
107
                recentsXml.getXmlTag().removeAllXmlTag();
108

    
109
                for (int i=0 ; i<trs.length ; i++){
110
                        recentsXml.addChild(trDataToXml(trs[i]));
111
                }
112

    
113
        }
114

    
115
        /**
116
         * This method adds a TrData using the Anadami
117
         * persistence model. If the Transformation exists just actualizes
118
         * the type name and the wkt string.
119
         * @param tr
120
         * TrData
121
         */
122
        public void addTrData(TrData tr){
123
                TrData[] trs = getArrayOfTrData();
124

    
125
                boolean found = false;
126

    
127
                for (int i = 0; i < trs.length; i++) {
128
                        if (trs[i].getAuthority().equals(tr.getAuthority())&& trs[i].getCode()==tr.getCode() 
129
                                        && trs[i].getCrsSource().equals(tr.getCrsSource()) && trs[i].getCrsTarget().equals(tr.getCrsTarget()) && trs[i].getDetails().equals(tr.getDetails())) {
130
                                found = true;
131
                                trs[i].setName(tr.getName());
132
                                trs[i].setProperies(tr.getProperies());
133
                                trs[i].updateLastAccess();
134
                                setArrayOfTrData(trs);
135
                        }
136
                }
137
                
138
                if (!found) {
139
                        if (trs.length<10){ //L?mitie de almacenamiento: 10 Transf.
140
                                TrData[] newTrs = new TrData[trs.length + 1];
141
                                System.arraycopy(trs, 0, newTrs, 0, trs.length);
142
                                newTrs[trs.length] = tr;
143
                                setArrayOfTrData(newTrs);
144
                        }
145
                        else {
146
                                Arrays.sort(trs);
147
                                trs[0]=tr;
148
                                setArrayOfTrData(trs);
149
                        }
150
                }
151
        }
152

    
153

    
154

    
155
        /**
156
         * This method returns an array of TrData objects that
157
         * have been saved using the Andami persistence model.
158
         * @return
159
         * TrData[]
160
         */
161
        public TrData[] getArrayOfTrData(){
162
                TrData[] trs = new TrData[recentsXml.getChildrenCount()];
163
                for (int i=0 ; i<recentsXml.getChildrenCount() ; i++){
164
                        trs[i] = xmlToTrData(recentsXml.getChild(i));
165
                }
166
                
167
                /*
168
                 * Ordenar el array por fechas
169
                 */
170
                Arrays.sort(trs);
171
                
172
                return trs;
173
        }
174

    
175

    
176
        /**
177
         * This method creates and returns a new XMLEntity.
178
         * @param tr
179
         * TrData with all the Transformation information
180
         * @return
181
         * XMLEntity
182
         */
183
        public XMLEntity trDataToXml(TrData tr){
184
                String dFormat="Y-m-d H:i:s.Z";
185
        
186
                XMLEntity xmlEnt = new XMLEntity();
187
                xmlEnt.putProperty(TR_AUTHORITY,tr.getAuthority());
188
                xmlEnt.putProperty(TR_CODE,Integer.toString(tr.getCode()));
189
                xmlEnt.putProperty(TR_NAME,(tr.getName()));
190
                xmlEnt.putProperty(TR_CRS_SOURCE,(tr.getCrsSource()));
191
                xmlEnt.putProperty(TR_CRS_TARGET,(tr.getCrsTarget()));
192
                xmlEnt.putProperty(TR_DETAILS,(tr.getDetails()));
193
                xmlEnt.putProperty(TR_DATE,DateTime.dateToString(tr.getDate(),dFormat));
194
                Set keys = tr.getProperies().keySet();
195
                Iterator it = keys.iterator();
196
            while (it.hasNext()) {
197
                    String next = (String)it.next();
198
                    xmlEnt.putProperty(next,
199
                                    tr.getProperies().get(next));
200
            }        
201
            
202
            if (tr instanceof CompTrData) {
203
                    CompTrData compTr = (CompTrData) tr;
204
                    xmlEnt.addChild(trDataToXml(compTr.getFirstTr()));
205
                    xmlEnt.addChild(trDataToXml(compTr.getSecondTr()));
206
                }
207
            
208
                return xmlEnt;
209
        }
210

    
211
        /**
212
         * This method creates a new TrData from a XMLEntity
213
         * @param xmlEnt
214
         * XMLRntity that contains the Tr information
215
         * @return
216
         * TrData
217
         */
218
        public TrData xmlToTrData(XMLEntity xmlEnt){
219
                
220
                TrData trData = null;
221
                
222
                Properties props = null;
223
                
224
                String authority = "";
225
                int code = 0;
226
                String name = "";
227
                String crsSource = "";
228
                String crsTarget = "";
229
                String details = "";
230
                Date date = null;
231

    
232
                authority = xmlEnt.getStringProperty(TR_AUTHORITY);
233
                code = Integer.valueOf(xmlEnt.getStringProperty(TR_CODE)).intValue();
234
                name = xmlEnt.getStringProperty(TR_NAME);
235
                crsSource = xmlEnt.getStringProperty(TR_CRS_SOURCE);
236
                crsTarget = xmlEnt.getStringProperty(TR_CRS_TARGET);
237
                details = xmlEnt.getStringProperty(TR_DETAILS);
238
                date = DateTime.stringToDate(xmlEnt.getStringProperty(TR_DATE));
239
                
240
                if (xmlEnt.getChildrenCount()==2){
241
                        authority = xmlEnt.getChild(0).getStringProperty(TR_AUTHORITY);
242
                        code = Integer.valueOf(xmlEnt.getChild(0).getStringProperty(TR_CODE)).intValue();
243
                        name = xmlEnt.getChild(0).getStringProperty(TR_NAME);
244
                        crsSource = xmlEnt.getChild(0).getStringProperty(TR_CRS_SOURCE);
245
                        crsTarget = xmlEnt.getChild(0).getStringProperty(TR_CRS_TARGET);
246
                        details = xmlEnt.getChild(0).getStringProperty(TR_DETAILS);
247
                        date = DateTime.stringToDate(xmlEnt.getChild(0).getStringProperty(TR_DATE));
248
                        TrData firstTr = new TrData(authority,code,name,crsSource,crsTarget,details,date);
249
                        
250
                        props = new Properties();
251
                        for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){
252
                                String property = xmlEnt.getChild(0).getPropertyName(i);
253
                                if (!((property.equals(TR_AUTHORITY)) ||
254
                                        (property.equals(TR_CODE)) ||
255
                                        (property.equals(TR_NAME)) ||
256
                                        (property.equals(TR_CRS_SOURCE)) ||
257
                                        (property.equals(TR_CRS_TARGET)) ||
258
                                        (property.equals(TR_DETAILS)) ||
259
                                        (property.equals(TR_DATE)))){
260
                                                props.put(property,xmlEnt.getChild(0).getStringProperty(property));
261
                                        }                                        
262
                        }
263
                        firstTr.setProperies(props);
264
                        
265
                        authority = xmlEnt.getChild(1).getStringProperty(TR_AUTHORITY);
266
                        code = Integer.valueOf(xmlEnt.getChild(1).getStringProperty(TR_CODE)).intValue();
267
                        name = xmlEnt.getChild(1).getStringProperty(TR_NAME);
268
                        crsSource = xmlEnt.getChild(1).getStringProperty(TR_CRS_SOURCE);
269
                        crsTarget = xmlEnt.getChild(1).getStringProperty(TR_CRS_TARGET);
270
                        details = xmlEnt.getChild(1).getStringProperty(TR_DETAILS);
271
                        date = DateTime.stringToDate(xmlEnt.getChild(1).getStringProperty(TR_DATE));
272
                        TrData secondTr = new TrData(authority,code,name,crsSource,crsTarget,details,date);
273
                        
274
                        props = new Properties();
275
                        for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){
276
                                String property = xmlEnt.getChild(0).getPropertyName(i);
277
                                if (!((property.equals(TR_AUTHORITY)) ||
278
                                        (property.equals(TR_CODE)) ||
279
                                        (property.equals(TR_NAME)) ||
280
                                        (property.equals(TR_CRS_SOURCE)) ||
281
                                        (property.equals(TR_CRS_TARGET)) ||
282
                                        (property.equals(TR_DETAILS)) ||
283
                                        (property.equals(TR_DATE)))){
284
                                                props.put(property,xmlEnt.getChild(0).getStringProperty(property));
285
                                        }                                        
286
                        }
287
                        secondTr.setProperies(props);
288
                        
289
                        trData = new CompTrData(firstTr,secondTr);
290
                }
291
                else{
292
                        authority = xmlEnt.getStringProperty(TR_AUTHORITY);
293
                        code = Integer.valueOf(xmlEnt.getStringProperty(TR_CODE)).intValue();
294
                        name = xmlEnt.getStringProperty(TR_NAME);
295
                        crsSource = xmlEnt.getStringProperty(TR_CRS_SOURCE);
296
                        crsTarget = xmlEnt.getStringProperty(TR_CRS_TARGET);
297
                        details = xmlEnt.getStringProperty(TR_DETAILS);
298
                        date = DateTime.stringToDate(xmlEnt.getStringProperty(TR_DATE));        
299
                        
300
                        trData = new TrData(authority,code,name,crsSource,crsTarget,details,date);
301
                }
302
                
303
                props = new Properties();
304
                for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){
305
                        String property = xmlEnt.getPropertyName(i);
306
                        if (!((property.equals(TR_AUTHORITY)) ||
307
                                (property.equals(TR_CODE)) ||
308
                                (property.equals(TR_NAME)) ||
309
                                (property.equals(TR_CRS_SOURCE)) ||
310
                                (property.equals(TR_CRS_TARGET)) ||
311
                                (property.equals(TR_DETAILS)) ||
312
                                (property.equals(TR_DATE)))){
313
                                        props.put(property,xmlEnt.getStringProperty(property));
314
                                }                                        
315
                }
316
                trData.setProperies(props);
317
                return trData;
318
        }
319

    
320
        public XMLEntity getXml() {
321
                return xml;
322
        }
323

    
324
        public void setXml(XMLEntity xml) {
325
                this.xml = xml;
326
        }
327
}