Example usage for org.hibernate StatelessSession createCriteria

List of usage examples for org.hibernate StatelessSession createCriteria

Introduction

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

Prototype

@Deprecated
Criteria createCriteria(Class persistentClass);

Source Link

Document

Create Criteria instance for the given class (entity or subclasses/implementors).

Usage

From source file:monasca.api.infrastructure.persistence.hibernate.AlarmDefinitionSqlRepoImpl.java

License:Apache License

@Override
public String exists(final String tenantId, final String name) {
    logger.trace(ORM_LOG_MARKER, "exists(...) entering...");

    StatelessSession session = null;
    try {/*from w  w w  .  j a  va2 s.c o  m*/
        session = sessionFactory.openStatelessSession();

        List<?> ids = session.createCriteria(AlarmDefinitionDb.class).add(Restrictions.eq("tenantId", tenantId))
                .add(Restrictions.eq("name", name)).add(Restrictions.isNull("deletedAt"))
                .setProjection(Projections.property("id")).setMaxResults(1).list();

        final String existingId = CollectionUtils.isEmpty(ids) ? null : (String) ids.get(0);

        if (null == existingId) {
            logger.debug(ORM_LOG_MARKER, "No AlarmDefinition matched tenantId={} and name={}", tenantId, name);
        }

        return existingId;
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.geolatte.demo1.transformers.RiverSegmentSource.java

License:Open Source License

private static void buildGraph(StatelessSession session) {

    if (graph != null) { // already build
        return;//from  ww w.j a  v  a2 s .  co  m
    }

    List<Waterway> waterways = null;
    waterways = (List<Waterway>) session.createCriteria(Waterway.class).list();

    try {
        final int sourceSrid = waterways.get(0).getGeometry().getSRID();
        final int targetSrid = 31370;
        CrsConvertor toTargetConvertor = CrsConvertorFactory.createConvertor(sourceSrid, targetSrid);
        toSourceConvertor = CrsConvertorFactory.createConvertor(targetSrid, sourceSrid);

        Coordinate[] bbox = toTargetConvertor
                .convert(new Coordinate[] { new Coordinate(2.33, 49.3), new Coordinate(6.6, 51.6) });

        Extent extent = new Extent(bbox[0].x, bbox[0].y, bbox[1].x, bbox[1].y);
        GraphBuilder<Node, Geometry> graphBuilder = Graphs.createGridIndexedGraphBuilder(extent, 20000);

        for (Waterway waterway : waterways) {

            // Convert points to lambert
            waterway.setJTSGeometry(toTargetConvertor.convert(waterway.getJTSGeometry()));
            if (waterway.getBeginNode().getJTSLocation().getSRID() != targetSrid) {
                waterway.getBeginNode()
                        .setJTSLocation(toTargetConvertor.convert(waterway.getBeginNode().getJTSLocation()));
            }
            if (waterway.getEndNode().getJTSLocation().getSRID() != targetSrid) {
                waterway.getEndNode()
                        .setJTSLocation(toTargetConvertor.convert(waterway.getEndNode().getJTSLocation()));
            }

            if (waterway.getBeginNode() != null && waterway.getEndNode() != null) {
                //if (extent.contains(waterway.getBeginNode().getLocation()) &&  extent.contains(waterway.getEndNode().getLocation())) {
                graphBuilder.addEdge(waterway.getBeginNode(), waterway.getEndNode(), new BasicEdgeWeight(1),
                        waterway.getGeometry());
                //}
            }
        }

        graph = graphBuilder.build();

    } catch (BuilderException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (GeoTransformationException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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   www.j a  v  a 2 s  . c  o  m*/
    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();//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();//from   w  w w . j  a v a2s .  c om

    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 ww.  j  av  a2 s  .c  om*/

    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;//from  w w  w  .  ja v a 2s  . 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  ww  w  .  j  a  v  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();//from ww  w  .ja  va  2 s . c o  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.impl.external.ExternalSessionImpl.java

License:Open Source License

public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
    if (!repository.getDataSource().isSupportsUuid() || uuid.startsWith("translation:")) {
        // Translate uuid to external mapping
        SessionFactory hibernateSession = repository.getStoreProvider().getHibernateSession();
        StatelessSession statelessSession = hibernateSession.openStatelessSession();
        try {/*from w ww  . ja  v  a  2  s  .  c om*/
            Criteria criteria = statelessSession.createCriteria(UuidMapping.class);
            criteria.add(Restrictions.eq("internalUuid", uuid));
            List list = criteria.list();
            if (list.size() > 0) {
                uuid = ((UuidMapping) list.get(0)).getExternalId();
            } else {
                throw new ItemNotFoundException("Item " + uuid + " could not been found in this repository");
            }
        } finally {
            statelessSession.close();
        }
    }
    return getNodeByLocalIdentifier(uuid);
}