Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / catalog / csw / messages / CSWAbstractMessages.java @ 39586

History | View | Annotate | Download (11.8 KB)

1
package es.gva.cit.catalog.csw.messages;
2

    
3
import org.apache.commons.httpclient.NameValuePair;
4
import org.gvsig.i18n.Messages;
5

    
6
import com.iver.utiles.swing.jcomboServer.ServerData;
7

    
8
import es.gva.cit.catalog.csw.drivers.CSWCapabilities;
9
import es.gva.cit.catalog.csw.drivers.profiles.CSWAbstractProfile;
10
import es.gva.cit.catalog.csw.parsers.CSWConstants;
11
import es.gva.cit.catalog.languages.FilterEncoding;
12
import es.gva.cit.catalog.querys.CatalogQuery;
13
import es.gva.cit.catalog.querys.Search;
14
import es.gva.cit.catalog.utils.CatalogConstants;
15
import es.gva.cit.catalog.utils.Strings;
16

    
17
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
18
 *
19
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
20
 *
21
 * This program is free software; you can redistribute it and/or
22
 * modify it under the terms of the GNU General Public License
23
 * as published by the Free Software Foundation; either version 2
24
 * of the License, or (at your option) any later version.
25
 *
26
 * This program is distributed in the hope that it will be useful,
27
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29
 * GNU General Public License for more details.
30
 *
31
 * You should have received a copy of the GNU General Public License
32
 * along with this program; if not, write to the Free Software
33
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
34
 *
35
 * For more information, contact:
36
 *
37
 *  Generalitat Valenciana
38
 *   Conselleria d'Infraestructures i Transport
39
 *   Av. Blasco Ib??ez, 50
40
 *   46010 VALENCIA
41
 *   SPAIN
42
 *
43
 *      +34 963862235
44
 *   gvsig@gva.es
45
 *      www.gvsig.gva.es
46
 *
47
 *    or
48
 *
49
 *   IVER T.I. S.A
50
 *   Salamanca 50
51
 *   46005 Valencia
52
 *   Spain
53
 *
54
 *   +34 963163400
55
 *   dac@iver.es
56
 */
57
/* CVS MESSAGES:
58
 *
59
 * $Id$
60
 * $Log$
61
 *
62
 */
63
/**
64
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
65
 */
66
public abstract class CSWAbstractMessages {
67
        private CSWCapabilities capabilities = null;
68
        private CatalogQuery query = null;
69
        private CSWAbstractProfile profile = null;
70
        
71
        /**
72
         * @param profile
73
         * The profile to create the query
74
         */
75
        public CSWAbstractMessages(CSWAbstractProfile profile) {
76
                super();
77
                this.profile = profile;                
78
        }
79

    
80
        public static NameValuePair[] getHTTPGETCapabilities() {        
81
                NameValuePair nvp1 = new NameValuePair("request", CSWConstants.OPERATION_GETCAPABILITIES);
82
                NameValuePair nvp2 = new NameValuePair("service", ServerData.SERVER_SUBTYPE_CATALOG_CSW);
83
                NameValuePair nvp3 = new NameValuePair("acceptFormats","text/xml");
84
                return new NameValuePair[] { nvp1, nvp2, nvp3 };
85
        } 
86
        
87
        /**
88
         * The get capabilities operation started with lower case.
89
         * There are some servers that has this bug 
90
         * @return
91
         */
92
        public static NameValuePair[] getHTTPGETCapabilitiesLower() {        
93
                NameValuePair nvp1 = new NameValuePair("request", CSWConstants.OPERATION_GETCAPABILITIESToLOWER);
94
                NameValuePair nvp2 = new NameValuePair("service", ServerData.SERVER_SUBTYPE_CATALOG_CSW);
95
                NameValuePair nvp3 = new NameValuePair("acceptFormats","text/xml");
96
                return new NameValuePair[] { nvp1, nvp2, nvp3 };
97
        }
98

    
99
        public static NameValuePair[] getHTTPGETDescribeRecord(String version) {        
100
                NameValuePair nvp1 = new NameValuePair("request", CSWConstants.OPERATION_DESCRIBERECORD);
101
                NameValuePair nvp2 = new NameValuePair("service", ServerData.SERVER_SUBTYPE_CATALOG_CSW);
102
                NameValuePair nvp3 = new NameValuePair("version", version);
103
                return new NameValuePair[] { nvp1, nvp2, nvp3 };
104
        }         
105

    
106
        /**
107
         * Creates and returns the GetRecords request
108
         * @param query
109
         * @param firstRecord
110
         * @return
111
         */         
112
        public String getHTTPPostGetRecordsMessage(CSWCapabilities capabilities, CatalogQuery query, ServerData serverData, int firstRecord) {
113
                this.capabilities = capabilities;
114
                this.query = query;                
115
                profile.setServerData(serverData);
116
                StringBuffer buffer = new StringBuffer();
117
                buffer.append(createGetRecordsHeader(firstRecord));
118
                return buffer.toString();
119
        }
120

    
121
        /**
122
         * Create the GetRecords header. 
123
         * @param firstRecord
124
         * @return
125
         */
126
        protected String createGetRecordsHeader(int firstRecord){
127
                StringBuffer buffer = new StringBuffer();
128
                buffer.append(CatalogConstants.XML_HEADER_ENCODING); 
129
                buffer.append("<" + CSWConstants.CSW_NAMESPACE + 
130
                                ":" + CSWConstants.CSW_GET_RECORDS);
131
                buffer.append(" " + CSWConstants.CSW_SERVICE + "=\"" + ServerData.SERVER_SUBTYPE_CATALOG_CSW + "\" ");
132
                buffer.append(CSWConstants.CSW_VERSION + "=\"" + capabilities.getVersion() + "\" ");
133
                buffer.append(CatalogConstants.XML_NS + "=\"" + getCswNamespace() + "\" ");
134
                buffer.append(CatalogConstants.XML_NS + ":" + CSWConstants.CSW_NAMESPACE + 
135
                                "=\"" + getCswNamespace() + "\" ");
136
                buffer.append(CatalogConstants.XML_NS + ":" + CSWConstants.OGC_NAMESPACE + 
137
                                "=\"" + CSWConstants.OGC_NAMESPACE_URI + "\" ");
138
                buffer.append(CatalogConstants.XML_NS + ":" + CSWConstants.GML_NAMESPACE + 
139
                                "=\"" + CSWConstants.GML_NAMESPACE_URI + "\" ");
140
                buffer.append(CSWConstants.CSW_START_POSITION + "='" +        firstRecord  + "' ");
141
                buffer.append(CSWConstants.CSW_MAX_RECORDS + "='10' ");
142
                if (capabilities.getOutputFormat() != null){
143
                        buffer.append(CSWConstants.CSW_OUTPUTFORMAT + "=\"" + capabilities.getOutputFormat() + "\" ");
144
                }
145
                if (getOutputSchema(capabilities.getOutputSchema()) != null){
146
                        buffer.append(CSWConstants.CSW_OUTPUTSCHEMA + "=\"" + getOutputSchema(capabilities.getOutputSchema()) + "\" ");
147
                }else{
148
                        buffer.append(CSWConstants.CSW_OUTPUTSCHEMA + "=\"csw:IsoRecord\" ");
149
                }
150
                buffer.append("resultType=\"" +
151
                                getResultType(capabilities.getResultType()) + "\">");
152
                buffer.append(createGetRecordsQuery());
153
                buffer.append("</" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.OPERATION_GETRECORDS + ">");
154
                return buffer.toString();
155
        }
156

    
157
        /**
158
         * Returns a common result type if the getCapabilities doesn't have
159
         * one. If several result types are available, prefer "results" type.
160
         * Otherwise, return the first available one. If none is declared, return
161
         * "results" type, which sometimes works.
162
         * 
163
         * 
164
         * @return Just one String
165
         * @param resultType The array of result types parsed.
166
         */
167
        protected String getResultType(String[] resultType) {        
168
                if (resultType == null || resultType.length==0)
169
                        return "results";
170
                for (int i=0 ; i<resultType.length ; i++){
171
                        if (resultType[i].toLowerCase().equals("results")) {
172
                                return resultType[i];
173
                        }
174
                }
175
                return resultType[0];
176
        } 
177

    
178
        /**
179
         * Returns the OutputSchema. If the ISO19115 is supported,
180
         * we prefer this
181
         * @param OutputFormat The array of outputFormats parsed.
182
         * @return Just one String
183
         * @param outputSchemas 
184
         */
185
        protected String getOutputSchema(String[] outputSchemas) {        
186
                if (outputSchemas == null){
187
                        return null;
188
                }                    
189
                return outputSchemas[0];
190
        } 
191

    
192
        /**
193
         * @return the Query xml sub tree of the GetRecords
194
         * operation
195
         */
196
        protected String createGetRecordsQuery(){
197
                StringBuffer buffer = new StringBuffer();
198
                buffer.append("<" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.QUERY + " ");
199
                if (capabilities.getTypeNames() != null){
200
                        buffer.append(CSWConstants.TYPENAMES + "=\"" +
201
                                        Strings.getBlankSeparated(capabilities.getTypeNames()) + "\"");
202
                }
203
                buffer.append(">");
204
                buffer.append(getElementSetNameLabel(query.getService()));
205
                buffer.append(createGetRecordsConstraint());
206
                buffer.append("</" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.QUERY + ">");
207
                return buffer.toString();
208
        }
209

    
210
        /**
211
         * Create and return the ElementName and the ElementSetName tags
212
         * @param searchType
213
         * @return
214
         */
215
        protected String getElementSetNameLabel(Search searchType) {        
216
                StringBuffer buffer = new StringBuffer();
217
                buffer.append("<" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.ELEMENTSETNAME + ">");
218
                buffer.append(CSWConstants.FULL);
219
                buffer.append("</" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.ELEMENTSETNAME + ">");
220
                if (profile.getElementName() != null){
221
                        buffer.append("<" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.ELEMENTNAME + ">");
222
                        buffer.append(profile.getElementName());
223
                        buffer.append("</" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.ELEMENTNAME + ">");
224
                }
225
                return buffer.toString();        
226
        }         
227

    
228
        /**
229
         * @return the Constarint xml sub tree of the GetRecords
230
         * operation
231
         */
232
        protected String createGetRecordsConstraint(){
233
                StringBuffer buffer = new StringBuffer();
234
                buffer.append("<" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.CONSTRAINT);
235
                buffer.append(" " + CSWConstants.VERSION + "='" + getContraintVersion() + "'>");
236
                buffer.append(createFilterEncoding());
237
                buffer.append("</" + CSWConstants.CSW_NAMESPACE + ":" + CSWConstants.CONSTRAINT + ">");
238
                return buffer.toString();
239
        }        
240

    
241
        /**
242
         * @return the constraint version
243
         */
244
        protected abstract String getContraintVersion();
245

    
246
        /**
247
         * Creates the filter encoding. It can be
248
         * overridden by the children classes. 
249
         * @return
250
         * The filter encoding to use
251
         */
252
        protected FilterEncoding getFilterEncoding(){
253
                return new FilterEncoding();
254
        }
255
        
256
        /**
257
         * Creates the filter encoding query
258
         * @param query
259
         * @return
260
         */
261
        protected String createFilterEncoding(){                
262
                FilterEncoding filter = getFilterEncoding();
263
                if (query.getTitle() != null){
264
                        filter.addClauses(profile.getTitle(),
265
                                        query.getTitle(),
266
                                        query.getTitleFilter(),                                        
267
                                        FilterEncoding.PROPERTY_IS_LIKE, 
268
                                        FilterEncoding.TYPE_LITERAL,
269
                                        FilterEncoding.AND);
270
                }
271
                if (query.isMinimized()){
272
                        if (query.getAbstract() != null) {
273
                                filter.addClauses(profile.getAbstract(), 
274
                                                query.getAbstract(),
275
                                                FilterEncoding.ANY_WORDS,
276
                                                FilterEncoding.PROPERTY_IS_LIKE,
277
                                                FilterEncoding.TYPE_LITERAL,
278
                                                FilterEncoding.OR);
279
                        }
280
                }else{
281
                        if (query.getAbstract() != null) {
282
                                filter.addClauses(profile.getAbstract(), 
283
                                                query.getAbstract(),
284
                                                FilterEncoding.ANY_WORDS);
285
                        }
286
                        if (query.getThemeKey() != null) {
287
                                filter.addClauses(profile.getKeywords(), query.getThemeKey(),
288
                                                FilterEncoding.ANY_WORDS);
289

    
290
                        }
291
                        if ((query.getTopic() != null) && (!query.getTopic().equals(Messages.getText("cathegoryAny")))) {
292
                                filter.addClauses(profile.getTopic(), profile.getTopicValue(query.getTopic()),
293
                                                FilterEncoding.EXACT_WORDS);
294
                        }
295
                        if (query.getScale() != null) {
296
                                filter.addClauses(profile.getScale(), query.getScale(),
297
                                                FilterEncoding.EXACT_WORDS);
298
                        }
299
                        if (query.getProvider() != null) {
300
                                filter.addClauses(profile.getProvider(), 
301
                                                filter.getWildCard() + query.getProvider() + filter.getWildCard(),
302
                                                FilterEncoding.EXACT_WORDS,
303
                                                FilterEncoding.PROPERTY_IS_EQUALS_TO,
304
                                                FilterEncoding.TYPE_LITERAL,
305
                                                FilterEncoding.AND);                                                
306
                        }
307

    
308
                        if (query.getDateFrom() != null) {
309
                                filter.addClauses(profile.getDateFrom(), query.getDateFrom(), 
310
                                                FilterEncoding.EXACT_WORDS,
311
                                                FilterEncoding.PROPERTY_IS_GREATER_THAN,
312
                                                FilterEncoding.TYPE_LITERAL,
313
                                                FilterEncoding.AND);                                                                
314
                        }
315
                        if (query.getDateTo() != null) {
316
                                filter.addClauses(profile.getDateTo(), query.getDateTo(),
317
                                                FilterEncoding.EXACT_WORDS,
318
                                                FilterEncoding.PROPERTY_IS_LESS_THAN,
319
                                                FilterEncoding.TYPE_LITERAL,
320
                                                FilterEncoding.AND);                                        
321
                        }
322
                }
323
                if ((query.getCoordenates() != null) && (query.isCoordinatesClicked())){
324
                        filter.addBoundingBox(query.getCoordenates(), profile.getCoordinates(),
325
                                        getCoordinatesOption(query.getCoordenatesFilter()));
326

    
327
                }        
328
                return filter.toString();
329
        }
330

    
331
        /**
332
         * This function returns true only when the user has choosen the
333
         * "Fully Outside Of" of the coordinates option.
334
         * @param coordinatesOption 
335
         * @return
336
         */
337
        protected boolean getCoordinatesOption(String coordinatesOption) {        
338
                if ((coordinatesOption.equals(Messages.getText("coordinatesEqual"))) ||
339
                                (coordinatesOption.equals(Messages.getText("coordinatesContains"))) ||
340
                                (coordinatesOption.equals(Messages.getText("coordinatesEnclose"))))
341
                        return false;
342

    
343
                return true; 
344
        }
345
        
346
        protected String getCswNamespace() {
347
                return CSWConstants.CSW_NAMESPACE_URI;
348
        }
349
}