Example usage for javax.persistence Persistence createEntityManagerFactory

List of usage examples for javax.persistence Persistence createEntityManagerFactory

Introduction

In this page you can find the example usage for javax.persistence Persistence createEntityManagerFactory.

Prototype

public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) 

Source Link

Document

Create and return an EntityManagerFactory for the named persistence unit using the given properties.

Usage

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public static void main(String[] argv) throws Exception {
    if (argv.length != 2) {
        System.err.println("Usage: Bootstrap <jpa.properties> <output file>");
        System.exit(1);/*from   w w  w.  j ava 2s  . c om*/
    }
    Properties properties = new Properties();
    try (InputStream is = new FileInputStream(new File(argv[0]))) {
        properties.load(is);
    }

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(WellKnownObject.CORE, properties);
    EntityManager em = emf.createEntityManager();
    Bootstrap bootstrap = new Bootstrap(em);
    bootstrap.clear();
    em.getTransaction().begin();
    bootstrap.bootstrap();
    em.getTransaction().commit();
    em.clear();
    bootstrap.serialize(argv[1]);
}

From source file:org.isatools.isatab.commandline.ReindexShellCommand.java

public static void main(String[] args) {

    try {//  www. ja va 2 s  .  c  o  m
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                ReindexShellCommand.class);

        args = cmdl.getArgs();
        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(ReindexShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        BIIObjectStore store = new BIIObjectStore();
        PermissionManager permMgr = new PermissionManager(entityManager);

        List<Study> studies = permMgr.getStudies();
        if (studies == null || studies.size() == 0) {
            log.info("No study in the BII DB");
        } else {
            for (Study s : studies) {
                store.put(Study.class, s.getAcc(), s);
            }
        }
        PersistenceShellCommand.reindexStudies(store, hibProps);
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while reindexing the BII DB: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UserDelShellCommand.java

public static void main(String[] args) {

    try {//from   w ww  . j a  va2  s  .  c  om
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserDelShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserDelShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        permMgr.deleteUser(args[0]);

        transaction.commit();
        entityManager.close();

        log.info("User deleted.");
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.PermModShellCommand.java

public static void main(String[] args) {

    try {/*  w ww. java 2s .c  o  m*/
        Options clopts = createCommonOptions();
        PermissionManager.createPermModOptions(clopts);

        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                PermModShellCommand.class);

        args = cmdl.getArgs();
        setup(args);

        setupLog4JPath(cmdl, null);
        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PermModShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        permMgr.setStudyOwners(cmdl);
        permMgr.setVisibility(cmdl);

        transaction.commit();
        entityManager.close();

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UserAddShellCommand.java

public static void main(String[] args) {

    try {/*from  www. ja  v  a2 s.c om*/
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserAddShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserAddShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.addUser(user);

        transaction.commit();
        entityManager.close();

        log.info(PermissionManager.formatUser(user));
        log.info("User added.");
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UserModShellCommand.java

public static void main(String[] args) {

    try {//from  w  w w. j a v a2s  .c om
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserModShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserModShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.updateUser(user);

        transaction.commit();

        log.info("User changed, new details:");
        log.info(PermissionManager.formatUser(permMgr.getUserByLogin(user.getUserName())));
        log.info("\n");

        entityManager.close();
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.ISATABExportShellCommand.java

@SuppressWarnings("static-access")
public static void main(String[] args) {
    try {/*from   www.  j a v  a 2 s.co  m*/
        Options clopts = createCommonOptions();
        clopts.addOption(OptionBuilder.withArgName("directory-path")
                .withDescription("Exports to local path (instead of the BII repository), useful for checking")
                .hasArg().withLongOpt("to-dir").create("d"));
        clopts.addOption("s", "skip-data-files", false,
                "Avoids to export (-to-dir option) or backup (BII repo export) the data files");
        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                ISATABExportShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);

        setupLog4JPath(cmdl, null);
        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(ISATABExportShellCommand.class);

        String exportPath = cmdl.getOptionValue("d");
        boolean skipDataFiles = cmdl.hasOption("s");

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "manual");
        // We assume this, since we are unloading something that is supposed to already exist (including during tests).
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        DaoFactory daoFactory = DaoFactory.getInstance(entityManager);

        ISATABDBExporter dbexp = new ISATABDBExporter(daoFactory, args);

        if (exportPath != null) {
            dbexp.exportToPath(exportPath, skipDataFiles);
        } else {
            dbexp.exportToRepository(skipDataFiles);
        }

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB exporter: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UnloadShellCommand.java

public static void main(String[] args) {

    try {/*  w w  w . j a  v  a2 s. co m*/
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UnloadShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);

        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UnloadShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        ISATABUnloader unloader = null;

        for (String studyAcc : args) {
            unloader = new ISATABUnloader(DaoFactory.getInstance(entityManager), studyAcc);
            log.info("\n\n " + i18n.msg("unloader_unloading_study", studyAcc));
            unloader.unload();

            Set<String> messages = unloader.getMessages();
            log.info("\n\n  Unloading of " + studyAcc + ", messages: ");
            for (String message : messages) {
                log.info(message);
            }
            log.info("\n\n");
        }

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB unloader: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.PersistenceShellCommand.java

public static void main(String[] args) {
    EntityTransaction transaction = null;
    try {//from   w w w . j  a  v  a  2  s . co  m

        Options clopts = createCommonOptions();
        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                PersistenceShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PersistenceShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "manual");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        BIIObjectStore store = loadIsaTab();
        log.info(i18n.msg("mapping_done_now_persisting", store.size()));

        ISATABPersister persister = new ISATABPersister(store, DaoFactory.getInstance(entityManager));
        transaction = entityManager.getTransaction();
        transaction.begin();
        Timestamp ts = persister.persist(sourceDirPath);
        transaction.commit();
        entityManager.close();

        reindexStudies(store, hibProps);

        log.info("\n\n" + i18n.msg("mapping_done_data_saved_in_db"));
        log.info("\n\n" + i18n.msg("submission_done_ts_reported", "" + ts.getTime(),
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(ts) + "." + ts.getNanos()) + "\n");
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB loader: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.ContentsShellCommand.java

@SuppressWarnings("static-access")
public static void main(String[] args) {

    try {/*from w  ww  .  java2  s  .  c  om*/
        Options clopts = createCommonOptions();
        clopts.addOption(OptionBuilder.withDescription("List all the studies and their permissions/ownership")
                .withLongOpt("studies").create("s"));
        clopts.addOption(OptionBuilder.withDescription("List all the users").withLongOpt("users").create("u"));

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                ContentsShellCommand.class);

        boolean isStudies = cmdl.hasOption("s"), isUsers = cmdl.hasOption("u");
        if (!isStudies && !isUsers) {
            printUsage(clopts);
            System.exit(1);
        }

        args = cmdl.getArgs();
        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(ContentsShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        if (isStudies) {
            List<Study> studies = permMgr.getStudies();
            if (studies == null || studies.size() == 0) {
                log.info("No study in the BII DB");
            } else {
                for (Study s : studies) {
                    log.info(PermissionManager.formatStudy(s));
                }
            }
        } else {
            List<Person> persons = permMgr.getUsers();
            if (persons == null || persons.size() == 0) {
                log.info("No user in the BII DB");
            } else {
                for (Person u : persons) {
                    log.info(PermissionManager.formatUser(u));
                }
            }
        }

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}