Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / persistence / serverData / ServerDataPersistence.java @ 40596

History | View | Annotate | Download (7.07 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.andami.persistence.serverData;
25

    
26
import java.util.Date;
27
import java.util.Iterator;
28
import java.util.Properties;
29
import java.util.Set;
30

    
31
import org.gvsig.andami.PluginServices;
32
import org.gvsig.utils.DateTime;
33
import org.gvsig.utils.NotExistInXMLEntity;
34
import org.gvsig.utils.XMLEntity;
35
import org.gvsig.utils.swing.jcomboServer.ServerData;
36

    
37

    
38
/**
39
 * This class is used to save a list of servers (using the
40
 * Andami persistence model) to the plugins-persistence.xml file.
41
 * It has methods to create a set of ServerData objects  from an
42
 * xml file. It can also save a set of ServerData objects in an
43
 * xml file.
44
 *
45
 * @see es.gva.cit.catalogClient.utils.comboserver.ServerData
46
 * @author Jorge Piera Llodra (piera_jor@gva.es)
47
 */
48
public class ServerDataPersistence {
49
        private XMLEntity xml = null;
50
        private PluginServices ps = null;
51
        private String serviceType = null;
52
        private static final String SERVER_URL = "serverURL";
53
        private static final String SERVER_ADDED = "added";
54
        private static final String SERVER_LASTACCESS = "lastAccess";
55
        private static final String SERVER_TYPE = "type";
56
        private static final String SERVER_SUBTYPE = "subType";
57
        private static final String SERVER_DATABASE = "database";
58
        
59
        /**
60
         * Constructor
61
         * @param view
62
         * The View Class
63
         * @param sevice Type
64
         * Service type to load
65
         */
66
        public ServerDataPersistence(Object pluginClassInstance,String serviceType){
67
                ps = PluginServices.getPluginServices(pluginClassInstance);
68
                xml = ps.getPersistentXML();
69
                this.serviceType = serviceType;
70
        }
71

    
72
        /**
73
         * This methos is used to save the information in an XML file
74
         */
75
        public void setPersistent(){
76
                ps.setPersistentXML(xml);
77
        }
78

    
79
        /**
80
         * This method saves an array of ServerData using the Anadami
81
         * persistence model
82
         * @param servers
83
         * Array of servers
84
         */
85
        public void setArrayOfServerData(ServerData[] servers){
86
                xml.getXmlTag().removeAllXmlTag();
87

    
88
                for (int i=0 ; i<servers.length ; i++){
89
                        xml.addChild(serverDataToXml(servers[i]));
90
                }
91

    
92
        }
93

    
94
        /**
95
         * This method adds a ServerData using the Anadami
96
         * persistence model. If the server exists just actualizes
97
         * the type and subtype fileds and changes the last access
98
         * value to the current time.
99
         * @param server
100
         * ServerData
101
         */
102
        public void addServerData(ServerData server){
103
                ServerData[] servers = getArrayOfServerData();
104
                ServerData[] newServers = new ServerData[servers.length + 1];
105

    
106
                boolean found = false;
107

    
108
                for (int i = 0; i < servers.length; i++) {
109
                        if (servers[i].getServerAddress().equals(server.getServerAddress())) {
110
                                found = true;
111
                                servers[i].updateLastAccess();
112
                                servers[i].setServiceSubType(server.getServiceSubType());
113
                                servers[i].setServiceType(server.getServiceType());
114
                                servers[i].setDatabase(server.getDatabase());
115
                                servers[i].setProperies(server.getProperies());
116
                                setArrayOfServerData(servers);
117
                        }
118
                }
119

    
120
                if (!found) {
121
                        System.arraycopy(servers, 0, newServers, 0, servers.length);
122
                        newServers[servers.length] = server;
123
                        setArrayOfServerData(newServers);
124

    
125
                }
126
        }
127

    
128

    
129

    
130
        /**
131
         * This method returns an array of ServerData objects that
132
         * have been saved using the Andami persistence model.
133
         * @return
134
         * String[]
135
         */
136
        public ServerData[] getArrayOfServerData(){
137
                ServerData[] servers = new ServerData[xml.getChildrenCount()];
138
                for (int i=0 ; i<xml.getChildrenCount() ; i++){
139
                        servers[i] = xmlToServerData(xml.getChild(i));
140
                }
141
                return servers;
142
        }
143

    
144

    
145
        /**
146
         * This method creates and returns a new XMLEntity.
147
         * @param server
148
         * ServerData with all the server information
149
         * @return
150
         * XMLEntity
151
         */
152
        public XMLEntity serverDataToXml(ServerData server){
153
                String dFormat="Y-m-d H:i:s.Z";
154

    
155
                XMLEntity xmlEnt = new XMLEntity();
156
                xmlEnt.putProperty(SERVER_URL,server.getServerAddress());
157
                xmlEnt.putProperty(SERVER_ADDED,DateTime.dateToString(server.getAdded(),dFormat));
158
                xmlEnt.putProperty(SERVER_LASTACCESS,DateTime.dateToString(server.getLastAccess(),dFormat));
159
                xmlEnt.putProperty(SERVER_TYPE,server.getServiceType());
160
                xmlEnt.putProperty(SERVER_SUBTYPE,server.getServiceSubType());
161
                if (server.getServiceSubType().equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
162
                        xmlEnt.putProperty(SERVER_DATABASE,server.getDatabase());
163
                }
164
                Set keys = server.getProperies().keySet();
165
                Iterator it = keys.iterator();
166
            while (it.hasNext()) {
167
                    String next = (String)it.next();
168
                    xmlEnt.putProperty(next,
169
                                    server.getProperies().get(next));
170
            }                   
171
                return xmlEnt;
172
        }
173

    
174
        /**
175
         * This method creates a new serverData from a XMLEntity
176
         * @param xmlEnt
177
         * XMLRntity that contains the server information
178
         * @return
179
         * ServerData
180
         */
181
        public ServerData xmlToServerData(XMLEntity xmlEnt){
182
                String serverAddress;
183
                Date added;
184
                Date lastAccess;
185
                String serviceType = "";
186
                String serviceSubType = "";
187
                String databaseName = "";
188

    
189
                serverAddress = xmlEnt.getStringProperty(SERVER_URL);
190
                added = DateTime.stringToDate(xmlEnt.getStringProperty(SERVER_ADDED));
191
                lastAccess = DateTime.stringToDate(xmlEnt.getStringProperty(SERVER_LASTACCESS));
192
                serviceType = xmlEnt.getStringProperty(SERVER_TYPE).toUpperCase();
193
                serviceSubType = xmlEnt.getStringProperty(SERVER_SUBTYPE).toUpperCase();
194
                
195
                if (serviceSubType.equals(ServerData.SERVER_SUBTYPE_CATALOG_Z3950)){
196
                        try{
197
                                databaseName = xmlEnt.getStringProperty(SERVER_DATABASE);
198
                        }catch(NotExistInXMLEntity e){
199

    
200
                        }
201
                }
202
                
203
                ServerData serverData = new ServerData(serverAddress,added,lastAccess,serviceType,serviceSubType,databaseName);
204
                
205
                Properties props = new Properties();
206
                for (int i=0 ; i<xmlEnt.getPropertyCount() ; i++){
207
                        String property = xmlEnt.getPropertyName(i);
208
                        if (!((property.equals(SERVER_URL)) ||
209
                                (property.equals(SERVER_ADDED)) ||
210
                                (property.equals(SERVER_LASTACCESS)) ||
211
                                (property.equals(SERVER_TYPE)) ||
212
                                (property.equals(SERVER_SUBTYPE)) ||
213
                                (property.equals(SERVER_DATABASE)))){
214
                                        props.put(property,xmlEnt.getStringProperty(property));
215
                                }                                        
216
                }
217
                serverData.setProperies(props);
218
                return serverData;
219
        }
220

    
221
        public String getServiceType() {
222
                return serviceType;
223
        }
224

    
225
        public void setServiceType(String serviceType) {
226
                this.serviceType = serviceType;
227
        }
228

    
229
        public XMLEntity getXml() {
230
                return xml;
231
        }
232

    
233
        public void setXml(XMLEntity xml) {
234
                this.xml = xml;
235
        }
236
}