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 share.DescEntry;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import java.util.Hashtable;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /**
35*cdf0e10cSrcweir  *
36*cdf0e10cSrcweir  * @author  sg128468
37*cdf0e10cSrcweir  */
38*cdf0e10cSrcweir public abstract class DataBaseOutProducer implements LogWriter {
39*cdf0e10cSrcweir     protected Hashtable mSqlInput = null;
40*cdf0e10cSrcweir     protected Hashtable mSqlOutput = null;
41*cdf0e10cSrcweir     protected String[] mWriteableEntryTypes = null;
42*cdf0e10cSrcweir     protected SQLExecution mSqlExec;
43*cdf0e10cSrcweir     protected boolean m_bDebug = false;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     /** Creates a new instance of DataBaseOutProducer
47*cdf0e10cSrcweir      * @param param The Hashtable with test parameters
48*cdf0e10cSrcweir      */
49*cdf0e10cSrcweir     public DataBaseOutProducer(Hashtable param) {
50*cdf0e10cSrcweir         mSqlInput = new Hashtable();
51*cdf0e10cSrcweir         mSqlInput.putAll(param);
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir         Object o = param.get("DebugIsActive");
54*cdf0e10cSrcweir         String debug = null;
55*cdf0e10cSrcweir         if (o instanceof String)
56*cdf0e10cSrcweir             debug = (String)o;
57*cdf0e10cSrcweir         else
58*cdf0e10cSrcweir             debug = o.toString();
59*cdf0e10cSrcweir         if (debug != null && (debug.equalsIgnoreCase("true") || debug.equalsIgnoreCase("yes"))) {
60*cdf0e10cSrcweir             m_bDebug = true;
61*cdf0e10cSrcweir         }
62*cdf0e10cSrcweir         // set default for writeable entries: method
63*cdf0e10cSrcweir         setWriteableEntryTypes(new String[]{"method"});
64*cdf0e10cSrcweir     }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir     /** initialization
67*cdf0e10cSrcweir      *
68*cdf0e10cSrcweir      */
69*cdf0e10cSrcweir     public boolean initialize(DescEntry entry, boolean active) {
70*cdf0e10cSrcweir         if (entry.UserDefinedParams != null)
71*cdf0e10cSrcweir             mSqlInput.putAll(entry.UserDefinedParams);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         String jdbcClass = (String)mSqlInput.get("JDBC");
74*cdf0e10cSrcweir         if (jdbcClass == null)
75*cdf0e10cSrcweir             jdbcClass = "org.gjt.mm.mysql.Driver";
76*cdf0e10cSrcweir         String dbURL = (String)mSqlInput.get("DataBaseURL");
77*cdf0e10cSrcweir         String user = (String)mSqlInput.get("User");
78*cdf0e10cSrcweir         String password = (String)mSqlInput.get("Password");
79*cdf0e10cSrcweir         if (user == null)
80*cdf0e10cSrcweir             user = (String)mSqlInput.get("OperatingSystem");
81*cdf0e10cSrcweir         if (password == null)
82*cdf0e10cSrcweir             password = user;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir         mSqlExec = new SQLExecution(jdbcClass, dbURL, user, password, m_bDebug);
85*cdf0e10cSrcweir         mSqlExec.openConnection();
86*cdf0e10cSrcweir         prepareDataBase(entry.Logger);
87*cdf0e10cSrcweir         return true;
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     /**
91*cdf0e10cSrcweir      *
92*cdf0e10cSrcweir      *
93*cdf0e10cSrcweir      */
94*cdf0e10cSrcweir     public boolean summary(DescEntry entry) {
95*cdf0e10cSrcweir         mSqlExec.openConnection();
96*cdf0e10cSrcweir         findTypeInEntryTree(entry, entry.Logger);
97*cdf0e10cSrcweir //        checkDataBase(entry.Logger);
98*cdf0e10cSrcweir         mSqlExec.closeConnection();
99*cdf0e10cSrcweir         return true;
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     /**
103*cdf0e10cSrcweir      * Step recursively through the entry tree: write all entries of the
104*cdf0e10cSrcweir      * defined types to the database.
105*cdf0e10cSrcweir      * @param entry The description entry that is take as root
106*cdf0e10cSrcweir      * @param log The log writer
107*cdf0e10cSrcweir      */
108*cdf0e10cSrcweir     protected boolean findTypeInEntryTree(DescEntry entry, LogWriter log) {
109*cdf0e10cSrcweir         boolean returnVal = true;
110*cdf0e10cSrcweir         if (isWriteableEntryType(entry)) {
111*cdf0e10cSrcweir             returnVal &= insertEntry(entry, log);
112*cdf0e10cSrcweir         }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         if (entry.SubEntryCount >0) {
115*cdf0e10cSrcweir             for (int i=0; i<entry.SubEntryCount; i++) {
116*cdf0e10cSrcweir                 returnVal &= findTypeInEntryTree(entry.SubEntries[i], log);
117*cdf0e10cSrcweir             }
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         // if we are not on method leaf, exit here
120*cdf0e10cSrcweir         // insert one method result into database
121*cdf0e10cSrcweir         return returnVal;
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     /**
125*cdf0e10cSrcweir      * Insert this entrry to the database.
126*cdf0e10cSrcweir      * @param entry The entry to write.
127*cdf0e10cSrcweir      * @param log The log writer.
128*cdf0e10cSrcweir      */
129*cdf0e10cSrcweir     protected boolean insertEntry(DescEntry entry, LogWriter log) {
130*cdf0e10cSrcweir         // copy the swlInput Hashtable, so it can be reset easily for the next run
131*cdf0e10cSrcweir         Hashtable copySqlInput = new Hashtable();
132*cdf0e10cSrcweir         copySqlInput.putAll(mSqlInput);
133*cdf0e10cSrcweir         // put some stuff from entry in the Hashtable
134*cdf0e10cSrcweir         mSqlInput.put("EntryLongName", entry.longName);
135*cdf0e10cSrcweir         mSqlInput.put("EntryName", entry.entryName);
136*cdf0e10cSrcweir         mSqlInput.put("EntryState", entry.State);
137*cdf0e10cSrcweir         mSqlInput.put("EntryType", entry.EntryType);
138*cdf0e10cSrcweir         boolean result = insertEntry(log);
139*cdf0e10cSrcweir         // reset the Hashtable
140*cdf0e10cSrcweir         mSqlInput = copySqlInput;
141*cdf0e10cSrcweir         return result;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     /**
146*cdf0e10cSrcweir      * Set the writeable entry types: for example "method", "interface", etc.
147*cdf0e10cSrcweir      * All these entries are written to the database.
148*cdf0e10cSrcweir      * @param types A String array with all types that have to be written.
149*cdf0e10cSrcweir      */
150*cdf0e10cSrcweir     public void setWriteableEntryTypes(String[] types) {
151*cdf0e10cSrcweir         mWriteableEntryTypes = types;
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     /**
155*cdf0e10cSrcweir      * Is the entry of the writeable entry type?
156*cdf0e10cSrcweir      * @param entry The entry that is checked
157*cdf0e10cSrcweir      * @return True, if it is indeed a writeable entry.
158*cdf0e10cSrcweir      */
159*cdf0e10cSrcweir     protected boolean isWriteableEntryType(DescEntry entry) {
160*cdf0e10cSrcweir         boolean result = false;
161*cdf0e10cSrcweir         for (int i=0; i<mWriteableEntryTypes.length; i++) {
162*cdf0e10cSrcweir             if (entry.EntryType.equals(mWriteableEntryTypes[i])) {
163*cdf0e10cSrcweir                 result = true;
164*cdf0e10cSrcweir                 break;
165*cdf0e10cSrcweir             }
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir         return result;
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     /**
171*cdf0e10cSrcweir      * Wrap the command of SQLExecution class for transparency.
172*cdf0e10cSrcweir      */
173*cdf0e10cSrcweir     protected boolean executeSQLCommand(String command, boolean mergeOutput) {
174*cdf0e10cSrcweir         return mSqlExec.executeSQLCommand(command, mSqlInput, mSqlOutput, mergeOutput);
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     /**
178*cdf0e10cSrcweir      * Wrap the command of SQLExecution class for transparency.
179*cdf0e10cSrcweir      */
180*cdf0e10cSrcweir     protected boolean executeSQLCommand(String command) {
181*cdf0e10cSrcweir         return mSqlExec.executeSQLCommand(command, mSqlInput, mSqlOutput);
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     /**
185*cdf0e10cSrcweir      * Method to print: empty here
186*cdf0e10cSrcweir      */
187*cdf0e10cSrcweir     public void println(String msg) {
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     /**
191*cdf0e10cSrcweir      * Prepare the database: executed once at the beginning.
192*cdf0e10cSrcweir      * Abstract method, so derived classes have to overwrite it.
193*cdf0e10cSrcweir      */
194*cdf0e10cSrcweir     protected abstract boolean prepareDataBase(LogWriter log);
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     /**
197*cdf0e10cSrcweir      * Insert one entr into the database.
198*cdf0e10cSrcweir      * Abstract method, so derived classes have to overwrite it.
199*cdf0e10cSrcweir      */
200*cdf0e10cSrcweir     protected abstract boolean insertEntry(LogWriter log);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     /**
203*cdf0e10cSrcweir      *
204*cdf0e10cSrcweir     protected abstract boolean checkDataBase(LogWriter log);
205*cdf0e10cSrcweir      */
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir }
208