1   package uk.ac.ebi.intenz.tools.sib;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.sql.Connection;
6   import java.sql.SQLException;
7   import java.text.SimpleDateFormat;
8   import java.util.ArrayList;
9   import java.util.Date;
10  import java.util.List;
11  
12  import org.apache.log4j.Logger;
13  
14  import uk.ac.ebi.biobabel.util.db.OracleDatabaseInstance;
15  import uk.ac.ebi.intenz.domain.constants.Status;
16  import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber;
17  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
18  import uk.ac.ebi.intenz.domain.enzyme.EnzymeCommissionNumber.Type;
19  import uk.ac.ebi.intenz.domain.exceptions.DomainException;
20  import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
21  import uk.ac.ebi.intenz.tools.sib.helper.SibEntryHelper;
22  import uk.ac.ebi.intenz.tools.sib.sptr_enzyme.EnzymeEntryImpl;
23  import uk.ac.ebi.intenz.tools.sib.writer.EnzymeFlatFileWriter;
24  import uk.ac.ebi.interfaces.sptr.SPTRException;
25  import uk.ac.ebi.xchars.SpecialCharacters;
26  import uk.ac.ebi.xchars.domain.EncodingType;
27  
28  
29  
30  
31  
32  
33  
34  public class EnzymeFlatFileWriterApp {
35  
36    private static final Logger LOGGER =
37  	  Logger.getLogger(EnzymeFlatFileWriterApp.class.getName());
38  
39  
40    private static float versionNumber = 1.0f;
41    private static String versionNumberString ="1";
42    
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53    public static void main(String[] args) {
54  
55  	  assignDateAsVersion();
56  
57  	  List allEnzymes = null;
58  	  allEnzymes = getAllEnzymes(args);
59  	  if (allEnzymes == null) { 
60  		  System.exit(1);
61  	  }
62  
63  	  try {
64  		  LOGGER.info("Exporting '" + ApplicationResources.getInstance().getExportFlatFileName() + "...");
65  		  Long elapsedTimeForExporting = new Long(EnzymeFlatFileWriter.export(allEnzymes, "" + versionNumberString,
66  				  new File(ApplicationResources.getInstance().getExportFlatFileName())));
67  		  LOGGER.info("...export finished (" + (elapsedTimeForExporting.floatValue() / 1000) + " s)");
68  	  } catch (SPTRException e) {
69  		  LOGGER.error("SPTRException: ", e);
70  	  }
71    }
72  
73  
74  
75  
76     
77     
78  
79  
80  
81     private static void assignDateAsVersion () {
82        try{
83          versionNumberString = new SimpleDateFormat("yyyyMMdd").format(new Date(System.currentTimeMillis())).toString();
84        }catch (NumberFormatException e){
85           LOGGER.error(e);
86           System.exit(1);
87        }
88     }
89  
90    
91  
92  
93  
94  
95  
96  
97  
98  
99  
100 
101   private static List getAllEnzymes(String[] args) {
102     LOGGER.debug("Getting all enzymes.");
103     Connection con = null;
104     List allEnzymes = new ArrayList();
105     Long errorId = null;
106     String errorEc = null;
107     try {
108       String dbConfig = ApplicationResources.getInstance().getDbConfig();
109       con = OracleDatabaseInstance.getInstance(dbConfig).getConnection();
110       con.setAutoCommit(false);
111 
112       EnzymeEntryMapper enzymeEntryMapper = new EnzymeEntryMapper();
113       List sibEntries = null;
114        
115       if (args == null || args.length == 0){
116     	  sibEntries = enzymeEntryMapper.exportApprovedSibEntries(con);
117       } else {
118     	  sibEntries = new ArrayList();
119     	  EnzymeCommissionNumber ec = EnzymeCommissionNumber.valueOf(args[0]);
120           EnzymeEntry entry = enzymeEntryMapper.findByEc(
121     			  ec.getEc1(), ec.getEc2(), ec.getEc3(), ec.getEc4(),
122     			  ec.getType().equals(Type.PRELIMINARY)? Status.PRELIMINARY : Status.APPROVED,
123     			  con);
124     	  sibEntries.add(entry);
125       }
126       if (sibEntries != null) {
127         SpecialCharacters encoding = SpecialCharacters.getInstance(null);
128          
129          for (int iii = 0; iii < sibEntries.size(); iii++) {
130           EnzymeEntry enzymeEntry = (EnzymeEntry) sibEntries.get(iii);
131           errorId = enzymeEntry.getId();
132           errorEc = enzymeEntry.getEc().toString();
133           EnzymeEntryImpl sibEnzymeEntry = SibEntryHelper.getSibEnzymeEntry(enzymeEntry, encoding, EncodingType.SWISSPROT_CODE);
134           allEnzymes.add(sibEnzymeEntry);
135         }
136       } else {
137         LOGGER.fatal("No ENZYME data could be retreived from the database.");
138         return null;
139       }
140     } catch (IOException e) {
141       LOGGER.error("Unable to read database configuration: ", e);
142       return null;
143     } catch (DomainException e) {
144       LOGGER.error("Domain exception: ", e);
145       return null;
146     } catch (SPTRException e) {
147       LOGGER.error(errorEc + " [" + errorId + "] - SPTRException: ", e);
148       return null;
149     } catch (SQLException e) {
150       LOGGER.error(errorEc + " [" + errorId + "] - Error while loading ENZYME data from the database: ", e);
151       return null;
152     } catch (Exception e) {
153       LOGGER.error(errorEc + " [" + errorId + "]", e);
154       return null;
155     } finally {
156       try {
157         con.close();
158       } catch (SQLException e) {
159         LOGGER.error("Error while closing the database connection: ", e);
160       }
161     }
162 
163     return allEnzymes;
164   }
165 
166   
167 
168 
169 
170 
171 
172 
173 
174   private static boolean argumentsOk(String[] args) {
175     if (args.length == 2) {
176       if (args[0].equals("version")) {
177         try {
178           Float argVersionNumber = new Float(args[1]);
179           versionNumber = argVersionNumber.floatValue();
180           LOGGER.info("Version number: "+versionNumber);
181         } catch (NumberFormatException e) {
182           System.err.println("The version number is not a valid floating-point number.");
183           return false;
184         }
185       }
186     } else {
187       System.err.println("Please provide a version number as shown below.");
188       return false;
189     }
190     return true;
191   }
192 
193   
194 
195 
196   private static void printHelp() {
197     StringBuilder help = new StringBuilder();
198     help.append("Usage: java EnzymeFlatFileWriterApp version <version number>\n");
199     help.append("The <version number> must be a valid floating-point number and will be used in the version line of the flat file's header.\n");
200     help.append("Examples: java EnzymeFlatFileWriterApp version 33.0\n");
201     help.append("          java EnzymeFlatFileWriterApp version 33.12\n");
202     System.out.println(help.toString());
203   }
204 
205 
206 }