View Javadoc
1   package org.kuali.coeus.dc;
2   
3   
4   import org.kuali.coeus.dc.common.db.DbValidatorDaoService;
5   
6   import java.io.BufferedInputStream;
7   import java.io.FileInputStream;
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.sql.Connection;
11  import java.sql.SQLException;
12  import java.util.Collection;
13  import java.util.Properties;
14  import java.util.logging.Level;
15  import java.util.logging.LogManager;
16  import java.util.logging.Logger;
17  
18  /**
19   * The main driver program that runs this conversion program.
20   *
21   * Example arguments:
22   *
23   * -debug -dbcoeuscon jdbc:mysql://localhost/kcbnd?user=kcbnd&password=bndpass -dbricecon jdbc:mysql://localhost/kcbnd?user=kcbnd&password=bndpass proposal
24   */
25  public final class Main {
26  
27      private static final Logger LOG = Logger.getLogger(Main.class.getName());
28  
29      public static void main(String[] args) {
30          initLogging("/org/kuali/coeus/dc/jul-default.properties");
31  
32          CliOptions options = new CliOptions(args);
33  
34          if (!options.isValid()) {
35              System.out.println(options.getCliHelpString());
36              return;
37          }
38  
39          if (options.containsDebug()) {
40              initLogging("/org/kuali/coeus/dc/jul-debug.properties");
41          }
42  
43          if (options.containsHelp()) {
44              System.out.println(options.getCliHelpString());
45              return;
46          }
47  
48          if (options.containsVersion()) {
49              Properties buildProperties = new Properties();
50              try(InputStream stream = Main.class.getResourceAsStream("/META-INF/org/kuali/coeus/coeus-data-conv/project.properties")) {
51                  if (stream != null) {
52                      buildProperties.load(stream);
53                  }
54              } catch (IOException e) {
55                  LOG.log(Level.WARNING, e.getMessage(), e);
56              }
57              final String version = buildProperties.getProperty("project.version");
58  
59              System.out.println(version != null && !version.trim().equals("") ? version : "UNKNOWN");
60              return;
61          }
62  
63          CliOptionsBasedDaoFactory factory = new CliOptionsBasedDaoFactory();
64          factory.setCliOptions(options);
65  
66          if (options.containsValidate()) {
67              DbValidatorDaoService validator = factory.getDbValidatorDaoService();
68  
69              if (validator.isValidCoeusConnection()) {
70                  System.out.println("COEUS SUCCESS: " + options.getCoeusConnectionString());
71              } else {
72                  System.out.println("COEUS FAILED: " + options.getCoeusConnectionString());
73              }
74  
75              if (validator.isValidRiceConnection()) {
76                  System.out.println("RICE SUCCESS: " + options.getRiceConnectionString());
77              } else {
78                  System.out.println("RICE FAILED: " + options.getRiceConnectionString());
79              }
80              return;
81          }
82  
83  
84          try (Connection coeusConnection = factory.getConnectionDaoService().getCoeusConnection();
85              Connection riceConnection = factory.getConnectionDaoService().getRiceConnection()) {
86  
87              if(options.containsProposalPersonRole()) {
88                  factory.getProposalPersonRoleDao().convertParameterValues();
89              }
90  
91              if (options.containsProposal()) {
92                  Collection<String> roleIds = factory.getProposalRoleDao().getRoleIdsToConvert();
93                  factory.getRoleDao().copyRoleMembersToDocAccessType(roleIds, factory.getProposalKimAttributeDocumentValueHandler());
94              }
95  
96              if (options.containsIrb()) {
97                  System.out.println("IRB Conversion not supported");
98              }
99  
100             if (options.containsIacuc()) {
101                 System.out.println("IACUC Conversion not supported");
102             }
103 
104             if (options.containsDryRun()) {
105                 coeusConnection.rollback();
106                 riceConnection.rollback();
107             } else {
108                 coeusConnection.commit();
109                 riceConnection.commit();
110             }
111         } catch (SQLException e) {
112             throw new RuntimeException(e);
113         }
114     }
115 
116     private static void initLogging(String file) {
117         String fname = System.getProperty("java.util.logging.config.file");
118         if (fname == null) {
119             fname = Main.class.getResource(file).getFile();
120         }
121 
122         try (InputStream in = new FileInputStream(fname); BufferedInputStream bin = new BufferedInputStream(in)) {
123             LogManager.getLogManager().readConfiguration(bin);
124         } catch (IOException e) {
125             throw new RuntimeException(e);
126         }
127     }
128 
129 
130 }