Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / persistence / RecentCRSsPersistence.java @ 8711

History | View | Annotate | Download (7.11 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 CRSs (using the
55
 * Andami persistence model) to the plugins-persistence.xml file.
56
 * It has methods to create a set of ServerData objects  from an
57
 * xml file. It can also save a set of CRSs objects in an
58
 * xml file.
59
 *
60
 * @author Diego Guerrero Sevilla diego.guerrero@uclm.es
61
 */
62
public class RecentCRSsPersistence {
63
        private XMLEntity xml = null;
64
        private XMLEntity recentsXml = null;
65
        private PluginServices ps = null;
66
        private static final String CRS_RECENTS = "recentCrss";
67
        private static final String CRS_AUTHORITY = "authority";
68
        private static final String CRS_NAME = "name";
69
        private static final String CRS_CODE = "code";
70
        private static final String CRS_WKT = "wkt";
71
        private static final String CRS_DATE = "date";
72
        
73
        
74
        public static Object pluginClassInstance = null;
75
        
76
        /**
77
         * Constructor
78
         * @param pluginClassInstance
79
         */
80
        public RecentCRSsPersistence(Object pluginClassInstance){
81
                ps = PluginServices.getPluginServices(pluginClassInstance);
82
                xml = ps.getPersistentXML();
83
                for (int child = 0; child<xml.getChildrenCount();child++)
84
                        if (xml.getChild(child).getPropertyValue(0).equals(CRS_RECENTS))
85
                                recentsXml = xml.getChild(child); 
86
                if (recentsXml == null){
87
                        XMLEntity xmlEntity = new XMLEntity();
88
                        xmlEntity.putProperty("groupName", CRS_RECENTS);
89
                        xml.addChild(xmlEntity);
90
                        recentsXml = xmlEntity;
91
                }
92
        }
93
        
94
        /**
95
         * Constructor
96
         */
97
        public RecentCRSsPersistence(){
98
                ps = PluginServices.getPluginServices(pluginClassInstance);
99
                xml = ps.getPersistentXML();
100
                for (int child = 0; child<xml.getChildrenCount();child++)
101
                        if (xml.getChild(child).getPropertyValue(0).equals(CRS_RECENTS))
102
                                recentsXml = xml.getChild(child); 
103
                if (recentsXml == null){
104
                        XMLEntity xmlEntity = new XMLEntity();
105
                        xmlEntity.putProperty("groupName", CRS_RECENTS);
106
                        xml.addChild(xmlEntity); 
107
                        recentsXml = xmlEntity;
108
                }
109
        }
110

    
111
        /**
112
         * This methos is used to save the information in an XML file
113
         */
114
        public void setPersistent(){
115
                ps.setPersistentXML(xml);
116
        }
117

    
118
        /**
119
         * This method saves an array of CrsData using the Anadami
120
         * persistence model
121
         * @param crsss
122
         * Array of crss
123
         */
124
        public void setArrayOfCrsData(CrsData[] crss){
125
                recentsXml.getXmlTag().removeAllXmlTag();
126

    
127
                for (int i=0 ; i<crss.length ; i++){
128
                        recentsXml.addChild(crsDataToXml(crss[i]));
129
                }
130

    
131
        }
132

    
133
        /**
134
         * This method adds a CrsData using the Anadami
135
         * persistence model. If the Crs exists just actualizes
136
         * the type name and the wkt string.
137
         * @param crs
138
         * CrsData
139
         */
140
        public void addCrsData(CrsData crs){
141
                CrsData[] crss = getArrayOfCrsData();
142

    
143
                boolean found = false;
144

    
145
                for (int i = 0; i < crss.length; i++) {
146
                        if (crss[i].getAuthority().equals(crs.getAuthority())&& crss[i].getCode()==crs.getCode()) {
147
                                found = true;
148
                                crss[i].setName(crs.getName());
149
                                crss[i].setWkt(crs.getWkt());
150
                                crss[i].setProperies(crs.getProperies());
151
                                crss[i].updateLastAccess();
152
                                setArrayOfCrsData(crss);
153
                        }
154
                }
155
                
156
                if (!found) {
157
                        if (crss.length<10){
158
                                CrsData[] newCrss = new CrsData[crss.length + 1];
159
                                System.arraycopy(crss, 0, newCrss, 0, crss.length);
160
                                newCrss[crss.length] = crs;
161
                                setArrayOfCrsData(newCrss);
162
                        }
163
                        else {
164
                                Arrays.sort(crss);
165
                                crss[0]=crs;
166
                                setArrayOfCrsData(crss);
167
                        }
168
                }
169
        }
170

    
171

    
172

    
173
        /**
174
         * This method returns an array of CrsData objects that
175
         * have been saved using the Andami persistence model.
176
         * @return
177
         * CrsData[]
178
         */
179
        public CrsData[] getArrayOfCrsData(){
180
                CrsData[] crss = new CrsData[recentsXml.getChildrenCount()];
181
                for (int i=0 ; i<recentsXml.getChildrenCount() ; i++){
182
                        crss[i] = xmlToCrsData(recentsXml.getChild(i));
183
                }
184
                
185
                /*
186
                 * Ordenar el array por fechas
187
                 */
188
                Arrays.sort(crss);
189
                
190
                return crss;
191
        }
192

    
193

    
194
        /**
195
         * This method creates and returns a new XMLEntity.
196
         * @param crs
197
         * CrsData with all the Crs information
198
         * @return
199
         * XMLEntity
200
         */
201
        public XMLEntity crsDataToXml(CrsData crs){
202
                String dFormat="Y-m-d H:i:s.Z";
203
        
204
                XMLEntity xmlEnt = new XMLEntity();
205
                xmlEnt.putProperty(CRS_AUTHORITY,crs.getAuthority());
206
                xmlEnt.putProperty(CRS_CODE,Integer.toString(crs.getCode()));
207
                xmlEnt.putProperty(CRS_NAME,(crs.getName()));
208
                xmlEnt.putProperty(CRS_WKT,crs.getWkt());
209
                xmlEnt.putProperty(CRS_DATE,DateTime.dateToString(crs.getDate(),dFormat));
210
                Set keys = crs.getProperies().keySet();
211
                Iterator it = keys.iterator();
212
            while (it.hasNext()) {
213
                    String next = (String)it.next();
214
                    xmlEnt.putProperty(next,
215
                                    crs.getProperies().get(next));
216
            }                   
217
                return xmlEnt;
218
        }
219

    
220
        /**
221
         * This method creates a new CrsData from a XMLEntity
222
         * @param xmlEnt
223
         * XMLRntity that contains the Crs information
224
         * @return
225
         * CrsData
226
         */
227
        public CrsData xmlToCrsData(XMLEntity xmlEnt){
228
                String authority = "";
229
                int code = 0;
230
                String name = "";
231
                String wkt = "";
232
                Date date = null;
233

    
234
                authority = xmlEnt.getStringProperty(CRS_AUTHORITY);
235
                code = Integer.valueOf(xmlEnt.getStringProperty(CRS_CODE)).intValue();
236
                name = xmlEnt.getStringProperty(CRS_NAME);
237
                wkt = xmlEnt.getStringProperty(CRS_WKT);
238
                date = DateTime.stringToDate(xmlEnt.getStringProperty(CRS_DATE));
239
                
240
                CrsData crsData = new CrsData(authority,code,name,wkt,date);
241
                
242
                Properties props = new Properties();
243
                for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){
244
                        String property = xmlEnt.getPropertyName(i);
245
                        if (!((property.equals(CRS_AUTHORITY)) ||
246
                                (property.equals(CRS_CODE)) ||
247
                                (property.equals(CRS_NAME)) ||
248
                                (property.equals(CRS_WKT)) ||
249
                                (property.equals(CRS_DATE)))){
250
                                        props.put(property,xmlEnt.getStringProperty(property));
251
                                }                                        
252
                }
253
                crsData.setProperies(props);
254
                return crsData;
255
        }
256

    
257
        public XMLEntity getXml() {
258
                return xml;
259
        }
260

    
261
        public void setXml(XMLEntity xml) {
262
                this.xml = xml;
263
        }
264
}