root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / persistence / serverData / ServerDataPersistence.java @ 29593
History | View | Annotate | Download (8.27 KB)
1 |
package org.gvsig.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 29593 2009-06-29 15:54:31Z jpiera $
|
45 |
* $Log$
|
46 |
* Revision 1.7 2006-09-01 13:39:38 jorpiell
|
47 |
* Se ha a?adido la capacidad de almacenar properties en los server data
|
48 |
*
|
49 |
* Revision 1.6 2006/09/01 02:23:09 luisw2
|
50 |
* Geters and Seters added
|
51 |
*
|
52 |
* Revision 1.5 2006/08/28 07:56:54 jaume
|
53 |
* *** empty log message ***
|
54 |
*
|
55 |
* Revision 1.4 2006/05/19 06:41:50 jaume
|
56 |
* removed unnecessary imports
|
57 |
*
|
58 |
* Revision 1.3 2006/05/02 15:53:06 jorpiell
|
59 |
* 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
|
60 |
*
|
61 |
* Revision 1.2 2006/03/21 18:06:46 jorpiell
|
62 |
* Se ha hecho una modificaci?n para continuar soportando los servidores anteriores
|
63 |
*
|
64 |
* Revision 1.1 2006/03/07 11:32:13 jorpiell
|
65 |
* Se ha a?adido la clase ServerDataPersistence en el paquete persistence.serverData que se usa para persistir los servidores por orden de ?ltimo acceso.
|
66 |
*
|
67 |
*
|
68 |
*/
|
69 |
|
70 |
import java.util.Date; |
71 |
import java.util.Iterator; |
72 |
import java.util.Properties; |
73 |
import java.util.Set; |
74 |
|
75 |
import org.gvsig.andami.PluginServices; |
76 |
import org.gvsig.utils.DateTime; |
77 |
import org.gvsig.utils.NotExistInXMLEntity; |
78 |
import org.gvsig.utils.XMLEntity; |
79 |
import org.gvsig.utils.swing.jcomboServer.ServerData; |
80 |
|
81 |
|
82 |
/**
|
83 |
* This class is used to save a list of servers (using the
|
84 |
* Andami persistence model) to the plugins-persistence.xml file.
|
85 |
* It has methods to create a set of ServerData objects from an
|
86 |
* xml file. It can also save a set of ServerData objects in an
|
87 |
* xml file.
|
88 |
*
|
89 |
* @see es.gva.cit.catalogClient.utils.comboserver.ServerData
|
90 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
91 |
*/
|
92 |
public class ServerDataPersistence { |
93 |
private XMLEntity xml = null; |
94 |
private PluginServices ps = null; |
95 |
private String serviceType = null; |
96 |
private static final String SERVER_URL = "serverURL"; |
97 |
private static final String SERVER_ADDED = "added"; |
98 |
private static final String SERVER_LASTACCESS = "lastAccess"; |
99 |
private static final String SERVER_TYPE = "type"; |
100 |
private static final String SERVER_SUBTYPE = "subType"; |
101 |
private static final String SERVER_DATABASE = "database"; |
102 |
|
103 |
/**
|
104 |
* Constructor
|
105 |
* @param view
|
106 |
* The View Class
|
107 |
* @param sevice Type
|
108 |
* Service type to load
|
109 |
*/
|
110 |
public ServerDataPersistence(Object pluginClassInstance,String serviceType){ |
111 |
ps = PluginServices.getPluginServices(pluginClassInstance); |
112 |
xml = ps.getPersistentXML(); |
113 |
this.serviceType = serviceType;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* This methos is used to save the information in an XML file
|
118 |
*/
|
119 |
public void setPersistent(){ |
120 |
ps.setPersistentXML(xml); |
121 |
} |
122 |
|
123 |
/**
|
124 |
* This method saves an array of ServerData using the Anadami
|
125 |
* persistence model
|
126 |
* @param servers
|
127 |
* Array of servers
|
128 |
*/
|
129 |
public void setArrayOfServerData(ServerData[] servers){ |
130 |
xml.getXmlTag().removeAllXmlTag(); |
131 |
|
132 |
for (int i=0 ; i<servers.length ; i++){ |
133 |
xml.addChild(serverDataToXml(servers[i])); |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* This method adds a ServerData using the Anadami
|
140 |
* persistence model. If the server exists just actualizes
|
141 |
* the type and subtype fileds and changes the last access
|
142 |
* value to the current time.
|
143 |
* @param server
|
144 |
* ServerData
|
145 |
*/
|
146 |
public void addServerData(ServerData server){ |
147 |
ServerData[] servers = getArrayOfServerData();
|
148 |
ServerData[] newServers = new ServerData[servers.length + 1]; |
149 |
|
150 |
boolean found = false; |
151 |
|
152 |
for (int i = 0; i < servers.length; i++) { |
153 |
if (servers[i].getServerAddress().equals(server.getServerAddress())) {
|
154 |
found = true;
|
155 |
servers[i].updateLastAccess(); |
156 |
servers[i].setServiceSubType(server.getServiceSubType()); |
157 |
servers[i].setServiceType(server.getServiceType()); |
158 |
servers[i].setDatabase(server.getDatabase()); |
159 |
servers[i].setProperies(server.getProperies()); |
160 |
setArrayOfServerData(servers); |
161 |
} |
162 |
} |
163 |
|
164 |
if (!found) {
|
165 |
System.arraycopy(servers, 0, newServers, 0, servers.length); |
166 |
newServers[servers.length] = server; |
167 |
setArrayOfServerData(newServers); |
168 |
|
169 |
} |
170 |
} |
171 |
|
172 |
|
173 |
|
174 |
/**
|
175 |
* This method returns an array of ServerData objects that
|
176 |
* have been saved using the Andami persistence model.
|
177 |
* @return
|
178 |
* String[]
|
179 |
*/
|
180 |
public ServerData[] getArrayOfServerData(){ |
181 |
ServerData[] servers = new ServerData[xml.getChildrenCount()]; |
182 |
for (int i=0 ; i<xml.getChildrenCount() ; i++){ |
183 |
servers[i] = xmlToServerData(xml.getChild(i)); |
184 |
} |
185 |
return servers;
|
186 |
} |
187 |
|
188 |
|
189 |
/**
|
190 |
* This method creates and returns a new XMLEntity.
|
191 |
* @param server
|
192 |
* ServerData with all the server information
|
193 |
* @return
|
194 |
* XMLEntity
|
195 |
*/
|
196 |
public XMLEntity serverDataToXml(ServerData server){
|
197 |
String dFormat="Y-m-d H:i:s.Z"; |
198 |
|
199 |
XMLEntity xmlEnt = new XMLEntity();
|
200 |
xmlEnt.putProperty(SERVER_URL,server.getServerAddress()); |
201 |
xmlEnt.putProperty(SERVER_ADDED,DateTime.dateToString(server.getAdded(),dFormat)); |
202 |
xmlEnt.putProperty(SERVER_LASTACCESS,DateTime.dateToString(server.getLastAccess(),dFormat)); |
203 |
xmlEnt.putProperty(SERVER_TYPE,server.getServiceType()); |
204 |
xmlEnt.putProperty(SERVER_SUBTYPE,server.getServiceSubType()); |
205 |
if (server.getServiceSubType().equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
|
206 |
xmlEnt.putProperty(SERVER_DATABASE,server.getDatabase()); |
207 |
} |
208 |
Set keys = server.getProperies().keySet();
|
209 |
Iterator it = keys.iterator();
|
210 |
while (it.hasNext()) {
|
211 |
String next = (String)it.next(); |
212 |
xmlEnt.putProperty(next, |
213 |
server.getProperies().get(next)); |
214 |
} |
215 |
return xmlEnt;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* This method creates a new serverData from a XMLEntity
|
220 |
* @param xmlEnt
|
221 |
* XMLRntity that contains the server information
|
222 |
* @return
|
223 |
* ServerData
|
224 |
*/
|
225 |
public ServerData xmlToServerData(XMLEntity xmlEnt){
|
226 |
String serverAddress;
|
227 |
Date added;
|
228 |
Date lastAccess;
|
229 |
String serviceType = ""; |
230 |
String serviceSubType = ""; |
231 |
String databaseName = ""; |
232 |
|
233 |
serverAddress = xmlEnt.getStringProperty(SERVER_URL); |
234 |
added = DateTime.stringToDate(xmlEnt.getStringProperty(SERVER_ADDED)); |
235 |
lastAccess = DateTime.stringToDate(xmlEnt.getStringProperty(SERVER_LASTACCESS)); |
236 |
serviceType = xmlEnt.getStringProperty(SERVER_TYPE).toUpperCase(); |
237 |
serviceSubType = xmlEnt.getStringProperty(SERVER_SUBTYPE).toUpperCase(); |
238 |
|
239 |
if (serviceSubType.equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
|
240 |
try{
|
241 |
databaseName = xmlEnt.getStringProperty(SERVER_DATABASE); |
242 |
}catch(NotExistInXMLEntity e){
|
243 |
|
244 |
} |
245 |
} |
246 |
|
247 |
ServerData serverData = new ServerData(serverAddress,added,lastAccess,serviceType,serviceSubType,databaseName);
|
248 |
|
249 |
Properties props = new Properties(); |
250 |
for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){ |
251 |
String property = xmlEnt.getPropertyName(i);
|
252 |
if (!((property.equals(SERVER_URL)) ||
|
253 |
(property.equals(SERVER_ADDED)) || |
254 |
(property.equals(SERVER_LASTACCESS)) || |
255 |
(property.equals(SERVER_TYPE)) || |
256 |
(property.equals(SERVER_SUBTYPE)) || |
257 |
(property.equals(SERVER_DATABASE)))){ |
258 |
props.put(property,xmlEnt.getStringProperty(property)); |
259 |
} |
260 |
} |
261 |
serverData.setProperies(props); |
262 |
return serverData;
|
263 |
} |
264 |
|
265 |
public String getServiceType() { |
266 |
return serviceType;
|
267 |
} |
268 |
|
269 |
public void setServiceType(String serviceType) { |
270 |
this.serviceType = serviceType;
|
271 |
} |
272 |
|
273 |
public XMLEntity getXml() {
|
274 |
return xml;
|
275 |
} |
276 |
|
277 |
public void setXml(XMLEntity xml) { |
278 |
this.xml = xml;
|
279 |
} |
280 |
} |