Java tutorial
/** * Copyright (C) 2012 Picon software * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package fr.eo.util.dumper; import fr.eo.Const; import fr.eo.util.DumpFilesDescriptor; import fr.eo.util.dumper.property.RequestDefinitionBean; import fr.eo.util.dumper.property.RequestDefinitionParser; import org.apache.commons.codec.binary.Hex; import java.io.*; import java.sql.*; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.zip.Deflater; /** * @author picon.software * */ public class Dumper { private static String assetFolder = null; private static final String COPYRIGHTS = "--Copyright (C) 2012 Picon software\n\n" + "--This program is free software; you can redistribute it and/or modify\n" + "--it under the terms of the GNU General Public License as published by\n" + "--the Free Software Foundation; either version 2 of the License, or\n" + "--(at your option) any later version.\n\n" + "--This program is distributed in the hope that it will be useful,\n" + "--but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "--GNU General Public License for more details.\n\n" + "--You should have received a copy of the GNU General Public License along\n" + "--with this program; if not, write to the Free Software Foundation, Inc.,\n" + "--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n\n"; private static final int MAX_FILE_SIZE = 800 * 1024; private static final DecimalFormat nf = new DecimalFormat("#0.############", new DecimalFormatSymbols(Locale.US)); private static List<String> dumpFileNames = new ArrayList<String>(); static { dumpFileNames.add(Const.DB_STRUCTURE_SQL_FILE_NAME); } /** * @param args */ public static void main(String[] args) { String appName = args[0]; System.out.println("Starting dumper ..."); Statement stmt = null; try { System.out.println("Getting database connection ..."); Connection conn = getJtdsConnection(); List<RequestDefinitionBean> requests = RequestDefinitionParser.getRequests(appName); assetFolder = RequestDefinitionParser.getAppBaseDir(appName) + "assets/"; for (RequestDefinitionBean request : requests) { int currentFileSize = 0, cpt = 1; stmt = conn.createStatement(); System.out.println("Dumping " + request.name + "..."); ResultSet rs = stmt.executeQuery(request.sql); BufferedWriter bw = getWriter(request.name, cpt); bw.append(COPYRIGHTS); currentFileSize += COPYRIGHTS.length(); bw.append("--" + request.name + "\n"); while (rs.next()) { StringBuilder sb = new StringBuilder(); sb.append("INSERT INTO "); sb.append(request.table).append(" VALUES ("); int pos = 0; for (String fieldName : request.fields) { String str = getFieldValue(request, pos, rs, fieldName); sb.append(str); pos++; if (pos < request.fields.size()) { sb.append(","); } } sb.append(");\n"); currentFileSize += sb.length(); bw.append(sb.toString()); bw.flush(); if (currentFileSize > MAX_FILE_SIZE) { bw.close(); bw = getWriter(request.name, ++cpt); bw.append(COPYRIGHTS); bw.append("--" + request.name + "\n"); currentFileSize = COPYRIGHTS.length(); } } bw.flush(); bw.close(); } System.out.println("done."); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } DumpFilesDescriptor descriptor = new DumpFilesDescriptor(); descriptor.lineNumbers = getDumpLinesNumber(); descriptor.dumpFileNames = dumpFileNames; writeDescriptorFile(descriptor); System.out.println("nb :" + descriptor.lineNumbers); } private static String getFieldValue(RequestDefinitionBean request, int pos, ResultSet rs, String fieldName) throws SQLException { String str = String.valueOf(rs.getObject(fieldName)).trim(); if (str.endsWith(".0")) { str = str.substring(0, str.length() - 2); } if (str.equals("null") || str.isEmpty()) { return "NULL"; } if (request.compressedStrPos.contains(pos)) { str = getCompressedString(str); } else if (request.numberPos.contains(pos)) { Double number = rs.getDouble(fieldName); str = nf.format(number); } else if (request.fieldStrPos.contains(pos)) { str = formatString(str); } return str; } private static String getCompressedString(String value) { byte[] output = new byte[8096]; try { byte[] input = value.getBytes("UTF-8"); Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); return "X'" + Hex.encodeHexString(Arrays.copyOf(output, compressedDataLength)) + "'"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } private static BufferedWriter getWriter(String requestName, int cpt) throws IOException { String path = "", fileName = ""; if (cpt > 1) { fileName = "dump_" + requestName.toLowerCase().replaceAll(" ", "_") + "_" + cpt + ".sql"; } else { fileName = "dump_" + requestName.toLowerCase().replaceAll(" ", "_") + ".sql"; } dumpFileNames.add(fileName); path = assetFolder + "/" + fileName; File dumpFile = new File(path); System.out.println("Getting writer for file : '" + dumpFile.getAbsolutePath() + "'"); dumpFile.createNewFile(); return new BufferedWriter(new FileWriter(dumpFile)); } private static void writeDescriptorFile(DumpFilesDescriptor descriptor) { String path = assetFolder + "/" + Const.DUMP_FILE_DESCRIPTOR_NAME; ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream(new File(path))); oos.writeObject(descriptor); oos.flush(); } catch (IOException e) { e.printStackTrace(); } } private static Connection getSqliteConnection() throws SQLException, ClassNotFoundException { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:C:/tools/sqlite/eo.sqlite", "", ""); return conn; } @SuppressWarnings("unused") private static Connection getJtdsConnection() throws SQLException, ClassNotFoundException { Class.forName("net.sourceforge.jtds.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/ebs_DATADUMP", "eve", "eve"); return conn; } @SuppressWarnings("unused") private static Connection getMySQLConnection() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eve_dump", "root", ""); return conn; } private static String formatString(String value) { value = value.replaceAll("'", "''"); value = "'" + value + "'"; return value; } private static int getDumpLinesNumber() { File assertDir = new File(assetFolder); String[] dumpFileNames = assertDir.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith("dump") && name.endsWith(".sql"); } }); int nbDumpLines = 0; BufferedReader reader = null; try { for (String name : dumpFileNames) { reader = new BufferedReader(new InputStreamReader(new FileInputStream(assetFolder + name))); String currentLine = null; while ((currentLine = reader.readLine()) != null) { currentLine = currentLine.trim(); if (currentLine.length() != 0 && !currentLine.startsWith("--")) { nbDumpLines++; } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return nbDumpLines; } }