Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCatalogYNomenclator / src / es / gva / cit / gvsig / catalogClient / loaders / PostgisLayerLoader.java @ 10626

History | View | Annotate | Download (4.69 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.exceptions.layers.LoadLayerException;
52
import com.iver.cit.gvsig.fmap.layers.FLayer;
53
import com.iver.utiles.extensionPoints.ExtensionPoint;
54
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
55

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

    
59

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

    
66
public class PostgisLayerLoader extends LayerLoader{
67

    
68

    
69

    
70
        public PostgisLayerLoader(Resource resource) {
71
                super(resource);
72
        }
73

    
74
        /**
75
     * This function adds a Postgis Layer to gvSIG
76
     * @param jdbcUrl
77
            * JDBC url connection
78
            * @param table
79
            * Table to load
80
         * @throws LayerLoaderException
81
            */
82
           public void loadLayer() throws LoadLayerException {
83
               FLayer flayer;
84
               String jdbcUrl = getResource().getLinkage();
85
               String table = getResource().getName();
86
        flayer = createPostgisLayer(jdbcUrl, table);
87
        addLayerToView(flayer);
88
          }
89

    
90
    /**
91
            * It returns a Postgis Layer
92
            * @param jdbcUrl
93
            * JDBC url connection
94
            * @param table
95
            * Table to load
96
            * @return
97
     * @throws SQLException
98
     * @throws DriverLoadException
99
     * @throws ClassNotFoundException
100
     * @throws LayerLoaderException
101
            */
102
    private FLayer createPostgisLayer(String jdbcUrl, String table) throws  LoadLayerException  {
103
            ExtensionPoint extensionPoint = (ExtensionPoint)ExtensionPointsSingleton.getInstance().get("CatalogLayers");
104
                Map args = createParamsMap(jdbcUrl,table);
105
                String sLayer = getResource().getName();
106
                try {
107
                        return (FLayer)extensionPoint.create("POSTGIS", args  );
108
                } catch(Exception e) {
109
                        throw new LoadLayerException(sLayer,e);
110
                }
111
   }
112

    
113
    /**
114
     * Creates a Map with all the params
115
            * @param jdbcUrl
116
            * JDBC url connection
117
            * @param table
118
            * Table to load
119
     * @return
120
     * Map
121
     */
122
    private Map createParamsMap(String jdbcUrl, String table){
123
            StringTokenizer sti = new StringTokenizer(jdbcUrl,"?");
124
        String dbURL = sti.nextToken();
125
        String p = sti.nextToken();
126
        TreeMap map = separateParams(p);
127
        String user = (String) map.get((String) "USER");
128
                String pwd = (String) map.get((String) "PASSWORD");
129
        map = Strings.separateParams(table);
130
        map.put("USER",user);
131
        map.put("PASSWORD",pwd);
132
                map.put("WHERECLAUSE","");
133
                map.put("DBURL",dbURL);
134
                return map;
135
    }
136

    
137
    private TreeMap separateParams(String pairValues){
138
        TreeMap map = new TreeMap();
139
                String[] params = pairValues.split("&");
140
                for (int i = 0; i < params.length; i++) {
141
                        String[] nameValue = params[i].split("=");
142
                        map.put(nameValue[0].toUpperCase(), nameValue[1]);
143
                }
144
                return map;
145
    }
146

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

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

    
167

    
168
}