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 java.text.DecimalFormat;
30 import share.LogWriter;
31 import java.util.Hashtable;
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 
35 
36 /**
37  *
38  */
39 public class FatDataBaseOutProducer extends DataBaseOutProducer {
40 
41 
42     /** Creates a new instance of APIDataBaseOutProducer */
43     public FatDataBaseOutProducer(Hashtable param) {
44         super(param);
45         String testBase = (String)mSqlInput.get("TestBase");
46         int sep = testBase.indexOf('_');
47         String testLanguage = testBase.substring(0,sep);
48         testBase = testBase.substring(sep+1);
49         String apiVersion = (String)mSqlInput.get("Version");
50         String descriptionString = testLanguage+":"+(String)mSqlInput.get("OperatingSystem")+":"+testBase+":"+apiVersion;
51         apiVersion = apiVersion.substring(0, 6);
52         // build date
53         if (mSqlInput.get("Date") != null) {
54             mSqlInput.put("date", mSqlInput.get("Date"));
55         }
56         if (mSqlInput.get("date") == null) {
57             // build date if it's not there
58             Calendar cal = new GregorianCalendar();
59             DecimalFormat dfmt = new DecimalFormat("00");
60             String year = Integer.toString(cal.get(GregorianCalendar.YEAR));
61             // month is starting with "0"
62             String month = dfmt.format(cal.get(GregorianCalendar.MONTH) + 1);
63             String day = dfmt.format(cal.get(GregorianCalendar.DATE));
64             String dateString = year + "-" + month + "-" + day;
65 
66             mSqlInput.put("date", dateString);
67         }
68 
69         setWriteableEntryTypes(new String[]{"property", "method", "component", "interface", "service"});
70 
71         mSqlInput.put("test_run.description", descriptionString);
72         mSqlInput.put("api_version_name", apiVersion);
73     }
74 
75     protected boolean prepareDataBase(LogWriter log) {
76         executeSQLCommand("SHOW TABLES");
77 
78         executeSQLCommand("SELECT id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
79                           " WHERE date = \"$date\" AND description = \"$test_run.description\";", true);
80         String id = (String)mSqlInput.get("test_run.id");
81         // create an entry for this test run
82         if (id == null) {
83             executeSQLCommand("SELECT id AS api_version_id FROM api_version" +
84                               " WHERE version = \"$api_version_name\";", true);
85             String api_version_id = (String)mSqlInput.get("api_version_id");
86             // create an entry for this API
87             if (api_version_id == null) {
88                 executeSQLCommand("INSERT api_version (api_name, version)" +
89                                   " VALUES (\"soapi\", \"$api_version_name\")");
90                 executeSQLCommand("SELECT id AS api_version_id FROM api_version" +
91                                   " WHERE version = \"$api_version_name\";", true);
92             }
93             // complete entry for the test run
94             executeSQLCommand("INSERT test_run (api_version_id, description, date)" +
95                               " VALUES ($api_version_id, \"$test_run.description\", \"$date\");");
96             executeSQLCommand("SELECT test_run.id AS \"test_run.id\", api_version_id, description, date FROM test_run" +
97                               " WHERE date = \"$date\" AND description = \"$test_run.description\";", true);
98         }
99         return true;
100     }
101 
102     // check the database afterwards
103     protected boolean checkDataBase(LogWriter log) {
104         return true;
105     }
106 
107     protected boolean insertEntry(LogWriter log) {
108 
109         executeSQLCommand("SELECT id AS \"entry.id\", name AS \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
110         if (mSqlInput.get("entry.id") == null) {
111             executeSQLCommand("INSERT entry (name, type)" +
112                               " VALUES (\"$EntryLongName\", \"$EntryType\");");
113             executeSQLCommand("SELECT id AS \"entry.id\", name AS \"entry.name\" FROM entry WHERE name = \"$EntryLongName\";", true);
114         }
115         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" +
116                           " WHERE entry_id = $entry.id;", true);
117         if (mSqlInput.get("api_entry.id") == null) {
118             executeSQLCommand("INSERT api_entry (entry_id, api_version_id)"+
119                               " VALUES ($entry.id, $api_version_id);");
120             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" +
121                               " WHERE entry_id = $entry.id;", true);
122         }
123         executeSQLCommand("SELECT status AS \"test_state.status\" FROM test_state"+
124                           " WHERE test_run_id = $test_run.id AND entry_id = $entry.id;", true);
125 
126         String status = (String)mSqlInput.get("test_state.status");
127         if (status == null) {
128             executeSQLCommand("INSERT test_state (test_run_id, entry_id, status)"+
129                               " VALUES ($test_run.id, $entry.id, \"$EntryState\");");
130         }
131         else {
132             if (!status.endsWith("OK")) {
133                 executeSQLCommand("UPDATE test_state SET status = \"$EntryState\""+
134                                   " WHERE test_run_id = $test_run.id AND entry_id = $entry.id;");
135             }
136         }
137         return true;
138     }
139 
140     public Object getWatcher() {
141         return null;
142     }
143 
144     public void setWatcher(Object watcher) {
145     }
146 
147 }
148