Example usage for org.hibernate StatelessSession beginTransaction

List of usage examples for org.hibernate StatelessSession beginTransaction

Introduction

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

Prototype

Transaction beginTransaction();

Source Link

Document

Begin a unit of work and return the associated Transaction object.

Usage

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

License:Open Source License

@Override
public JahiaGroup getGroup(String name) throws GroupNotFoundException {
    JahiaGroup foundGroup = null;//from  w ww .  j  a  va2s .com
    StatelessSession hib = sessionFactoryBean.openStatelessSession();
    hib.beginTransaction();

    try {
        @SuppressWarnings("unchecked")
        List<Group> groups = hib.createCriteria(Group.class).add(Restrictions.idEq(name)).list();

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

    if (foundGroup == null) {
        throw new GroupNotFoundException("Unable to find group " + name + " for the provider " + getKey());
    }

    return foundGroup;
}

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();

    try {/*  w w  w  .  j  a  va 2s  .  c o  m*/

        @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();

    try {//from   w  w w . j  a  v a  2s .c  om

        @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 . ja va  2 s.com

    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;/*ww  w.java 2 s  .co  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();
    }//from   w w w  . jav a  2  s .co m

    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();

    try {/*from  ww  w. j a v  a  2 s . c  om*/
        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 w w .j  av a2s  . c o  m*/
        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.  jav a2  s .c o  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 {/*from  ww w  .  j a  v 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();
        }
    }
}