Statistics
| Revision:

svn-gvsig-desktop / trunk / frameworks / _fwAndami / src / com / iver / andami / persistence / serverData / ServerDataPersistence.java @ 6861

History | View | Annotate | Download (6.56 KB)

1
package com.iver.andami.persistence.serverData;
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
/* CVS MESSAGES:
43
 *
44
 * $Id: ServerDataPersistence.java 6861 2006-08-28 07:57:18Z jaume $
45
 * $Log$
46
 * Revision 1.5  2006-08-28 07:56:54  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.4  2006/05/19 06:41:50  jaume
50
 * removed unnecessary imports
51
 *
52
 * Revision 1.3  2006/05/02 15:53:06  jorpiell
53
 * Se ha cambiado la interfaz Extension por dos clases: una interfaz (IExtension) y una clase abstract(Extension). A partir de ahora todas las extensiones deben heredar de Extension
54
 *
55
 * Revision 1.2  2006/03/21 18:06:46  jorpiell
56
 * Se ha hecho una modificaci?n para continuar soportando los servidores anteriores
57
 *
58
 * Revision 1.1  2006/03/07 11:32:13  jorpiell
59
 * Se ha a?adido la clase ServerDataPersistence en el paquete persistence.serverData que se usa para persistir los servidores por orden de ?ltimo acceso.
60
 *
61
 *
62
 */
63

    
64
import java.util.Date;
65

    
66
import com.iver.andami.PluginServices;
67
import com.iver.utiles.DateTime;
68
import com.iver.utiles.NotExistInXMLEntity;
69
import com.iver.utiles.XMLEntity;
70
import com.iver.utiles.swing.jcomboServer.ServerData;
71

    
72
/**
73
 * This class is used to save a list of servers (using the
74
 * Andami persistence model) to the plugins-persistence.xml file.
75
 * It has methods to create a set of ServerData objects  from an
76
 * xml file. It can also save a set of ServerData objects in an
77
 * xml file.
78
 *
79
 * @see es.gva.cit.catalogClient.utils.comboserver.ServerData
80
 * @author Jorge Piera Llodra (piera_jor@gva.es)
81
 */
82
public class ServerDataPersistence {
83
        private XMLEntity xml = null;
84
        private PluginServices ps = null;
85
        private String serviceType = null;
86

    
87
        /**
88
         * Constructor
89
         * @param view
90
         * The View Class
91
         * @param sevice Type
92
         * Service type to load
93
         */
94
        public ServerDataPersistence(Object pluginClassInstance,String serviceType){
95
                ps = PluginServices.getPluginServices(pluginClassInstance);
96
                xml = ps.getPersistentXML();
97
                this.serviceType = serviceType;
98
        }
99

    
100
        /**
101
         * This methos is used to save the information in an XML file
102
         */
103
        public void setPersistent(){
104
                ps.setPersistentXML(xml);
105
        }
106

    
107
        /**
108
         * This method saves an array of ServerData using the Anadami
109
         * persistence model
110
         * @param servers
111
         * Array of servers
112
         */
113
        public void setArrayOfServerData(ServerData[] servers){
114
                xml.getXmlTag().removeAllXmlTag();
115

    
116
                for (int i=0 ; i<servers.length ; i++){
117
                        xml.addChild(serverDataToXml(servers[i]));
118
                }
119

    
120
        }
121

    
122
        /**
123
         * This method adds a ServerData using the Anadami
124
         * persistence model. If the server exists just actualizes
125
         * the type and subtype fileds and changes the last access
126
         * value to the current time.
127
         * @param server
128
         * ServerData
129
         */
130
        public void addServerData(ServerData server){
131
                ServerData[] servers = getArrayOfServerData();
132
                ServerData[] newServers = new ServerData[servers.length + 1];
133

    
134
                boolean found = false;
135

    
136
                for (int i = 0; i < servers.length; i++) {
137
                        if (servers[i].getServerAddress().equals(server.getServerAddress())) {
138
                                found = true;
139
                                servers[i].updateLastAccess();
140
                                servers[i].setServiceSubType(server.getServiceSubType());
141
                                servers[i].setServiceType(server.getServiceType());
142
                                servers[i].setDatabase(server.getDatabase());
143
                                setArrayOfServerData(servers);
144
                        }
145
                }
146

    
147
                if (!found) {
148
                        System.arraycopy(servers, 0, newServers, 0, servers.length);
149
                        newServers[servers.length] = server;
150
                        setArrayOfServerData(newServers);
151

    
152
                }
153
        }
154

    
155

    
156

    
157
        /**
158
         * This method returns an array of ServerData objects that
159
         * have been saved using the Andami persistence model.
160
         * @return
161
         * String[]
162
         */
163
        public ServerData[] getArrayOfServerData(){
164
                ServerData[] servers = new ServerData[xml.getChildrenCount()];
165
                for (int i=0 ; i<xml.getChildrenCount() ; i++){
166
                        servers[i] = xmlToServerData(xml.getChild(i));
167
                }
168
                return servers;
169
        }
170

    
171

    
172
        /**
173
         * This method creates and returns a new XMLEntity.
174
         * @param server
175
         * ServerData with all the server information
176
         * @return
177
         * XMLEntity
178
         */
179
        public XMLEntity serverDataToXml(ServerData server){
180
                String dFormat="Y-m-d H:i:s.Z";
181

    
182
                XMLEntity xmlEnt = new XMLEntity();
183
                xmlEnt.putProperty("serverURL",server.getServerAddress());
184
                xmlEnt.putProperty("added",DateTime.dateToString(server.getAdded(),dFormat));
185
                xmlEnt.putProperty("lastAccess",DateTime.dateToString(server.getLastAccess(),dFormat));
186
                xmlEnt.putProperty("type",server.getServiceType());
187
                xmlEnt.putProperty("subType",server.getServiceSubType());
188
                if (server.getServiceSubType().equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
189
                        xmlEnt.putProperty("database",server.getDatabase());
190
                }
191
                return xmlEnt;
192
        }
193

    
194
        /**
195
         * This method creates a new serverData from a XMLEntity
196
         * @param xmlEnt
197
         * XMLRntity that contains the server information
198
         * @return
199
         * ServerData
200
         */
201
        public ServerData xmlToServerData(XMLEntity xmlEnt){
202
                String serverAddress;
203
                Date added;
204
                Date lastAccess;
205
                String serviceType = "";
206
                String serviceSubType = "";
207
                String databaseName = "";
208

    
209
                serverAddress = xmlEnt.getStringProperty("serverURL");
210
                added = DateTime.stringToDate(xmlEnt.getStringProperty("added"));
211
                lastAccess = DateTime.stringToDate(xmlEnt.getStringProperty("lastAccess"));
212
                serviceType = xmlEnt.getStringProperty("type").toUpperCase();
213
                serviceSubType = xmlEnt.getStringProperty("subType").toUpperCase();
214

    
215
                if (serviceSubType.equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
216
                        try{
217
                                databaseName = xmlEnt.getStringProperty("database");
218
                        }catch(NotExistInXMLEntity e){
219

    
220
                        }
221
                }
222
                return new ServerData(serverAddress,added,lastAccess,serviceType,serviceSubType,databaseName);
223
        }
224
}