Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / catalogClient / loaders / PostgisLayerLoader.java @ 12520

History | View | Annotate | Download (4.65 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 es.gva.cit.gvsig.catalogClient.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.i18n.Messages;
49

    
50
import com.hardcode.driverManager.DriverLoadException;
51
import com.iver.cit.gvsig.fmap.layers.FLayer;
52
import com.iver.utiles.extensionPoints.ExtensionPoint;
53
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
54

    
55
import es.gva.cit.catalogClient.schemas.discoverer.Resource;
56
import es.gva.cit.catalogClient.utils.Strings;
57

    
58

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

    
65
public class PostgisLayerLoader extends LayerLoader{
66
        
67
        
68
        
69
        public PostgisLayerLoader(Resource resource) {
70
                super(resource);
71
        }
72

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

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

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

    
165
        
166
}