Search in sources :

Example 1 with JpaPersistentItem

use of org.openhab.persistence.jpa.internal.model.JpaPersistentItem in project openhab1-addons by openhab.

the class JpaPersistenceService method store.

@Override
public void store(Item item, String alias) {
    logger.debug("Storing item: {}", item.getName());
    if (item.getState() instanceof UnDefType) {
        logger.debug("This item is of undefined type. Cannot persist it!");
        return;
    }
    if (!JpaConfiguration.isInitialized) {
        logger.debug("Trying to create EntityManagerFactory but we don't have configuration yet!");
        return;
    }
    // determine item name to be stored
    String name = (alias != null) ? alias : item.getName();
    JpaPersistentItem pItem = new JpaPersistentItem();
    try {
        String newValue = StateHelper.toString(item.getState());
        pItem.setValue(newValue);
        logger.debug("Stored new value: {}", newValue);
    } catch (Exception e1) {
        logger.error("Error on converting state value to string: {}", e1.getMessage());
        return;
    }
    pItem.setName(name);
    pItem.setRealName(item.getName());
    pItem.setTimestamp(new Date());
    EntityManager em = getEntityManagerFactory().createEntityManager();
    try {
        logger.debug("Persisting item...");
        // In RESOURCE_LOCAL calls to EntityManager require a begin/commit
        em.getTransaction().begin();
        em.persist(pItem);
        em.getTransaction().commit();
        logger.debug("Persisting item...done");
    } catch (Exception e) {
        logger.error("Error on persisting item! Rolling back!");
        logger.error(e.getMessage(), e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
    logger.debug("Storing item...done");
}
Also used : EntityManager(javax.persistence.EntityManager) UnDefType(org.openhab.core.types.UnDefType) JpaPersistentItem(org.openhab.persistence.jpa.internal.model.JpaPersistentItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) Date(java.util.Date)

Example 2 with JpaPersistentItem

use of org.openhab.persistence.jpa.internal.model.JpaPersistentItem in project openhab1-addons by openhab.

the class JpaPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    logger.debug("Querying for historic item: {}", filter.getItemName());
    if (!JpaConfiguration.isInitialized) {
        logger.warn("Trying to create EntityManagerFactory but we don't have configuration yet!");
        return Collections.emptyList();
    }
    String itemName = filter.getItemName();
    Item item = getItemFromRegistry(itemName);
    String sortOrder;
    if (filter.getOrdering() == Ordering.ASCENDING) {
        sortOrder = "ASC";
    } else {
        sortOrder = "DESC";
    }
    boolean hasBeginDate = false;
    boolean hasEndDate = false;
    String queryString = "SELECT n FROM " + JpaPersistentItem.class.getSimpleName() + " n WHERE n.realName = :itemName";
    if (filter.getBeginDate() != null) {
        queryString += " AND n.timestamp >= :beginDate";
        hasBeginDate = true;
    }
    if (filter.getEndDate() != null) {
        queryString += " AND n.timestamp <= :endDate";
        hasEndDate = true;
    }
    queryString += " ORDER BY n.timestamp " + sortOrder;
    logger.debug("The query: " + queryString);
    EntityManager em = getEntityManagerFactory().createEntityManager();
    try {
        // In RESOURCE_LOCAL calls to EntityManager require a begin/commit
        em.getTransaction().begin();
        logger.debug("Creating query...");
        Query query = em.createQuery(queryString);
        query.setParameter("itemName", item.getName());
        if (hasBeginDate) {
            query.setParameter("beginDate", filter.getBeginDate());
        }
        if (hasEndDate) {
            query.setParameter("endDate", filter.getEndDate());
        }
        query.setFirstResult(filter.getPageNumber() * filter.getPageSize());
        query.setMaxResults(filter.getPageSize());
        logger.debug("Creating query...done");
        logger.debug("Retrieving result list...");
        @SuppressWarnings("unchecked") List<JpaPersistentItem> result = query.getResultList();
        logger.debug("Retrieving result list...done");
        List<HistoricItem> historicList = JpaHistoricItem.fromResultList(result, item);
        if (historicList != null) {
            logger.debug(String.format("Convert to HistoricItem: %d", historicList.size()));
        }
        em.getTransaction().commit();
        return historicList;
    } catch (Exception e) {
        logger.error("Error on querying database!");
        logger.error(e.getMessage(), e);
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
    return Collections.emptyList();
}
Also used : Item(org.openhab.core.items.Item) JpaPersistentItem(org.openhab.persistence.jpa.internal.model.JpaPersistentItem) HistoricItem(org.openhab.core.persistence.HistoricItem) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) JpaPersistentItem(org.openhab.persistence.jpa.internal.model.JpaPersistentItem) HistoricItem(org.openhab.core.persistence.HistoricItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 3 with JpaPersistentItem

use of org.openhab.persistence.jpa.internal.model.JpaPersistentItem in project openhab1-addons by openhab.

the class JpaHistoricItem method fromResultList.

/**
     * This method maps a jpa result item to this historic item.
     * 
     * @param jpaQueryResult the result which jpa items
     * @param item used for query information, like the state (State)
     * @return list of historic items
     */
public static List<HistoricItem> fromResultList(List<JpaPersistentItem> jpaQueryResult, Item item) {
    List<HistoricItem> ret = new ArrayList<HistoricItem>();
    for (JpaPersistentItem i : jpaQueryResult) {
        HistoricItem hi = fromPersistedItem(i, item);
        ret.add(hi);
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) JpaPersistentItem(org.openhab.persistence.jpa.internal.model.JpaPersistentItem) HistoricItem(org.openhab.core.persistence.HistoricItem)

Aggregations

JpaPersistentItem (org.openhab.persistence.jpa.internal.model.JpaPersistentItem)3 EntityManager (javax.persistence.EntityManager)2 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)2 HistoricItem (org.openhab.core.persistence.HistoricItem)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Query (javax.persistence.Query)1 Item (org.openhab.core.items.Item)1 UnDefType (org.openhab.core.types.UnDefType)1