KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > results > persistent > jdbc > AggregatedResults


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23
24 package org.hammurapi.results.persistent.jdbc;
25
26 import java.lang.reflect.InvocationHandler JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.TreeMap JavaDoc;
43
44 import org.hammurapi.HammurapiException;
45 import org.hammurapi.HammurapiMeasurement;
46 import org.hammurapi.HammurapiRuntimeException;
47 import org.hammurapi.Inspector;
48 import org.hammurapi.InspectorDescriptor;
49 import org.hammurapi.SimpleViolation;
50 import org.hammurapi.Violation;
51 import org.hammurapi.Waiver;
52 import org.hammurapi.WaiverSet;
53 import org.hammurapi.results.Annotation;
54 import org.hammurapi.results.InspectorSummary;
55 import org.hammurapi.results.persistent.jdbc.sql.AggregatedResultsInspectorViolations;
56 import org.hammurapi.results.persistent.jdbc.sql.AggregatedResultsMetric;
57 import org.hammurapi.results.persistent.jdbc.sql.AggregatedResultsMetricMeasurement;
58 import org.hammurapi.results.persistent.jdbc.sql.MeasurementImpl;
59 import org.hammurapi.results.persistent.jdbc.sql.ResultsEngine;
60 import org.hammurapi.results.persistent.jdbc.sql.ViolationImpl;
61 import org.hammurapi.results.persistent.jdbc.sql.ViolationJoined;
62
63 import com.pavelvlasov.config.ConfigurationException;
64 import com.pavelvlasov.convert.Converter;
65 import com.pavelvlasov.metrics.Metric;
66 import com.pavelvlasov.persistence.PersistenceException;
67 import com.pavelvlasov.review.Signed;
68 import com.pavelvlasov.review.SimpleSourceMarker;
69 import com.pavelvlasov.review.SourceMarker;
70 import com.pavelvlasov.sql.Parameterizer;
71 import com.pavelvlasov.sql.RowProcessor;
72 import com.pavelvlasov.sql.RowProcessorEx;
73
74 /**
75  *
76  * @author Pavel Vlasov
77  * @version $Revision: 1.19 $
78  */

79 public class AggregatedResults extends BasicResults implements org.hammurapi.results.AggregatedResults {
80     protected static final byte VIOLATION = 0;
81     protected static final byte WAIVED_VIOLATION = 1;
82     protected static final byte WARNING = 2;
83
84     AggregatedResults(WaiverSet waiverSet, ResultsFactory factory) throws SQLException JavaDoc {
85         super(factory);
86         this.waiverSet=waiverSet;
87         id=factory.nextPK("RESULT");
88         factory.addToCache(this);
89         date=new Date JavaDoc();
90         factory.getSQLProcessor().processUpdate(
91                 "INSERT INTO RESULT (ID, TYPE, RESULT_DATE, CODEBASE) VALUES (?, ?, ?, 0)",
92                 new Parameterizer() {
93                     public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
94                         ps.setInt(1, id);
95                         ps.setString(2, AggregatedResults.this.getClass().getName());
96                         ps.setDate(3, new java.sql.Date JavaDoc(date.getTime()));
97                     }
98                 });
99         
100         // Self parent for Oracle
101
factory.getSQLProcessor().processUpdate(
102                 "INSERT INTO RESULT_NET (PARENT, CHILD) VALUES (?,?)",
103                 new Parameterizer() {
104                     public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
105                         ps.setInt(1, id);
106                         ps.setInt(2, id);
107                     }
108                 });
109         
110         // Self kindred
111
factory.getSQLProcessor().processUpdate(
112                 "INSERT INTO KINDRED (ANCESTOR, DESCENDANT) VALUES (?, ?)",
113                 new Parameterizer() {
114                     public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
115                         ps.setInt(1, id);
116                         ps.setInt(2, id);
117                     }
118                 });
119     }
120         
121     /**
122      * @param id
123      * @param factory
124      * @throws SQLException
125      */

126     public AggregatedResults(int id, ResultsFactory factory) throws SQLException JavaDoc {
127         super(id, factory);
128     }
129     
130     /**
131      * Add violation to severity summary
132      * @param violation
133      * @return Waiver
134      * @throws HammurapiException
135      */

136     public Waiver addViolation(final Violation violation) throws HammurapiException {
137         final Waiver ret = waiverSet==null ? null : waiverSet.requestWaiver(violation, false);
138         try {
139             ResultsEngine resultsEngine = factory.getResultsEngine();
140             if (ret==null) {
141                 severitySummary=null;
142                 InspectorDescriptor descriptor = insertViolation(violation, VIOLATION, null);
143                 ++violationsNumber;
144                 resultsEngine.updateAggregatedResultsViolations(violationsNumber, id);
145                 
146                 final int severity = descriptor.getSeverity().intValue();
147                 if (maxSeverity==null || maxSeverity.intValue()>severity) {
148                     maxSeverity=new Integer JavaDoc(severity);
149                     resultsEngine.updateAggregatedResultsMaxSeverity((short) severity, id);
150                 }
151                             
152                 /**
153                  * Violations with severity >=5 considered as hints.
154                  */

155                 if (severity>0 && severity<5) {
156                     violationLevel+=Math.pow(10, 1-descriptor.getSeverity().doubleValue());
157                     resultsEngine.setViolationLevel(violationLevel, getId());
158                 }
159             } else {
160                 ++waivedViolationsNumber;
161                 resultsEngine.incWaivedViolations(getId());
162             }
163         } catch (SQLException JavaDoc e) {
164             throw new HammurapiException(e);
165         }
166         return ret;
167     }
168     
169     protected interface ViolationConfigurator {
170         void setViolationInfo(org.hammurapi.results.persistent.jdbc.sql.Violation sqlViolation) throws SQLException JavaDoc;
171     }
172     
173     protected InspectorDescriptor insertViolation(final Violation violation, byte type, ViolationConfigurator configurator) {
174         InspectorDescriptor descriptor = violation.getDescriptor();
175         try {
176             maybeInsertInspector(descriptor);
177             
178             ViolationImpl sqlViolation=new ViolationImpl(true);
179             sqlViolation.setId(factory.getSQLProcessor().nextPK("PRIMARY_KEY", "VIOLATION"));
180             sqlViolation.setViolationType(type);
181
182             sqlViolation.setResultId(getId());
183             sqlViolation.setReportId(factory.getReportId());
184             if (descriptor!=null) {
185                 sqlViolation.setInspector(descriptor.getName()); // INSPECTOR
186
}
187             
188             String JavaDoc message = violation.getMessage();
189             if (message!=null) {
190                 sqlViolation.setMessageId(new Integer JavaDoc(factory.addMessage(message))); // MESSAGE
191
}
192             
193             SourceMarker source = violation.getSource();
194             if (source!=null) {
195                 sqlViolation.setSourceId(source.getSourceId()); // SOURCE
196
sqlViolation.setLine(source.getLine()); // LINE
197
sqlViolation.setCol(source.getColumn()); // COL
198

199                 if (source instanceof Signed) {
200                      String JavaDoc signature = ((Signed) source).getSignature();
201                      if (signature!=null && signature.startsWith(source.getSourceURL())) {
202                         sqlViolation.setSignaturePostfix(signature.substring(source.getSourceURL().length())); // SOURCE_SIGNATURE
203
}
204                 }
205             }
206             
207             if (configurator!=null) {
208                 configurator.setViolationInfo(sqlViolation);
209             }
210             
211             factory.getResultsEngine().insertViolation(sqlViolation);
212         } catch (SQLException JavaDoc e1) {
213             throw new HammurapiRuntimeException(e1);
214         }
215         return descriptor;
216     }
217
218     public void addWarning(Violation warning) {
219         insertViolation(warning, WARNING, null);
220         hasWarnings=true;
221     }
222         
223     private Set JavaDoc metricIds=new HashSet JavaDoc();
224     
225     private class MetricEntry {
226         int meid;
227         String JavaDoc name;
228         
229         public MetricEntry(int id, String JavaDoc name) {
230             super();
231             this.meid = id;
232             this.name = name;
233         }
234         
235         public boolean equals(Object JavaDoc obj) {
236             return obj instanceof MetricEntry && ((MetricEntry) obj).meid==meid && ((MetricEntry) obj).name.equals(name);
237         }
238         
239         public int hashCode() {
240             return meid ^ name.hashCode();
241         }
242     }
243     
244     public void addMetric(final SourceMarker source, final String JavaDoc name, final double value) {
245         try {
246             ResultsEngine engine = factory.getResultsEngine();
247             MetricEntry metricEntry = new MetricEntry(getId(), name);
248             if (!metricIds.contains(metricEntry)) {
249                 org.hammurapi.results.persistent.jdbc.sql.Metric dbMetric = engine.getMetric(getId(), name);
250                 if (dbMetric==null) {
251                     engine.insertMetric(getId(), name);
252                 }
253                 metricIds.add(metricEntry);
254             }
255             
256             MeasurementImpl measurement = new MeasurementImpl(true);
257             measurement.setId(factory.getSQLProcessor().nextPK("PRIMARY_KEY", "MEASUREMENT"));
258             if (source!=null) {
259                 measurement.setCol(source.getColumn());
260                 measurement.setLine(source.getLine());
261                 measurement.setSource(source.getSourceURL());
262             }
263             measurement.setResultId(getId());
264             measurement.setMeasurementValue(value);
265             measurement.setName(name);
266             engine.insertMeasurement(measurement);
267             
268             metrics=null;
269         } catch (SQLException JavaDoc e) {
270             throw new HammurapiRuntimeException(e);
271         }
272     }
273     
274     private Map JavaDoc metrics;
275     
276     public Map JavaDoc getMetrics() {
277         if (metrics==null) {
278             metrics=new TreeMap JavaDoc();
279             Iterator JavaDoc it=factory.getResultsEngine().getAggregatedResultsMetric(id).iterator();
280             while (it.hasNext()) {
281                 final AggregatedResultsMetric arm=(AggregatedResultsMetric) it.next();
282                 metrics.put(arm.getName(), new Metric() {
283                     
284                         public int getNumber() {
285                             return arm.getMeasurements();
286                         }
287
288                         public double getMin() {
289                             return arm.getMinValue();
290                         }
291
292                         public double getMax() {
293                             return arm.getMaxValue();
294                         }
295
296                         public double getAvg() {
297                             return arm.getTotalValue()/arm.getMeasurements();
298                         }
299
300                         public double getTotal() {
301                             return arm.getTotalValue();
302                         }
303
304                         public void add(double measurement, long time) {
305                             throw new UnsupportedOperationException JavaDoc("Read-only");
306                         }
307
308                         public void add(Metric metric) {
309                             throw new UnsupportedOperationException JavaDoc("Read-only");
310                         }
311
312                         public double getDeviation() {
313                             // TODO - Calcualte deviation
314
return 0;
315                         }
316                         
317                         private Collection JavaDoc measurements;
318                         
319                         public Collection JavaDoc getMeasurements() {
320                             if (measurements==null) {
321 // System.out.println("getMeasurements: "+measurementsCounter++);
322
try {
323                                     //System.out.println("Loading measurements for "+getId()+" "+arm.getName());
324
measurements=factory.getResultsEngine().getAggregatedResultsMetricMeasurement(
325                                             id,
326                                             arm.getName(),
327                                             new ArrayList JavaDoc(),
328                                             new Converter() {
329
330                                                 public Object JavaDoc convert(Object JavaDoc source) {
331                                                     final AggregatedResultsMetricMeasurement armm=(AggregatedResultsMetricMeasurement) source;
332                                                     return new HammurapiMeasurement() {
333                                                         SourceMarker source=new SourceMarker() {
334                                                             
335                                                             public int getLine() {
336                                                                 return armm.getLine();
337                                                             }
338                         
339                                                             public int getColumn() {
340                                                                 return armm.getCol();
341                                                             }
342                         
343                                                             public String JavaDoc getSourceURL() {
344                                                                 return armm.getSource();
345                                                             }
346                     
347                                                             public Integer JavaDoc getSourceId() {
348                                                                 return null;
349                                                             }
350                                                             
351                                                         };
352                                                         
353                                                         public SourceMarker getSource() {
354                                                             return source;
355                                                         }
356                     
357                                                         public double getValue() {
358                                                             return armm.getMeasurementValue();
359                                                         }
360
361                                                         public long getTime() {
362                                                             return 0;
363                                                         }
364                                                     };
365                                                 }
366                                                 
367                                             });
368                                 } catch (SQLException JavaDoc e) {
369                                     throw new HammurapiRuntimeException("Cannot create metric measurement: "+e, e);
370                                 }
371                             }
372                             return measurements;
373                         }
374
375                         public String JavaDoc getName() {
376                             return arm.getName();
377                         }
378                         
379                     });
380             }
381         }
382         return metrics;
383     }
384             
385     public void aggregate(final org.hammurapi.results.AggregatedResults agregee) {
386         final AggregatedResults unWrapped = factory.unWrap(agregee);
387         codeBase+=agregee.getCodeBase();
388         reviews+=agregee.getReviewsNumber();
389         violationsNumber+=agregee.getViolationsNumber();
390         waivedViolationsNumber+=agregee.getWaivedViolationsNumber();
391         violationLevel+=agregee.getViolationLevel();
392         hasWarnings|=agregee.hasWarnings();
393                 
394         try {
395             Number JavaDoc ams=agregee.getMaxSeverity();
396             if (ams!=null && (maxSeverity==null || maxSeverity.intValue()>ams.intValue())) {
397                 maxSeverity=ams;
398                 factory.getResultsEngine().updateAggregatedResultsMaxSeverity(ams.shortValue(), id);
399             }
400                         
401             final Parameterizer parameterizer = new Parameterizer() {
402                 public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
403                     idParameterizer.parameterize(ps);
404                     ps.setInt(2, unWrapped.getId());
405                 }
406             };
407             
408             factory.getSQLProcessor().processSelect(
409                     "SELECT * FROM RESULT_NET WHERE PARENT=? AND CHILD=?",
410                     parameterizer,
411                     new RowProcessorEx() {
412                         public void onEmptyResultSet() throws SQLException JavaDoc {
413                             factory.getSQLProcessor().processUpdate(
414                                     "INSERT INTO RESULT_NET (PARENT, CHILD) VALUES (?,?)",
415                                     parameterizer);
416                         }
417
418                         public boolean process(ResultSet JavaDoc rs) {
419                             // Doing nothing - already set.
420
return false;
421                         }
422                     });
423         
424             factory.getSQLProcessor().processUpdate(
425                     "INSERT INTO KINDRED (ANCESTOR, DESCENDANT) " +
426                     "SELECT A.ANCESTOR, D.DESCENDANT FROM KINDRED A, KINDRED D WHERE A.DESCENDANT=? AND D.ANCESTOR=? ",
427                     //"AND NOT EXISTS (SELECT * FROM KINDRED C WHERE C.ANCESTOR=A.ANCESTOR AND C.DESCENDANT=D.DESCENDANT)",
428
new Parameterizer() {
429                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
430                             ps.setInt(1, getId());
431                             ps.setInt(2, unWrapped.getId());
432                         }
433                     });
434         } catch (SQLException JavaDoc e) {
435             throw new HammurapiRuntimeException(e);
436         }
437     }
438                     
439     /**
440      * Sets number of reviews.
441      * @param reviews The reviews to set.
442      */

443     public void setReviewsNumber(final long reviews) {
444         this.reviews = reviews;
445         try {
446             factory.getSQLProcessor().processUpdate(
447                     "UPDATE RESULT SET REVIEWS=? WHERE ID=?",
448                     new Parameterizer() {
449                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
450                             ps.setLong(1, reviews);
451                             ps.setInt(2, getId());
452                         }
453                     });
454         } catch (SQLException JavaDoc e) {
455             throw new HammurapiRuntimeException(e);
456         }
457     }
458
459     public void setCodeBase(final long codeBase) {
460         this.codeBase=codeBase;
461         try {
462             factory.getSQLProcessor().processUpdate(
463                     "UPDATE RESULT SET CODEBASE=? WHERE ID=?",
464                     new Parameterizer() {
465                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
466                             ps.setLong(1, codeBase);
467                             ps.setInt(2, getId());
468                         }
469                     });
470         } catch (SQLException JavaDoc e) {
471             throw new HammurapiRuntimeException(e);
472         }
473     }
474
475     public void addAnnotation(Annotation annotation) {
476         try {
477             final String JavaDoc handle=factory.getStorage().put(annotation);
478             factory.getSQLProcessor().processUpdate(
479                     "INSERT INTO ANNOTATION (RESULT_ID, HANDLE) VALUES (?, ?)",
480                     new Parameterizer() {
481                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
482                             idParameterizer.parameterize(ps);
483                             ps.setString(2, handle);
484                         }
485                     });
486         } catch (SQLException JavaDoc e) {
487             throw new HammurapiRuntimeException(e);
488         } catch (PersistenceException e) {
489             throw new HammurapiRuntimeException(e);
490         }
491     }
492
493     public Collection JavaDoc getAnnotations() {
494         final Collection JavaDoc ret=new LinkedList JavaDoc();
495         try {
496             factory.getSQLProcessor().processSelect(
497                     "SELECT HANDLE FROM ANNOTATION WHERE RESULT_ID=?",
498                     idParameterizer,
499                     new RowProcessor() {
500                         public boolean process(ResultSet JavaDoc rs) throws SQLException JavaDoc {
501                             try {
502                                 ret.add(factory.getStorage().get(rs.getString("HANDLE")));
503                             } catch (PersistenceException e) {
504                                 throw new HammurapiRuntimeException(e);
505                             }
506                             return true;
507                         }
508                     });
509         } catch (SQLException JavaDoc e) {
510             throw new HammurapiRuntimeException(e);
511         }
512         return ret;
513     }
514
515     private WaiverSet waiverSet;
516
517     /**
518      * @param descriptor
519      * @throws SQLException
520      */

521     protected void maybeInsertInspector(final InspectorDescriptor descriptor) throws SQLException JavaDoc {
522         if (descriptor!=null) {
523             factory.getSQLProcessor().processSelect(
524                     "SELECT * FROM INSPECTOR WHERE REPORT_ID=? AND NAME=?",
525                     new Parameterizer() {
526                         public void parameterize(PreparedStatement JavaDoc preparedStatement) throws SQLException JavaDoc {
527                             preparedStatement.setInt(1, factory.getReportId());
528                             preparedStatement.setString(2, descriptor.getName());
529                         }
530                     },
531                     
532                     new RowProcessorEx() {
533                         public boolean process(ResultSet JavaDoc resultSet) {
534                             return false;
535                         }
536     
537                         public void onEmptyResultSet() throws SQLException JavaDoc {
538                             factory.getSQLProcessor().processUpdate(
539                                     "INSERT INTO INSPECTOR (REPORT_ID, NAME, SEVERITY, DESCRIPTION, CONFIG_INFO) VALUES (?, ?, ?, ?, ?)",
540                                     new Parameterizer() {
541                                         public void parameterize(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
542                                             ps.setInt(1, factory.getReportId());
543                                             ps.setString(2, descriptor.getName());
544                                             ps.setShort(3, descriptor.getSeverity().shortValue());
545                                             ps.setString(4, descriptor.getDescription());
546                                             try {
547                                                 Inspector inspector = descriptor.getInspector();
548                                                 if (inspector!=null) {
549                                                     ps.setString(5, inspector.getConfigInfo());
550                                                 }
551                                             } catch (ConfigurationException e) {
552                                                 throw new HammurapiRuntimeException("Can't obtain inspector configuration info", e);
553                                             }
554                                         }
555                                     });
556                         }
557                     });
558         }
559     }
560         
561     private Map JavaDoc severitySummary;
562     
563     public Map JavaDoc getSeveritySummary() {
564         if (severitySummary==null) {
565             severitySummary=new TreeMap JavaDoc();
566             Iterator JavaDoc it=factory.getResultsEngine().getAggregatedResultsSeveritySummary(id).iterator();
567             while (it.hasNext()) {
568                 Number JavaDoc severity=(Number JavaDoc) it.next();
569                 severitySummary.put(severity, createInspectorSummaryMapProxy(severity));
570             }
571         }
572         return severitySummary;
573     }
574     
575     public Collection JavaDoc getWarnings() {
576         try {
577             return factory.getResultsEngine().getAllWarningsJoined(getId(), new LinkedList JavaDoc(), violationConverter);
578         } catch (SQLException JavaDoc e) {
579             throw new HammurapiRuntimeException(e);
580         }
581     }
582     
583     private Map JavaDoc createInspectorSummaryMapProxy(final Number JavaDoc severity) {
584         return (Map JavaDoc) Proxy.newProxyInstance(
585                 getClass().getClassLoader(),
586                 new Class JavaDoc[] {Map JavaDoc.class},
587                 new InvocationHandler JavaDoc() {
588
589                     private Map JavaDoc target;
590                     
591                     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
592                         if (target==null) {
593                             target=instantiateInspectorSummaryMap(severity);
594                         }
595                         return method.invoke(target, args);
596                     }
597                 });
598     }
599     
600     /**
601      * @param severity
602      * @param map
603      */

604     private Map JavaDoc instantiateInspectorSummaryMap(final Number JavaDoc severity) {
605         class InspectorInfo {
606             String JavaDoc name;
607             String JavaDoc description;
608             String JavaDoc configInfo;
609             
610             InspectorInfo(org.hammurapi.results.persistent.jdbc.sql.Inspector arim) {
611                 name=arim.getName();
612                 description=arim.getDescription();
613                 configInfo=arim.getConfigInfo();
614             }
615             
616             public boolean equals(Object JavaDoc obj) {
617                 if (obj==this) {
618                     return true;
619                 } else if (obj instanceof InspectorInfo) {
620                     InspectorInfo otherInfo=(InspectorInfo) obj;
621                     return equalStrings(name, otherInfo.name)
622                         && equalStrings(description, otherInfo.description)
623                         && equalStrings(configInfo, otherInfo.configInfo);
624                 } else {
625                     return super.equals(obj);
626                 }
627             }
628             
629             boolean equalStrings(String JavaDoc a, String JavaDoc b) {
630                 if (a==null) {
631                     return b==null;
632                 }
633                 
634                 return a.equals(b);
635             }
636             
637             public int hashCode() {
638                 return (name==null ? 0 : name.hashCode()) ^ (description==null ? 0 : description.hashCode()) ^ (configInfo==null ? 0 : configInfo.hashCode());
639             }
640         }
641         
642         final Map JavaDoc infoMap=new HashMap JavaDoc();
643         
644         Iterator JavaDoc it=factory.getResultsEngine().getAggregatedResultsInspectorSummary(id, severity.shortValue()).iterator();
645         while (it.hasNext()) {
646             org.hammurapi.results.persistent.jdbc.sql.Inspector arim=(org.hammurapi.results.persistent.jdbc.sql.Inspector) it.next();
647             InspectorInfo inspectorInfo=new InspectorInfo(arim);
648             Set JavaDoc ids=(Set JavaDoc) infoMap.get(inspectorInfo);
649             if (ids==null) {
650                 ids=new HashSet JavaDoc();
651                 infoMap.put(inspectorInfo, ids);
652             }
653             
654             ids.add(new Integer JavaDoc(arim.getReportId()));
655         }
656                 
657         final Map JavaDoc map=new TreeMap JavaDoc();
658         it=infoMap.entrySet().iterator();
659         while (it.hasNext()) {
660             Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
661             InspectorInfo info=(InspectorInfo) entry.getKey();
662             Set JavaDoc ids = (Set JavaDoc) entry.getValue();
663             StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
664             Iterator JavaDoc iit=ids.iterator();
665             while (iit.hasNext()) {
666                 sb.append(iit.next());
667                 if (iit.hasNext()) {
668                     sb.append(",");
669                 }
670             }
671             
672             map.put(info.name+" ["+sb.toString()+"]",
673                 createInspectorSummary(
674                     severity,
675                     info.name,
676                     info.description,
677                     info.configInfo,
678                     sb.toString()));
679         }
680         
681         return map;
682     }
683     
684     /**
685      * @param severity
686      * @param inspectorName
687      * @param description
688      * @param configInfo
689      * @return inspector summary
690      */

691     private InspectorSummary createInspectorSummary(
692             final Number JavaDoc severity,
693             final String JavaDoc inspectorName,
694             final String JavaDoc description,
695             final String JavaDoc configInfo,
696             final String JavaDoc inspectorReportIds)
697     {
698         return new InspectorSummary() {
699
700             public String JavaDoc getDescription() {
701                 return description;
702             }
703
704             private List JavaDoc locations;
705             
706             public List JavaDoc getLocations() {
707                 if (locations==null) {
708                     locations=new ArrayList JavaDoc();
709                     Iterator JavaDoc it=factory.getResultsEngine().getAggregatedResultsInspectorViolations(id, inspectorName).iterator();
710                     while (it.hasNext()) {
711                         final AggregatedResultsInspectorViolations ariv=(AggregatedResultsInspectorViolations) it.next();
712                         locations.add(new SourceMarker() {
713
714                             public int getLine() {
715                                 return ariv.getLine();
716                             }
717
718                             public int getColumn() {
719                                 return ariv.getCol();
720                             }
721
722                             public String JavaDoc getSourceURL() {
723                                 if (ariv.getCompilationUnitPackage()==null || ariv.getCompilationUnitPackage().length()==0) {
724                                     return ariv.getCompilationUnitName();
725                                 }
726                                 
727                                 return ariv.getCompilationUnitPackage().replace('.','/')+'/'+ariv.getCompilationUnitName();
728                             }
729
730                             public Integer JavaDoc getSourceId() {
731                                 return ariv.getSourceId();
732                             }
733                             
734                         });
735                     }
736                 }
737                 
738                 return locations;
739             }
740             
741             public int getLocationsCount() {
742                 return getLocations().size();
743             }
744
745             public String JavaDoc getName() {
746                 return inspectorName;
747             }
748
749             public Number JavaDoc getSeverity() {
750                 return severity;
751             }
752
753             public String JavaDoc getConfigInfo() {
754                 return configInfo;
755             }
756
757             public int compareTo(Object JavaDoc o) {
758                 if (o instanceof InspectorSummary) {
759                     return inspectorName.compareTo(((InspectorSummary) o).getName());
760                 } else if (o instanceof Comparable JavaDoc) {
761                     return -((Comparable JavaDoc) o).compareTo(this);
762                 } else {
763                     return this.hashCode()-o.hashCode();
764                 }
765             }
766
767             public String JavaDoc getVersion() {
768                 return inspectorReportIds;
769             }
770
771             private int baseLineLocationsCount=-1;
772             
773             public int getBaseLineLocationsCount() {
774                 if (baseLineLocationsCount==-1 && baseLineId!=null) {
775                     try {
776                         baseLineLocationsCount=factory.getResultsEngine().getAggregatedResultsInspectorViolationsCount(baseLineId.intValue(), inspectorName);
777                     } catch (SQLException JavaDoc e) {
778                         throw new HammurapiRuntimeException(e);
779                     }
780                 }
781                 return baseLineLocationsCount;
782             }
783         };
784     }
785
786     protected Converter violationConverter=new Converter() {
787         public Object JavaDoc convert(Object JavaDoc o) {
788             final ViolationJoined vj=(ViolationJoined) o;
789             String JavaDoc path;
790             if (vj.getCompilationUnitPackage()==null || vj.getCompilationUnitPackage().length()==0) {
791                 path=vj.getCompilationUnitName();
792             } else {
793                 path=vj.getCompilationUnitPackage().replace('.','/')+'/'+vj.getCompilationUnitName();
794             }
795             return new SimpleViolation(
796                     new SimpleSourceMarker(vj.getCol(), vj.getLine(), path, vj.getSourceId()),
797                     vj.getMessage(),
798                     factory.getInspectorDescriptor(vj.getInspector()));
799         }
800     };
801     
802     private Integer JavaDoc baseLineId;
803
804     public WaiverSet getWaiverSet() {
805         return waiverSet;
806     }
807
808     public void commit() throws HammurapiException {
809         try {
810             factory.getResultsEngine().commit(getId());
811         } catch (SQLException JavaDoc e) {
812             throw new HammurapiException(e);
813         }
814     }
815     
816     public boolean isNew() {
817         try {
818             return factory.getResultsEngine().getAggregateResultsIsNew(id)>0;
819         } catch (SQLException JavaDoc e) {
820             throw new HammurapiRuntimeException(e);
821         }
822     }
823     
824     private BasicResults baseLine;
825
826     public org.hammurapi.results.BasicResults getBaseLine() {
827         if (baseLineId!=null && baseLine==null) {
828             try {
829                 baseLine=new BasicResults(baseLineId.intValue(), factory);
830             } catch (SQLException JavaDoc e) {
831                 throw new HammurapiRuntimeException(e);
832             }
833         }
834         
835         return baseLine;
836     }
837
838     /**
839      * @param baseLineId
840      */

841     void setBaseLineId(Integer JavaDoc baseLineId) {
842         this.baseLineId=baseLineId;
843     }
844 }
845
Popular Tags