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