View Javadoc

1   package uk.ac.ebi.intenz.stats.db;
2   
3   import java.io.File;
4   import java.io.FileWriter;
5   import java.io.IOException;
6   import java.io.Writer;
7   import java.sql.Connection;
8   import java.sql.SQLException;
9   import java.text.SimpleDateFormat;
10  import java.util.Date;
11  import java.util.Properties;
12  
13  import org.apache.log4j.Logger;
14  import org.apache.velocity.Template;
15  import org.apache.velocity.VelocityContext;
16  import org.apache.velocity.app.Velocity;
17  import org.apache.velocity.exception.ParseErrorException;
18  import org.apache.velocity.exception.ResourceNotFoundException;
19  
20  import uk.ac.ebi.biobabel.util.db.OracleDatabaseInstance;
21  import uk.ac.ebi.intenz.stats.IIntEnzStatistics;
22  
23  /**
24   * Standalone application to dump statistics from a database connection.
25   * @author rafalcan
26   * @since 1.1.0
27   */
28  public class IntEnzDbStatisticsApp {
29  	
30      private static final Logger LOGGER = Logger.getLogger(IntEnzDbStatisticsApp.class);                                                                                                                             
31      
32      /**
33       * Gathers statistics and dumps them to stdout in CSV format.                                                                                                                                                 
34       * @param args <ol>
35       *      <li>database connection configuration (see
36       *          {@link OracleDatabaseInstance})</li>
37       *      <li>output directory for generated file(s)</li>
38       * </ol>
39       * @throws SQLException In case of problem closing the connection                                                                                                                                             
40       */
41      public static void main(String args[]) throws SQLException{                                                                                                                                                   
42          Connection con = null;    
43          try {
44              con = OracleDatabaseInstance.getInstance(args[0]).getConnection();                                                                                                                                    
45              File outputDir = new File(args[1]);
46              IIntEnzStatistics statistics = new IntEnzDbStatistics(con);                                                                                                                                           
47              
48              Properties velocityProps = new Properties();
49              velocityProps.load(IntEnzDbStatisticsApp.class.getClassLoader()
50                      .getResourceAsStream("velocity.properties"));
51              Velocity.init(velocityProps);
52              VelocityContext context = new VelocityContext();
53              context.put("statistics", statistics);
54              String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());                                                                                                                                  
55              context.put("date", date);
56              output(context, "csv", outputDir);
57          } catch (Exception e){        
58              LOGGER.error("Unable to output statistics", e);                                                                                                                                                       
59          } finally {
60              if (con != null) con.close();                                                                                                                                                                         
61          }
62      }
63  
64      private static void output(VelocityContext context, String format, File outputDir)                                                                                                                            
65      throws IOException {
66          Template template = null; 
67          try {
68              template = Velocity.getTemplate(format + ".vm");
69          } catch (ResourceNotFoundException e) {
70              LOGGER.error("Template not found", e);                                                                                                                                                                
71              return;
72          } catch (ParseErrorException e) {
73              LOGGER.error("Error parsing the template", e);                                                                                                                                                        
74              return;
75          } catch (Exception e) {       
76              LOGGER.error("Error getting velocity template", e);                                                                                                                                                   
77              return;
78          }
79          File outputFile = new File(outputDir,
80                  "intenz-statistics_" + context.get("date") + "." + format);                                                                                                                                         
81          Writer writer = null;     
82          try {
83              writer = new FileWriter(outputFile);
84              template.merge(context, writer);
85          } catch (IOException e) {     
86              LOGGER.error("Unable to write file", e);                                                                                                                                                              
87          } finally {
88              if (writer != null) writer.close();                                                                                                                                                                   
89          }
90      }
91  
92  }