Statistics
| Revision:

gvsig-catalog / org.gvsig.catalog / branches / org.gvsig.catalog-CSW2.0.2 / org.gvsig.catalog / org.gvsig.catalog.lib / src / main / java / org / gvsig / catalog / csw / messages / CSWAbstractMessages.java @ 55

History | View | Annotate | Download (11.5 KB)

1
package org.gvsig.catalog.csw.messages;
2

    
3
import org.apache.commons.httpclient.NameValuePair;
4
import org.gvsig.catalog.csw.drivers.CSWCapabilities;
5
import org.gvsig.catalog.csw.drivers.profiles.CSWAbstractProfile;
6
import org.gvsig.catalog.csw.parsers.CSWConstants;
7
import org.gvsig.catalog.languages.FilterEncoding;
8
import org.gvsig.catalog.querys.CatalogQuery;
9
import org.gvsig.catalog.querys.Search;
10
import org.gvsig.catalog.utils.CatalogConstants;
11
import org.gvsig.catalog.utils.Strings;
12
import org.gvsig.i18n.Messages;
13
import org.gvsig.utils.swing.jcomboServer.ServerData;
14

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

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

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

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

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

    
155
        /**
156
         * Returns a common reslut type if the getCapabilities doesn't have
157
         * one. Sometimes it works.
158
         * 
159
         * 
160
         * @return Just one String
161
         * @param resultType The array of result types parsed.
162
         */
163
        protected String getResultType(String[] resultType) {        
164
                if (resultType == null)
165
                        return "results";
166

    
167
                for (int i=0 ; i<resultType.length ; i++){
168

    
169
                }
170
                return resultType[0];
171
        } 
172

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

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

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

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

    
236
        /**
237
         * @return the constraint version
238
         */
239
        protected abstract String getContraintVersion();
240

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

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

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

    
322
                }        
323
                return filter.toString();
324
        }
325

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

    
338
                return true; 
339
        } 
340
}