~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to local/in/dhis-web-dashboard/src/main/java/org/hisp/dhis/dashboard/util/DBConnection.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.dashboard.util;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.io.BufferedReader;
 
31
import java.io.File;
 
32
import java.io.FileNotFoundException;
 
33
import java.io.FileReader;
 
34
import java.io.IOException;
 
35
import java.sql.Connection;
 
36
import java.sql.DriverManager;
 
37
import java.util.ArrayList;
 
38
import java.util.List;
 
39
 
 
40
import javax.xml.parsers.DocumentBuilder;
 
41
import javax.xml.parsers.DocumentBuilderFactory;
 
42
 
 
43
import org.w3c.dom.Document;
 
44
import org.w3c.dom.Element;
 
45
import org.w3c.dom.Node;
 
46
import org.w3c.dom.NodeList;
 
47
import org.xml.sax.SAXException;
 
48
import org.xml.sax.SAXParseException;
 
49
 
 
50
public class DBConnection
 
51
{
 
52
 
 
53
    Connection con = null;
 
54
 
 
55
    String dbConnectionXMLFileName = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator
 
56
        + "db" + File.separator + "DBConnections.xml";
 
57
 
 
58
    /*
 
59
     * To retrieve the db details from xml file
 
60
     */
 
61
    public List getDBDeatilsFromXML()
 
62
    {
 
63
        List<String> li = null;
 
64
        try
 
65
        {
 
66
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
 
67
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
 
68
            Document doc = docBuilder.parse( new File( dbConnectionXMLFileName ) );
 
69
 
 
70
            NodeList listOfDBConnections = doc.getElementsByTagName( "db-connection" );
 
71
 
 
72
            Node dbConnectionsNode = listOfDBConnections.item( 0 );
 
73
            li = new ArrayList<String>();
 
74
            if ( dbConnectionsNode.getNodeType() == Node.ELEMENT_NODE )
 
75
            {
 
76
                Element dbConnectionElement = (Element) dbConnectionsNode;
 
77
 
 
78
                NodeList dbUserNameList = dbConnectionElement.getElementsByTagName( "uname" );
 
79
                Element dbUserNameElement = (Element) dbUserNameList.item( 0 );
 
80
                NodeList textDBUNList = dbUserNameElement.getChildNodes();
 
81
                li.add( 0, ( textDBUNList.item( 0 )).getNodeValue().trim() );
 
82
 
 
83
                NodeList dbUserPwdList = dbConnectionElement.getElementsByTagName( "upwd" );
 
84
                Element dbUserPwdElement = (Element) dbUserPwdList.item( 0 );
 
85
                NodeList textDUPwdList = dbUserPwdElement.getChildNodes();
 
86
                li.add( 1, ( textDUPwdList.item( 0 )).getNodeValue().trim() );
 
87
 
 
88
                NodeList dbURLList = dbConnectionElement.getElementsByTagName( "dburl" );
 
89
                Element dbURLElement = (Element) dbURLList.item( 0 );
 
90
                NodeList textDBURLList = dbURLElement.getChildNodes();
 
91
                li.add( 2, ( textDBURLList.item( 0 )).getNodeValue().trim() );
 
92
 
 
93
                NodeList dbStateNameList = dbConnectionElement.getElementsByTagName( "state-name" );
 
94
                Element dbStateNameElement = (Element) dbStateNameList.item( 0 );
 
95
                NodeList textDBSNameList = dbStateNameElement.getChildNodes();
 
96
                li.add( 3, ( textDBSNameList.item( 0 )).getNodeValue().trim() );
 
97
            }// end of if clause
 
98
        }// try block end
 
99
        catch ( SAXParseException err )
 
100
        {
 
101
            System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() );
 
102
            System.out.println( " " + err.getMessage() );
 
103
        }
 
104
        catch ( SAXException e )
 
105
        {
 
106
            Exception x = e.getException();
 
107
            ((x == null) ? e : x).printStackTrace();
 
108
        }
 
109
        catch ( Throwable t )
 
110
        {
 
111
            t.printStackTrace();
 
112
        }
 
113
        return li;
 
114
    }
 
115
 
 
116
    public List getDBDeatilsFromHibernate()
 
117
    {
 
118
        String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator
 
119
            + "hibernate.properties";
 
120
        FileReader fr = null;
 
121
        BufferedReader input = null;
 
122
 
 
123
        List<String> li = new ArrayList<String>();
 
124
        try
 
125
        {
 
126
            fr = new FileReader( path );
 
127
            input = new BufferedReader( fr );
 
128
 
 
129
            String s = input.readLine();
 
130
            while ( s instanceof String )
 
131
            {
 
132
                if ( s.contains( "jdbc:mysql:" ) )
 
133
                {
 
134
                    /*
 
135
                     * String tempS2[] = s.split("/"); String dbName =
 
136
                     * "jdbc:mysql://localhost/"+tempS2[tempS2.length-1].substring(0,
 
137
                     * tempS2[tempS2.length-1].indexOf('?'));
 
138
                     * System.out.println("DBName : "+dbName); li.add(0,dbName);
 
139
                     */
 
140
                    String tempS2[] = s.split( "=" );
 
141
                    String dbName = tempS2[1].substring( 0, tempS2[1].indexOf( '?' ) ).trim();
 
142
                    //System.out.println( "DBName : " + dbName );
 
143
                    li.add( 0, dbName );
 
144
                }
 
145
                if ( s.contains( "hibernate.connection.username" ) )
 
146
                {
 
147
                    String tempS2[] = s.split( "=" );
 
148
                    //System.out.println( "UserName : " + tempS2[tempS2.length - 1].trim() );
 
149
                    li.add( 1, tempS2[tempS2.length - 1].trim() );
 
150
                }
 
151
                if ( s.contains( "hibernate.connection.password" ) )
 
152
                {
 
153
                    String tempS2[] = s.split( "=" );
 
154
                    //System.out.println( "PassWord : " + tempS2[tempS2.length - 1].trim() );
 
155
                    li.add( 2, tempS2[tempS2.length - 1].trim() );
 
156
                }
 
157
                // System.out.println(s);
 
158
                s = input.readLine();
 
159
            }// while loop end
 
160
        }
 
161
        catch ( FileNotFoundException e )
 
162
        {
 
163
            System.out.println( e.getMessage() );
 
164
        }
 
165
        catch ( IOException e )
 
166
        {
 
167
            System.out.println( e.getMessage() );
 
168
        }
 
169
        finally
 
170
        {
 
171
            try
 
172
            {
 
173
                if ( fr != null )
 
174
                    fr.close();
 
175
                if ( input != null )
 
176
                    input.close();
 
177
            }
 
178
            catch ( Exception e )
 
179
            {
 
180
                System.out.println( e.getMessage() );
 
181
            }
 
182
        }
 
183
 
 
184
        return li;
 
185
    }// getDBDeatilsFromHibernate end
 
186
 
 
187
    public Connection openConnection()
 
188
    {
 
189
 
 
190
        try
 
191
        {
 
192
 
 
193
            // To get From XML File
 
194
            // List li = (ArrayList)getDBDeatilsFromXML();
 
195
            // String userName = (String) li.get(0);
 
196
            // String userPass = (String) li.get(1);
 
197
            // String urlForConnection = (String) li.get(2);
 
198
 
 
199
            // To get From Hibernate.Properties File
 
200
            List li = (ArrayList) getDBDeatilsFromHibernate();
 
201
            String urlForConnection = (String) li.get( 0 );
 
202
            String userName = (String) li.get( 1 );
 
203
            String userPass = (String) li.get( 2 );
 
204
 
 
205
            // Direct DBConnection
 
206
            // String userName = "dhis";
 
207
            // String userPass = "";
 
208
            // String urlForConnection = "jdbc:mysql://localhost/jh_dhis2";
 
209
 
 
210
            Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
 
211
            con = DriverManager.getConnection( urlForConnection, userName, userPass );
 
212
        }
 
213
        catch ( Exception e )
 
214
        {
 
215
            System.out.println( "Exception while opening connection : " + e.getMessage() );
 
216
            return null;
 
217
        }
 
218
        return con;
 
219
    } // openConnection end
 
220
 
 
221
    public void closeConnection()
 
222
    {
 
223
        try
 
224
        {
 
225
        }
 
226
        finally
 
227
        {
 
228
            try
 
229
            {
 
230
                if ( con != null )
 
231
                    con.close();
 
232
            }
 
233
            catch ( Exception e )
 
234
            {
 
235
                System.out.println( e.getMessage() );
 
236
            }
 
237
        }
 
238
    } // closeConnection end
 
239
 
 
240
}