Example usage for javax.persistence EntityManager createNamedQuery

List of usage examples for javax.persistence EntityManager createNamedQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createNamedQuery.

Prototype

public Query createNamedQuery(String name);

Source Link

Document

Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).

Usage

From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#countOrganizations()
 *///w  w  w  .j a  v  a 2 s  .  c o m
@Override
public int countOrganizations() throws OrganizationDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query query = em.createNamedQuery("Organization.getCount");
        Long total = (Long) query.getSingleResult();
        return total.intValue();
    } catch (Exception e) {
        logger.error("Could not find number of organizations.", e);
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:it.infn.ct.futuregateway.apiserver.v1.TaskCollectionService.java

/**
 * Retrieve a task list for the user.//  w  w w  . j  a v a 2 s  . com
 * Tasks are retrieved from the storage for the user performing the request.
 *
 * @return A list of tasks
 */
private List<Task> retrieveTaskList() {
    List<Task> lstTasks = new LinkedList<>();
    EntityManager em = getEntityManager();
    EntityTransaction et = null;
    List<Object[]> taskList = null;
    try {
        et = em.getTransaction();
        et.begin();
        taskList = em.createNamedQuery("tasks.userAll").setParameter("user", getUser()).getResultList();
        et.commit();
    } catch (RuntimeException re) {
        if (et != null && et.isActive()) {
            et.rollback();
        }
        log.error("Impossible to retrieve the task list");
        log.error(re);
        throw new RuntimeException("Impossible to access the task list");
    } finally {
        em.close();
    }
    if (taskList != null && !taskList.isEmpty()) {
        for (Object[] elem : taskList) {
            int idElem = 0;
            Task tmpTask = new Task();
            tmpTask.setId((String) elem[idElem++]);
            tmpTask.setDescription((String) elem[idElem++]);
            tmpTask.setStatus((Task.STATUS) elem[idElem++]);
            tmpTask.setDateCreated((Date) elem[idElem]);
            lstTasks.add(tmpTask);
        }
    }
    return lstTasks;
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@Override
public void documentListener() {
    Menus menus;/*from w  w w. j a va  2s  .co m*/
    documentMenu.clear();
    path = Paths.get(DIR, "AnaDepo", family, type, brand);
    //path = Paths.get("D:", "Teknosa", family, type, brand);
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    documentMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setLabel(menus.getLabel1());
            menus.setValue(menus.getValued());
            documentMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@Override
public void brandListener() {
    Menus menus;/* www .jav a 2s. com*/
    brandMenu.clear();
    documentMenu.clear();
    path = Paths.get(DIR, "AnaDepo", family, type);
    //path = Paths.get("D:", "Teknosa", family, type);
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    brandMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setLabel(menus.getLabel1());
            menus.setValue(menus.getValued());
            brandMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@Override
public void typeListener() {
    Menus menus;/* w w w  .  ja  v a2s.  c om*/
    typeMenu.clear();
    brandMenu.clear();
    documentMenu.clear();

    path = Paths.get(DIR, "AnaDepo", family);
    //path = Paths.get("D:", "Teknosa", family);
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    typeMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setLabel(menus.getLabel1());
            menus.setValue(menus.getValued());
            typeMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:com.edsoft.teknosaproject.bean.ReportBean.java

@PostConstruct
public void initialize() {
    list = new ArrayList<>();
    filtered = new ArrayList<>();
    Menus menus;/*from w  w  w . j  a  v a2s . c  om*/
    familyMenu = new ArrayList<>();
    typeMenu = new ArrayList<>();
    brandMenu = new ArrayList<>();
    documentMenu = new ArrayList<>();
    path = Paths.get(DIR, "AnaDepo");
    //path = Paths.get("D:", "Teknosa");
    fileList = path.toFile().listFiles();
    EntityManager em = Persistence.createEntityManagerFactory("teknosa").createEntityManager();
    em.getTransaction().begin();
    Query query = em.createNamedQuery("Menus.findByValued");
    familyMenu.add(new SelectItem("", "Seiniz"));
    for (File file : fileList) {
        if (file.isDirectory()) {
            query.setParameter("valued", file.getName());
            menus = (Menus) query.getResultList().get(0);
            menus.setValue(menus.getValued());
            menus.setLabel(menus.getLabel1());
            familyMenu.add(menus);
        }
    }
    em.getTransaction().commit();
    em.close();
}

From source file:org.apache.james.user.jpa.JPAUsersRepository.java

/**
 * Returns whether or not this user is in the repository
 * //from   w  ww  .  j a  v a  2  s  .  c  o m
 * @param name
 *            the name to check in the repository
 * @return whether the user is in the repository
 * @throws UsersRepositoryException
 */
public boolean contains(String name) throws UsersRepositoryException {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    try {
        return (Long) entityManager.createNamedQuery("containsUser").setParameter("name", name.toLowerCase())
                .getSingleResult() > 0;
    } catch (PersistenceException e) {
        getLogger().debug("Failed to find user", e);
        throw new UsersRepositoryException("Failed to find user" + name, e);
    } finally {
        entityManager.close();
    }
}

From source file:org.apache.james.user.jpa.JPAUsersRepository.java

/**
 * Returns a count of the users in the repository.
 * //w  w w  . ja  v a 2  s . co m
 * @return the number of users in the repository
 * @throws UsersRepositoryException
 */
public int countUsers() throws UsersRepositoryException {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    try {
        return ((Long) entityManager.createNamedQuery("countUsers").getSingleResult()).intValue();
    } catch (PersistenceException e) {
        getLogger().debug("Failed to find user", e);
        throw new UsersRepositoryException("Failed to count users", e);
    } finally {
        entityManager.close();
    }
}

From source file:org.apache.james.user.jpa.JPAUsersRepository.java

/**
 * Get the user object with the specified user name. Return null if no such
 * user./* ww w .  j av  a 2s .  c om*/
 * 
 * @param name
 *            the name of the user to retrieve
 * @return the user being retrieved, null if the user doesn't exist
 * 
 * @since James 1.2.2
 */
public User getUserByName(String name) throws UsersRepositoryException {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    try {
        return (JPAUser) entityManager.createNamedQuery("findUserByName").setParameter("name", name)
                .getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (PersistenceException e) {
        getLogger().debug("Failed to find user", e);
        throw new UsersRepositoryException("Unable to search user", e);
    } finally {
        entityManager.close();
    }
}

From source file:fr.mby.opa.picsimpl.dao.DbProposalDao.java

@Override
public ProposalBranch forkBranch(final ProposalBranch fork, final long branchToForkId) {
    Assert.notNull(fork, "No ProposalBag supplied !");
    Assert.isNull(fork.getId(), "Id should not be set for creation !");

    // Duplicate the branch to Fork
    new TxCallback(this.getEmf()) {

        @Override/*www . j a  va2 s  .co m*/
        @SuppressWarnings("unchecked")
        protected void executeInTransaction(final EntityManager em) {
            final Query loadFullBranchQuery = em.createNamedQuery(ProposalBranch.LOAD_FULL_BRANCH);
            loadFullBranchQuery.setParameter("branchId", branchToForkId);
            final ProposalBranch branchToFork = Iterables.getFirst(loadFullBranchQuery.getResultList(), null);
            if (branchToFork == null) {
                throw new ProposalBranchNotFoundException();
            }

            fork.setBags(branchToFork.getBags());
            em.persist(fork);
        }
    };

    return fork;
}