KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > DbInsertion


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Zpracuj.java
22  *
23  * Created on November 13, 2006, 6:52 PM
24  *
25  */

26
27 package org.netbeans.modules.exceptions;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.ObjectInputStream JavaDoc;
33 import java.io.ObjectOutputStream JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Statement JavaDoc;
38 import java.text.MessageFormat JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.logging.LogRecord JavaDoc;
41 import org.netbeans.modules.exceptions.data.Record;
42 import static org.netbeans.modules.exceptions.Utils.*;
43 /**
44  *
45  * @author jindra
46  */

47 public class DbInsertion implements Runnable JavaDoc{
48     private static final int MAXUsrName = 30;
49     private static final int MAXMethodName = 100;
50     private static final int MAXMessage = 100;
51     private static final int MAXClass = 100;
52     private static final int MAXSummary = 200;
53     private static final int MAXVM = 50;
54     private static final int MAXProductVersion = 50;
55     private static final int MAXOperatingSystem = 50;
56     private static final int MAXComment = 1000;
57     private static final int MAXComponent = 50;
58     private static final int MAXSubComponent = 50;
59     private static final int MAXAssignTo = 50;
60     private static int UserMax = 0;
61     private static int MethodMax = 0;
62     private static int IssueMax = 0;
63     private static int FilesMax = 0;
64     private static int LoggerMax = 0;
65     
66     byte[] array;
67     /** Creates a new instance of Zpracuj */
68     public DbInsertion(byte[] _array) {
69         array = _array;
70     }
71     
72     public void run() {
73         try {
74             ObjectInputStream JavaDoc str = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(array));
75             // create Record Object
76
Record JavaDoc rec = (Record JavaDoc) str.readObject();
77             //add into DB
78
insertIntoDB(rec);
79         } catch (IOException JavaDoc ex) {
80             java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE,
81                     ex.getMessage(), ex);
82         } catch (ClassNotFoundException JavaDoc ex) {
83             java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE,
84                     ex.getMessage(), ex);
85         };
86     }
87     
88     
89     private static void insertIntoDB(Record JavaDoc rec){
90         try {
91             rec = check(rec);
92             PreparedStatement JavaDoc pStmt;
93             pStmt = Utils.getConnection().prepareStatement(INSERT_NBUSER);
94             Integer JavaDoc issueId = getIssueNext();
95             Integer JavaDoc userId = getExistUserId(rec.getUserName());
96             Integer JavaDoc commentNext = new Integer JavaDoc(1);// adding new issue so first comment as well
97
Integer JavaDoc loggerId = getLoggerNext();
98             if (userId == 0){//Adding NbUser to my DB
99
userId = getUserNext();
100                 pStmt.setInt(1, userId);
101                 pStmt.setString(2, rec.getUserName());
102                 pStmt.execute();
103                 pStmt.close();
104             }
105             // adding logger
106
LinkedList JavaDoc<LogRecord JavaDoc> logger = rec.getLogger();
107             ByteArrayOutputStream JavaDoc bStream = new ByteArrayOutputStream JavaDoc();
108             ObjectOutputStream JavaDoc stream = new ObjectOutputStream JavaDoc(bStream);
109             stream.writeObject(logger);
110             stream.flush();
111             byte[] array = bStream.toByteArray();
112             
113             pStmt = getConnection().prepareStatement(INSERT_LOGGER);
114             pStmt.setInt(1, loggerId);
115             pStmt.setBinaryStream(2, new ByteArrayInputStream JavaDoc(array), array.length);
116             pStmt.execute();
117             pStmt.close();
118             // adding issue
119
pStmt = getConnection().prepareStatement(INSERT_ISSUE);
120             pStmt.setInt(1, issueId);
121             pStmt.setString(2, rec.getUserData()[3]);
122             pStmt.setInt(3, loggerId);
123             pStmt.setString(4, rec.getVm());
124             pStmt.setString(5, rec.getVersion());
125             pStmt.setString(6, rec.getUserData()[0]);
126             pStmt.setString(7, rec.getUserData()[1]);
127             pStmt.setString(8, rec.getUserData()[2]);
128             pStmt.setString(9, rec.getOs());
129             pStmt.setString(10, rec.getSeverity().toString());
130             pStmt.execute();
131             pStmt.close();
132             // adding stacktrace
133
pStmt = getConnection().prepareStatement(INSERT_STACKTRACE);
134             pStmt.setInt(1, issueId);
135             pStmt.setString(2, rec.getMessage());
136             pStmt.setString(3, rec.getClassName());
137             pStmt.setNull(4, java.sql.Types.INTEGER);
138             pStmt.execute();
139             pStmt.close();
140             // adding comment
141
pStmt = getConnection().prepareStatement(INSERT_COMMENT);
142             pStmt.setInt(1, commentNext);
143             pStmt.setInt(2, issueId);
144             pStmt.setInt(3, userId);
145             pStmt.setString(4, rec.getUserData()[4]);
146             pStmt.execute();
147             pStmt.close();
148             // adding issueNbUser
149
pStmt = getConnection().prepareStatement(INSERT_ISSUENBUSER);
150             pStmt.setInt(1, issueId);
151             pStmt.setInt(2, userId);
152             java.util.Calendar JavaDoc now = new java.util.GregorianCalendar JavaDoc();
153             now.setTime(new java.util.Date JavaDoc());
154             pStmt.setTimestamp(3, new java.sql.Timestamp JavaDoc(now.getTimeInMillis()));
155             pStmt.execute();
156             pStmt.close();
157             //add hashCode
158
pStmt = getConnection().prepareStatement(INSERT_HASH);
159             pStmt.setInt(1, new CheckIssue().countHash(rec.getThrowable()));
160             pStmt.setInt(2, issueId);
161             pStmt.execute();
162             pStmt.close();
163             //add stack trace
164
pStmt = getConnection().prepareStatement(INSERT_METHOD);
165             PreparedStatement JavaDoc pStmtFiles = getConnection().prepareStatement(INSERT_NBFILES);
166             PreparedStatement JavaDoc pStmtLines = getConnection().prepareStatement(INSERT_LINE);
167             pStmtLines.setInt(1, issueId);
168             Integer JavaDoc methodId, fileId, lineNumber;
169             for (int i = 0; i < rec.getThrowable().length; i++) {
170                 StackTraceElement JavaDoc element = rec.getThrowable()[i];
171                 String JavaDoc methodName = element.getClassName().concat(".").concat(element.getMethodName());
172                 String JavaDoc fileName = element.getFileName();
173                 if (methodName.length()> MAXMethodName) methodName = methodName.substring(methodName.length()-MAXMethodName);
174                 methodId = getExistingMethodId(methodName);
175                 if (methodId==0){//adding method into table
176
methodId=getMethodNext();
177                     pStmt.setInt(1, methodId);
178                     pStmt.setString(2, methodName);
179                     pStmt.execute();
180                 }
181                 fileId = getExistingFileId(fileName);
182                 if (fileId==0){//adding file into table
183
fileId=getFilesNext();
184                     pStmtFiles.setInt(1, fileId);
185                     pStmtFiles.setString(2, fileName);
186                     pStmtFiles.execute();
187                 }
188                 if (element.getLineNumber()>=0) lineNumber = element.getLineNumber();
189                 else lineNumber = 0;
190                 pStmtLines.setInt(2, methodId);
191                 pStmtLines.setInt(3, fileId);
192                 pStmtLines.setInt(4, lineNumber);
193                 pStmtLines.setInt(5, i);
194                 pStmtLines.execute();
195             }
196             pStmt.close();
197             pStmtFiles.close();
198             pStmtLines.close();
199             getConnection().commit();
200             java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.INFO, "TRANSACTION COMMITED");
201         } catch (SQLException JavaDoc ex) {
202             java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, ex.getMessage(), ex);
203         } catch (IOException JavaDoc ex) {
204             java.util.logging.Logger.getLogger(DbInsertion.class.getName()).log(java.util.logging.Level.SEVERE, ex.getMessage(), ex);
205         };
206         
207     }
208     
209     
210     private static Record JavaDoc check(Record JavaDoc rec){
211         if (rec.getUserName().length() > MAXUsrName) rec.setUserName(rec.getUserName().substring(0, MAXUsrName));
212         if (rec.getMessage().length() > MAXMessage) rec.setMessage(rec.getMessage().substring(0, MAXMessage));
213         if (rec.getClassName().length() > MAXClass) rec.setClassName(rec.getClassName().substring(0, MAXClass));
214         if (rec.getVm().length() > MAXVM) rec.setVm(rec.getVm().substring(0, MAXVM));
215         if (rec.getVersion().length() > MAXProductVersion) rec.setVersion(rec.getVersion().substring(0, MAXProductVersion));
216         if (rec.getOs().length() > MAXOperatingSystem) rec.setOs(rec.getOs().substring(0, MAXOperatingSystem));
217         String JavaDoc[] userData = rec.getUserData();
218         if (userData[0].length()> MAXComponent)userData[0] = userData[0].substring(0, MAXComponent);
219         if (userData[1].length()> MAXSubComponent)userData[1] = userData[1].substring(0, MAXSubComponent);
220         if (userData[2].length()> MAXAssignTo)userData[2] = userData[2].substring(0, MAXAssignTo);
221         if (userData[3].length()> MAXSummary)userData[3] = userData[3].substring(0, MAXSummary);
222         if (userData[4].length()> MAXComment)userData[4] = userData[4].substring(0, MAXComment);
223         return rec;
224     }
225     
226     private static int getExistingMethodId(String JavaDoc methodName) throws SQLException JavaDoc{
227         return getExistId("Method", methodName);
228     }
229     
230     private static int getExistingFileId(String JavaDoc fileName) throws SQLException JavaDoc{
231         return getExistId("NbFiles", fileName);
232     }
233     
234     private static int getExistUserId(String JavaDoc userName) throws SQLException JavaDoc{
235         return getExistId("NbUser", userName);
236     }
237     
238     private static int getExistId(String JavaDoc tableName, String JavaDoc subjectName) throws SQLException JavaDoc{
239         Statement JavaDoc stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
240         ResultSet JavaDoc result = executeQuery(stmt, MessageFormat.format(SELECT_EXIST_ID, tableName, subjectName));
241         int value = 0;
242         if (result.next()) value = result.getInt(1);
243         stmt.close();
244         return value;
245     }
246     
247     private static Integer JavaDoc getCommentNext(Integer JavaDoc issueID) throws SQLException JavaDoc{
248         Statement JavaDoc stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
249         ResultSet JavaDoc result = executeQuery(stmt, "SELECT max(Id) FROM Comment WHERE Id = '" + issueID.toString()+"'");
250         result.next();
251         int value = result.getInt(1);
252         stmt.close();
253         return value+1;
254     }
255     
256     private static synchronized Integer JavaDoc getLoggerNext() throws SQLException JavaDoc{
257         if (LoggerMax == 0) LoggerMax = getMax("LOGGER");
258         return ++LoggerMax;
259     }
260     
261     
262     private static synchronized Integer JavaDoc getFilesNext() throws SQLException JavaDoc{
263         if (FilesMax == 0) FilesMax = getMax("NbFiles");
264         return ++FilesMax;
265     }
266     
267     private static synchronized Integer JavaDoc getUserNext() throws SQLException JavaDoc{
268         if (UserMax == 0) UserMax = getMax("NbUser");
269         return ++UserMax;
270     }
271     
272     private static synchronized Integer JavaDoc getMethodNext() throws SQLException JavaDoc{
273         if (MethodMax == 0) MethodMax = getMax("Method");
274         return ++MethodMax;
275     }
276     
277     private static synchronized Integer JavaDoc getIssueNext() throws SQLException JavaDoc{
278         if (IssueMax == 0) IssueMax = getMax("ISSUE");
279         return ++IssueMax;
280     }
281     
282     private static int getMax(String JavaDoc tableName) throws SQLException JavaDoc{
283         Statement JavaDoc stmt = Utils.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
284         ResultSet JavaDoc result = executeQuery(stmt, MessageFormat.format(SELECT_MAX_ID, tableName));
285         result.next();
286         int value = result.getInt(1);
287         stmt.close();
288         return value;
289     }
290     
291     public static void main(String JavaDoc[] args) throws SQLException JavaDoc{
292         Record JavaDoc rec = new Record JavaDoc();
293         String JavaDoc[] userData = new String JavaDoc[rec.ITEMS_COUNT];
294         rec.setClassName("java.lang.NPE");
295         rec.setVersion("version");
296         rec.setUserName("jindra");
297         rec.setOs("OS");
298         Throwable JavaDoc tr = new NullPointerException JavaDoc("PROBLEM");
299         rec.setThrowable(tr.getStackTrace());
300         rec.setVm("TIGER");
301         rec.setUserData(userData);
302     }
303     
304     
305 }
306
Popular Tags