View Javadoc

1   package org.opensync.engine.server.adapter;
2   
3   import java.io.IOException;
4   import java.sql.Connection;
5   import java.sql.SQLException;
6   import java.sql.Statement;
7   
8   import org.opensync.engine.server.DefaultAdapter;
9   import org.opensync.engine.server.Log;
10  import org.opensync.engine.server.OpenSync;
11  import org.opensync.engine.server.Statistic;
12  import org.opensync.engine.server.View;
13  import org.opensync.engine.server.Window;
14  import org.opensync.engine.server.connector.BDConnector;
15  import org.opensync.importexport.ExportOfView;
16  import org.opensync.importexport.ImportOfView;
17  import org.opensync.importexport.ViewXml;
18  import org.opensync.tools.DbFile;
19  import org.opensync.tools.ThemesReference;
20  
21  /***
22   * This class represents the adpater to adapt database data source format to the OpenSync
23   * data format
24   *
25   * @version 1.0
26   * @author  SOFTMED
27   */
28  
29  public class BDAdapter extends DefaultAdapter{
30  
31    /***
32     * The url of the tables descriptor
33     *
34     */
35    //private String tablesDescriptor;
36    /***
37     * The url of the themes descriptor
38     *
39     */
40    //private String themesDescriptor;
41  
42    private Connection conn;
43  
44    private Statement stmt;
45  
46    private boolean batchMode;
47  
48    /***
49     * Construct the BDAdpater
50     *
51     *
52     */
53    public BDAdapter() throws SQLException {
54    }
55  
56    public void init() {
57      conn = null;
58      stmt = null;
59    }
60  
61    /***
62     * Set the tables descriptor
63     *
64     * @param tablesDescriptor the url of the tables descriptor
65     */
66    /*
67    public void setTablesDescriptor(String tablesDescriptor) {
68      this.tablesDescriptor = tablesDescriptor;
69    }
70    */
71    /***
72     * Get the tables descriptor
73     *
74     */
75    /*
76    public String getTablesDescriptor() {
77      return tablesDescriptor;
78    }
79    */
80    /***
81     * Set the themes descriptor
82     *
83     * @param themesDescriptor the url of the themes descriptor
84     */
85    /*
86    public void setThemesDescriptor(String themesDescriptor) {
87      this.themesDescriptor = themesDescriptor;
88    }
89    */
90    /***
91     * Get the themes descriptor
92     *
93     */
94    /*
95    public String getThemesDescriptor() {
96      return themesDescriptor;
97    }
98    */
99    /***
100    * Adapt the the data source format to the OpenSync
101    * data format
102    *
103    * @param xmlNotUsed
104    * @param view the view
105    * @exception Exception
106    */
107   synchronized public String adaptInputView(String xmlNotUsed,View view, Statistic stat, Window win)throws Exception{
108     String fileSeparator = System.getProperty("file.separator");
109     String configFolder = System.getProperty("opensync.configfolder");
110     configFolder = (configFolder == null ? "" : configFolder);
111 
112     String path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"table"+fileSeparator + tablesDescriptor,true);
113     DbFile dbfile = new DbFile(path);
114     ThemesReference themesref = new ThemesReference(dbfile);
115     path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"theme"+fileSeparator + themesDescriptor,true);
116     themesref.processThemeXml(path);
117     path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"query"+fileSeparator + view.getDescriptor() ,true);
118     ViewXml viewXml = new ViewXml(path,dbfile,themesref);
119 
120     ExportOfView querySelect = new ExportOfView(
121       ((BDConnector)connector).getConnectionPool(),
122       viewXml.getView(viewXml.getKeyWordOfView()),
123       null,
124       win.getStartline(),
125       win.getNumline()
126     );
127 
128     stat.query += querySelect.getNbResults();
129     win.setStartline(win.getStartline() + win.getNumline());
130 
131     if(win.getStartline() >= querySelect.getNbResultsIndex()) {
132       win.setStartline(-1);
133     }
134 
135     String xml = querySelect.getDataListXml();
136 
137     return xml;
138   }
139 
140   /***
141    * Adapt the the data OpenSync format to the
142    * data source format
143    *
144    * @param xml the xml view
145    * @param view the view
146    * @exception IOException
147    */
148   synchronized public String adaptOutputView(String xml,View view, Statistic stat)throws IOException{
149     ImportOfView importOfView = null;
150     try {
151       String fileSeparator = System.getProperty("file.separator");
152       String configFolder = System.getProperty("opensync.configfolder");
153       configFolder = (configFolder == null ? "" : configFolder);
154 
155       //if (stmt == null) stmt = getConn().createStatement();
156       batchMode = ((BDConnector)getConnector()).getConnectionPool().getBatchmode();
157       String path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"table"+fileSeparator + tablesDescriptor,true);
158       DbFile dbfile = new DbFile(path);
159       ThemesReference themesref = new ThemesReference(dbfile);
160       path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"theme"+fileSeparator + themesDescriptor,true);
161       themesref.processThemeXml(path);
162       path = OpenSync.getInstance().getFilePath("etc"+fileSeparator+configFolder+fileSeparator+"query"+fileSeparator + view.getDescriptor() ,true);
163       ViewXml viewXml = new ViewXml(path,dbfile,themesref);
164 
165       /*
166       // Autocommit version !
167       importOfView = new ImportOfView(
168         ((BDConnector)getConnector()).getConnectionPool(),
169         viewXml.getView(viewXml.getKeyWordOfView())
170       );
171       */
172 
173       importOfView = new ImportOfView(
174         getConn(),
175 	stmt,
176 	batchMode,
177         viewXml.getView(viewXml.getKeyWordOfView())
178       );
179 
180      importOfView.processStringXml(xml);
181      if ( (importOfView != null) && (importOfView.getExceptionMsg().length() > 0)) {
182        for(int i=0; i<importOfView.getNbExceptions(); i++) {
183          stat.error.add(importOfView.getException(i));
184        }
185 
186      }
187       // throw new IOException(importOfView.getExceptionMsg());
188       stat.insert += importOfView.getNbInsert();
189       stat.update += importOfView.getNbUpdate();
190 
191     }
192     catch (Exception ex) {
193       throw new IOException(Log.formatStackTrace(ex));
194     }
195     finally {
196       OpenSync openSync = OpenSync.getInstance();
197       if (openSync != null) {
198 	Log log = openSync.getLog();
199 	if ( (log != null) && (importOfView != null)) {
200 //	  log.debug(Log.ROOT, importOfView.getExceptionMsg());
201 	  log.info(Log.ROOT, importOfView.getExceptionMsg());
202 
203 	}
204       }
205     }
206 /*
207 
208     BufferedWriter bufferedWriter = null;
209     FileWriter fileWriter = null;
210     try{
211       String path = OpenSync.getInstance().getDirectoryPath("export",true);
212       fileWriter = new FileWriter(path + "/test.xml");
213       bufferedWriter = new BufferedWriter(fileWriter);
214       bufferedWriter.write(xml);
215     }
216     finally{
217       if(bufferedWriter != null){
218         bufferedWriter.close();
219       }
220       if(fileWriter != null){
221         fileWriter.close();
222       }
223     }*/
224     return null;
225   }
226 
227   /***
228    *
229    * @return
230    */
231   public void release() throws SQLException {
232     if (conn != null && !conn.isClosed()) {
233      if (this.batchMode) stmt.executeBatch();
234      stmt = null;
235      conn.commit();
236      conn.close();
237      System.out.println("Connection " + conn.toString() + " commited and closed.");
238      ((BDConnector)this.getConnector()).getConnectionPool().freeConnection(conn);
239      conn = null;
240    }
241   }
242 
243   /***
244    *
245    * @return
246    */
247   public void releaseWithException() throws SQLException {
248    if (conn != null && !conn.isClosed()) {
249      stmt = null;
250      conn.rollback();
251      conn.close();
252      ((BDConnector)this.getConnector()).getConnectionPool().freeConnection(conn);
253      System.out.println("Connection " + conn.toString() + " rollbacked and closed.");
254      conn = null;
255    }
256   }
257 
258   public Connection getConn() throws SQLException {
259     if (conn == null) conn = ((BDConnector)this.getConnector()).getConnectionPool().getConnection();
260     return conn;
261   }
262 
263   public void setConn(Connection _conn) {
264     this.conn = _conn;
265   }
266   /***
267    * Use fo debug only
268    *
269    */
270   public String toString(){
271     StringBuffer buffer = new StringBuffer();
272     buffer.append("BDAdapter{\n");
273     buffer.append("\ttablesDescriptor : ").append(tablesDescriptor).append("\n");
274     buffer.append("\tthemesDescriptor : ").append(themesDescriptor).append("\n");
275     buffer.append("}\n");
276     return buffer.toString();
277   }
278 
279 
280 }