svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / persistence / serverData / ServerDataPersistence.java @ 41482
History | View | Annotate | Download (7.4 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.andami.persistence.serverData; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.Arrays; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
|
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dynobject.DynStruct; |
33 |
import org.gvsig.tools.persistence.PersistenceManager; |
34 |
import org.gvsig.tools.persistence.Persistent; |
35 |
import org.gvsig.tools.persistence.PersistentState; |
36 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
37 |
import org.gvsig.utils.swing.jcomboServer.ServerData; |
38 |
|
39 |
/**
|
40 |
* This class is used to save a list of servers (using the Andami persistence
|
41 |
* model) to the plugins-persistence.xml file. It has methods to create a set of
|
42 |
* ServerData objects from an xml file. It can also save a set of ServerData
|
43 |
* objects in an 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 implements Persistent, Comparable<ServerDataPersistence> { |
49 |
|
50 |
public static final String PERSISTENT_NAME = "ServerDataList_Persistent"; |
51 |
public static final String PERSISTENT_DESCRIPTION = "ServerDataList Persistent"; |
52 |
|
53 |
private String serviceType = null; |
54 |
private List<ServerData> serverList = null; |
55 |
|
56 |
/**
|
57 |
* Constructor
|
58 |
*
|
59 |
* @param sevice Type Service type to load
|
60 |
*/
|
61 |
public ServerDataPersistence(String serviceType) { |
62 |
this.serviceType = serviceType;
|
63 |
this.serverList = new ArrayList<ServerData>(); |
64 |
} |
65 |
|
66 |
public ServerDataPersistence() {
|
67 |
this.serverList = new ArrayList<ServerData>(); |
68 |
this.serviceType = "UNKNOW"; |
69 |
} |
70 |
|
71 |
/**
|
72 |
* This method adds a ServerData using the Andami persistence model. If the
|
73 |
* server exists just actualizes the type and subtype fileds and changes the
|
74 |
* last access value to the current time.
|
75 |
*
|
76 |
* @param server ServerData
|
77 |
*/
|
78 |
public void addServerData(ServerData server) { |
79 |
String address = server.getServerAddress();
|
80 |
for (int i = 0; i < serverList.size(); i++) { |
81 |
ServerData sd = serverList.get(i); |
82 |
if (sd.getServerAddress().equals(address)) {
|
83 |
// Already exist, update it
|
84 |
serverList.set(i, server); |
85 |
return;
|
86 |
} |
87 |
} |
88 |
// Add the new server
|
89 |
serverList.add(server); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* Returns true if exists a server in this list
|
94 |
*
|
95 |
* @param address
|
96 |
* @return
|
97 |
*/
|
98 |
public boolean existsServer(String address) { |
99 |
return contains(address);
|
100 |
} |
101 |
|
102 |
/**
|
103 |
* This method returns an array of ServerData objects that have been saved
|
104 |
* using the gvsig tools persistence model.
|
105 |
*
|
106 |
* @return ServerData[]
|
107 |
*/
|
108 |
public ServerData[] getArrayOfServerData() { |
109 |
return (ServerData[]) serverList.toArray(new ServerData[serverList.size()]); |
110 |
} |
111 |
|
112 |
public List<ServerData> getServerData() { |
113 |
return serverList;
|
114 |
} |
115 |
|
116 |
public void setArrayOfServerData(ServerData[] servers) { |
117 |
if (servers != null) { |
118 |
serverList = Arrays.asList(servers);
|
119 |
} |
120 |
} |
121 |
|
122 |
public String getServiceType() { |
123 |
return serviceType;
|
124 |
} |
125 |
|
126 |
public void setServiceType(String serviceType) { |
127 |
this.serviceType = serviceType;
|
128 |
} |
129 |
|
130 |
public void saveToState(PersistentState state) throws PersistenceException { |
131 |
state.set("serverData", getArrayOfServerData());
|
132 |
state.set("serviceType", serviceType);
|
133 |
} |
134 |
|
135 |
public void loadFromState(PersistentState state) |
136 |
throws PersistenceException {
|
137 |
if (state.get("serverData") instanceof List) { |
138 |
serverList = (List<ServerData>) state.get("serverData"); |
139 |
} |
140 |
serviceType = state.getString("serviceType");
|
141 |
} |
142 |
|
143 |
public static void registerPersistence() { |
144 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
145 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
146 |
if (definition == null) { |
147 |
definition = manager.addDefinition( |
148 |
ServerDataPersistence.class, |
149 |
PERSISTENT_NAME, |
150 |
PERSISTENT_DESCRIPTION, |
151 |
null,
|
152 |
null
|
153 |
); |
154 |
} |
155 |
|
156 |
definition.addDynFieldList("serverData").setClassOfItems(ServerData.class).setMandatory(false); |
157 |
definition.addDynFieldString("serviceType").setMandatory(true); |
158 |
} |
159 |
|
160 |
public int compareTo(ServerDataPersistence a) { |
161 |
if (a.getServiceType().equals(getServiceType())) {
|
162 |
if (getServerData().size() != a.getServerData().size()) {
|
163 |
return -1; |
164 |
} |
165 |
for (int i = 0; i < getServerData().size(); i++) { |
166 |
ServerData serverData = getServerData().get(i); |
167 |
if (!a.existsServer(serverData.getServerAddress())) {
|
168 |
return -1; |
169 |
} |
170 |
} |
171 |
} |
172 |
return 0; |
173 |
} |
174 |
|
175 |
public boolean isEmpty() { |
176 |
if (serverList == null) { |
177 |
return true; |
178 |
} |
179 |
return serverList.isEmpty();
|
180 |
} |
181 |
|
182 |
public void remove(String serverAddress) { |
183 |
if (StringUtils.isBlank(serverAddress)) {
|
184 |
return;
|
185 |
} |
186 |
Iterator<ServerData> it = getServerData().iterator();
|
187 |
while (it.hasNext()) {
|
188 |
ServerData x = it.next(); |
189 |
if (serverAddress.equalsIgnoreCase(x.getServerAddress())) {
|
190 |
it.remove(); |
191 |
return;
|
192 |
} |
193 |
} |
194 |
} |
195 |
|
196 |
public void add(String server, String protocol) { |
197 |
if (StringUtils.isBlank(server)) {
|
198 |
return;
|
199 |
} |
200 |
this.add(new ServerData(server, protocol)); |
201 |
} |
202 |
|
203 |
public void add(ServerData server) { |
204 |
String address = server.getServerAddress().trim();
|
205 |
for (int i = 0; i < getServerData().size(); i++) { |
206 |
ServerData sd = getServerData().get(i); |
207 |
if (sd.getServerAddress().trim().equals(address)) {
|
208 |
getServerData().set(i, server); |
209 |
return;
|
210 |
} |
211 |
} |
212 |
getServerData().add(server); |
213 |
} |
214 |
|
215 |
public boolean contains(String serverAddress) { |
216 |
if (StringUtils.isBlank(serverAddress)) {
|
217 |
return false; |
218 |
} |
219 |
serverAddress = serverAddress.trim(); |
220 |
for (int i = 0; i < getServerData().size(); i++) { |
221 |
String address = getServerData().get(i).getServerAddress().trim();
|
222 |
if (serverAddress.equalsIgnoreCase(address)) {
|
223 |
return true; |
224 |
} |
225 |
} |
226 |
return false; |
227 |
} |
228 |
|
229 |
} |