Statistics
| Revision:

svn-gvsig-desktop / branches / CatalogYNomenclator_v1_1_0_1005 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / CatalogClient.java @ 12750

History | View | Annotate | Download (4.83 KB)

1

    
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
package es.gva.cit.catalogClient;
43
import java.io.IOException;
44
import java.net.Socket;
45
import java.net.URI;
46
import java.net.URISyntaxException;
47
import java.net.UnknownHostException;
48

    
49
import es.gva.cit.catalogClient.drivers.CatalogCapabilities;
50
import es.gva.cit.catalogClient.drivers.GetRecordsReply;
51
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
52
import es.gva.cit.catalogClient.exceptions.ServerIsNotReadyException;
53
import es.gva.cit.catalogClient.querys.Query;
54
import es.gva.cit.catalogClient.schemas.records.Record;
55
import es.gva.cit.catalogClient.utils.CatalogClientRegister;
56
import es.gva.cit.catalogClient.utils.URIUtils;
57

    
58
/**
59
 * This class represents a catalogClient. It must be created to
60
 * use the catalog service
61
 * @author Jorge Piera Llodra (piera_jor@gva.es)
62
 */
63
public class CatalogClient {
64
        /**
65
         * The server URI
66
         */
67
        private URI uri = null;
68
        /**
69
         * The driver to make the querys
70
         */
71
        private ICatalogServiceDriver catalogDriver;
72

    
73
        /**
74
         * The server status message
75
         */
76
        private String serverStatus = null; 
77
        private CatalogCapabilities capabilities = null;
78
        
79
        /**
80
         * Cosntructor
81
         * @param sUri
82
         * The URI typed by the user
83
         * @param database
84
         * The selected database
85
         * @param driver
86
         * The selected catalog driver
87
         */
88
        public CatalogClient(String sUri, String database, ICatalogServiceDriver driver) {        
89
                CatalogClientRegister catRegister = CatalogClientRegister.getInstance();                
90
                catalogDriver = driver;
91
                if (catalogDriver == null){
92
                        serverStatus = "errorServerNotFound";
93
                }else{
94
                        try {
95
                                this.uri = URIUtils.createUri(sUri,
96
                                                catalogDriver.getDefaultSchema(),
97
                                                catalogDriver.getDefaultPort());
98
                        } catch (URISyntaxException e) {
99
                                serverStatus = "errorServerNotFound";
100
                        }
101
                }                
102
        } 
103

    
104
        /**
105
         * It make a getCapabilities operation
106
         * @return the service version
107
         * @throws ServerIsNotReadyException 
108
         */
109
        public CatalogCapabilities getCapabilities() throws ServerIsNotReadyException {        
110
                if (serverIsReady()){
111
                        if (getDriver().isProtocolSupported(getUri())) {
112
                                 capabilities = getDriver().getCapabilities(getUri());
113
                                 return capabilities;
114
                        }
115
                }
116
                return null;     
117
        } 
118

    
119
        /**
120
         * @return Node array with the retrieved records
121
         * @param query It contains the values to do the query (title="XXX",abstract="YYY",...)
122
         * @param firstRecord Number of the first record to retrieve
123
         */
124
        public GetRecordsReply getRecords(Query query, int firstRecord) {        
125
                return getDriver().getRecords(uri,query,firstRecord);
126
        } 
127

    
128
        /**
129
         * This method is used to create a new Query
130
         * 
131
         * 
132
         * @return 
133
         */
134
        public Query createNewQuery() {        
135
                return new Query();   
136
        }
137

    
138
        /**
139
         * @return Returns the catalogDriver.
140
         */
141
        public ICatalogServiceDriver getDriver() {        
142
                return catalogDriver;
143
        } 
144

    
145
        /**
146
         * @return the server protocol
147
         */
148
        public String getProtocol() {        
149
                return catalogDriver.getServiceName();
150
        } 
151

    
152
        /**
153
         * It tries if the server is ready 
154
         * @return boolean
155
         * true --> server is ready
156
         * false --> server is not ready
157
         */
158
        public boolean serverIsReady() throws ServerIsNotReadyException {        
159
                Socket sock = null;
160
                try {
161
                        sock = new Socket(getUri().getHost(),
162
                                        getUri().getPort());
163
                } catch (UnknownHostException e) {
164
                        throw new ServerIsNotReadyException(e);
165
                } catch (IOException e) {
166
                        throw new ServerIsNotReadyException(e);
167
                }
168
                return (sock != null);
169
        } 
170

    
171
        /**
172
         * @return Returns the serverStatus.
173
         */
174
        public String getServerStatus() {        
175
                return serverStatus;
176
        }
177

    
178
        /**
179
         * @return the server URI
180
         */
181
        public URI getUri() {
182
                return uri;
183
        }
184
        
185
        /**
186
         * @return Return the server URI like a String
187
         */
188
        public String getSUri() {
189
                return uri.toString();
190
        } 
191

    
192
}