Example usage for org.hibernate Query setEntity

List of usage examples for org.hibernate Query setEntity

Introduction

In this page you can find the example usage for org.hibernate Query setEntity.

Prototype

@Deprecated
@SuppressWarnings("unchecked")
Query<R> setEntity(String name, Object val);

Source Link

Document

Bind an instance of a mapped persistent class to a named query parameter.

Usage

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

protected void deepRemoveNode(NodeEntity node, String deleteStamp) {
    Session session = HibernateUtil.getSession();
    Query qry;

    long deleteTime = System.currentTimeMillis();

    // delete the kids
    if (node.isCollection()) {
        qry = session.getNamedQuery("Repo.removeCollection");
        qry.setEntity("repo", this);
        qry.setString("uriPat", node.getLocalUri() + "/%");
        qry.setString("deleteStamp", deleteStamp);
        qry.setLong("deleted", deleteTime);
        qry.executeUpdate();/*from w w w  .  j  a v  a2 s .c  o m*/
    }

    node.setLocalUri(node.getLocalUri() + deleteStamp);
    node.setDeleted(deleteTime);
    session.update(node);
}

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

/**
 * @param uriPat - SQL - LIKE style pattern
 * @param limit - how many to return//from   w w w  .j a va  2  s  .c  o m
 * @param minPerms - the minimum permission level to include
 * @return
 */
public List<MutableRepositoryNode> getRecentlyModified(String uriPat, int limit, NodePermission minPerms) {

    Query qry = HibernateUtil.getSession().getNamedQuery("Repo.recentlyModifiedNodes");
    qry.setEntity("repo", this);
    qry.setString("uriPat", localizeUri(uriPat));
    qry.setMaxResults(limit * 2);
    ArrayList<MutableRepositoryNode> nodes = new ArrayList<MutableRepositoryNode>(limit);
    int qualified = 0;
    for (Object obj : qry.list()) {
        MutableRepositoryNode nod = (MutableRepositoryNode) obj;
        try {
            if (nod.hasPermission(minPerms)) {
                nodes.add(nod);
                qualified++;
                if (qualified >= limit)
                    break;
            }
        } catch (AccessDeniedException e) {
            // eat it
        }
    }
    return nodes;
}

From source file:itensil.repository.hibernate.RepositoryEntity.java

License:Open Source License

protected boolean existingAncestorBlock(User user, String uri) throws AccessDeniedException {
    Session session = HibernateUtil.getSession();
    String parentUri = UriHelper.getParent(uri);
    Query qry = session.getNamedQuery("Repo.getNodeByUri");
    qry.setEntity("repo", this);

    String lastUri = uri;/*from   w w  w .  ja  va  2  s.c  o m*/
    while (!parentUri.equals(lastUri)) {
        qry.setString("uri", parentUri);
        NodeEntity parentNode = (NodeEntity) qry.uniqueResult();
        if (parentNode != null) {
            return !hasPermission(user, parentNode, DefaultNodePermission.READ);
        }
        lastUri = parentUri;
        parentUri = UriHelper.getParent(parentUri);
    }
    return false;
}

From source file:itensil.repository.RepositoryJunit.java

License:Open Source License

public void testGetLastModified() throws Exception {

    SecurityAssociation.setUser(user);//ww w . j  a v a2  s.  com
    HibernateUtil.beginTransaction();
    Repository repository = RepositoryHelper.getRepository(mount);

    Query qry = HibernateUtil.getSession().getNamedQuery("Repo.recentlyModifiedNodes");
    qry.setEntity("repo", repository);
    qry.setString("uriPat", "%.txt");

    List nodes = qry.list();
    assertNotNull(nodes);

    HibernateUtil.commitTransaction();
}

From source file:itensil.security.hibernate.UserSpaceEntity.java

License:Open Source License

public Group createGroup(String groupName, String simpleName) throws UserSpaceException {

    HashSet<String> addGrpRoles = new HashSet<String>(ADMIN_ROLE);
    // special cases
    if (DEF_GROUPS.contains(simpleName)) {
        addGrpRoles.add("inviter");
    }/*from  w  ww  .j ava 2s.  c  o  m*/
    checkAccess(addGrpRoles);

    // check for duplicates
    Session session = HibernateUtil.getSession();
    Query qry = session.getNamedQuery("USpace.groupByName");
    qry.setString("name", groupName);
    qry.setEntity("uspace", this);
    if (qry.uniqueResult() != null) {
        throw new DuplicatePrincipalException("Duplicate group name");
    }

    GroupEntity groupEnt = new GroupEntity();
    groupEnt.initNew();
    groupEnt.setGroupName(groupName);
    groupEnt.setSimpleName(simpleName);
    groupEnt.setUserSpaceEntity(this);

    session.persist(groupEnt);
    Set<GroupEntity> groups = getGroupEntities();
    if (Hibernate.isInitialized(groups)) {
        groups.add(groupEnt);
    }
    return groupEnt;
}

From source file:itensil.security.hibernate.UserSpaceEntity.java

License:Open Source License

public Set<? extends User> getUsersInRole(String role) throws UserSpaceException {
    Session session = HibernateUtil.getSession();
    Query qry = session.getNamedQuery("USpace.usersByRole");
    qry.setEntity("uspace", this);
    qry.setString("role", "%" + role + "%");
    List res = qry.list();/*from  w  ww  .j  a  v  a 2s  .c o  m*/
    HashSet<UserEntity> users = new HashSet<UserEntity>();
    for (Object obj : res) {
        users.add((UserEntity) obj);
    }
    return users;
}

From source file:itensil.security.hibernate.UserSpaceEntity.java

License:Open Source License

protected GroupEntity resolveGroup(Group group) {
    if (group instanceof GroupEntity) {
        return (GroupEntity) group;
    }/*from   w w w . j  a v  a  2 s. com*/
    String groupId = group.getGroupId();
    Session session = HibernateUtil.getSession();
    Query qry;
    if (!Check.isEmpty(groupId)) {
        qry = session.getNamedQuery("USpace.groupById");
        qry.setString("id", groupId);
    } else {
        qry = session.getNamedQuery("USpace.groupByName");
        qry.setString("name", group.getName());
    }
    qry.setEntity("uspace", this);
    return (GroupEntity) qry.uniqueResult();
}

From source file:itensil.security.hibernate.UserSpaceEntity.java

License:Open Source License

protected UserEntity resolveUser(User user) {
    if (user instanceof UserEntity) {
        return (UserEntity) user;
    }//from  w w w . j a va  2  s . c  om
    String userId = user.getUserId();
    Session session = HibernateUtil.getSession();
    Query qry;
    if (!Check.isEmpty(userId)) {
        qry = session.getNamedQuery("USpace.userById");
        qry.setString("id", userId);
    } else {
        qry = session.getNamedQuery("USpace.userByName");
        qry.setString("name", user.getName());
    }
    qry.setEntity("uspace", this);
    return (UserEntity) qry.uniqueResult();
}

From source file:itensil.security.hibernate.UserSpaceEntity.java

License:Open Source License

protected HashSet<UserEntity> findGroupRoleUsers(GroupEntity cGrp, String role) throws UserSpaceException {
    // Group Memeber query
    HashSet<UserEntity> roleUsers = new HashSet<UserEntity>();
    Query qry = HibernateUtil.getSession().getNamedQuery("USpace.groupUsersByRole");
    qry.setEntity("grp", cGrp);
    qry.setString("role", "%" + role + "%");
    for (Object obj : qry.list()) {
        GroupUserEntity gusr = (GroupUserEntity) obj;
        // recheck to remove partial string matches 
        if (gusr.getRoles().contains(role)) {
            roleUsers.add(gusr.getUserEntity());
        }/*from   ww w . j  av a 2 s .co  m*/
    }
    return roleUsers;
}

From source file:itensil.security.web.UserSpaceServlet.java

License:Open Source License

/**
 *  /groupManList//from w  w w . java2 s . c o  m
 *
 *  Output:
 *      Group/User List XML
 *
 */
@SuppressWarnings("unchecked")
@ContentType("text/xml")
public void webGroupManList(HttpServletRequest request, HttpServletResponse response) throws Exception {
    AuthenticatedUser self = (AuthenticatedUser) request.getUserPrincipal();
    HibernateUtil.beginTransaction();
    HibernateUtil.readOnlySession();

    if (!self.getRoles().contains("admin"))
        throw new UserSpaceException("Admin role required");

    UserSpaceEntity uspace = (UserSpaceEntity) self.getUserSpace();

    //HibernateUtil.getSession().refresh(uspace);

    Document doc = DocumentHelper.createDocument();
    Element root = doc.addElement("list");
    Query qry = HibernateUtil.getSession().getNamedQuery("USpace.groupsByType");
    qry.setInteger("gtype", 0);
    qry.setEntity("uspace", uspace);

    for (GroupEntity grp : (List<GroupEntity>) qry.list()) {
        Element gElem = root.addElement("group");
        gElem.addAttribute("id", grp.getGroupId());
        gElem.addAttribute("name", grp.getSimpleName());
        gElem.addAttribute("parentId", Check.emptyIfNull(grp.getParentGroupId()));
        for (User user : grp.getUserEntities()) {
            Element uElem = gElem.addElement("user");
            uElem.addAttribute("id", user.getUserId());
            uElem.addAttribute("name", user.getSimpleName());
            uElem.addAttribute("email", user.getName());
        }
    }
    HibernateUtil.commitTransaction();

    ServletUtil.noCache(response);
    doc.write(response.getWriter());
}