Example usage for org.hibernate StatelessSession getTransaction

List of usage examples for org.hibernate StatelessSession getTransaction

Introduction

In this page you can find the example usage for org.hibernate StatelessSession getTransaction.

Prototype

Transaction getTransaction();

Source Link

Document

Get the Transaction instance associated with this session.

Usage

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<Member> getGroupMembers(String groupName) {
    List<Member> members = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();/*w  w w  . ja v  a  2 s  .  c  o  m*/

    try {

        @SuppressWarnings("unchecked")
        Set<Group> groups = new LinkedHashSet<>(hib.createCriteria(Group.class)
                .add(Restrictions.idEq(groupName)).setFetchMode("members", FetchMode.JOIN).list());
        if (groups.size() > 0) {
            members = new LinkedList<>();
            for (GroupMember m : groups.iterator().next().getMembers()) {
                members.add(new Member(m.getName(), MemberType.USER));
            }
        }
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    return members != null ? members : Collections.<Member>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<String> getMembership(Member member) {
    List<String> owners = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();/*w  w w.j av  a2  s.  c o m*/

    try {

        @SuppressWarnings("unchecked")
        List<Group> groups = hib.createCriteria(Group.class).createCriteria("members")
                .add(Restrictions.eq("name", member.getName())).list();
        if (groups.size() > 0) {
            owners = new LinkedList<>();
            for (Group g : groups) {
                owners.add(g.getGroupname());
            }
        }
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    return owners != null ? owners : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserGroupProvider.java

License:Open Source License

@Override
public List<String> searchGroups(Properties searchCriteria, long offset, long limit) {

    if (hasUnknownCriteria(searchCriteria, Collections.singleton("groupname"))) {
        return Collections.emptyList();
    }/*  w  w w . j av  a 2  s.c  o m*/

    List<String> groups = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        Criteria query = hib.createCriteria(Group.class);
        if (offset > 0) {
            query.setFirstResult((int) offset);
        }
        if (limit > 0) {
            query.setMaxResults((int) limit);
        }

        filterQuery(query, searchCriteria);

        logger.info("Executing query {}", query);

        @SuppressWarnings("unchecked")
        List<Group> list = query.list();
        if (list.size() > 0) {
            groups = new LinkedList<>();
            for (Group g : list) {
                groups.add(g.getGroupname());
            }
        }

        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    logger.info("Found {} groups matching the search criteria {}", groups != null ? groups.size() : 0,
            searchCriteria);

    return groups != null ? groups : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.DbUserProvider.java

License:Open Source License

@Override
public JahiaUser getUser(String name) throws UserNotFoundException {
    JahiaUser foundUser = null;/* w ww  . ja v a 2s  . c o m*/
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        Set<User> users = new LinkedHashSet<>(hib.createCriteria(User.class).add(Restrictions.idEq(name))
                .setFetchMode("properties", FetchMode.JOIN).list());

        foundUser = !users.isEmpty() ? toUser(users.iterator().next()) : null;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    if (foundUser == null) {
        throw new UserNotFoundException("Unable to find user " + name + " for the provider " + getKey());
    }

    return foundUser;
}

From source file:org.jahia.modules.ugp.showcase.DbUserProvider.java

License:Open Source License

@Override
public List<String> searchUsers(Properties searchCriteria, long offset, long limit) {

    if (hasUnknownCriteria(searchCriteria,
            Arrays.asList(new String[] { "username", "j:firstName", "j:lastName", "j:email" }))) {
        return Collections.emptyList();
    }/*w ww . jav a 2s. c  om*/

    List<String> users = null;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        Criteria query = hib.createCriteria(User.class);
        if (offset > 0) {
            query.setFirstResult((int) offset);
        }
        if (limit > 0) {
            query.setMaxResults((int) limit);
        }

        Criteria rootQuery = query;

        query = filterQuery(query, searchCriteria);

        logger.info("Executing query {}", rootQuery);

        @SuppressWarnings("unchecked")
        List<User> list = query.list();
        if (list.size() > 0) {
            users = new LinkedList<>();
            for (User u : list) {
                users.add(u.getUsername());
            }
        }

        hib.getTransaction().commit();
    } catch (HibernateException e) {
        logger.error(e.getMessage(), e);
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }

    logger.info("Found {} users matching the search criteria {}", users != null ? users.size() : 0,
            searchCriteria);

    return users != null ? users : Collections.<String>emptyList();
}

From source file:org.jahia.modules.ugp.showcase.persistence.DataInitializer.java

License:Open Source License

private boolean checkData() {
    boolean dataPresent = false;
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();/* w  ww  .j a  va 2 s.  co m*/

    try {
        long count = ((Number) hib.createCriteria(User.class).setProjection(Projections.rowCount())
                .uniqueResult()).longValue();
        dataPresent = count > 0;
        hib.getTransaction().commit();
    } catch (HibernateException e) {
        hib.getTransaction().rollback();
    } finally {
        hib.close();
    }
    return dataPresent;
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public String readFile(String filename) throws RepositoryException {
    StatelessSession session = null;
    try {/*from w ww .j  a  va2  s . c om*/
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(true).uniqueResult();
        session.getTransaction().commit();
        if (nodeTypesDBProvider != null) {
            return nodeTypesDBProvider.getCndFile();
        }
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return null;
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public List<String> getFilesList() throws RepositoryException {
    StatelessSession session = null;
    try {/*from   w  ww. j  ava  2  s .co  m*/
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        List<String> nodeTypesDBProviderList = session
                .createQuery("select filename from NodeTypesDBProvider order by id").setReadOnly(true).list();
        session.getTransaction().commit();
        if (nodeTypesDBProviderList != null) {
            return nodeTypesDBProviderList;
        }
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return null;
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public void saveCndFile(String filename, String content, Properties properties) throws RepositoryException {
    StatelessSession session = null;
    try {//  ww w . j av  a2 s  . c o  m
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null && content != null) {
            nodeTypesDBProvider.setCndFile(content);
            session.update(nodeTypesDBProvider);
        } else if (nodeTypesDBProvider != null) {
            session.delete(nodeTypesDBProvider);
        } else if (content != null) {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(filename);
            nodeTypesDBProvider.setCndFile(content);
            session.insert(nodeTypesDBProvider);
        }

        final StringWriter writer = new StringWriter();
        properties.store(writer, "");

        nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", DEFINITIONS_PROPERTIES).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null) {
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.update(nodeTypesDBProvider);
        } else {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(DEFINITIONS_PROPERTIES);
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.insert(nodeTypesDBProvider);
        }

        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.openbravo.test.dal.DalPerformanceProxyTest.java

License:Open Source License

@Test
public void testStatelessBPCreate() {
    try {//from  w  ww  . jav  a  2 s  .c  o  m
        setTestAdminContext();

        final StatelessSession session = SessionFactoryController.getInstance().getSessionFactory()
                .openStatelessSession();
        session.beginTransaction();
        for (int i = 0; i < CNT; i++) {
            BusinessPartner bp = OBProvider.getInstance().get(BusinessPartner.class);

            // Generating random strings for testing
            UUID name = UUID.randomUUID();
            UUID key = UUID.randomUUID();

            bp.setName(name.toString());
            bp.setSearchKey(key.toString());

            bp.setBusinessPartnerCategory(createReferencedObject(Category.class, TEST_BP_CATEGORY_ID));

            // note the following things are currently done in the OBInterceptor
            // it is quite easy to add a util method which can do this in a generic
            // way for any business object
            bp.setOrganization(createReferencedObject(Organization.class, TEST_ORG_ID));
            bp.setClient(createReferencedObject(Client.class, TEST_CLIENT_ID));
            bp.setCreatedBy(createReferencedObject(User.class, "100"));
            bp.setCreationDate(new Date());
            bp.setUpdatedBy(createReferencedObject(User.class, "100"));
            bp.setUpdated(new Date());

            session.insert(BusinessPartner.ENTITY_NAME, bp);
            // session.refresh(BusinessPartner.ENTITY_NAME, bp);
            Assert.assertTrue(bp.getId() != null);
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        throw new OBException(e);
    }
}