Statistics
| Revision:

gvsig-publish / org.gvsig.publish / trunk / org.gvsig.publish / org.gvsig.publish.mapserver / org.gvsig.publish.lib.impl.mapserver / src / main / java / org / gvsig / publish / impl / mapserver / utils / MapProxy.java @ 102

History | View | Annotate | Download (22.6 KB)

1
package org.gvsig.publish.impl.mapserver.utils;
2

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.FileWriter;
8
import java.io.IOException;
9
import java.io.PrintWriter;
10
import java.util.ArrayList;
11
import java.util.Arrays;
12
import java.util.Hashtable;
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Map.Entry;
16

    
17
import org.apache.commons.lang3.StringUtils;
18
import org.gvsig.app.project.documents.view.ViewDocument;
19
import org.gvsig.fmap.dal.exception.DataException;
20
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
21
import org.gvsig.fmap.geom.primitive.Envelope;
22
import org.gvsig.fmap.geom.primitive.Point;
23
import org.gvsig.fmap.mapcontext.layers.FLayer;
24
import org.gvsig.fmap.mapcontext.layers.FLayers;
25
import org.gvsig.publish.PublishProperties;
26
import org.gvsig.publish.impl.mapproxy.Cache;
27
import org.gvsig.publish.impl.mapproxy.Globals;
28
import org.gvsig.publish.impl.mapproxy.GlobalsImage;
29
import org.gvsig.publish.impl.mapproxy.Grid;
30
import org.gvsig.publish.impl.mapproxy.Keyword;
31
import org.gvsig.publish.impl.mapproxy.Layer;
32
import org.gvsig.publish.impl.mapproxy.MapProxyConfiguration;
33
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationCaches;
34
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationGlobals;
35
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationGrids;
36
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationLayers;
37
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationServices;
38
import org.gvsig.publish.impl.mapproxy.MapProxyConfigurationSources;
39
import org.gvsig.publish.impl.mapproxy.Md;
40
import org.gvsig.publish.impl.mapproxy.Service;
41
import org.gvsig.publish.impl.mapproxy.Source;
42
import org.gvsig.publish.impl.mapserver.MapserverToDynObjectHelper;
43
import org.gvsig.raster.wms.app.wmsclient.layer.FLyrWMS;
44
import org.gvsig.raster.wms.io.RemoteWMSStyle;
45
import org.gvsig.raster.wms.io.WMSDataParameters;
46
import org.gvsig.raster.wms.io.WMSServerExplorer;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.dynobject.DynObject;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
import com.esotericsoftware.yamlbeans.YamlWriter;
53

    
54
public class MapProxy {
55

    
56
        private Logger logger = LoggerFactory.getLogger(MapProxy.class);
57
        public static final String MAPSERVER_MAPPROXY_PROPERTIES = "MapServer-mapproxy-properties";
58
        public static final String MAPSERVER_MAPPROXYPANEL_PROPERTIES = "MapServer-mapproxy-panel-properties";
59
        
60
        public static final String MAPSERVER_MAPPROXYLAYER_PROPERTIES = "MapServer-mapproxy-layer-properties";
61
        public static final String MAPSERVER_MAPPROXYLAYERPANEL_PROPERTIES = "MapServer-mapproxy-layer-panel-properties";
62
        
63
        private static final String MAPPROXY_TEMPFILE = "preconfiguration.xml";
64
        private static final String MAPPROXY_FILE = "mapproxy.yaml";
65
        private static final String MAPPROXY_PATH = "mapproxy";
66
        
67
        
68
        private PublishProperties properties;
69
        
70
        private File mapProxyFile;
71
        private boolean isEditedMapProxy;
72
        private MapProxyConfiguration mapproxy;
73

    
74
        public MapProxy(PublishProperties publishProperties) {
75
                this.properties = publishProperties;
76
        }
77
        
78
        
79
        public PublishProperties getPublishProperties() {
80
                return properties;
81
        }
82
        
83
        
84

    
85
        public static void createXML(File file, MapProxyConfiguration proxy){
86
                try {
87
                        YamlWriter writer = new YamlWriter(new FileWriter(file));
88
                        writer.getConfig().writeConfig.setAutoAnchor(false);
89
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
90
                        writer.getConfig().writeConfig.setCanonical(false);
91
                        writer.getConfig().setPropertyElementType(
92
                                        MapProxyConfigurationCaches.class, "caches", Cache.class);
93
                        MapProxyConfigurationCaches caches = new MapProxyConfigurationCaches();
94
                        caches.setCaches(proxy.getCaches());
95
                        writer.write(caches);
96
                        writer.write("gvsig_additional_caches");
97
                        
98
                        MapProxyConfigurationGlobals globals = new MapProxyConfigurationGlobals();
99
                        globals.setGlobals(proxy.getGlobals());
100
                        writer.getConfig().writeConfig.setAutoAnchor(false);
101
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
102
                        writer.getConfig().writeConfig.setCanonical(false);
103
                        writer.write(globals);
104
                        writer.write("gvsig_additional_globals");
105
                        
106
                        
107
                        writer.getConfig().setPropertyElementType(
108
                                        MapProxyConfigurationGrids.class, "grids", Grid.class);
109
                        MapProxyConfigurationGrids grids = new MapProxyConfigurationGrids();
110
                        grids.setGrids(proxy.getGrids());
111
                        writer.getConfig().writeConfig.setAutoAnchor(false);
112
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
113
                        writer.getConfig().writeConfig.setCanonical(false);
114
                        writer.write(grids);
115
                        writer.write("gvsig_additional_grids");
116
                        
117
                        
118
                        writer.getConfig().setPropertyElementType(
119
                                        MapProxyConfigurationLayers.class, "layers", Layer.class);
120
                        MapProxyConfigurationLayers layers = new MapProxyConfigurationLayers();
121
                        layers.setLayers(proxy.getLayers());
122
                        writer.getConfig().writeConfig.setAutoAnchor(false);
123
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
124
                        writer.getConfig().writeConfig.setCanonical(false);
125
                        writer.write(layers);
126
                        writer.write("gvsig_additional_layers");
127
                        
128
                        
129
                        writer.getConfig().setPropertyElementType(
130
                                        MapProxyConfigurationServices.class, "services", Service.class);
131
                        MapProxyConfigurationServices services = new MapProxyConfigurationServices();
132
                        services.setServices(proxy.getServices());
133
                        writer.getConfig().writeConfig.setAutoAnchor(false);
134
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
135
                        writer.getConfig().writeConfig.setCanonical(false);
136
                        writer.write(services);
137
                        writer.write("gvsig_additional_services");
138
                        
139
                        
140
                        writer.getConfig().setPropertyElementType(
141
                                        MapProxyConfigurationSources.class, "sources", Source.class);
142
                        MapProxyConfigurationSources sources = new MapProxyConfigurationSources();
143
                        sources.setSources(proxy.getSources());
144
                        writer.getConfig().writeConfig.setAutoAnchor(false);
145
                        writer.getConfig().writeConfig.setAlwaysWriteClassname(false);
146
                        writer.getConfig().writeConfig.setCanonical(false);
147
                        writer.write(sources);
148
                        writer.write("gvsig_additional_sources");
149
                        
150
                        
151
                        writer.close();
152
                        
153
                        removeHeader(file.getAbsolutePath());
154
                        
155
                        
156
                
157
                } catch (Exception e) {
158
                        // TODO Auto-generated catch block
159
                        e.printStackTrace();
160
                }
161

    
162
        }
163

    
164
        private static void removeHeader(String file) {
165
                try {
166
                        File inFile = new File(file);
167

    
168
                        if (!inFile.isFile()) {
169
                                //logger.warn("Parameter is not an existing file");
170
                                return;
171
                        }
172
                        File tempFile = new File(inFile.getAbsolutePath().replace(MAPPROXY_TEMPFILE, MAPPROXY_FILE));
173

    
174
                        BufferedReader br = new BufferedReader(new FileReader(file));
175
                        PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
176

    
177
                        String line = null;
178
                        boolean isFirst = true;
179
                        
180
                        while ((line = br.readLine()) != null) {
181
                                if(line.startsWith("---")){
182
                                        if(!line.startsWith("--- !")){
183
                                                line = line.replace("--- ", "#");
184
                                                pw.println(line);
185
                                                pw.flush();
186
                                        }
187
                                }else{if (!isFirst && 
188
                                                (!line.contains("{}") && !line.contains("[]")) ) {
189
                                        line = line.replace("'''", "'");
190
                                        pw.println(line);
191
                                        pw.flush();
192
                                }}
193
                                isFirst = false;
194
                        }
195
                        pw.close();
196
                        br.close();
197

    
198
                        if (!inFile.delete()) {
199
                                return;
200
                        } 
201
                        
202
                        File originalTemp = new File(file);
203
                        originalTemp.delete();
204

    
205
                        //if (!tempFile.renameTo(inFile))
206
                                //System.out.println("Could not rename file");
207

    
208
                }
209
                catch (FileNotFoundException ex) {
210
                        ex.printStackTrace();
211
                }
212
                catch (IOException ex) {
213
                        ex.printStackTrace();
214
                }
215
        }
216

    
217

    
218

    
219
        public void initializeMapProxy(MapProxyConfiguration mapp) {
220
                Grid grid = new Grid();
221
                grid.setBase("GLOBAL_MERCATOR");
222
                grid.setOrigin("ul");
223
//                Integer[] size = new Integer[]{512,512};
224
//                grid.setTile_size(size);
225
                mapp.getGrids().put("spherical_mercator_grid", grid);
226
                
227
                Grid grid1 = new Grid();
228
                grid1.setBase("GLOBAL_GEODETIC");
229
                grid1.setRes_factor("\'sqrt2\'");
230
                mapp.getGrids().put("global_geodetic_sqrt2", grid1);
231
                
232
                mapp.getServices().put("demo", null);
233
                mapp.getServices().put("kml", null);
234
                mapp.getServices().put("tms", null);
235
                mapp.getServices().put("wms", null);
236
                mapp.getServices().put("wmts", null);
237
                
238
                
239
                Layer layer1 = new Layer();
240
                layer1.setName("google_street");
241
                layer1.setTitle("Google street layer");
242
                layer1.getSources().add("google_street");
243
                mapp.getLayers().add(layer1);
244

    
245
                Layer layer2 = new Layer();
246
                layer2.setName("google_satellite");
247
                layer2.setTitle("Google satellite layer");
248
                layer2.getSources().add("google_satellite");
249
                mapp.getLayers().add(layer2);
250
                
251
                Layer layer3 = new Layer();
252
                layer3.setName("mapquest");
253
                layer3.setTitle("MapQuest");
254
                layer3.getSources().add("mapquest");
255
                mapp.getLayers().add(layer3);
256
                
257
                Layer layer4 = new Layer();
258
                layer4.setName("bing_street");
259
                layer4.setTitle("Bing street layer");
260
                layer4.getSources().add("bing_street");
261
                mapp.getLayers().add(layer4);
262
                
263
                Layer layer5 = new Layer();
264
                layer5.setName("bing_satellite");
265
                layer5.setTitle("Bing satellite layer");
266
                layer5.getSources().add("bing_satellite");
267
                mapp.getLayers().add(layer5);
268
                
269
                Globals globals = new Globals();
270
                GlobalsImage image = new GlobalsImage();
271
                image.setResampling_method("nearest");
272
                mapp.setGlobals(globals);
273
                
274
                Source source1 = new Source();
275
                source1.setType("tile");
276
                source1.setUrl("http://mt0.google.com/vt/lyrs=m@0&hl=en&x=%(x)s&y=%(y)s&z=%(z)s&src=apiv2");
277
                source1.setGrid("spherical_mercator_grid");
278
                mapp.getSources().put("google_street", source1);
279
                
280
                Source source2 = new Source();
281
                source2.setType("tile");
282
                source2.setUrl("http://khm0.google.co.ve/kh?v=137&hl=en-US&x=%(x)s&y=%(y)s&z=%(z)s&src=apiv2");
283
                source2.setGrid("spherical_mercator_grid");
284
                mapp.getSources().put("google_satellite", source2);
285
                
286
                Source source3 = new Source();
287
                source3.setType("tile");
288
                source3.setUrl("http://otile2.mqcdn.com/tiles/1.0.0/osm/%(z)s/%(x)s/%(y)s.png");
289
                source3.setGrid("spherical_mercator_grid");
290
                mapp.getSources().put("mapquest", source3);
291
                
292
                Source source4 = new Source();
293
                source4.setType("tile");
294
                source4.setUrl("http://ecn.t2.tiles.virtualearth.net/tiles/r[q]?g=761&mkt=en-us&shading=hill");
295
                source4.setGrid("spherical_mercator_grid");
296
                mapp.getSources().put("bing_street", source4);
297
                
298
                Source source5 = new Source();
299
                source5.setType("tile");
300
                source5.setUrl("http://ecn.t3.tiles.virtualearth.net/tiles/h[q]?g=1595&mkt={culture}");
301
                source5.setGrid("spherical_mercator_grid");
302
                mapp.getSources().put("bing_satellite", source5);
303
                
304
                Cache cache1 = new Cache();
305
                cache1.getGrids().add("spherical_mercator_grid");
306
                cache1.getSources().add("google_street");
307
                mapp.getCaches().put("google_street", cache1);
308
                
309
                Cache cache2 = new Cache();
310
                cache2.getGrids().add("spherical_mercator_grid");
311
                cache2.getSources().add("google_satellite");
312
                mapp.getCaches().put("google_satellite", cache2);
313
                
314
                Cache cache3 = new Cache();
315
                cache3.getGrids().add("spherical_mercator_grid");
316
                cache3.getSources().add("mapquest");
317
                mapp.getCaches().put("mapquest", cache3);
318
                
319
                Cache cache4 = new Cache();
320
                cache4.getGrids().add("spherical_mercator_grid");
321
                cache4.getSources().add("bing_street");
322
                mapp.getCaches().put("bing_street", cache4);
323
                
324
                Cache cache5 = new Cache();
325
                cache5.getGrids().add("spherical_mercator_grid");
326
                cache5.getSources().add("bing_satellite");
327
                mapp.getCaches().put("bing_satellite", cache5);
328
                
329
                if(!StringUtils.isBlank(getPublishProperties().getMapProxyBaseDir())){
330
                        mapp.getGlobals().getCache().setBase_dir(getPublishProperties().getMapProxyBaseDir());
331
                }
332
        }
333
        
334
        public MapProxyConfiguration publish(){
335
                return publish(true);
336
        }
337
        
338
        public MapProxyConfiguration publish(boolean createFile) {
339
                isEditedMapProxy = false;
340
                
341
                ViewDocument doc = MapServerUtils.getCurrentViewDocument();
342
                if (doc == null) {
343
                        return null;
344
                }
345
                
346
                mapproxy = getPublishedMapProxy(doc);
347
                
348
                FLayers layers = doc.getMapContext().getLayers();
349
                mapproxy = createMapProxyConfig(layers, createFile);
350
                
351
                if(getPublishProperties().getJoinToCurrent()){
352
                        //TODO  Juntar con uno existente
353

    
354
                }
355
                if(createFile){
356
                        MapProxy.createXML(getMapProxyFile(), mapproxy);
357
                }
358
                
359
                return mapproxy;
360
        }
361

    
362
        
363
        private MapProxyConfiguration createMapProxyConfig(FLayers layers, boolean createFile){
364
                MapserverToDynObjectHelper helper = new MapserverToDynObjectHelper();
365
                
366
                if(mapproxy != null){
367
                        for (int i = 0; i < layers.getLayersCount(); i++) {
368
                                FLayer layer = layers.getLayer(i);
369
                                if(layer instanceof FLayers){
370
                                        mapproxy = createMapProxyConfig((FLayers) layer, createFile);
371
                                }else{
372
                                        DynObject dynLayer = getPublishedMapProxyLayer(layer);
373
                                        if (layer instanceof FLyrWMS) {
374
                                                FLyrWMS flyrWMS = (FLyrWMS) layer;
375
                                                WMSDataParameters wmsparams = flyrWMS.getParameters();
376
                                                mapproxy.getLayers().add(
377
                                                                helper.createMapProxyLayer((DynObject) dynLayer.getDynValue("layer")));
378
                                                mapproxy.getSources().put(
379
                                                                wmsparams.getLayerQuery(), 
380
                                                                helper.createSource((DynObject) dynLayer.getDynValue("source")));
381
                                                mapproxy.getCaches().put(
382
                                                                wmsparams.getLayerQuery(), 
383
                                                                helper.createCache((DynObject) dynLayer.getDynValue("cache")));
384
                                        }
385
                                }
386
                        }
387
                        
388
                }
389
                return mapproxy;
390
        }
391
        
392
        private DynObject getLayerdata(FLayer layer) {
393
                MapserverToDynObjectHelper helper = new MapserverToDynObjectHelper();
394
                DynObject dynlayer = ToolsLocator.getDynObjectManager()
395
                                .createDynObject(helper.getMapProxyLayerDefinition());
396
                try{
397
                        if (layer instanceof FLyrWMS) {
398
                                FLyrWMS flyrWMS = (FLyrWMS) layer;
399
                                try {
400
                                        
401
                                        
402
                                        Layer mapProxyLayer = new Layer();
403
                                        Md md = mapProxyLayer.getMd();
404
                                        Source source = new Source();
405
                                        mapproxy.getServices().put("wms", new Service());
406
                                        mapproxy.getServices().put("wmts", new Service());
407
                                        
408
                                        WMSServerExplorer explorer = (WMSServerExplorer) flyrWMS.getDataStore()
409
                                                        .getExplorer();
410
//                                        List<String> versions = mapproxy.getServices().get("wms").getVersions();
411
//                                        if(!versions.contains(explorer.getVersion())){
412
//                                                versions.add(explorer.getVersion());
413
//                                        }
414
                                                                        
415
                                        mapproxy.getServices().get("wms").setMd(new Md());
416
                                        mapproxy.getServices().get("wms").getMd().setTitle(explorer.getServiceTitle());
417
                                        Hashtable resources = explorer.getOnlineResources();
418
                                        if(!resources.isEmpty() && resources.containsKey("GetCapabilities")){
419
                                                mapproxy.getServices().get("wms").getMd().setOnline_resource((String) resources.get("GetCapabilities"));
420
                                        }
421
                                        mapproxy.getServices().get("wms").getMd().setAbstract(explorer.getAbstract());
422
                                        
423
                                        
424
                                        source.getWms_opts().setVersion(explorer.getVersion());
425
                                        source.getWms_opts().setSupported_formats(Arrays.asList(explorer.getFormats()));
426
                                        mapproxy.getServices().get("wms").setImage_formats(
427
                                                        new ArrayList<String>(Arrays.asList(explorer.getFormats())));
428
                                        //String Title = explorer.getTitle();
429
                                        WMSDataParameters wmsparams = flyrWMS.getParameters();
430
                                        
431
                                        mapProxyLayer.setName(wmsparams.getLayerQuery());        
432
                                        mapProxyLayer.setTitle(wmsparams.getLayerQuery());
433
                                        
434
                                        md.setTitle(wmsparams.getLayerQuery());
435
                                        md.setAbstract(wmsparams.getLayerQuery());
436
                                        
437
                                        source.setType("wms");
438
                                        source.setConcurrent_requests(4);
439
                                        source.getReq().setUrl(wmsparams.getURI());
440
                                        source.getReq().setTransparent(true);
441
                                        source.getReq().setLayers(wmsparams.getLayerQuery());
442
                                        source.getReq().setFormat(wmsparams.getFormat());
443
                                        source.getWms_opts().setFeatureinfo(true);
444
                                        
445
                                        mapProxyLayer.getSources().add(wmsparams.getLayerQuery());
446
                                        
447
                                        if(layer.getProjection() != null){
448
                                                source.getCoverage().setSrs(layer.getProjection().getAbrev());
449
                                        }else{
450
                                                if(wmsparams.getSRSCode() != null){
451
                                                        source.getCoverage().setSrs(wmsparams.getSRSCode());
452
                                                }
453
                                        }
454
                                        if(layer.getFullEnvelope() != null){
455
                                                Envelope env = layer.getFullEnvelope();
456
                                                Point lower = env.getLowerCorner();
457
                                                Point upper = env.getUpperCorner();
458
                                                Double[] bbox = new Double[]{
459
                                                                lower.getX(), lower.getY(), 
460
                                                                upper.getX(), upper.getY()
461
                                                };
462
                                                 source.getCoverage().setBbox(bbox);
463
                                        }
464

    
465
                                        if (wmsparams.getStyles() != null) {
466
                                                Iterator it4 = wmsparams.getStyles().iterator();
467
                                                while (it4.hasNext()) {
468
                                                        RemoteWMSStyle wmsStyle = (RemoteWMSStyle) it4.next();
469
                                                        String style = wmsStyle.getName();
470
                                                        if(wmsStyle.getParent() != null){
471
                                                                md.setTitle(wmsStyle.getParent().getTitle());
472
                                                                md.setAbstract(wmsStyle.getParent().getAbstract());
473
                                                                List<String> srss = new ArrayList<String>();
474
                                                                srss.add("EPSG:3857");
475
                                                                srss.add("EPSG:900913");
476
                                                                srss.add("EPSG:4326");
477
                                                                srss.add("EPSG:4189");
478
                                                                if(srss != null){
479
                                                                        mapproxy.getServices().get("wms").setSrs(srss);
480
                                                                        mapproxy.getServices().get("wmts").setSrs(srss);
481
                                                                        source.getWms_opts().setSupported_srs(srss);
482
                                                                }
483
                                                                List<String> keywords = wmsStyle.getParent().getKeywords();
484
                                                                if(keywords != null){
485
                                                                        Keyword keyw = new Keyword();
486
                                                                        keyw.setKeywords(keywords);
487
                                                                        mapProxyLayer.getMd().getKeyword_list().add(keyw);
488
                                                                }
489
                                                        }
490
                                                        if(wmsStyle.getParent() != null){
491
                                                                List<RemoteWMSStyle> children = wmsStyle.getParent().getStyles();
492
                                                                if(!children.isEmpty() && !StringUtils.isBlank(wmsStyle.getHref())){
493
                                                                        source.getWms_opts().setLegendurl(children.get(0).getHref());
494
                                                                }
495
                                                        }
496
                                                }
497
                                        }
498
                                        
499
                                        mapProxyLayer.setMd(md);
500
                                        //mapproxy.getLayers().add(mapProxyLayer);
501
                                        //mapproxy.getSources().put(wmsparams.getLayerQuery(), source);
502
                                        if(!mapproxy.getGrids().isEmpty()){
503
                                                Cache cache = new Cache();
504
                                                Iterator it = mapproxy.getGrids().entrySet().iterator();
505
                                                while(it.hasNext()){
506
                                                        Entry<String, Grid> entry = (Entry<String, Grid>) it.next();
507
                                                        //if(entry.getValue().equals(mapproxy.getGrids().get(0))){
508
                                                                cache.getGrids().add(entry.getKey());
509
                                                                cache.getSources().add(wmsparams.getLayerQuery());
510
                                                                //mapproxy.getCaches().put(wmsparams.getLayerQuery(), cache);
511
                                                                break;
512
                                                        //}
513
                                                }
514
                                                dynlayer.setDynValue("cache", helper.createMapProxyCache(cache));
515
                                        }
516
                                        dynlayer.setDynValue("source", helper.createMapProxySource(source));
517
                                        dynlayer.setDynValue("layer", helper.createMapProxyLayerLayer(mapProxyLayer));
518
                                        
519
                                } catch (ValidateDataParametersException e) {
520
                                        // TODO Auto-generated catch block
521
                                        e.printStackTrace();
522
                                } catch (DataException e) {
523
                                        // TODO Auto-generated catch block
524
                                        e.printStackTrace();
525
                                }
526
                        }
527
                } catch(NoClassDefFoundError e1){
528
                        logger.info("No est? instalada la extensi?n de WMS");
529
                } catch (Exception e) {
530
                        // TODO Auto-generated catch block
531
                        e.printStackTrace();
532
                }
533
                
534
                return dynlayer;
535
        }
536

    
537

    
538
        public MapProxyConfiguration getPublishedMapProxy(ViewDocument viewDoc) {
539
                if(viewDoc.getProperty(MAPSERVER_MAPPROXYPANEL_PROPERTIES) != null && 
540
                                viewDoc.getProperty(MAPSERVER_MAPPROXYPANEL_PROPERTIES) instanceof Boolean){
541
                        boolean active = (Boolean) viewDoc.getProperty(MAPSERVER_MAPPROXYPANEL_PROPERTIES);
542
                        if(active){
543
                                Object prop = viewDoc.getProperty(MapProxy.MAPSERVER_MAPPROXY_PROPERTIES);
544
                                if (prop != null && prop instanceof DynObject){
545
                                        isEditedMapProxy = true;
546
                                        return (MapProxyConfiguration) prop;
547
                                }
548
                                
549
                        }
550
                }
551
                
552
                MapserverToDynObjectHelper helper = new MapserverToDynObjectHelper();
553
                mapproxy = new MapProxyConfiguration();
554
                initializeMapProxy(mapproxy);
555
                if(mapproxy != null){
556
                        //viewDoc.setProperty(MapProxy.MAPSERVER_MAPPROXY_PROPERTIES, mapproxy);
557
                }
558
                return mapproxy;
559
        }
560
        
561
        public DynObject getPublishedMapProxyLayer(FLayer layer) {
562
                if(layer.getProperty(MAPSERVER_MAPPROXYLAYERPANEL_PROPERTIES) != null && 
563
                                layer.getProperty(MAPSERVER_MAPPROXYLAYERPANEL_PROPERTIES) instanceof Boolean){
564
                        boolean active = (Boolean) layer.getProperty(MAPSERVER_MAPPROXYLAYERPANEL_PROPERTIES);
565
                        if(active){
566
                                Object prop = layer.getProperty(MapProxy.MAPSERVER_MAPPROXYLAYER_PROPERTIES);
567
                                if (prop != null && prop instanceof DynObject){
568
                                        isEditedMapProxy = true;
569
                                        return (DynObject) prop;
570
                                }
571
                                
572
                        }
573
                }
574
                
575
                MapserverToDynObjectHelper helper = new MapserverToDynObjectHelper();
576
                DynObject mapproxylayer = createMapProxyLayer(layer);
577
                //layer.setProperty(MapProxy.MAPSERVER_MAPPROXYLAYER_PROPERTIES, mapproxylayer);
578
                return mapproxylayer;
579
        }
580

    
581

    
582
        private File getMapProxyFile() {
583
                if (mapProxyFile == null) {
584
                        mapProxyFile = new File(getCorrectPath(getPublishProperties().getMapFile()
585
                                         + File.separator + MAPPROXY_PATH + File.separator + MAPPROXY_TEMPFILE));
586
                }
587
                return mapProxyFile;
588
        }
589
        
590
        
591
        public  String getCorrectPath(String absolutePath) {
592
                return getCorrectPath(absolutePath, false);
593
        }
594

    
595
        public String getCorrectPath(String fileFormat, boolean b) {
596
                if (getPublishProperties().isAbsolutePath()) {
597
                        if (fileFormat.startsWith(getPublishProperties().getLocalPath())) {
598
                                if (fileFormat.endsWith(File.separator)) {
599
                                        fileFormat.substring(0, fileFormat.length() - 2);
600
                                }
601
                                fileFormat = fileFormat
602
                                                .replace(getPublishProperties().getLocalPath(),
603
                                                                getPublishProperties().getRemotePath());
604
                        }
605
                } else {
606
                        String localPath = getPublishProperties().getLocalPath();
607
                        if (localPath != null
608
                                        && fileFormat.startsWith(getPublishProperties()
609
                                                        .getLocalPath())) {
610

    
611
                                if (!localPath.endsWith(File.separator)) {
612
                                        localPath = localPath + File.separator;
613
                                }
614
                                fileFormat = fileFormat.replace(localPath, "");
615
                                fileFormat = "." + File.separator + fileFormat;
616
                        }
617
                }
618
                return MapServerUtils.getCorrectFilePathFromUrl(fileFormat);
619
        }
620
        
621
        
622
        public boolean createProject(){
623
                boolean exit = true;
624
                if(!StringUtils.isBlank(getPublishProperties().getMapFile())){
625
                        mapProxyFile = getMapProxyFile();
626
                        File tempFile = null;
627
                        try {
628
                                tempFile = File.createTempFile("gvSIG-publish-XML-", null);
629
                        } catch (IOException e2) {
630
                                logger.error("[MapserverPublishService - publish()] Error creating the file: "
631
                                                + tempFile.getAbsolutePath() + " \n" + e2);
632
                                return false;
633
                        }
634
                        if (!mapProxyFile.exists()) {
635
                                try {
636
                                        mapProxyFile.getParentFile().mkdirs();
637
                                        mapProxyFile.createNewFile();
638
                                } catch (IOException e) {
639
                                        logger.error("[MapserverPublishService - publish()] Error creating the file: "
640
                                                        + mapProxyFile.getAbsolutePath() + " \n" + e);
641
                                        return false;
642
                                }
643
                        }else{
644
                                if (!mapProxyFile.canWrite()) {
645
                                        logger.error("[MapserverPublishService - publish()] Error writing in file: "
646
                                                        + mapProxyFile.getAbsolutePath());
647
                                        return false;
648
                                }
649
                        }                        
650
                }
651
                return exit;
652
        }
653

    
654

    
655
        public DynObject createMapProxyLayer(FLayer layer) {
656
                if(mapproxy == null){
657
                        ViewDocument viewDoc = MapServerUtils.getViewDocumentFromLayer(layer);
658
                        getPublishedMapProxy(viewDoc);
659
                }
660
                
661
                if(layer.getProperty(MAPSERVER_MAPPROXYLAYER_PROPERTIES) != null && 
662
                                layer.getProperty(MAPSERVER_MAPPROXYLAYER_PROPERTIES) instanceof DynObject){
663
                        return (DynObject) layer.getProperty(MAPSERVER_MAPPROXYLAYER_PROPERTIES);
664
                }
665
                
666
                return getLayerdata(layer);
667
        }
668

    
669
}