Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublish / src / org / gvsig / remoteservices / conf / geonetwork / test / DDF2postgres.java @ 10874

History | View | Annotate | Download (3.43 KB)

1

    
2
package org.gvsig.remoteservices.conf.geonetwork.test;
3

    
4
import java.io.BufferedReader;
5
import java.io.DataInputStream;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileNotFoundException;
9
import java.io.FileWriter;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.io.InputStreamReader;
13
import java.util.Vector;
14

    
15

    
16
public class DDF2postgres{
17
        
18
        File f=null;
19
        File sql=null;
20
        File f2=null;
21
        FileWriter write = null;
22
        File aux[]=null;
23
        String path=null;
24
        String squema="public";
25
        BufferedReader read= null;
26
        
27
        //squema --> squema of the ddf files (must belong same squema)
28
        //path   -->path to the ddf files
29
        DDF2postgres(String squema,String path){
30
                if(squema!=null){
31
                        this.squema=squema;
32
                }
33
                this.path=path;
34
                f=new File(path);
35
                sql=null;
36
                f2=null;
37
                aux=f.listFiles();
38
                int i;
39
                //created sql dir
40
                sql=new File(path+"/sql");
41
                sql.mkdir();
42
                File aux2[]=sql.listFiles();
43
                //force delete of existing .sql
44
                for(i=0;i<aux2.length;i++){
45
                        f2=new File(path+"/sql/"+aux2[i].getName());
46
                        f2.delete();
47
                }
48
        }
49
        
50
        //conver all ddf files in path to .sql format
51
        //all .sql files are created into path/sql subdir (if exist will be deleted, be carefull my friend)
52
        public void convert(){
53
                int i;
54
                for(i=0;i<aux.length;i++){
55
                        Vector data=new Vector();
56
                        String s3=null;
57
                        String s6=null;
58
                        int contd=0;
59
                        int contf=0;
60
                        sql=new File(path+"/"+"sql/"+aux[i].getName().split(".ddf")[0]+".sql");
61
                        String table=sql.getName().split(".sql")[0];
62
                        //System.out.println(table);
63
                        //System.out.println(sql.getName());
64
                        try{
65
                                read = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(aux[i]))));
66
                                write = new FileWriter(sql);
67
                                String s=read.readLine();
68
                                String s2[]=null;
69
                                if(squema.compareTo("public")==0){
70
                                        s3="INSERT INTO public."+table.toLowerCase()+"(";
71
                                }
72
                                else{
73
                                        s3="INSERT INTO "+squema+"."+table.toLowerCase()+"(";
74
                                }
75
                                String s4="";
76
                                String s5=null;
77
                                if(s!=null){
78
                                        while(s.compareTo("[FIELDS]")!=0){
79
                                                s=read.readLine();
80
                                        }
81
                                }
82
                                s=read.readLine();
83
                                while(s.compareTo("")!=0){
84
                                        s2=s.split(",");
85
                                        contd++;
86
                                        data.addElement(s2[0]);
87
                                        //System.out.println(s2[0]);
88
                                        s=read.readLine();
89
                                }
90
                                s4=s3;
91
                                int j;
92
                                for(j=0;j<data.size()-1;j++){
93
                                        s4=s4+data.elementAt(j)+",";
94
                                }
95
                                s4=s4+data.elementAt(data.size()-1)+") VALUES(";
96
                                while(s.compareTo("[DATA]")!=0){
97
                                        s=read.readLine();
98
                                }
99
                                s=read.readLine();
100
                                s6=s.replaceAll("\'"," ");
101
                                s5=s4;
102
                                while(s!=null){
103
                                        s2=s6.split("\t");
104
                                        contf=s2.length;
105
                                        int k;
106
                                        write.write(s5);
107
                                        for(k=0;k<s2.length-1;k++){
108
                                                if(s2[k].compareTo("")==0){
109
                                                        write.write("null,");
110
                                                        //System.out.print(s2[k]);
111
                                                }
112
                                                else{
113
                                                        write.write("\'"+s2[k]+"\',");
114
                                                        //System.out.print(s2[k]);
115
                                                }
116
                                        }
117
                                        write.write("\'"+s2[s2.length-1]+"\'");
118
                                        if(contd-contf>0){
119
                                                int l;
120
                                                write.write(",");
121
                                                for(l=0;l<(contd-contf)-1;l++){
122
                                                        write.write("null,");
123
                                                }
124
                                                write.write("null");
125
                                                
126
                                        }
127
                                        write.write(");\n");
128
                                        //System.out.println();
129
                                        s=read.readLine();
130
                                        if(s!=null){
131
                                                s6=s.replaceAll("\'"," ");
132
                                                if(s.compareTo("")==0){
133
                                                        s=read.readLine();
134
                                                        s6=s.replaceAll("\'"," ");
135
                                                }
136
                                        }
137
                                }
138
                                write.close();
139
                        }
140
                        catch(FileNotFoundException e1){
141
                                System.out.println("File not found.");
142
                        }
143
                        catch(IOException e){
144
                                System.out.println("I/O error.");
145
                        }
146
                }
147
        }
148
}