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 @ 40627

History | View | Annotate | Download (5.03 KB)

1 40559 jjdelcerro
/**
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 40435 jjdelcerro
package org.gvsig.andami.persistence.serverData;
25
26 40627 nbrodin
import java.util.ArrayList;
27
import java.util.Arrays;
28
import java.util.List;
29 40435 jjdelcerro
30 40627 nbrodin
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DynStruct;
32
import org.gvsig.tools.persistence.PersistenceManager;
33
import org.gvsig.tools.persistence.Persistent;
34
import org.gvsig.tools.persistence.PersistentState;
35
import org.gvsig.tools.persistence.exception.PersistenceException;
36 40435 jjdelcerro
import org.gvsig.utils.swing.jcomboServer.ServerData;
37
38
39
/**
40
 * This class is used to save a list of servers (using the
41
 * Andami persistence model) to the plugins-persistence.xml file.
42
 * It has methods to create a set of ServerData objects  from an
43
 * xml file. It can also save a set of ServerData objects in an
44
 * xml file.
45
 *
46
 * @see es.gva.cit.catalogClient.utils.comboserver.ServerData
47
 * @author Jorge Piera Llodra (piera_jor@gva.es)
48
 */
49 40627 nbrodin
public class ServerDataPersistence implements Persistent, Comparable<ServerDataPersistence> {
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 40435 jjdelcerro
56
        /**
57
         * Constructor
58
         * @param view
59
         * The View Class
60
         * @param sevice Type
61
         * Service type to load
62
         */
63 40627 nbrodin
        public ServerDataPersistence(String serviceType) {
64 40435 jjdelcerro
                this.serviceType = serviceType;
65 40627 nbrodin
                serverList = new ArrayList<ServerData>();
66 40435 jjdelcerro
        }
67 40627 nbrodin
68
        public ServerDataPersistence() {
69 40435 jjdelcerro
        }
70
71
        /**
72 40627 nbrodin
         * This method adds a ServerData using the Andami
73 40435 jjdelcerro
         * persistence model. If the server exists just actualizes
74
         * the type and subtype fileds and changes the last access
75
         * value to the current time.
76
         * @param server
77
         * ServerData
78
         */
79 40627 nbrodin
        public void addServerData(ServerData server) {
80
                serverList.add(server);
81 40435 jjdelcerro
        }
82 40627 nbrodin
83 40435 jjdelcerro
        /**
84 40627 nbrodin
         * Returns true if exists a server in this list
85
         * @param address
86 40435 jjdelcerro
         * @return
87
         */
88 40627 nbrodin
        public boolean existsServer(String address) {
89
                for (int i = 0; i < serverList.size(); i++) {
90
                        ServerData sd = serverList.get(i);
91
                        if(sd.getServerAddress().equals(address))
92
                                return true;
93 40435 jjdelcerro
                }
94 40627 nbrodin
                return false;
95 40435 jjdelcerro
        }
96
97
        /**
98 40627 nbrodin
         * This method returns an array of ServerData objects that
99
         * have been saved using the gvsig tools persistence model.
100 40435 jjdelcerro
         * @return
101 40627 nbrodin
         * ServerData[]
102 40435 jjdelcerro
         */
103 40627 nbrodin
        public ServerData[] getArrayOfServerData() {
104
                return (ServerData[])serverList.toArray(new ServerData[serverList.size()]);
105 40435 jjdelcerro
        }
106 40627 nbrodin
107
        public List<ServerData> getServerData() {
108
                return serverList;
109 40435 jjdelcerro
        }
110 40627 nbrodin
111
        public void setArrayOfServerData(ServerData[] servers) {
112
                if(servers != null)
113
                        serverList = Arrays.asList(servers);
114
        }
115 40435 jjdelcerro
116
        public String getServiceType() {
117
                return serviceType;
118
        }
119
120
        public void setServiceType(String serviceType) {
121
                this.serviceType = serviceType;
122
        }
123
124 40627 nbrodin
        public void saveToState(PersistentState state) throws PersistenceException {
125
                state.set("serverData", getArrayOfServerData());
126
                state.set("serviceType", serviceType);
127 40435 jjdelcerro
        }
128
129 40627 nbrodin
        public void loadFromState(PersistentState state)
130
                        throws PersistenceException {
131
                if(state.get("serverData") instanceof List) {
132
                        serverList = (List<ServerData>)state.get("serverData");
133
                }
134
                serviceType = state.getString("serviceType");
135 40435 jjdelcerro
        }
136 40627 nbrodin
137
        public static void registerPersistence() {
138
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
139
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
140
                if( definition == null ) {
141
                        definition = manager.addDefinition(
142
                                        ServerDataPersistence.class,
143
                                        PERSISTENT_NAME,
144
                                        PERSISTENT_DESCRIPTION,
145
                                        null,
146
                                        null
147
                        );
148
                }
149
150
                definition.addDynFieldList("serverData").setClassOfItems(ServerData.class).setMandatory(false);
151
                definition.addDynFieldString("serviceType").setMandatory(true);
152
        }
153
154
        public int compareTo(ServerDataPersistence a) {
155
                if(a.getServiceType().equals(getServiceType())) {
156
                        if(getServerData().size() != a.getServerData().size())
157
                                return -1;
158
                        for (int i = 0; i < getServerData().size(); i++) {
159
                                ServerData serverData = getServerData().get(i);
160
                                if(!a.existsServer(serverData.getServerAddress()))
161
                                        return -1;
162
                        }
163
                }
164
                return 0;
165
        }
166
167 40435 jjdelcerro
}