2
0
Fork 0

basic slit in multi module project

This commit is contained in:
willem.cazander 2010-09-19 22:02:18 +02:00
parent f79378dacb
commit d6d77072d9
198 changed files with 25404 additions and 0 deletions

View file

@ -0,0 +1,96 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import org.hibernate.Session;
import com.idcanet.vasc.core.AbstractVascBackend;
import com.idcanet.vasc.core.VascException;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2007
*/
abstract public class AbstractHibernateVascBackend extends AbstractVascBackend {
/**
* Provides a hibernate session which is closed !! when transaction is compleeted.
* @return
*/
abstract Session getHibernateSession();
public void persist(Object object) throws VascException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
s.persist(object);
s.getTransaction().commit();
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
public Object merge(Object object) throws VascException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Object result = s.merge(object);
s.getTransaction().commit();
return result;
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
public void delete(Object object) throws VascException {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Object newObject = s.merge(object);
s.delete(newObject);
s.getTransaction().commit();
} finally {
if (s!=null && s.isOpen()) {
if (s.getTransaction().isActive()) {
s.getTransaction().rollback();
}
//s.close();
}
}
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import java.lang.reflect.Method;
import javax.persistence.EntityManager;
import com.idcanet.vasc.core.AbstractVascBackend;
import com.idcanet.vasc.core.VascException;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2007
*/
abstract public class AbstractPersistenceVascBackend extends AbstractVascBackend {
protected boolean emTransaction = true;
abstract EntityManager getEntityManager();
public void persist(Object object) throws VascException {
EntityManager s = getEntityManager();
try {
if (emTransaction) {
s.getTransaction().begin();
}
s.persist(object);
if (emTransaction) {
s.getTransaction().commit();
}
} finally {
if (s!=null) {
//s.close();
}
}
}
public Object merge(Object object) throws VascException {
EntityManager s = getEntityManager();
try {
if (emTransaction) {
s.getTransaction().begin();
}
Object result = s.merge(object);
if (emTransaction) {
s.getTransaction().commit();
}
return result;
} finally {
if (s!=null) {
//s.close();
}
}
}
public void delete(Object object) throws VascException {
EntityManager s = getEntityManager();
try {
if (emTransaction) {
s.getTransaction().begin();
}
Object newObject = s.merge(object);
s.remove(newObject);
if (emTransaction) {
s.getTransaction().commit();
}
} finally {
if (s!=null) {
//s.close();
}
}
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import javax.persistence.EntityManager;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2009
*/
public interface EntityManagerProvider {
public EntityManager getEntityManager();
public boolean hasEntityManagerTransaction();
}

View file

@ -0,0 +1,39 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import org.hibernate.Session;
/**
*
* @author Willem Cazander
* @version 1.0 Mar 21, 2009
*/
public interface HibernateSessionProvider {
public Session getHibernateSession();
}

View file

@ -0,0 +1,433 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import com.idcanet.vasc.backends.BeanVascEntryFieldValue;
import com.idcanet.vasc.backends.BeanVascEntryRecordCreator;
import com.idcanet.vasc.core.VascBackendState;
import com.idcanet.vasc.core.VascEntry;
import com.idcanet.vasc.core.VascEntryField;
import com.idcanet.vasc.core.VascException;
import com.idcanet.vasc.core.entry.VascEntryFieldValue;
import com.idcanet.vasc.core.entry.VascEntryRecordCreator;
import com.idcanet.xtes.xpql.query.QueryParameterValue;
/**
* Manages persistance with xpql queries
*
* @author Willem Cazander
* @version 1.0 Mar 17, 2009
*/
public class XpqlHibernateVascBackend extends AbstractHibernateVascBackend {
private HibernateSessionProvider hibernateSessionProvider = null;
private com.idcanet.xtes.xpql.query.Query query = null;
private com.idcanet.xtes.xpql.query.Query queryTotal = null;
private com.idcanet.xtes.xpql.query.Query queryMoveUp = null;
private com.idcanet.xtes.xpql.query.Query queryMoveUpDown = null;
private com.idcanet.xtes.xpql.query.Query queryMoveDown = null;
private com.idcanet.xtes.xpql.query.Query queryMoveDownUp = null;
private Class<?> resultClass = null;
public XpqlHibernateVascBackend() {
}
/**
* @see com.idcanet.vasc.backends.jpa.AbstractPersistenceVascBackend#getEntityManager()
*/
@Override
Session getHibernateSession() {
return hibernateSessionProvider.getHibernateSession();
}
@SuppressWarnings("unchecked")
public List<Object> execute(VascBackendState state) throws VascException {
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
query.setQueryParameter(key, value);
if (queryTotal!=null) {
queryTotal.setQueryParameter(key, value);
}
}
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Query q = s.createQuery(query.toPreparedSQL(query));
List<QueryParameterValue> values = query.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
if (isPageable()) {
q.setFirstResult(state.getPageIndex());
q.setMaxResults(state.getPageSize());
}
List<Object> data = q.list();
s.getTransaction().commit();
return data;
} finally {
if (s!=null) {
//em.close();
}
}
}
/**
* @see com.idcanet.vasc.core.VascBackend#provideVascEntryFieldValue(com.idcanet.vasc.core.VascEntryField)
*/
public VascEntryFieldValue provideVascEntryFieldValue(VascEntryField field) {
BeanVascEntryFieldValue result = new BeanVascEntryFieldValue();
return result;
}
/**
* @see com.idcanet.vasc.core.VascBackend#provideVascEntryRecordCreator(com.idcanet.vasc.core.VascEntry)
*/
public VascEntryRecordCreator provideVascEntryRecordCreator(VascEntry vascEntry) {
return new BeanVascEntryRecordCreator(resultClass);
}
/**
* @return the query
*/
public com.idcanet.xtes.xpql.query.Query getQuery() {
return query;
}
/**
* @param query the query to set
*/
public void setQuery(com.idcanet.xtes.xpql.query.Query query) {
this.query = query;
}
/**
* @return the queryTotal
*/
public com.idcanet.xtes.xpql.query.Query getQueryTotal() {
return queryTotal;
}
/**
* @param queryTotal the queryTotal to set
*/
public void setQueryTotal(com.idcanet.xtes.xpql.query.Query queryTotal) {
this.queryTotal = queryTotal;
}
/**
* @return the resultClass
*/
public Class<?> getResultClass() {
return resultClass;
}
/**
* @param resultClass the resultClass to set
*/
public void setResultClass(Class<?> resultClass) {
this.resultClass = resultClass;
}
/**
* @return the hibernateSessionProvider
*/
public HibernateSessionProvider getHibernateSessionProvider() {
return hibernateSessionProvider;
}
/**
* @param hibernateSessionProvider the hibernateSessionProvider to set
*/
public void setHibernateSessionProvider(HibernateSessionProvider hibernateSessionProvider) {
this.hibernateSessionProvider = hibernateSessionProvider;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#isPageable()
*/
@Override
public boolean isPageable() {
if (queryTotal==null) {
return false;
}
return true;
}
/**
* @see com.idcanet.vasc.core.VascBackend#fetchTotalExecuteSize(VascBackendState state)
*/
public long fetchTotalExecuteSize(VascBackendState state) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
Query q = s.createQuery(queryTotal.toPreparedSQL(queryTotal));
List<QueryParameterValue> values = queryTotal.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
Long resultTotal = (Long)q.uniqueResult();
s.getTransaction().commit();
return resultTotal;
} finally {
if (s!=null) {
//em.close();
}
}
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#doRecordMoveDownById(java.lang.Object)
*/
@Override
public long doRecordMoveDownById(VascBackendState state,Object primaryId) throws VascException {
long result = 0l;
if (queryMoveDown!=null) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDown.setQueryParameter(key, value);
}
// set id
queryMoveDown.setQueryParameter("id", primaryId);
// execute update
Query q = s.createQuery(queryMoveDown.toPreparedSQL(queryMoveDown));
List<QueryParameterValue> values = queryMoveDown.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDownUp.setQueryParameter(key, value);
}
// set id
queryMoveDownUp.setQueryParameter("id", primaryId);
// execute update
q = s.createQuery(queryMoveDownUp.toPreparedSQL(queryMoveDownUp));
values = queryMoveDownUp.getOrderQueryParameterValues();
i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
s.getTransaction().commit();
} finally {
if (s!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#doRecordMoveUpById(java.lang.Object)
*/
@Override
public long doRecordMoveUpById(VascBackendState state,Object primaryId) throws VascException {
long result = 0l;
if (queryMoveUp!=null) {
Session s = getHibernateSession();
try {
s.getTransaction().begin();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUp.setQueryParameter(key, value);
}
// set id
queryMoveUp.setQueryParameter("id", primaryId);
// execute update
Query q = s.createQuery(queryMoveUp.toPreparedSQL(queryMoveUp));
List<QueryParameterValue> values = queryMoveUp.getOrderQueryParameterValues();
int i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUpDown.setQueryParameter(key, value);
}
// set id
queryMoveUpDown.setQueryParameter("id", primaryId);
// execute update
q = s.createQuery(queryMoveUpDown.toPreparedSQL(queryMoveUpDown));
values = queryMoveUpDown.getOrderQueryParameterValues();
i = 0;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
if (valueObject!=null) {
q.setParameter(i,valueObject);
} else {
q.setParameter(i,valueObject,Hibernate.INTEGER);
}
i++;
}
result+=q.executeUpdate();
s.getTransaction().commit();
} finally {
if (s!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#isRecordMoveable()
*/
@Override
public boolean isRecordMoveable() {
return queryMoveUp!=null & queryMoveDown!=null;
}
/**
* @return the queryMoveUp
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveUp() {
return queryMoveUp;
}
/**
* @param queryMoveUp the queryMoveUp to set
*/
public void setQueryMoveUp(com.idcanet.xtes.xpql.query.Query queryMoveUp) {
this.queryMoveUp = queryMoveUp;
}
/**
* @return the queryMoveDown
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveDown() {
return queryMoveDown;
}
/**
* @param queryMoveDown the queryMoveDown to set
*/
public void setQueryMoveDown(com.idcanet.xtes.xpql.query.Query queryMoveDown) {
this.queryMoveDown = queryMoveDown;
}
/**
* @return the queryMoveUpDown
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveUpDown() {
return queryMoveUpDown;
}
/**
* @param queryMoveUpDown the queryMoveUpDown to set
*/
public void setQueryMoveUpDown(com.idcanet.xtes.xpql.query.Query queryMoveUpDown) {
this.queryMoveUpDown = queryMoveUpDown;
}
/**
* @return the queryMoveDownUp
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveDownUp() {
return queryMoveDownUp;
}
/**
* @param queryMoveDownUp the queryMoveDownUp to set
*/
public void setQueryMoveDownUp(com.idcanet.xtes.xpql.query.Query queryMoveDownUp) {
this.queryMoveDownUp = queryMoveDownUp;
}
}

View file

@ -0,0 +1,437 @@
/*
* Copyright 2004-2007 IDCA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and
* the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the authors and
* should not be interpreted as representing official policies, either expressed or implied, of IDCA.
*/
package com.idcanet.vasc.backends.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.idcanet.vasc.backends.BeanVascEntryFieldValue;
import com.idcanet.vasc.backends.BeanVascEntryRecordCreator;
import com.idcanet.vasc.core.VascBackendState;
import com.idcanet.vasc.core.VascEntry;
import com.idcanet.vasc.core.VascEntryField;
import com.idcanet.vasc.core.VascException;
import com.idcanet.vasc.core.entry.VascEntryFieldValue;
import com.idcanet.vasc.core.entry.VascEntryRecordCreator;
import com.idcanet.xtes.xpql.query.QueryParameterValue;
/**
* Manages persistance with xpql queries
*
* @author Willem Cazander
* @version 1.0 Mar 17, 2009
*/
public class XpqlPersistanceVascBackend extends AbstractPersistenceVascBackend {
private EntityManagerProvider entityManagerProvider = null;
private com.idcanet.xtes.xpql.query.Query query = null;
private com.idcanet.xtes.xpql.query.Query queryTotal = null;
private com.idcanet.xtes.xpql.query.Query queryMoveUp = null;
private com.idcanet.xtes.xpql.query.Query queryMoveUpDown = null;
private com.idcanet.xtes.xpql.query.Query queryMoveDown = null;
private com.idcanet.xtes.xpql.query.Query queryMoveDownUp = null;
private Class<?> resultClass = null;
public XpqlPersistanceVascBackend() {
}
/**
* @see com.idcanet.vasc.backends.jpa.AbstractPersistenceVascBackend#getEntityManager()
*/
@Override
EntityManager getEntityManager() {
emTransaction=entityManagerProvider.hasEntityManagerTransaction();
return entityManagerProvider.getEntityManager();
}
@SuppressWarnings("unchecked")
public List<Object> execute(VascBackendState state) throws VascException {
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
//System.out.println("Set para pame: "+key+" value: "+value);
query.setQueryParameter(key, value);
if (queryTotal!=null) {
queryTotal.setQueryParameter(key, value);
}
}
EntityManager em = getEntityManager();
try {
Query q = em.createQuery(query.toPreparedSQL(query));
List<QueryParameterValue> values = query.getOrderQueryParameterValues();
int i = 1;
for (QueryParameterValue value:values) {
Object valueObject = value.getValue();
q.setParameter(i,valueObject);
//System.out.println("Set para index: "+i+" value: "+valueObject+" valueClass: "+valueObject.getClass()+" valueType: "+value.getValueType());
i++;
}
if (isPageable()) {
q.setFirstResult(state.getPageIndex());
q.setMaxResults(state.getPageSize());
}
if (emTransaction) {
em.getTransaction().begin();
}
List<Object> data = q.getResultList();
if (emTransaction) {
em.getTransaction().commit();
}
return data;
} finally {
if (em!=null) {
//em.close();
}
}
}
/**
* @see com.idcanet.vasc.core.VascBackend#provideVascEntryFieldValue(com.idcanet.vasc.core.VascEntryField)
*/
public VascEntryFieldValue provideVascEntryFieldValue(VascEntryField field) {
VascEntryFieldValue result = new BeanVascEntryFieldValue();
return result;
}
/**
* @see com.idcanet.vasc.core.VascBackend#provideVascEntryRecordCreator(com.idcanet.vasc.core.VascEntry)
*/
public VascEntryRecordCreator provideVascEntryRecordCreator(VascEntry vascEntry) {
VascEntryRecordCreator result = new BeanVascEntryRecordCreator(resultClass);
return result;
}
/**
* @return the query
*/
public com.idcanet.xtes.xpql.query.Query getQuery() {
return query;
}
/**
* @param query the query to set
*/
public void setQuery(com.idcanet.xtes.xpql.query.Query query) {
this.query = query;
}
/**
* @return the queryTotal
*/
public com.idcanet.xtes.xpql.query.Query getQueryTotal() {
return queryTotal;
}
/**
* @param queryTotal the queryTotal to set
*/
public void setQueryTotal(com.idcanet.xtes.xpql.query.Query queryTotal) {
this.queryTotal = queryTotal;
}
/**
* @return the resultClass
*/
public Class<?> getResultClass() {
return resultClass;
}
/**
* @param resultClass the resultClass to set
*/
public void setResultClass(Class<?> resultClass) {
this.resultClass = resultClass;
}
/**
* @return the entityManagerProvider
*/
public EntityManagerProvider getEntityManagerProvider() {
return entityManagerProvider;
}
/**
* @param entityManagerProvider the entityManagerProvider to set
*/
public void setEntityManagerProvider(EntityManagerProvider entityManagerProvider) {
this.entityManagerProvider = entityManagerProvider;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#isPageable()
*/
@Override
public boolean isPageable() {
if (queryTotal==null) {
return false;
}
return true;
}
/**
* @see com.idcanet.vasc.core.VascBackend#fetchTotalExecuteSize(VascBackendState state)
*/
public long fetchTotalExecuteSize(VascBackendState state) {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery(queryTotal.toPreparedSQL(queryTotal));
List<QueryParameterValue> values = queryTotal.getOrderQueryParameterValues();
int i = 1;
for (QueryParameterValue value:values) {
q.setParameter(i,value.getValue());
i++;
}
if (emTransaction) {
em.getTransaction().begin();
}
Long resultTotal = (Long)q.getSingleResult();
if (emTransaction) {
em.getTransaction().commit();
}
return resultTotal;
} finally {
if (em!=null) {
//em.close();
}
}
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#doRecordMoveDownById(VascBackendState state,java.lang.Object)
*/
@Override
public long doRecordMoveDownById(VascBackendState state,Object primaryId) throws VascException {
long result = 0l;
if (queryMoveDown!=null) {
EntityManager em = getEntityManager();
try {
if (emTransaction) {
em.getTransaction().begin();
}
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDown.setQueryParameter(key, value);
}
// set id
queryMoveDown.setQueryParameter("id", primaryId);
// execute update
Query q = em.createQuery(queryMoveDown.toPreparedSQL(queryMoveDown));
List<QueryParameterValue> values = queryMoveDown.getOrderQueryParameterValues();
int i = 1;
for (QueryParameterValue value:values) {
q.setParameter(i,value.getValue());
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveDownUp.setQueryParameter(key, value);
}
// set id
queryMoveDownUp.setQueryParameter("id", primaryId);
// execute update
q = em.createQuery(queryMoveDownUp.toPreparedSQL(queryMoveDownUp));
values = queryMoveDownUp.getOrderQueryParameterValues();
i = 1;
for (QueryParameterValue value:values) {
q.setParameter(i,value.getValue());
i++;
}
result+=q.executeUpdate();
if (emTransaction) {
em.getTransaction().commit();
}
} finally {
if (em!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#doRecordMoveUpById(VascBackendState state,java.lang.Object)
*/
@Override
public long doRecordMoveUpById(VascBackendState state,Object primaryId) throws VascException {
long result = 0l;
if (queryMoveUp!=null) {
EntityManager em = getEntityManager();
try {
if (emTransaction) {
em.getTransaction().begin();
}
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUp.setQueryParameter(key, value);
}
// set id
queryMoveUp.setQueryParameter("id", primaryId);
// execute update
Query q = em.createQuery(queryMoveUp.toPreparedSQL(queryMoveUp));
List<QueryParameterValue> values = queryMoveUp.getOrderQueryParameterValues();
int i = 1;
for (QueryParameterValue value:values) {
q.setParameter(i,value.getValue());
i++;
}
result+=q.executeUpdate();
// Copy parameters
for (String key:state.getDataParameterKeys()) {
Object value = state.getDataParameter(key);
queryMoveUpDown.setQueryParameter(key, value);
}
// set id
queryMoveUpDown.setQueryParameter("id", primaryId);
// execute update
q = em.createQuery(queryMoveUpDown.toPreparedSQL(queryMoveUpDown));
values = queryMoveUpDown.getOrderQueryParameterValues();
i = 1;
for (QueryParameterValue value:values) {
q.setParameter(i,value.getValue());
i++;
}
result+=q.executeUpdate();
if (emTransaction) {
em.getTransaction().commit();
}
} finally {
if (em!=null) {
//em.close();
}
}
}
return result;
}
/**
* @see com.idcanet.vasc.core.AbstractVascBackend#isRecordMoveable()
*/
@Override
public boolean isRecordMoveable() {
return queryMoveUp!=null & queryMoveDown!=null;
}
/**
* @return the queryMoveUp
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveUp() {
return queryMoveUp;
}
/**
* @param queryMoveUp the queryMoveUp to set
*/
public void setQueryMoveUp(com.idcanet.xtes.xpql.query.Query queryMoveUp) {
this.queryMoveUp = queryMoveUp;
}
/**
* @return the queryMoveDown
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveDown() {
return queryMoveDown;
}
/**
* @param queryMoveDown the queryMoveDown to set
*/
public void setQueryMoveDown(com.idcanet.xtes.xpql.query.Query queryMoveDown) {
this.queryMoveDown = queryMoveDown;
}
/**
* @return the queryMoveUpDown
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveUpDown() {
return queryMoveUpDown;
}
/**
* @param queryMoveUpDown the queryMoveUpDown to set
*/
public void setQueryMoveUpDown(com.idcanet.xtes.xpql.query.Query queryMoveUpDown) {
this.queryMoveUpDown = queryMoveUpDown;
}
/**
* @return the queryMoveDownUp
*/
public com.idcanet.xtes.xpql.query.Query getQueryMoveDownUp() {
return queryMoveDownUp;
}
/**
* @param queryMoveDownUp the queryMoveDownUp to set
*/
public void setQueryMoveDownUp(com.idcanet.xtes.xpql.query.Query queryMoveDownUp) {
this.queryMoveDownUp = queryMoveDownUp;
}
/**
* @see com.idcanet.vasc.core.VascBackend#isSearchable()
*/
public boolean isSearchable() {
if (query.getQueryParameterValue("text_search")==null) {
return false;
}
//return true;
return false;
}
}