3 import java
.sql
.Connection
;
4 import java
.util
.StringTokenizer
;
5 import java
.util
.ArrayList
;
6 import helper
.OSHelper
;
8 // import convwatch.DBHelper;
10 public class DB
extends DBHelper
12 private static DB m_aDB
= null;
19 private static synchronized DB
getDB()
28 private String m_sSourceVersion
;
29 private String m_sDestinationVersion
;
30 private String m_sDocumentPool
;
31 private String m_sEnvironment
;
32 private String m_sDocID
;
33 private String m_sDBDistinct
;
35 public static void init(String _sDBInfoString
)
37 if (_sDBInfoString
== null) return;
38 getDB().fillVariables(_sDBInfoString
);
39 getDB().updatestate_status("source started");
42 public static void test()
47 public static void source_start()
49 getDB().updatestate_status("source started");
52 public static void source_finished()
54 getDB().updatestate_status( "source finished");
57 public static void source_failed(String _sMessage
)
59 getDB().updatestate_status("source failed");
60 getDB().updateinfo_status(_sMessage
);
63 public static void destination_start()
65 getDB().updatestate_status("destination started");
68 public static void destination_finished()
70 getDB().updatestate_status("PASSED-OK");
73 public static void destination_failed(String _sStatus
, String _sMessage
)
75 getDB().updatestate_status(_sStatus
);
76 getDB().updateinfo_status(_sMessage
);
78 public static void writeNumberOfPages(int _nPages
)
80 getDB().updatepagecount_documents(_nPages
);
82 public static void writeErrorFile(String _sErrorFile
)
84 getDB().updateerrorfile_status(_sErrorFile
);
86 public static void writeHTMLFile(String _sHTMLFile
)
88 getDB().updatehtmlfile_status(_sHTMLFile
);
91 public static void writeToDB(String _sFilename
,
98 GlobalLogWriter
.get().println("DB: Filename:" + _sFilename
);
99 GlobalLogWriter
.get().println("DB: Basename:" + _sBasename
);
100 GlobalLogWriter
.get().println("DB: FileFormat:" + _sFileFormat
);
101 GlobalLogWriter
.get().println("DB: BuildID:" + _sBuildID
);
102 GlobalLogWriter
.get().println("DB: SourceType:" + _sSourceType
);
103 GlobalLogWriter
.get().println("DB: Resolution:" + _nResolution
);
106 private String
getEnvironment()
108 if (OSHelper
.isWindows())
112 else if ( OSHelper
.isSolarisIntel())
116 else if ( OSHelper
.isSolarisSparc())
120 else if ( OSHelper
.isLinuxIntel())
126 GlobalLogWriter
.get().println("DB: Unknown environment.");
127 GlobalLogWriter
.get().println("DB: os.name := " + System
.getProperty("os.name").toLowerCase());
128 GlobalLogWriter
.get().println("DB: os.arch := " + System
.getProperty("os.arch"));
133 // fill some db access important variables with values given out of a simple string
134 // DOC_COMPARATOR_DB_INFO_STRING=p:m220,c:m224,d:demo_lla,src:m220,dest:m224,doc:demo_lla,id:294,distinct:81
136 private void fillVariables(String _sInfo
)
138 fillDBConnection(_sInfo
);
139 m_sEnvironment
= getEnvironment();
141 StringTokenizer aTokenizer
= new StringTokenizer(_sInfo
,",",false);
142 while (aTokenizer
.hasMoreTokens())
144 String sPart
= aTokenizer
.nextToken();
145 if (sPart
.startsWith("p:"))
147 m_sSourceVersion
= sPart
.substring(2);
148 GlobalLogWriter
.get().println("DB: source version: " + m_sSourceVersion
);
150 else if (sPart
.startsWith("src:"))
152 m_sSourceVersion
= sPart
.substring(4);
153 GlobalLogWriter
.get().println("DB: source version: " + m_sSourceVersion
);
155 else if (sPart
.startsWith("c:"))
157 m_sDestinationVersion
= sPart
.substring(2);
158 GlobalLogWriter
.get().println("DB: destination version: " + m_sDestinationVersion
);
160 else if (sPart
.startsWith("dest:"))
162 m_sDestinationVersion
= sPart
.substring(5);
163 GlobalLogWriter
.get().println("DB: destination version: " + m_sDestinationVersion
);
165 else if (sPart
.startsWith("d:"))
167 m_sDocumentPool
= sPart
.substring(2);
168 GlobalLogWriter
.get().println("DB: documentpool version: " + m_sDocumentPool
);
170 else if (sPart
.startsWith("doc:"))
172 m_sDocumentPool
= sPart
.substring(4);
173 GlobalLogWriter
.get().println("DB: documentpool version: " + m_sDocumentPool
);
175 else if (sPart
.startsWith("id:"))
177 m_sDocID
= sPart
.substring(3);
178 GlobalLogWriter
.get().println("DB: docid: " + m_sDocID
);
180 else if (sPart
.startsWith("distinct:"))
182 m_sDBDistinct
= sPart
.substring(9);
183 GlobalLogWriter
.get().println("DB: distinct: " + m_sDBDistinct
);
191 // public static void insertinto_file(String _sFilename,
192 // String _sBasename,
193 // String _sFileFormat,
195 // String _sSourceType,
196 // int _nResolution )
198 // Connection aCon = new ShareConnection().getConnection();
200 // String sFilename = _sFilename.replace('\\', '/');
202 // String sDeleteOld = "DELETE FROM file WHERE filename = " + Quote(sFilename);
203 // ExecSQL(aCon, sDeleteOld);
205 // String sValueLine = "type, filename, basename, fileformat, buildid, resolution, date";
206 // StringBuffer aDataLine = new StringBuffer();
207 // aDataLine.append( Quote(_sSourceType) ) . append( sComma ) .
208 // append( Quote( sFilename) ) . append( sComma ) .
209 // append( Quote( _sBasename) ) . append( sComma ) .
210 // append( Quote( _sFileFormat) ) . append( sComma ) .
211 // append( Quote( _sBuildID) ) . append( sComma ) .
212 // append( _nResolution) . append( sComma ) .
213 // append( Quote( today() ) );
215 // SQLinsertValues(aCon, "file", sValueLine, aDataLine.toString());
218 // public static void updatestate_currentdocs(String _sFilename,
221 // Connection aCon = new ShareConnection().getConnection();
223 // String sFilename = _sFilename.replace('\\', '/');
225 // // String sDeleteOld = "DELETE FROM file WHERE filename = " + Quote(sFilename);
226 // // ExecSQL(aCon, sDeleteOld);
228 // String sSet = "state=" + Quote(_sState);
229 // String sWhere = getWhereClause() + sAND + "name=" + Quote(sFilename);
230 // SQLupdateValue( aCon, "currentdocs", sSet, sWhere );
233 private void sql_test()
235 String sUUID
= getDBDistinct();
236 System
.out
.println("UUID: " + sUUID
);
239 public ArrayList
QuerySQL(Connection _aCon
, String _sSQL
)
241 java
.sql
.Statement oStmt
= null;
242 Connection oCon
= null;
243 ArrayList aResultList
= new ArrayList();
246 oStmt
= _aCon
.createStatement();
248 java
.sql
.ResultSet aResultSet
= oStmt
.executeQuery(_sSQL
);
249 java
.sql
.ResultSetMetaData aResultSetMetaData
= aResultSet
.getMetaData();
251 int nColumnCount
= aResultSetMetaData
.getColumnCount(); // java sql starts with '1'
252 // String[] aColumnName = new String[nColumnCount];
253 // for(int i=1;i<nColumnCount;i++)
255 // String aColumnName[i - 1] = aResultSetMetaData.getColumnName(i);
258 while( aResultSet
.next() )
260 StringBuffer aResult
= new StringBuffer();
266 aResult
.append("sqlresult: ");
267 for (int i
=1;i
<=nColumnCount
;i
++)
269 String sColumnName
= aResultSetMetaData
.getColumnName(i
);
270 aResult
.append(sColumnName
).append("=");
272 int nSQLType
= aResultSetMetaData
.getColumnType(i
);
275 case java
.sql
.Types
.VARCHAR
:
276 sValue
= "'" + aResultSet
.getString(i
) + "'";
278 case java
.sql
.Types
.INTEGER
:
280 int nValue
= aResultSet
.getInt(i
);
281 sValue
= String
.valueOf(nValue
);
286 sValue
= "UNSUPPORTED TYPE";
288 aResult
.append(sValue
).append(", ");
289 // String sName = aObj.getClass().getName();
290 // System.out.println("sqlresult: Class name: " + sName);
292 String sResult
= aResult
.toString();
293 aResultList
.add(sResult
);
294 // System.out.println(sResult);
296 catch (java
.sql
.SQLException e
)
301 catch (java
.sql
.SQLException e
)
303 String sError
= e
.getMessage();
304 GlobalLogWriter
.get().println("DB: Original SQL error: " + sError
);
305 // throw new ValueNotFoundException("Cant execute SQL: " + _sSQL);
310 private void updatestate_status(String _sStatus
)
312 Connection aCon
= new ShareConnection().getConnection();
314 // String sInfo = _sInfo.replace('\\', '/');
316 // String sDeleteOld = "DELETE FROM file WHERE filename = " + Quote(sFilename);
317 // ExecSQL(aCon, sDeleteOld);
319 String sSet
= "state=" + Quote(_sStatus
);
320 String sWhere
= getWhereClause();
321 if (sWhere
.length() > 0)
323 SQLupdateValue( aCon
, "status", sSet
, sWhere
);
326 private void updateinfo_status(String _sInfo
)
328 Connection aCon
= new ShareConnection().getConnection();
330 // String sInfo = _sInfo.replace('\\', '/');
332 // String sDeleteOld = "DELETE FROM file WHERE filename = " + Quote(sFilename);
333 // ExecSQL(aCon, sDeleteOld);
335 String sSet
= "info=" + Quote(_sInfo
);
336 String sWhere
= getWhereClause();
337 SQLupdateValue( aCon
, "status", sSet
, sWhere
);
339 private void updateerrorfile_status(String _sErrorFile
)
341 Connection aCon
= new ShareConnection().getConnection();
343 String sErrorFile
= _sErrorFile
.replace('\\', '/');
345 String sSet
= "errorfile=" + Quote(sErrorFile
);
346 String sWhere
= getWhereClause();
347 SQLupdateValue( aCon
, "status", sSet
, sWhere
);
349 private void updatehtmlfile_status(String _sHtmlFile
)
351 Connection aCon
= new ShareConnection().getConnection();
353 String sHtmlFile
= _sHtmlFile
.replace('\\', '/');
355 String sSet
= "htmlfile=" + Quote(sHtmlFile
);
356 String sWhere
= getWhereClause();
357 SQLupdateValue( aCon
, "status", sSet
, sWhere
);
359 private void updatepagecount_documents(int _nPageCount
)
361 Connection aCon
= new ShareConnection().getConnection();
363 String sSet
= "pagecount=" + _nPageCount
;
364 String sWhere
= getWhereClause();
365 SQLupdateValue( aCon
, "documents", sSet
, sWhere
);
370 private String
getWhereClause()
372 StringBuffer aWhereClause
= new StringBuffer();
373 // WHERE environment='' and referenceversion='' and currentversion='' and documentpool=''
374 // aWhere.append( "environment" ). append(sEqual) . append(Quote(m_sEnvironment)) .
376 // append( "referenceversion" ). append(sEqual) . append(Quote(m_sSourceVersion)) .
378 // append( "currentversion" ). append(sEqual) . append(Quote(m_sDestinationVersion)) .
380 // append( "documentpool" ). append(sEqual) . append(Quote(m_sDocumentPool));
381 boolean bAND
= false;
382 if (m_sDocID
!= null)
384 aWhereClause
.append( "docid" ). append(sEqual
) . append(m_sDocID
);
389 aWhereClause
.append(sAND
);
391 if (m_sDBDistinct
!= null)
393 aWhereClause
.append( "dbdistinct2" ). append(sEqual
) . append(Quote(m_sDBDistinct
));
395 return aWhereClause
.toString();
398 private String
getDBDistinct()
400 Connection aCon
= new ShareConnection().getConnection();
402 String sSQL
= "SELECT uuid()";
403 ArrayList aResultList
= QuerySQL(aCon
, sSQL
);
405 for (int i
=0;i
<aResultList
.size();i
++)
407 String sResult
= (String
)aResultList
.get(i
);
409 StringTokenizer aTokenizer
= new StringTokenizer(sResult
,",",false);
410 while (aTokenizer
.hasMoreTokens())
412 String sToken
= aTokenizer
.nextToken();
413 // System.out.println("PART: " + sToken);
414 int nIndex
= sToken
.indexOf("uuid()=");
415 // System.out.println("Index " + nIndex);
416 int nIndexTuettel
= sToken
.indexOf("'", nIndex
);
417 // System.out.println("IndexTuettel " + nIndexTuettel);
418 int nIndexTuettel2
= sToken
.lastIndexOf("'");
419 // System.out.println("IndexTuettel2 " + nIndexTuettel2);
420 String sUuid
= sToken
.substring(nIndexTuettel
+ 1, nIndexTuettel2
);
421 // if (sPart.startsWith("p:"))
423 // m_sSourceVersion = sPart.substring(2);
424 // GlobalLogWriter.get().println("DB: source version: " + m_sSourceVersion);
428 // System.out.println(sResult);
434 public static void insertinto_documentcompare(String _sSourceVersion
, String _sSourceName
, String _sSourceCreatorType
,
435 String _sDestinationVersion
, String _sDestinationName
, String _sDestinationCreatorType
,
436 String _sDocumentPoolDir
, String _sDocumentPoolName
, String _sMailAddress
,
437 String _sSpecial
, String _sParentDistinct
)
439 getDB().insertinto_documentcompare_impl( _sSourceVersion
, _sSourceName
, _sSourceCreatorType
,
440 _sDestinationVersion
, _sDestinationName
, _sDestinationCreatorType
,
441 _sDocumentPoolDir
, _sDocumentPoolName
, _sMailAddress
,
442 _sSpecial
, _sParentDistinct
);
445 private void insertinto_documentcompare_impl(String _sSourceVersion
, String _sSourceName
, String _sSourceCreatorType
,
446 String _sDestinationVersion
, String _sDestinationName
, String _sDestinationCreatorType
,
447 String _sDocumentPoolDir
, String _sDocumentPoolName
, String _sMailAddress
,
448 String _sSpecial
, String _sParentDistinct
)
450 // $sSQLInsert = "INSERT INTO documentcompare
451 if (_sParentDistinct
== null)
453 _sParentDistinct
= "";
456 Connection aCon
= new ShareConnection().getConnection();
458 String sValueLine
="dbdistinct2, environment, sourceversion, sourcename, sourcecreatortype, destinationversion, destinationname, destinationcreatortype, documentpoolpath, documentpool, mailfeedback, state, special, parentdistinct, startdate";
459 String sDocumentPoolDir
= _sDocumentPoolDir
.replace('\\', '/');
460 StringBuffer aDataLine
= new StringBuffer();
461 aDataLine
.append( Quote(getDBDistinct()) ) . append( sComma
) .
462 append( Quote( getEnvironment()) ) . append( sComma
) .
463 append( Quote( _sSourceVersion
) ) . append( sComma
) .
464 append( Quote( _sSourceName
) ) . append( sComma
) .
465 append( Quote( _sSourceCreatorType
) ) . append( sComma
) .
466 append( Quote( _sDestinationVersion
) ) . append( sComma
) .
467 append( Quote( _sDestinationName
) ) . append( sComma
) .
468 append( Quote( _sDestinationCreatorType
) ) . append( sComma
) .
469 append( Quote( sDocumentPoolDir
) ) . append( sComma
) .
470 append( Quote( _sDocumentPoolName
) ) . append( sComma
) .
471 append( Quote( _sMailAddress
) ) . append( sComma
) .
472 append( Quote( "new" )) . append ( sComma
) .
473 append( Quote( _sSpecial
) ) . append( sComma
) .
474 append( Quote( _sParentDistinct
) ) . append( sComma
) .
475 append( Quote( today() ));
477 SQLinsertValues(aCon
, "documentcompare", sValueLine
, aDataLine
.toString());
480 // public static void filesRemove(String _sDBInfoString)
482 // if (_sDBInfoString == null) return;
483 // fillVariables(_sDBInfoString);
485 // Connection aCon = new ShareConnection().getConnection();
487 // String sDeleteSQL = "DELETE FROM currentdocs WHERE " + getWhereClause();
489 // ExecSQL(aCon, sDeleteSQL);
492 // public static void fileInsert(String _sDBInfoString,
493 // String _sFilename,
494 // String _sRemovePath)
496 // if (_sDBInfoString == null) return;
497 // fillVariables(_sDBInfoString);
499 // String sFilename = _sFilename.replace('\\', '/');
501 // Connection aCon = new ShareConnection().getConnection();
503 // String sValueLine = "environment, referenceversion, currentversion, documentpool, name, state";
504 // StringBuffer aDataLine = new StringBuffer();
505 // aDataLine.append( Quote(m_sEnvironment) ) . append( sComma ) .
506 // append( Quote( m_sSourceVersion) ) . append( sComma ) .
507 // append( Quote( m_sDestinationVersion) ) . append( sComma ) .
508 // append( Quote( m_sDocumentPool) ) . append( sComma ) .
509 // append( Quote( sFilename) ) . append( sComma ) .
510 // append( Quote( "undone"));
512 // SQLinsertValues(aCon, "currentdocs", sValueLine, aDataLine.toString());
516 // public static void insertinto_file(String _sFilename, String _sFileFormat, String _sBuildID)
518 // Connection aCon = new ShareConnection().getConnection();
520 // String sValueLine = "type, filename, fileformat, buildid, date";
521 // StringBuffer aDataLine = new StringBuffer();
522 // aDataLine.append( "1" ) . append( sComma ) .
523 // append( Quote( _sFilename) ) . append( sComma ) .
524 // append( Quote( _sFileFormat) ) . append( sComma ) .
525 // append( Quote( _sBuildID) ) . append( sComma ) .
526 // append( Quote( today() ) );
528 // SQLinsertValues(aCon, "file", sValueLine, aDataLine.toString());
531 // public static void main( String[] args )
534 // String _sFilename = "";
535 // String _sFileFormat = "";
536 // String _sBuildID = "";
538 // // insertinto_file("c:\temp\test.txt", "test", "txt", "nix", "", 0);
539 // fillVariables("p:m128,c:m134,d:demo");