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