Example usage for org.hibernate Query setLong

List of usage examples for org.hibernate Query setLong

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setLong(String name, long val) 

Source Link

Document

Bind a named long-valued parameter.

Usage

From source file:dao.GroupHibernateDao.java

@Override
public MyGroup findById(Long id) {
    Query query = getSession().createQuery("from MyGroup where id = :id");
    query.setLong("id", id);
    query.setMaxResults(1);/* w  w  w .  j  av  a  2s .co  m*/
    return (MyGroup) query.uniqueResult();
}

From source file:database.services.common.DAOBase.java

public ElementType get(Long id, String queryName) {
    ElementType entity = null;/*from   w  ww.jav a2s.  c o  m*/
    Session session = this.getSessionFactory().openSession();
    Transaction tx = null;
    try {
        tx = session.beginTransaction();
        Query createQuery = session.getNamedQuery(queryName);
        createQuery.setLong(getMetadata().getIdentifierPropertyName(), id);
        List<ElementType> list = createQuery.list();
        if (list != null && !list.isEmpty())
            entity = list.get(0);
        tx.commit();
    } catch (Exception ex) {
        if (tx != null)
            tx.rollback();
        throw ex;
    } finally {
        session.close();
    }

    return entity;
}

From source file:de.decidr.model.acl.asserters.UserAdministratesWorkflowInstanceAsserter.java

License:Apache License

@Override
public void transactionStarted(TransactionStartedEvent evt) {
    if ((workflowInstanceIds == null) || (workflowInstanceIds.length == 0)) {
        isWorkflowAdmin = false;//  w  w  w.  j ava 2s. c  om
    } else {
        /*
         * The user administrates the workflow instance if explicitly stated
         * by the database relation user_administrates_workflow_instance or
         * if the is the administrator of the tenant that owns the workflow
         * instance.
         * 
         * The default tenant is a special case: every user is a workflow
         * admin there.
         */
        String hql = "select rel.user.id from UserAdministratesWorkflowInstance rel where "
                + "rel.workflowInstance.id = :workflowInstanceId and " + "rel.user.id = :userId";

        Query relationQuery = evt.getSession().createQuery(hql).setLong("userId", userId)
                .setLong("defaultTenantId", DecidrGlobals.DEFAULT_TENANT_ID).setMaxResults(1);

        hql = "select wi.id WorkflowInstance wi where " + "wi.id = :workflowInstanceId and "
                + "(wi.deployedWorkflowModel.tenant.admin.id = :userId or "
                + "wi.deployedWorkflowModel.tenant.id = :defaultTenantId)";

        Query adminQuery = evt.getSession().createQuery(hql).setLong("userId", userId)
                .setLong("defaultTenantId", DecidrGlobals.DEFAULT_TENANT_ID).setMaxResults(1);

        isWorkflowAdmin = true;
        // assume the user is a workflow admin until proven false
        for (Long workflowInstanceId : workflowInstanceIds) {
            relationQuery.setLong("workflowInstanceId", workflowInstanceId);
            Object found = relationQuery.uniqueResult();

            if (found == null) {
                adminQuery.setParameter("workflowInstanceId", workflowInstanceId);
                isWorkflowAdmin = adminQuery.uniqueResult() != null;
            }

            if (!isWorkflowAdmin) {
                break;
            }
        }
    }
}

From source file:de.decidr.model.acl.asserters.UserAdministratesWorkflowModelAsserter.java

License:Apache License

@Override
public void transactionStarted(TransactionStartedEvent evt) {
    if (workflowModelIds == null) {
        isWorkflowAdmin = false;//w  w w  .ja v  a2  s .c  om
    } else {
        /*
         * The user administrates the workflow model if explicitly stated by
         * the database relation user_administrates_workflow_model or if he
         * is the administrator of the tenant that owns the workflow model.
         * This also covers the special case of the default tenant because
         * the super admin is the tenant admin of the default tenant.
         */
        Query relationQuery = evt.getSession()
                .createQuery("select rel.user.id from UserAdministratesWorkflowModel rel where "
                        + "rel.workflowModel.id = :workflowModelId and " + "rel.user.id = :userId")
                .setLong("userId", userId).setMaxResults(1);

        Query adminQuery = evt
                .getSession().createQuery("select wm.id from WorkflowModel wm where "
                        + "wm.id = :workflowModelId and " + "wm.tenant.admin.id = :userId")
                .setLong("userId", userId).setMaxResults(1);

        isWorkflowAdmin = true;
        // assume the user is a workflow admin until proven false
        for (Long workflowModelId : workflowModelIds) {
            relationQuery.setLong("workflowModelId", workflowModelId);
            Object found = relationQuery.uniqueResult();

            if (found == null) {
                adminQuery.setLong("workflowModelId", workflowModelId);
                isWorkflowAdmin = adminQuery.uniqueResult() != null;
            }

            if (!isWorkflowAdmin) {
                break;
            }
        }
    }
}

From source file:de.decidr.model.acl.asserters.UserIsTenantMemberAsserter.java

License:Apache License

@Override
public void transactionStarted(TransactionStartedEvent evt) {
    // a user is a member of a tenant if he is the admin or a regular member
    String hql = "select u.id from User u where u.id = :userId and "
            + "exists(from UserIsMemberOfTenant rel where "
            + "rel.user.id = :userId and rel.tenant.id = :tenantId) " + " or exists(from Tenant t where "
            + "t.admin.id = :userId and t.id = :tenantId)";

    // The "tenantId" parameter is set in the for loop below
    Query q = evt.getSession().createQuery(hql).setMaxResults(1);

    if (tenantIds == null) {
        // no tenant ids to check against, so we're returning false
        userIsMember = false;//from w  w w .  j  a v a2 s  .  c  om
    } else {
        // the user must be a member of each given tenant
        userIsMember = true;
        for (Long tenantId : tenantIds) {
            q.setLong("userId", userId);
            q.setLong("tenantId", tenantId);
            boolean isMember = q.uniqueResult() != null;
            userIsMember = userIsMember && isMember;
            if (!userIsMember) {
                break;
            }
        }
    }
}

From source file:de.decidr.model.acl.asserters.UserNotParticipatingInAnyWorkflowAsserter.java

License:Apache License

@Override
public void transactionStarted(TransactionStartedEvent evt) {
    notParticipating = false;//from   www. j av  a2s. co  m
    Query q = evt.getSession().createQuery(
            "select rel.user.id from UserParticipatesInWorkflow rel " + "where rel.user.id = :userId");
    q.setLong("userId", userid).setMaxResults(1);

    notParticipating = q.uniqueResult() == null;
}

From source file:de.decidr.model.commands.workflowmodel.DeployWorkflowModelCommand.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from w  ww .  ja v  a  2s  .c  o  m
public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException {
    DeploymentResult result = null;
    /*
     * Find the existing deployed workflow model.
     */
    WorkflowModel workflowModel = fetchWorkflowModel(evt.getSession());

    Query q = evt.getSession().createQuery("from DeployedWorkflowModel where (version = :version)"
            + "and (originalWorkflowModel = :original)");

    q.setLong("version", workflowModel.getVersion());
    q.setEntity("original", workflowModel);
    q.setMaxResults(1);

    DeployedWorkflowModel existing = (DeployedWorkflowModel) q.uniqueResult();

    if (existing == null) {
        /*
         * There is no current deployed version of our workflow model, so we
         * have to deploy it now.
         */
        Deployer deployer = new Deployer();

        // Fill deployedWorkflowModel with data
        DeployedWorkflowModel dwfm = new DeployedWorkflowModel();
        dwfm.setOriginalWorkflowModel(workflowModel);
        dwfm.setTenant(workflowModel.getTenant());
        dwfm.setName(workflowModel.getName());
        dwfm.setDescription(workflowModel.getDescription());
        dwfm.setDwdl(new byte[0]);
        dwfm.setVersion(workflowModel.getVersion());
        // XXX remove deprecated wsdl and soap template from database schema
        // ~dh
        dwfm.setWsdl(new byte[0]);
        dwfm.setSoapTemplate(new byte[0]);
        dwfm.setDeployDate(DecidrGlobals.getTime().getTime());

        evt.getSession().save(dwfm);
        // Update DWDL
        TWorkflow dwdl = XmlTools.unmarshalJaxbEntity(workflowModel.getDwdl(), TWorkflow.class);
        dwdl.setId(workflowModel.getId());
        dwdl.setTargetNamespace(StringConventions.getWorkflowTargetNamespace(workflowModel.getId()));
        dwfm.setDwdl(XmlTools.marshalJaxbEntityIntoByteArray(new ObjectFactory().createWorkflow(dwdl)));

        /*
         * Get some potential deployment targets (servers)
         */
        String hqlString = "from ServerLoadView where serverType = :type)";
        Query q2 = evt.getSession().createQuery(hqlString).setString("type", ServerTypeEnum.Ode.toString());
        List<ServerLoadView> serverStatistics = q2.list();

        // deploy it
        try {
            result = deployer.deploy(dwdl, workflowModel.getTenant().getName(), serverStatistics,
                    new StandardDeploymentStrategy());

            // get servers on which it has been deployed
            List<Long> serverIds = result.getServers();
            Set<WorkflowModelIsDeployedOnServer> isDeployedRelations = new HashSet<WorkflowModelIsDeployedOnServer>();

            for (Long sid : serverIds) {
                WorkflowModelIsDeployedOnServer entry = new WorkflowModelIsDeployedOnServer();
                entry.setDeployedWorkflowModel(dwfm);
                entry.setServer((Server) evt.getSession().get(Server.class, sid));
                entry.setId(new WorkflowModelIsDeployedOnServerId(dwfm.getId(), sid));
                evt.getSession().save(entry);
                isDeployedRelations.add(entry);
            }

            dwfm.setDeployDate(DecidrGlobals.getTime().getTime());
            dwfm.setSoapTemplate(new byte[0]);
            dwfm.setWorkflowModelIsDeployedOnServers(isDeployedRelations);
        } catch (Exception e) {
            throw new TransactionException(e);
        }

        // write changes to DB
        evt.getSession().update(dwfm);
    }
}

From source file:de.decidr.model.commands.workitem.CreateWorkItemCommand.java

License:Apache License

@Override
public void transactionAllowed(TransactionStartedEvent evt) throws TransactionException {
    /*/* www.j a v a 2 s  . c o  m*/
     * A workflow instance is also uniquely identified by its deployed
     * workflow model and the ode process id. We're using this fact here to
     * find the workflow instance that "owns" the new work item.
     */
    String hql = "from WorkflowInstance w where w.odePid = :odePid "
            + "and w.deployedWorkflowModel.id = :deployedWorkflowModelId";

    Query q = evt.getSession().createQuery(hql);
    q.setString("odePid", odePid);
    q.setLong("deployedWorkflowModelId", deployedWorkflowModelId);

    WorkflowInstance owningInstance = (WorkflowInstance) q.uniqueResult();
    if (owningInstance == null) {
        throw new EntityNotFoundException(WorkflowInstance.class,
                "PID: " + odePid + ", DWFMID: " + deployedWorkflowModelId);
    }

    User owningUser = (User) evt.getSession().get(User.class, userId);
    if (owningUser == null) {
        throw new EntityNotFoundException(User.class, userId);
    }

    WorkItem newWorkItem = new WorkItem();
    newWorkItem.setCreationDate(DecidrGlobals.getTime().getTime());
    newWorkItem.setData(XmlTools.marshalJaxbEntityIntoByteArray(new ObjectFactory().createHumanTaskData(data)));
    newWorkItem.setDescription(description);
    newWorkItem.setName(name);
    newWorkItem.setStatus(WorkItemStatus.Fresh.toString());
    newWorkItem.setUser(owningUser);
    newWorkItem.setWorkflowInstance(owningInstance);

    evt.getSession().save(newWorkItem);

    /*
     * Now that we have saved the work item, we must persist and associate
     * any files that the HumanTaskData references.
     */
    persistAndAssociateFiles(newWorkItem);

    workItemId = newWorkItem.getId();

    if (notifyUser) {
        NotificationEvents.createdWorkItem(newWorkItem);
    }
}

From source file:de.decidr.model.commands.workitem.DeleteWorkItemCommand.java

License:Apache License

@Override
public void transactionAllowed(TransactionStartedEvent evt) {
    Query q = evt.getSession().createQuery("delete from WorkItem w where w.id = :workItemId");
    q.setLong("workItemId", workItemId);
    q.executeUpdate();//from   w  w  w .ja v a  2s  .  c om
}

From source file:de.fhdo.gui.admin.modules.collaboration.Benutzer.java

License:Apache License

public void onDeleted(String id, Object data) {
    logger.debug("onDeleted()");

    if (data != null && data instanceof Collaborationuser) {
        Collaborationuser user = (Collaborationuser) data;
        logger.debug("User deaktivieren: " + user.getName());

        // Person aus der Datenbank lschen
        Session hb_session = HibernateUtil.getSessionFactory().openSession();
        hb_session.getTransaction().begin();

        try {//  w  w  w  .j a v a  2 s . c  om
            Collaborationuser user_db = (Collaborationuser) hb_session.get(Collaborationuser.class,
                    user.getId());

            user_db.getRoles().clear();
            user_db.getDiscussiongroups().clear();
            user_db.getPrivileges().clear();
            user_db.setUsername("");
            user_db.setPassword("");
            user_db.setSalt("");
            user_db.setCity("");
            user_db.setCountry("");
            user_db.setEmail("");
            user_db.setNote("");
            user_db.setPhone("");
            user_db.setStreet("");
            user_db.setTitle("");
            user_db.setZip("");
            user_db.setSendMail(false);
            user_db.setActivated(false);
            user_db.setActivationTime(null);
            user_db.setActivationMd5("");
            user_db.setHidden(false);
            user_db.setEnabled(false);
            user_db.setDeleted(true);
            hb_session.update(user_db);

            //Check for userAssignment
            Query q = hb_session.createQuery("from Discussiongroup WHERE head= :p_head");
            q.setLong("p_head", user_db.getId());
            List<Discussiongroup> dgList = q.list();

            for (Discussiongroup dg : dgList) {

                Discussiongroup dg_db = (Discussiongroup) hb_session.get(Discussiongroup.class, dg.getId());
                dg.getCollaborationusers().clear();
                dg.getPrivileges().clear();
                dg.getDiscussiongroupobjects().clear();
                hb_session.delete(dg_db);
            }

            hb_session.getTransaction().commit();

            Messagebox.show("Benutzer wurde erfolgreich gelscht.", "Benutzer lschen", Messagebox.OK,
                    Messagebox.INFORMATION);
        } catch (Exception e) {
            hb_session.getTransaction().rollback();

            Messagebox.show("Fehler beim Lschen des Benutzers: " + e.getLocalizedMessage(),
                    "Benutzer lschen", Messagebox.OK, Messagebox.EXCLAMATION);
            initList();
        }
        hb_session.close();
    }
}