Example usage for javax.persistence TypedQuery setParameter

List of usage examples for javax.persistence TypedQuery setParameter

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setParameter.

Prototype

TypedQuery<X> setParameter(int position, Object value);

Source Link

Document

Bind an argument value to a positional parameter.

Usage

From source file:com.softwarecorporativo.monitoriaifpe.funcionais.atividade.TesteAtividade.java

@Test
public void testeAprovarAtividadesDeMonitoria() {
    Query query = super.entityManager
            .createQuery("UPDATE Atividade AS a SET a.situacao = ?1 WHERE a.situacao = ?2");
    query.setParameter(1, SituacaoAtividade.APROVADA);
    query.setParameter(2, SituacaoAtividade.AGUARDANDO_APROVACAO);
    query.executeUpdate();/*from   w w  w . j  a  v  a 2 s  .  c o m*/
    TypedQuery<Long> typedQuery = super.entityManager
            .createQuery("SELECT COUNT(a) FROM Atividade a where a.situacao = :situacao", Long.class);
    typedQuery.setParameter("situacao", SituacaoAtividade.AGUARDANDO_APROVACAO);
    Long quantidadeEsperada = 0L;
    Long quantidade = typedQuery.getSingleResult();
    assertEquals(quantidadeEsperada, quantidade);
}

From source file:org.openmeetings.app.data.user.dao.UsersDaoImpl.java

/**
 * check for duplicates//from w ww.  j  a  v  a 2 s.c  om
 * 
 * @param DataValue
 * @return
 */
public boolean checkUserLogin(String DataValue) {
    try {
        TypedQuery<Users> query = em.createQuery(
                "select c from Users as c where c.login = :DataValue AND c.deleted <> :deleted", Users.class);
        query.setParameter("DataValue", DataValue);
        query.setParameter("deleted", "true");
        int count = query.getResultList().size();

        if (count != 0) {
            return false;
        }
    } catch (Exception ex2) {
        log.error("[checkUserData]", ex2);
    }
    return true;
}

From source file:com.softwarecorporativo.monitoriaifpe.funcionais.atividade.TesteAtividade.java

@Test
public void testeRemoverAtividadesDeMonitoria() {
    Monitoria monitoria = super.entityManager.find(Monitoria.class, 2L);
    Query query = super.entityManager.createQuery("DELETE Atividade AS a WHERE a.monitoria = ?1");
    query.setParameter(1, monitoria);//  w  w  w .j av a2s.  c o  m
    query.executeUpdate();
    TypedQuery<Long> typedQuery = super.entityManager
            .createQuery("SELECT COUNT(a) FROM Atividade a where a.monitoria = :monitoria", Long.class);
    typedQuery.setParameter("monitoria", monitoria);
    Long quantidadeEsperada = 0L;
    Long quantidade = typedQuery.getSingleResult();
    assertEquals(quantidadeEsperada, quantidade);
}

From source file:org.noorganization.instalist.server.api.UnitResourceTest.java

@Test
public void testDeleteUnit() throws Exception {
    String url = "/groups/%d/units/%s";
    Instant preDelete = mUnit.getUpdated();

    Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString()))
            .request().delete();/*from ww  w.  j  a  v a  2  s.com*/
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").delete();
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAUnit.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(401, wrongGroupResponse.getStatus());

    Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAUnit.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(404, wrongListResponse.getStatus());

    Response okResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(200, okResponse.getStatus());

    TypedQuery<Unit> savedUnitQuery = mManager
            .createQuery("select u from Unit u where " + "u.group = :group and u.UUID = :uuid", Unit.class);
    savedUnitQuery.setParameter("group", mGroup);
    savedUnitQuery.setParameter("uuid", mUnit.getUUID());
    List<Unit> savedUnits = savedUnitQuery.getResultList();
    assertEquals(0, savedUnits.size());
    TypedQuery<DeletedObject> savedDeletedUnitQuery = mManager.createQuery(
            "select do from "
                    + "DeletedObject do where do.group = :group and do.UUID = :uuid and do.type = :type",
            DeletedObject.class);
    savedDeletedUnitQuery.setParameter("group", mGroup);
    savedDeletedUnitQuery.setParameter("uuid", mUnit.getUUID());
    savedDeletedUnitQuery.setParameter("type", DeletedObject.Type.UNIT);
    List<DeletedObject> savedDeletedUnits = savedDeletedUnitQuery.getResultList();
    assertEquals(1, savedDeletedUnits.size());
    assertTrue(preDelete.isBefore(savedDeletedUnits.get(0).getUpdated()));
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceGroupRepository.java

/**
 * Fetches the resourceGroup as required for the create deployment popup of the deploy screen
 *///from   w  w  w .  ja va2 s  .c om
public ResourceGroupEntity getResourceGroupForCreateDeploy(Integer groupId) {
    TypedQuery<ResourceGroupEntity> q = entityManager.createQuery("select r from ResourceGroupEntity r"
            + " left join fetch r.resources res" + " left join fetch res.consumedMasterRelations"
            + " left join fetch res.resourceTags" + " where r.id=:id", ResourceGroupEntity.class);
    q.setParameter("id", groupId);
    return q.getSingleResult();
}

From source file:org.noorganization.instalist.server.api.RecipeResourceTest.java

@Test
public void testPostRecipe() throws Exception {
    String url = "/groups/%d/recipes";
    RecipeInfo newRecipe = new RecipeInfo().withUUID(mRecipe.getUUID()).withName("recipe3");
    Instant preInsert = Instant.now();

    Response notAuthorizedResponse = target(String.format(url, mGroup.getId())).request()
            .post(Entity.json(newRecipe));
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").post(Entity.json(newRecipe));
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(String.format(url, mNAGroup.getId())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe));
    assertEquals(401, wrongGroupResponse.getStatus());

    Response goneResponse = target(String.format(url, mGroup.getId())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe));
    assertEquals(409, goneResponse.getStatus());
    mManager.refresh(mRecipe);/*from  w  w  w .j a  v a 2  s  .  co  m*/
    assertEquals("recipe1", mRecipe.getName());

    newRecipe.setUUID(UUID.randomUUID());
    Response okResponse = target(String.format(url, mGroup.getId())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).post(Entity.json(newRecipe));
    assertEquals(201, okResponse.getStatus());
    TypedQuery<Recipe> savedRecipeQuery = mManager
            .createQuery("select r from " + "Recipe r where r.group = :group and r.UUID = :uuid", Recipe.class);
    savedRecipeQuery.setParameter("group", mGroup);
    savedRecipeQuery.setParameter("uuid", UUID.fromString(newRecipe.getUUID()));
    List<Recipe> savedRecipes = savedRecipeQuery.getResultList();
    assertEquals(1, savedRecipes.size());
    assertEquals("recipe3", savedRecipes.get(0).getName());
    assertTrue(preInsert.isBefore(savedRecipes.get(0).getUpdated()));
}

From source file:org.noorganization.instalist.server.api.RecipeResource.java

/**
 * Get a list of recipes.// w  w  w . ja  v  a2  s.c o m
 * @param _groupId The id of the group containing the recipes.
 * @param _changedSince Limits the request to elements that changed since the given date. ISO
 *                      8601 time e.g. 2016-01-19T11:54:07+0100. Optional.
 */
@GET
@TokenSecured
@Produces({ "application/json" })
public Response getRecipes(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince)
        throws Exception {
    Instant changedSince = null;
    try {
        if (_changedSince != null)
            changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant();
    } catch (ParseException _e) {
        return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE);
    }

    EntityManager manager = DatabaseHelper.getInstance().getManager();
    List<Recipe> recipes;
    List<DeletedObject> deletedRecipes;
    DeviceGroup group = manager.find(DeviceGroup.class, _groupId);

    if (changedSince != null) {
        TypedQuery<Recipe> recipeQuery = manager.createQuery(
                "select r from Recipe r where " + "r.group = :group and r.updated > :updated", Recipe.class);
        recipeQuery.setParameter("group", group);
        recipeQuery.setParameter("updated", changedSince);
        recipes = recipeQuery.getResultList();

        TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery(
                "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and "
                        + "do.type = :type",
                DeletedObject.class);
        deletedRecipesQuery.setParameter("group", group);
        deletedRecipesQuery.setParameter("updated", changedSince);
        deletedRecipesQuery.setParameter("type", DeletedObject.Type.RECIPE);
        deletedRecipes = deletedRecipesQuery.getResultList();
    } else {
        recipes = new ArrayList<Recipe>(group.getRecipes());

        TypedQuery<DeletedObject> deletedRecipesQuery = manager.createQuery(
                "select do " + "from DeletedObject do where do.group = :group and do.type = :type",
                DeletedObject.class);
        deletedRecipesQuery.setParameter("group", group);
        deletedRecipesQuery.setParameter("type", DeletedObject.Type.RECIPE);
        deletedRecipes = deletedRecipesQuery.getResultList();
    }
    manager.close();

    ArrayList<RecipeInfo> rtn = new ArrayList<RecipeInfo>(recipes.size() + deletedRecipes.size());
    for (Recipe current : recipes) {
        RecipeInfo toAdd = new RecipeInfo().withDeleted(false);
        toAdd.setUUID(current.getUUID());
        toAdd.setName(current.getName());
        toAdd.setLastChanged(Date.from(current.getUpdated()));
        rtn.add(toAdd);
    }
    for (DeletedObject current : deletedRecipes) {
        RecipeInfo toAdd = new RecipeInfo().withDeleted(true);
        toAdd.setUUID(current.getUUID());
        toAdd.setLastChanged(Date.from(current.getUpdated()));
        rtn.add(toAdd);
    }

    return ResponseFactory.generateOK(rtn);
}

From source file:org.noorganization.instalist.server.api.RecipeResourceTest.java

@Test
public void testDeleteRecipe() throws Exception {
    String url = "/groups/%d/recipes/%s";
    Instant preDelete = mRecipe.getUpdated();

    Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString()))
            .request().delete();/*ww w. jav a  2  s .  c  om*/
    assertEquals(401, notAuthorizedResponse.getStatus());

    Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").delete();
    assertEquals(401, wrongAuthResponse.getStatus());

    Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNARecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(401, wrongGroupResponse.getStatus());

    Response wrongListResponse = target(String.format(url, mGroup.getId(), mNARecipe.getUUID().toString()))
            .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(404, wrongListResponse.getStatus());

    Response okResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())).request()
            .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).delete();
    assertEquals(200, okResponse.getStatus());

    TypedQuery<Recipe> savedRecipeQuery = mManager
            .createQuery("select r from Recipe r where " + "r.group = :group and r.UUID = :uuid", Recipe.class);
    savedRecipeQuery.setParameter("group", mGroup);
    savedRecipeQuery.setParameter("uuid", mRecipe.getUUID());
    List<Recipe> savedRecipes = savedRecipeQuery.getResultList();
    assertEquals(0, savedRecipes.size());
    TypedQuery<DeletedObject> savedDeletedUnitQuery = mManager.createQuery(
            "select do from "
                    + "DeletedObject do where do.group = :group and do.UUID = :uuid and do.type = :type",
            DeletedObject.class);
    savedDeletedUnitQuery.setParameter("group", mGroup);
    savedDeletedUnitQuery.setParameter("uuid", mRecipe.getUUID());
    savedDeletedUnitQuery.setParameter("type", DeletedObject.Type.RECIPE);
    List<DeletedObject> savedDeletedRecipes = savedDeletedUnitQuery.getResultList();
    assertEquals(1, savedDeletedRecipes.size());
    assertTrue(preDelete.isBefore(savedDeletedRecipes.get(0).getUpdated()));
}

From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java

/**
 * Checks if the given hash is present in the ow_base_avatar table
 *
 * @param aHash//  w  w w.  j a  v  a 2 s .co  m
 * @return
 */
public boolean isAvatarHashPresent(Long aHash) {
    if (myDryRun) {
        myLog.debug("DryRun is active. Not checking for duplicate avatar hash");
        return false;
    } // if myDryRun

    CriteriaBuilder cb = myEntityManager.getCriteriaBuilder();

    CriteriaQuery<OxAvatar> criteria = cb.createQuery(OxAvatar.class);
    Root<OxAvatar> root = criteria.from(OxAvatar.class);
    ParameterExpression<Long> avatarParam = cb.parameter(Long.class);
    avatarParam = cb.parameter(Long.class);
    criteria.select(root).where(cb.equal(root.get("hash"), avatarParam));
    TypedQuery<OxAvatar> query = myEntityManager.createQuery(criteria);
    query.setParameter(avatarParam, aHash);
    List<OxAvatar> results = query.getResultList();

    if (results.size() > 0) {
        myLog.debug("given avatar hash is already present: " + aHash);
        return true;
    } // if results.size() > 0

    return false;
}