View Javadoc

1   package org.codehaus.mojo.dashboard.report.plugin;
2   
3   /*
4    * Copyright 2007 David Vicente
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.text.SimpleDateFormat;
20  import java.util.Date;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.Set;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.codehaus.mojo.dashboard.report.plugin.beans.AbstractReportBean;
29  import org.codehaus.mojo.dashboard.report.plugin.beans.DashBoardMavenProject;
30  import org.hibernate.Query;
31  
32  /**
33   * A Dashboard report which aggregates all other report results and stores all results in database.
34   *
35   * @author <a href="dvicente72@gmail.com">David Vicente</a>
36   * @goal persist
37   */
38  public class DashBoardDBMojo extends AbstractDashBoardMojo
39  {
40  
41      private Date generatedDate;
42  
43      private boolean isPropHibernateSet = false;
44  
45      /*
46       * (non-Javadoc)
47       *
48       * @see org.apache.maven.plugin.Mojo#execute()
49       */
50      public void execute() throws MojoExecutionException, MojoFailureException
51      {
52  
53          boolean persistDB = this.canPersistDB();
54          boolean recursive = this.isRecursive();
55          if (recursive)
56          {
57              this.dashBoardUtils = DashBoardUtils.getInstance(this.getLog(), this.mavenProjectBuilder,
58                      this.localRepository, true);
59              this.generatedDate = new Date(System.currentTimeMillis());
60              DashBoardMavenProject mavenProject = this.dashBoardUtils.getDashBoardMavenProject(this.project,
61                      this.dashboardDataFile, this.generatedDate);
62              this.dashBoardUtils.saveXMLDashBoardReport(this.project, mavenProject, this.dashboardDataFile);
63              if (persistDB)
64              {
65                  this.configureHibernateDriver();
66  
67                  long start = System.currentTimeMillis();
68  
69                  this.getLog().info("DashBoardDBMojo project = " + this.project.getName());
70                  this.getLog().info("DashBoardDBMojo nb modules = " + this.project.getModules().size());
71                  this.getLog().info("DashBoardDBMojo is root = " + this.project.isExecutionRoot());
72                  this.getLog().info("DashBoardDBMojo base directory = " + this.project.getBasedir());
73                  this.getLog().info("DashBoardDBMojo output directory = " + this.outputDirectory);
74                  this.getLog().info(
75                          "DashBoardDBMojo project language = "
76                                  + this.project.getArtifact().getArtifactHandler().getLanguage());
77                  this.refactorMavenProject(mavenProject);
78  
79                  this.hibernateService.saveOrUpdate(mavenProject);
80  
81                  long end = System.currentTimeMillis();
82                  SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS", Locale.getDefault());
83                  this.getLog().info(
84                                      "DashBoardDBMojo save Dashboard elapsed time = "
85                                                      + formatter.format( new Date( end - start ) ) );
86              }
87          }
88      }
89  
90      private boolean canPersistDB()
91      {
92          boolean persist = false;
93  
94          boolean recursive = this.isRecursive();
95          boolean root = this.project.isExecutionRoot();
96  
97          this.isPropHibernateSet = this.isDBAvailable();
98  
99          if (recursive && root && this.isPropHibernateSet)
100         {
101             persist = true;
102         }
103         else
104         {
105             if (!root)
106             {
107                 this.getLog().warn("DashBoardDBMojo: Not root project - skipping persist goal.");
108             }
109             if (!this.isPropHibernateSet)
110             {
111                 this.getLog().warn("DashBoardDBMojo: Hibernate properties not set - skipping persist goal.");
112             }
113         }
114 
115         return persist;
116     }
117 
118     private boolean isRecursive()
119     {
120         boolean recursive =
121             ( this.project.getCollectedProjects().size() < this.project.getModules().size() ) ? false : true;
122         if (!recursive)
123         {
124             this.getLog().warn("DashBoardDBMojo: Not recursive into sub-projects - skipping XML generation.");
125         }
126         return recursive;
127     }
128 
129     private void refactorMavenProject(DashBoardMavenProject mavenProject)
130     {
131         StringBuffer queryString = new StringBuffer();
132         queryString.append("select m.id from DashBoardMavenProject m where ");
133         queryString.append("m.artifactId = :artifactid ");
134         queryString.append("and m.groupId = :groupid ");
135         queryString.append("and m.version = :version ");
136 
137         Query query = this.hibernateService.getSession().getNamedQuery(
138                 "org.codehaus.mojo.dashboard.report.plugin.beans.DashBoardMavenProject.getDashBoardMavenProjectID");
139         query.setParameter("artifactid", mavenProject.getArtifactId());
140         query.setParameter("groupid", mavenProject.getGroupId());
141         query.setParameter("version", mavenProject.getVersion());
142 
143         List result = query.list();
144         if (result != null && !result.isEmpty())
145         {
146             long id = ((Long) (result.get(0))).longValue();
147             mavenProject.setId(id);
148         }
149 
150         Set reports = mavenProject.getReports();
151         Iterator iter = reports.iterator();
152         while (iter.hasNext())
153         {
154             AbstractReportBean report = (AbstractReportBean) iter.next();
155             if (report != null)
156             {
157                 report.setMavenProject(mavenProject);
158             }
159         }
160         Set modules = mavenProject.getModules();
161         Iterator iterModule = modules.iterator();
162         while (iterModule.hasNext())
163         {
164             DashBoardMavenProject project = (DashBoardMavenProject) iterModule.next();
165             this.refactorMavenProject(project);
166         }
167     }
168 
169 }