1 |
package tools; |
2 |
|
3 |
import java.sql.*; |
4 |
import java.io.*; |
5 |
|
6 |
public class Report { |
7 |
|
8 |
private Connection myConnection; |
9 |
private mySQLConnect SQLC; |
10 |
private PrintStream Log; |
11 |
private int reportLevel; |
12 |
private String reportTbl; |
13 |
private String hostName; |
14 |
private String clientName; |
15 |
|
16 |
public Report(String connFile, String logFile, int reportLevel) { |
17 |
|
18 |
SQLC = new mySQLConnect(); |
19 |
SQLC.readConnectionFile(connFile); |
20 |
myConnection = SQLC.openConnection(); |
21 |
|
22 |
try { |
23 |
Log = new PrintStream(new FileOutputStream(logFile)); |
24 |
} catch (Exception e) { |
25 |
System.out.println(e); |
26 |
} |
27 |
|
28 |
this.reportLevel = reportLevel; |
29 |
this.reportTbl = "report"; |
30 |
clientName = Machine.getClient(); |
31 |
hostName = SQLC.getHost(); |
32 |
|
33 |
} |
34 |
|
35 |
public Report(String connFile, String logFile, int reportLevel, String reportTable) { |
36 |
|
37 |
SQLC = new mySQLConnect(); |
38 |
SQLC.readConnectionFile(connFile); |
39 |
myConnection = SQLC.openConnection(); |
40 |
|
41 |
try { |
42 |
Log = new PrintStream(new FileOutputStream(logFile)); |
43 |
} catch (Exception e) { |
44 |
System.out.println(e); |
45 |
} |
46 |
|
47 |
this.reportLevel = reportLevel; |
48 |
this.reportTbl = reportTable; |
49 |
clientName = Machine.getClient(); |
50 |
hostName = SQLC.getHost(); |
51 |
|
52 |
} |
53 |
|
54 |
/** Sets the reportLevel, so that only messages below or equal the level given are displayed. |
55 |
The following levels are available: (0 is default - no messages) |
56 |
3 - talkative mode |
57 |
2 - normal mode |
58 |
1 - basic mode |
59 |
0 - silent mode |
60 |
This means by a higher level you get more detailed information, while on a reportLevel |
61 |
of 0 only the most basic results and errors are displayed */ |
62 |
public void setReportLevel(int newLevel) { reportLevel = newLevel; } |
63 |
|
64 |
public int getReportLevel() { return reportLevel; } |
65 |
|
66 |
public void print(String text, int level) { |
67 |
if (level<=reportLevel) { |
68 |
System.out.print(text); |
69 |
} // end if level of the curent message is above the current level |
70 |
} // end of report |
71 |
|
72 |
public void println(String text, int level) { |
73 |
if (level<=reportLevel) { |
74 |
System.out.println(text); |
75 |
} // end if level of the curent message is above the current level |
76 |
} // end of report |
77 |
|
78 |
public void printLog(String text, int level) { |
79 |
if (level<=reportLevel) { |
80 |
Log.print(text); |
81 |
} // end if level of the curent message is above the current level |
82 |
} // end of report |
83 |
|
84 |
public void printlnLog(String text, int level) { |
85 |
if (level<=reportLevel) { |
86 |
Log.println(text); |
87 |
} // end if level of the curent message is above the current level |
88 |
} // end of report |
89 |
|
90 |
public void newProc(String description, int level) { |
91 |
|
92 |
if (level<=reportLevel) { |
93 |
try { |
94 |
Statement S = myConnection.createStatement(); |
95 |
S.executeUpdate("INSERT INTO "+reportTbl+" (Client, Host, Description, Start) VALUES (\""+clientName+"\", \""+hostName+"\", \""+description+"\", NOW());"); |
96 |
S.close(); |
97 |
} catch (SQLException E) { |
98 |
System.out.println("SQLException:\t" + E.getMessage()); |
99 |
System.out.println("SQLState:\t" + E.getSQLState()); |
100 |
System.out.println("VendorError: \t" + E.getErrorCode()); |
101 |
} |
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
public void updateProc(int level) { |
107 |
|
108 |
if (level<=reportLevel) { |
109 |
try { |
110 |
Statement S = myConnection.createStatement(); |
111 |
S.executeUpdate("UPDATE "+reportTbl+" SET End = NOW(), Total = TIMEDIFF(End, Start) WHERE ID = LAST_INSERT_ID();"); |
112 |
S.close(); |
113 |
} catch (SQLException E) { |
114 |
System.out.println("SQLException:\t" + E.getMessage()); |
115 |
System.out.println("SQLState:\t" + E.getSQLState()); |
116 |
System.out.println("VendorError: \t" + E.getErrorCode()); |
117 |
} |
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
public void updateProc(String description, int level) { |
123 |
|
124 |
if (level<=reportLevel) { |
125 |
try { |
126 |
Statement S = myConnection.createStatement(); |
127 |
S.executeUpdate("UPDATE "+reportTbl+" SET End = NOW(), Total = TIMEDIFF(End, Start) WHERE (Client = \""+clientName+"\") AND (Host = \""+hostName+"\") AND (Description = \""+description+"\");"); |
128 |
S.close(); |
129 |
} catch (SQLException E) { |
130 |
System.out.println("SQLException:\t" + E.getMessage()); |
131 |
System.out.println("SQLState:\t" + E.getSQLState()); |
132 |
System.out.println("VendorError: \t" + E.getErrorCode()); |
133 |
} |
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
public void close() { |
139 |
|
140 |
SQLC.closeConnection(myConnection); |
141 |
Log.close(); |
142 |
|
143 |
} |
144 |
|
145 |
} |