001    /*
002     * 
003     * $Revision: 13457 $ $Date: 2008-04-28 10:03:53 +0200 (Mo, 28 Apr 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.migration;
025    
026    import java.io.File;
027    import java.sql.Connection;
028    import java.sql.SQLException;
029    import java.sql.Statement;
030    import java.util.Properties;
031    
032    import org.hibernate.Session;
033    import org.hibernate.persister.entity.Joinable;
034    
035    import org.mycore.backend.hibernate.MCRHIBConnection;
036    import org.mycore.backend.hibernate.tables.MCRACCESS;
037    import org.mycore.common.MCRConfiguration;
038    
039    /**
040     * @author Thomas Scheffler (yagee)
041     * 
042     * @version $Revision: 13457 $ 
043     */
044    class MCRAccessMigrationHelper {
045    
046        static File getExportFile() {
047            Properties props = (Properties) (MCRConfiguration.instance().getProperties());
048            String workingDir = props.getProperty("MCR.BaseDirectory", null);
049            if (workingDir == null) {
050                workingDir = System.getProperty("java.io.tmpdir");
051            }
052    
053            File dir = new File(workingDir, "access-migration");
054            dir.mkdirs();
055            return new File(dir, "mcraccess-data.xml");
056        }
057    
058        static void dropTable() throws SQLException {
059            Session session = MCRHIBConnection.instance().getSession();
060            String tableName = getTableName();
061            Connection con = session.connection();
062            Statement stmt = con.createStatement();
063            stmt.executeUpdate("DROP TABLE " + tableName);
064            stmt.close();
065            con.commit();
066            session.clear();
067        }
068    
069        static void deleteExportFile() {
070            final File exportFile = getExportFile();
071            exportFile.delete();
072            exportFile.getParentFile().delete();
073        }
074    
075        private static String getTableName() {
076            Session session = MCRHIBConnection.instance().getSession();
077            if (session.getSessionFactory() instanceof Joinable) {
078                return ((Joinable) session.getSessionFactory().getClassMetadata(MCRACCESS.class)).getTableName();
079            } else {
080                return MCRACCESS.class.getSimpleName();
081            }
082        }
083    
084    }