1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package stats;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import share.LogWriter;
26cdf0e10cSrcweir import java.text.DecimalFormat;
27cdf0e10cSrcweir import java.util.Hashtable;
28cdf0e10cSrcweir import java.util.Calendar;
29cdf0e10cSrcweir import java.util.GregorianCalendar;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /**
32cdf0e10cSrcweir  *
33cdf0e10cSrcweir  *
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir public class ComplexDataBaseOutProducer extends DataBaseOutProducer {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     /** Creates a new instance of ComplexDataBaseOutProducer */
ComplexDataBaseOutProducer(Hashtable param)38cdf0e10cSrcweir     public ComplexDataBaseOutProducer(Hashtable param) {
39cdf0e10cSrcweir         super(param);
40cdf0e10cSrcweir         // do we have to write debug output?
41cdf0e10cSrcweir         Object o = param.get("DebugIsActive");
42cdf0e10cSrcweir         if (o != null) {
43cdf0e10cSrcweir             if (o instanceof String) {
44cdf0e10cSrcweir                 String debug = (String)o;
45cdf0e10cSrcweir                 m_bDebug = (debug.equalsIgnoreCase("yes") || debug.equalsIgnoreCase("true"));
46cdf0e10cSrcweir             }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         String testBase = (String)mSqlInput.get("TestBase");
51cdf0e10cSrcweir         String apiVersion = (String)mSqlInput.get("Version");
52cdf0e10cSrcweir         String os = (String)mSqlInput.get("OperatingSystem");
53cdf0e10cSrcweir         if (testBase == null || apiVersion == null || os == null) {
54cdf0e10cSrcweir             System.err.println("The ComplexDataBaseOutProducer constructor needs this parameters to work correctly:");
55cdf0e10cSrcweir             System.err.println("TestBase - " + testBase);
56cdf0e10cSrcweir             System.err.println("Version - " + apiVersion);
57cdf0e10cSrcweir             System.err.println("OperatingSystem - " + os);
58cdf0e10cSrcweir             System.err.println("Add the missing parameter.");
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir         int sep = testBase.indexOf('_');
61cdf0e10cSrcweir         String testLanguage = testBase.substring(0,sep);
62cdf0e10cSrcweir         testBase = testBase.substring(sep+1);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         // 2do fallback?
65cdf0e10cSrcweir //        if (os == null || os.equals(""))
66cdf0e10cSrcweir //            os = System.getProperty("os.name");
67cdf0e10cSrcweir         String descriptionString = testLanguage+":"+ os +":"+testBase+":"+apiVersion;
68cdf0e10cSrcweir         if (apiVersion != null)
69cdf0e10cSrcweir             apiVersion = apiVersion.substring(0, 6);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir         if (mSqlInput.get("date") == null) {
72cdf0e10cSrcweir             // build date if it's not there
73cdf0e10cSrcweir             Calendar cal = new GregorianCalendar();
74cdf0e10cSrcweir             DecimalFormat dfmt = new DecimalFormat("00");
75cdf0e10cSrcweir             String year = Integer.toString(cal.get(GregorianCalendar.YEAR));
76cdf0e10cSrcweir             // month is starting with "0"
77cdf0e10cSrcweir             String month = dfmt.format(cal.get(GregorianCalendar.MONTH) + 1);
78cdf0e10cSrcweir             String day = dfmt.format(cal.get(GregorianCalendar.DATE));
79cdf0e10cSrcweir             String dateString = year + "-" + month + "-" + day;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir             mSqlInput.put("date", dateString);
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         mSqlInput.put("test_run_description", descriptionString);
84cdf0e10cSrcweir         mSqlInput.put("api_version_name", apiVersion);
85cdf0e10cSrcweir         setWriteableEntryTypes(new String[]{"unit", "method"});
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * Prepare the database.
90cdf0e10cSrcweir      * @parameter log A log writer
91cdf0e10cSrcweir      * @return True, if everything worked.
92cdf0e10cSrcweir      */
prepareDataBase(LogWriter log)93cdf0e10cSrcweir     protected boolean prepareDataBase(LogWriter log) {
94cdf0e10cSrcweir         executeSQLCommand("SELECT id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
95cdf0e10cSrcweir                           " WHERE date = \"$date\" AND description = \"$test_run_description\";", true);
96cdf0e10cSrcweir         String id = (String)mSqlInput.get("test_run.id");
97cdf0e10cSrcweir         // create an entry for this test run
98cdf0e10cSrcweir         if (id == null) {
99cdf0e10cSrcweir             executeSQLCommand("SELECT id as api_version_id FROM api_version" +
100cdf0e10cSrcweir                               " WHERE version = \"$api_version_name\";", true);
101cdf0e10cSrcweir             String api_version_id = (String)mSqlInput.get("api_version_id");
102cdf0e10cSrcweir             // create an entry for this API
103cdf0e10cSrcweir             if (api_version_id == null) {
104cdf0e10cSrcweir                 executeSQLCommand("INSERT api_version (api_name, version)" +
105cdf0e10cSrcweir                                   " VALUES (\"soapi\", \"$api_version_name\")");
106cdf0e10cSrcweir                 executeSQLCommand("SELECT id as api_version_id FROM api_version" +
107cdf0e10cSrcweir                                   " WHERE version = \"$api_version_name\";", true);
108cdf0e10cSrcweir             }
109cdf0e10cSrcweir             executeSQLCommand("INSERT test_run (api_version_id, description, date)" +
110cdf0e10cSrcweir                               " VALUES ($api_version_id, \"$test_run_description\", \"$date\");");
111cdf0e10cSrcweir             executeSQLCommand("SELECT id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
112cdf0e10cSrcweir                               " WHERE date = \"$date\" AND description = \"$test_run_description\";", true);
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         return true;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /**
118cdf0e10cSrcweir      * Insert the result of the test of an entry into the database.
119cdf0e10cSrcweir      * @parameter log A log writer
120cdf0e10cSrcweir      * @return True, if everything worked.
121cdf0e10cSrcweir      */
insertEntry(LogWriter log)122cdf0e10cSrcweir     protected boolean insertEntry(LogWriter log) {
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         if ( m_bDebug ) {
125cdf0e10cSrcweir             System.out.println("DEBUG - ComplexDataBaseOutProducer: entry.id has to be null: " + (mSqlInput.get("entry.id")==null));
126cdf0e10cSrcweir             System.out.println("DEBUG - ComplexDataBaseOutProducer: EntryLongName: " + mSqlInput.get("EntryLongName"));
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir         executeSQLCommand("SELECT id as \"entry.id\", name as \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         if (mSqlInput.get("entry.id") == null) {
131cdf0e10cSrcweir             if ( m_bDebug ) {
132cdf0e10cSrcweir                 System.out.println("DEBUG - ComplexDataBaseOutProducer: No entry.id found, this is a new entry in the database.");
133cdf0e10cSrcweir             }
134cdf0e10cSrcweir             executeSQLCommand("INSERT entry (name, type)" +
135cdf0e10cSrcweir                               " VALUES (\"$EntryLongName\", \"$EntryType\");");
136cdf0e10cSrcweir             executeSQLCommand("SELECT id as \"entry.id\", name as \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         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" +
141cdf0e10cSrcweir                           " WHERE entry_id = $entry.id;", true);
142cdf0e10cSrcweir         if (mSqlInput.get("api_entry.id") == null) {
143cdf0e10cSrcweir             System.out.println("No api_entry.id found");
144cdf0e10cSrcweir             executeSQLCommand("INSERT api_entry (entry_id, api_version_id)"+
145cdf0e10cSrcweir                               " VALUES ($entry.id, $api_version_id);");
146cdf0e10cSrcweir             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" +
147cdf0e10cSrcweir                               " WHERE entry_id = $entry.id;", true);
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         executeSQLCommand("SELECT status as \"test_state.status\" FROM test_state"+
152cdf0e10cSrcweir                           " WHERE test_run_id = $test_run.id AND entry_id = $entry.id;", true);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         String entryState = (String)mSqlInput.get("EntryState");
155cdf0e10cSrcweir         String status = (String)mSqlInput.get("test_state.status");
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         if (!entryState.equals("SKIPPED.FAILED")) { // occurs in case of misspellings: do not make an database entry.
158cdf0e10cSrcweir             if (status == null) {
159cdf0e10cSrcweir                 executeSQLCommand("INSERT test_state (test_run_id, entry_id, status)"+
160cdf0e10cSrcweir                                   " VALUES ($test_run.id, $entry.id, \"$EntryState\");");
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir             else {
163cdf0e10cSrcweir                 executeSQLCommand("UPDATE test_state SET status = \"$EntryState\""+
164cdf0e10cSrcweir                                   " where test_run_id =$test_run.id AND entry_id = $entry.id;");
165cdf0e10cSrcweir             }
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         return true;
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
getWatcher()170cdf0e10cSrcweir     public Object getWatcher() {
171cdf0e10cSrcweir         return null;
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir 
setWatcher(Object watcher)174cdf0e10cSrcweir     public void setWatcher(Object watcher) {
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir }
178