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