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

History | View | Annotate | Download (5.75 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.ArrayList;
27
import java.util.Arrays;
28
import java.util.List;
29

    
30
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
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
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
        
56
        /**
57
         * Constructor
58
         * @param view
59
         * The View Class
60
         * @param sevice Type
61
         * Service type to load
62
         */
63
        public ServerDataPersistence(String serviceType) {
64
                this.serviceType = serviceType;
65
                this.serverList = new ArrayList<ServerData>();
66
        }
67
        
68
        public ServerDataPersistence() {
69
            this.serverList = new ArrayList<ServerData>();
70
            this.serviceType = "UNKNOW";
71
        }
72

    
73
        /**
74
         * This method adds a ServerData using the Andami persistence model. 
75
         * If the server exists just actualizes
76
         * the type and subtype fileds and changes the last access
77
         * value to the current time.
78
         * 
79
         * @param server
80
         *          ServerData
81
         */
82
        public void addServerData(ServerData server) {
83
            String address = server.getServerAddress();
84
            for (int i = 0; i < serverList.size(); i++) {
85
                ServerData sd = serverList.get(i);
86
                if(sd.getServerAddress().equals(address)) {
87
                    // Already exist, update  it
88
                    serverList.set(i, server);
89
                    return;
90
                }
91
            }
92
            // Add the new server
93
            serverList.add(server);
94
        }
95
        
96
        /**
97
         * Returns true if exists a server in this list
98
         * @param address
99
         * @return
100
         */
101
        public boolean existsServer(String address) {
102
                for (int i = 0; i < serverList.size(); i++) {
103
                        ServerData sd = serverList.get(i);
104
                        if(sd.getServerAddress().equals(address))
105
                                return true;
106
                }
107
                return false;
108
        }
109

    
110
        /**
111
         * This method returns an array of ServerData objects that
112
         * have been saved using the gvsig tools persistence model.
113
         * @return
114
         * ServerData[]
115
         */
116
        public ServerData[] getArrayOfServerData() {
117
                return (ServerData[])serverList.toArray(new ServerData[serverList.size()]);
118
        }
119
        
120
        public List<ServerData> getServerData() {
121
                return serverList;
122
        }
123
        
124
        public void setArrayOfServerData(ServerData[] servers) {
125
                if(servers != null)
126
                        serverList = Arrays.asList(servers);
127
        }
128

    
129
        public String getServiceType() {
130
                return serviceType;
131
        }
132

    
133
        public void setServiceType(String serviceType) {
134
                this.serviceType = serviceType;
135
        }
136

    
137
        public void saveToState(PersistentState state) throws PersistenceException {
138
                state.set("serverData", getArrayOfServerData());
139
                state.set("serviceType", serviceType);
140
        }
141

    
142
        public void loadFromState(PersistentState state)
143
                        throws PersistenceException {
144
                if(state.get("serverData") instanceof List) {
145
                        serverList = (List<ServerData>)state.get("serverData");
146
                }
147
                serviceType = state.getString("serviceType");
148
        }
149
        
150
        public static void registerPersistence() {
151
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
152
                DynStruct definition = manager.getDefinition(PERSISTENT_NAME);
153
                if( definition == null ) {
154
                        definition = manager.addDefinition(
155
                                        ServerDataPersistence.class,
156
                                        PERSISTENT_NAME,
157
                                        PERSISTENT_DESCRIPTION,
158
                                        null, 
159
                                        null
160
                        );
161
                }
162
                
163
                definition.addDynFieldList("serverData").setClassOfItems(ServerData.class).setMandatory(false);
164
                definition.addDynFieldString("serviceType").setMandatory(true);
165
        }
166

    
167
        public int compareTo(ServerDataPersistence a) {
168
                if(a.getServiceType().equals(getServiceType())) {
169
                        if(getServerData().size() != a.getServerData().size())
170
                                return -1;
171
                        for (int i = 0; i < getServerData().size(); i++) {
172
                                ServerData serverData = getServerData().get(i);
173
                                if(!a.existsServer(serverData.getServerAddress()))
174
                                        return -1;
175
                        }
176
                }
177
                return 0;
178
        }
179
        
180
        public boolean isEmpty() {
181
            if( serverList==null ) {
182
                return true;
183
            }
184
            return serverList.isEmpty();
185
        }
186
        
187
}