1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package stats;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import share.LogWriter;
30*cdf0e10cSrcweir import java.text.DecimalFormat;
31*cdf0e10cSrcweir import java.util.Hashtable;
32*cdf0e10cSrcweir import java.util.Calendar;
33*cdf0e10cSrcweir import java.util.GregorianCalendar;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /**
36*cdf0e10cSrcweir  *
37*cdf0e10cSrcweir  *
38*cdf0e10cSrcweir  */
39*cdf0e10cSrcweir public class ComplexDataBaseOutProducer extends DataBaseOutProducer {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir     /** Creates a new instance of ComplexDataBaseOutProducer */
42*cdf0e10cSrcweir     public ComplexDataBaseOutProducer(Hashtable param) {
43*cdf0e10cSrcweir         super(param);
44*cdf0e10cSrcweir         // do we have to write debug output?
45*cdf0e10cSrcweir         Object o = param.get("DebugIsActive");
46*cdf0e10cSrcweir         if (o != null) {
47*cdf0e10cSrcweir             if (o instanceof String) {
48*cdf0e10cSrcweir                 String debug = (String)o;
49*cdf0e10cSrcweir                 m_bDebug = (debug.equalsIgnoreCase("yes") || debug.equalsIgnoreCase("true"));
50*cdf0e10cSrcweir             }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir         }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir         String testBase = (String)mSqlInput.get("TestBase");
55*cdf0e10cSrcweir         String apiVersion = (String)mSqlInput.get("Version");
56*cdf0e10cSrcweir         String os = (String)mSqlInput.get("OperatingSystem");
57*cdf0e10cSrcweir         if (testBase == null || apiVersion == null || os == null) {
58*cdf0e10cSrcweir             System.err.println("The ComplexDataBaseOutProducer constructor needs this parameters to work correctly:");
59*cdf0e10cSrcweir             System.err.println("TestBase - " + testBase);
60*cdf0e10cSrcweir             System.err.println("Version - " + apiVersion);
61*cdf0e10cSrcweir             System.err.println("OperatingSystem - " + os);
62*cdf0e10cSrcweir             System.err.println("Add the missing parameter.");
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir         int sep = testBase.indexOf('_');
65*cdf0e10cSrcweir         String testLanguage = testBase.substring(0,sep);
66*cdf0e10cSrcweir         testBase = testBase.substring(sep+1);
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         // 2do fallback?
69*cdf0e10cSrcweir //        if (os == null || os.equals(""))
70*cdf0e10cSrcweir //            os = System.getProperty("os.name");
71*cdf0e10cSrcweir         String descriptionString = testLanguage+":"+ os +":"+testBase+":"+apiVersion;
72*cdf0e10cSrcweir         if (apiVersion != null)
73*cdf0e10cSrcweir             apiVersion = apiVersion.substring(0, 6);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir         if (mSqlInput.get("date") == null) {
76*cdf0e10cSrcweir             // build date if it's not there
77*cdf0e10cSrcweir             Calendar cal = new GregorianCalendar();
78*cdf0e10cSrcweir             DecimalFormat dfmt = new DecimalFormat("00");
79*cdf0e10cSrcweir             String year = Integer.toString(cal.get(GregorianCalendar.YEAR));
80*cdf0e10cSrcweir             // month is starting with "0"
81*cdf0e10cSrcweir             String month = dfmt.format(cal.get(GregorianCalendar.MONTH) + 1);
82*cdf0e10cSrcweir             String day = dfmt.format(cal.get(GregorianCalendar.DATE));
83*cdf0e10cSrcweir             String dateString = year + "-" + month + "-" + day;
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir             mSqlInput.put("date", dateString);
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir         mSqlInput.put("test_run_description", descriptionString);
88*cdf0e10cSrcweir         mSqlInput.put("api_version_name", apiVersion);
89*cdf0e10cSrcweir         setWriteableEntryTypes(new String[]{"unit", "method"});
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     /**
93*cdf0e10cSrcweir      * Prepare the database.
94*cdf0e10cSrcweir      * @parameter log A log writer
95*cdf0e10cSrcweir      * @return True, if everything worked.
96*cdf0e10cSrcweir      */
97*cdf0e10cSrcweir     protected boolean prepareDataBase(LogWriter log) {
98*cdf0e10cSrcweir         executeSQLCommand("SELECT id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
99*cdf0e10cSrcweir                           " WHERE date = \"$date\" AND description = \"$test_run_description\";", true);
100*cdf0e10cSrcweir         String id = (String)mSqlInput.get("test_run.id");
101*cdf0e10cSrcweir         // create an entry for this test run
102*cdf0e10cSrcweir         if (id == null) {
103*cdf0e10cSrcweir             executeSQLCommand("SELECT id as api_version_id FROM api_version" +
104*cdf0e10cSrcweir                               " WHERE version = \"$api_version_name\";", true);
105*cdf0e10cSrcweir             String api_version_id = (String)mSqlInput.get("api_version_id");
106*cdf0e10cSrcweir             // create an entry for this API
107*cdf0e10cSrcweir             if (api_version_id == null) {
108*cdf0e10cSrcweir                 executeSQLCommand("INSERT api_version (api_name, version)" +
109*cdf0e10cSrcweir                                   " VALUES (\"soapi\", \"$api_version_name\")");
110*cdf0e10cSrcweir                 executeSQLCommand("SELECT id as api_version_id FROM api_version" +
111*cdf0e10cSrcweir                                   " WHERE version = \"$api_version_name\";", true);
112*cdf0e10cSrcweir             }
113*cdf0e10cSrcweir             executeSQLCommand("INSERT test_run (api_version_id, description, date)" +
114*cdf0e10cSrcweir                               " VALUES ($api_version_id, \"$test_run_description\", \"$date\");");
115*cdf0e10cSrcweir             executeSQLCommand("SELECT id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
116*cdf0e10cSrcweir                               " WHERE date = \"$date\" AND description = \"$test_run_description\";", true);
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir         return true;
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /**
122*cdf0e10cSrcweir      * Insert the result of the test of an entry into the database.
123*cdf0e10cSrcweir      * @parameter log A log writer
124*cdf0e10cSrcweir      * @return True, if everything worked.
125*cdf0e10cSrcweir      */
126*cdf0e10cSrcweir     protected boolean insertEntry(LogWriter log) {
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         if ( m_bDebug ) {
129*cdf0e10cSrcweir             System.out.println("DEBUG - ComplexDataBaseOutProducer: entry.id has to be null: " + (mSqlInput.get("entry.id")==null));
130*cdf0e10cSrcweir             System.out.println("DEBUG - ComplexDataBaseOutProducer: EntryLongName: " + mSqlInput.get("EntryLongName"));
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir         executeSQLCommand("SELECT id as \"entry.id\", name as \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         if (mSqlInput.get("entry.id") == null) {
135*cdf0e10cSrcweir             if ( m_bDebug ) {
136*cdf0e10cSrcweir                 System.out.println("DEBUG - ComplexDataBaseOutProducer: No entry.id found, this is a new entry in the database.");
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             executeSQLCommand("INSERT entry (name, type)" +
139*cdf0e10cSrcweir                               " VALUES (\"$EntryLongName\", \"$EntryType\");");
140*cdf0e10cSrcweir             executeSQLCommand("SELECT id as \"entry.id\", name as \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir         executeSQLCommand("SELECT id as \"api_entry.id\", api_version_id as \"api_entry.api_version_id\", entry_id as \"api_entry.entry_id\" FROM api_entry" +
145*cdf0e10cSrcweir                           " WHERE entry_id = $entry.id;", true);
146*cdf0e10cSrcweir         if (mSqlInput.get("api_entry.id") == null) {
147*cdf0e10cSrcweir             System.out.println("No api_entry.id found");
148*cdf0e10cSrcweir             executeSQLCommand("INSERT api_entry (entry_id, api_version_id)"+
149*cdf0e10cSrcweir                               " VALUES ($entry.id, $api_version_id);");
150*cdf0e10cSrcweir             executeSQLCommand("SELECT id as \"api_entry.id\", api_version_id as \"api_entry.api_version_id\", entry_id as \"api_entry.entry_id\" FROM api_entry" +
151*cdf0e10cSrcweir                               " WHERE entry_id = $entry.id;", true);
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         executeSQLCommand("SELECT status as \"test_state.status\" FROM test_state"+
156*cdf0e10cSrcweir                           " WHERE test_run_id = $test_run.id AND entry_id = $entry.id;", true);
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir         String entryState = (String)mSqlInput.get("EntryState");
159*cdf0e10cSrcweir         String status = (String)mSqlInput.get("test_state.status");
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir         if (!entryState.equals("SKIPPED.FAILED")) { // occurs in case of misspellings: do not make an database entry.
162*cdf0e10cSrcweir             if (status == null) {
163*cdf0e10cSrcweir                 executeSQLCommand("INSERT test_state (test_run_id, entry_id, status)"+
164*cdf0e10cSrcweir                                   " VALUES ($test_run.id, $entry.id, \"$EntryState\");");
165*cdf0e10cSrcweir             }
166*cdf0e10cSrcweir             else {
167*cdf0e10cSrcweir                 executeSQLCommand("UPDATE test_state SET status = \"$EntryState\""+
168*cdf0e10cSrcweir                                   " where test_run_id =$test_run.id AND entry_id = $entry.id;");
169*cdf0e10cSrcweir             }
170*cdf0e10cSrcweir         }
171*cdf0e10cSrcweir         return true;
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     public Object getWatcher() {
175*cdf0e10cSrcweir         return null;
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     public void setWatcher(Object watcher) {
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir }
182