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");
}
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();
}
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;
}
Aggregations