1 package org.opensync.importexport;
2
3 import java.io.CharArrayWriter;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.StringReader;
9 import java.sql.Connection;
10 import java.sql.SQLException;
11 import java.sql.SQLWarning;
12 import java.sql.Statement;
13 import java.util.Hashtable;
14 import java.util.Vector;
15
16 import org.opensync.tools.ConnectionPool;
17 import org.opensync.tools.Element;
18 import org.xml.sax.Attributes;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.XMLReader;
22 import org.xml.sax.helpers.DefaultHandler;
23 import org.xml.sax.helpers.XMLReaderFactory;
24
25 public class ImportOfView extends DefaultHandler {
26
27 private Vector keysUpdate;
28 private Hashtable recordValues;
29 private View theme;
30 private CharArrayWriter contents;
31 private ConnectionPool pool;
32 private Connection conn;
33 private Statement stmt;
34 private boolean batchMode = false;
35 private StringBuffer exceptionsMsg = new StringBuffer();
36
37 private int nbExceptions;
38 private Vector exceptions;
39
40 private int nbInsert;
41 private int nbUpdate;
42
43 public String getExceptionMsg() {
44 return exceptionsMsg.toString();
45 }
46
47 public int getNbExceptions() {
48 return nbExceptions;
49 }
50
51 public String getException(int i) {
52 if(i < exceptions.size())
53 return (String)exceptions.get(i);
54 else return null;
55 }
56 /***
57 * Constructor
58 */
59 public ImportOfView(ConnectionPool _pool, View _view) throws SQLException,SQLWarning {
60 pool = _pool;
61 conn = null;
62 theme = _view;
63
64 keysUpdate = buildColumnsUpdate();
65
66 recordValues = new Hashtable();
67 nbInsert = 0;
68 nbUpdate = 0;
69 nbExceptions = 0;
70 exceptions = new Vector();
71 }
72
73 public ImportOfView(Connection _conn, Statement stmt, boolean batchMde, View _view) throws SQLException,SQLWarning {
74 pool = null;
75 conn = _conn;
76 this.stmt = stmt;
77 this.batchMode = batchMode;
78 theme = _view;
79
80 keysUpdate = buildColumnsUpdate();
81
82 recordValues = new Hashtable();
83 nbInsert = 0;
84 nbUpdate = 0;
85 nbExceptions = 0;
86 exceptions = new Vector();
87
88 }
89
90 private Vector buildColumnsUpdate () {
91 Vector vector;
92 Column v[];
93 int j,indexP,iMax,jMax,indexPMax;
94 boolean found ;
95 String keyW,keyWT,s1,visible;
96
97 j= 0;
98 vector = new Vector();
99
100 v = theme.getElements();
101 jMax = v.length;
102 for (j=0;j<jMax;j++) {
103 Column e = v[j];
104 keyW = e.getKeyWord();
105 if ( theme.getIdParentFile().equals(e.getFile()) && vector.indexOf(keyW) == -1 ) vector.addElement(keyW);
106 }
107 return vector;
108 }
109
110
111 public void processStringXml(File file)
112 throws SAXException,FileNotFoundException,IOException{
113
114 exceptionsMsg = new StringBuffer();
115 contents = new CharArrayWriter();
116 XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
117 xr.setContentHandler( this );
118 xr.parse( new InputSource(new FileInputStream(file)));
119 }
120
121
122 public void processStringXml(String xml)
123 throws SAXException,FileNotFoundException,IOException{
124
125 exceptionsMsg = new StringBuffer();
126 contents = new CharArrayWriter();
127 XMLReader xr = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
128 xr.setContentHandler( this );
129 xr.parse( new InputSource(new StringReader(xml)));
130 }
131
132 public void startDocument(String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
133 this.exceptionsMsg = new StringBuffer();
134 }
135
136 public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
137
138 contents.reset();
139 Element currentRefColumn;
140
141 if (qName.equals("ROW")) {
142 if (recordValues!=null && !recordValues.isEmpty()) recordValues.clear();
143 }
144 }
145
146 public void endElement( String namespaceURI, String localName, String qName ) throws SAXException {
147 if (qName.equals("ROW")) {
148
149 Update update = null;
150 Insert insert;
151 boolean okInsert=true;
152 try {
153 if (theme.getMode().equals("both") || theme.getMode().equals("updateonly")) {
154 if (pool != null) update = new Update(pool,theme,recordValues);
155 else if (conn != null) update = new Update(conn, stmt, batchMode, theme,recordValues);
156 okInsert = update.getInsertOK();
157 if(update != null && !okInsert ) nbUpdate ++;
158 }
159 }
160 catch (SQLWarning wx) {
161 throw new SAXException(wx);
162
163 }
164 catch (SQLException ex) {
165 throw new SAXException(ex);
166
167 }
168
169
170 if ( okInsert && ( theme.getMode().equals("both") || theme.getMode().equals("insertonly") ) ) {
171
172 try {
173 if (pool != null) {
174 insert = new Insert(pool, theme, recordValues);
175 nbInsert++;
176 }
177 else if (conn != null) {
178 insert = new Insert(conn,theme,recordValues, batchMode);
179 nbInsert++;
180 }
181
182
183 }
184 catch (SQLWarning wx) {
185
186 this.exceptionsMsg.append("\n"+wx.getLocalizedMessage()+"\n");
187 nbExceptions ++;
188 exceptions.add(wx.getLocalizedMessage());
189 }
190 catch (SQLException ex) {
191
192 this.exceptionsMsg.append("\n"+ex.getLocalizedMessage()+"\n");
193 nbExceptions ++;
194 exceptions.add(ex.getLocalizedMessage());
195 }
196
197 }
198 }
199
200 if (!qName.equals("ROW") && !qName.equals("DATA")) {
201 recordValues.put(qName,contents.toString());
202 }
203
204 }
205
206 public void characters( char[] ch, int start, int length ) throws SAXException {
207 contents.write( ch, start, length );
208 }
209
210 public int getNbUpdate() {
211 return nbUpdate;
212 }
213 public int getNbInsert() {
214 return nbInsert;
215 }
216
217 }