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