Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCatalog / src / org / gvsig / catalog / loaders / PostgisLayerLoader.java @ 29625

History | View | Annotate | Download (4.57 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package org.gvsig.catalog.loaders;
42

    
43
import java.sql.SQLException;
44
import java.util.Map;
45
import java.util.StringTokenizer;
46
import java.util.TreeMap;
47

    
48
import org.gvsig.catalog.schemas.Resource;
49
import org.gvsig.catalog.utils.Strings;
50
import org.gvsig.fmap.mapcontext.layers.FLayer;
51
import org.gvsig.i18n.Messages;
52
import org.gvsig.tools.extensionpoint.ExtensionPoint;
53
import org.gvsig.utils.extensionPointsOld.ExtensionPointsSingleton;
54

    
55

    
56

    
57
/**
58
 * This class is used to load a POSTGIS resource in gvSIG
59
 * 
60
 * @author Jorge Piera Llodra (piera_jor@gva.es)
61
 */
62

    
63
public class PostgisLayerLoader extends GvSigLayerLoader{
64
        
65
        
66
        
67
        public PostgisLayerLoader(Resource resource) {
68
                super(resource);
69
        }
70

    
71
        /**
72
     * This function adds a Postgis Layer to gvSIG 
73
     * @param jdbcUrl
74
            * JDBC url connection
75
            * @param table
76
            * Table to load
77
         * @throws LayerLoaderException 
78
            */        
79
           public void loadLayer() throws LayerLoaderException {
80
               FLayer flayer;
81
               String jdbcUrl = getResource().getLinkage();
82
               String table = getResource().getName();
83
        flayer = createPostgisLayer(jdbcUrl, table);
84
        addLayerToView(flayer);
85
          }
86
            
87
    /**
88
            * It returns a Postgis Layer
89
            * @param jdbcUrl
90
            * JDBC url connection
91
            * @param table
92
            * Table to load
93
            * @return
94
     * @throws SQLException
95
     * @throws DriverLoadException
96
     * @throws ClassNotFoundException
97
     * @throws LayerLoaderException 
98
            */
99
    private FLayer createPostgisLayer(String jdbcUrl, String table) throws  LayerLoaderException  {
100
            ExtensionPoint extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("CatalogLayers");
101
                Map args = createParamsMap(jdbcUrl,table);
102
                try {
103
                        return (FLayer)extensionPoint.create("POSTGIS", args  );
104
                } catch(Exception e) {
105
                        throw new LayerLoaderException(getErrorMessage(),getWindowMessage());
106
                }
107
   }
108
    
109
    /**
110
     * Creates a Map with all the params
111
            * @param jdbcUrl
112
            * JDBC url connection
113
            * @param table
114
            * Table to load
115
     * @return
116
     * Map
117
     */
118
    private Map createParamsMap(String jdbcUrl, String table){
119
            StringTokenizer sti = new StringTokenizer(jdbcUrl,"?");
120
        String dbURL = sti.nextToken();
121
        String p = sti.nextToken();
122
        TreeMap map = separateParams(p);
123
        String user = (String) map.get((String) "USER");
124
                String pwd = (String) map.get((String) "PASSWORD");
125
        map = Strings.separateParams(table);
126
        map.put("USER",user);
127
        map.put("PASSWORD",pwd);        
128
                map.put("WHERECLAUSE","");
129
                map.put("DBURL",dbURL);
130
                return map;        
131
    }
132
    
133
    private TreeMap separateParams(String pairValues){
134
        TreeMap map = new TreeMap(); 
135
                String[] params = pairValues.split("&");
136
                for (int i = 0; i < params.length; i++) {
137
                        String[] nameValue = params[i].split("=");
138
                        map.put(nameValue[0].toUpperCase(), nameValue[1]);
139
                }
140
                return map;
141
    }
142

    
143
    /*
144
     *  (non-Javadoc)
145
     * @see es.gva.cit.gvsig.catalogClient.loaders.LayerLoader#getErrorMessage()
146
     */
147
    protected String getErrorMessage() {
148
                return Messages.getText("postgisError") + ".\n" +
149
                                Messages.getText("server") + ": " + 
150
                                getResource().getLinkage() + "\n" +
151
                                Messages.getText("parameters") + ": " +
152
                                getResource().getName();
153
        }
154

    
155
    /*
156
     *  (non-Javadoc)
157
     * @see es.gva.cit.gvsig.catalogClient.loaders.LayerLoader#getWindowMessage()
158
     */
159
        protected String getWindowMessage() {
160
                return Messages.getText("postgisLoad");
161
        }
162

    
163
        
164
}