package com.vsc.study.ejb.sessionbean;
import com.vsc.study.ejb.entitybean.Function;
import com.vsc.study.ejb.remote.ManyActionSessionRemote;
import com.vsc.study.ejb.util.LoggerInterceptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.interceptor.Interceptors;
@Stateless
//@Interceptors({LoggerInterceptor.class})
public class ManyActionSessionBean implements ManyActionSessionRemote {
@PersistenceContext
private EntityManager em;
public Map getCurrentResults(int currentPage, int pageSize,
int maxResultSet,String querySql) {
if(maxResultSet==0){
maxResultSet = em.createQuery(querySql).getResultList().size();
}
List list = em.createQuery(querySql).setFirstResult(
(currentPage - 1) * pageSize).setMaxResults(pageSize)
.getResultList();
HashMap hashMap = new HashMap();
hashMap.put(maxResultSet, list);
return hashMap;
}
public Map getCurrentResults(int currentPage, int pageSize,
int maxResultSet,Object object) {
if(maxResultSet==0){
maxResultSet = em.createQuery("from " + object.getClass().getSimpleName()).getResultList().size();
}
List list = em.createQuery("from " + object.getClass().getSimpleName())
.setFirstResult((currentPage - 1) * pageSize).setMaxResults(
pageSize).getResultList();
HashMap hashMap = new HashMap();
hashMap.put(maxResultSet, list);
return hashMap;
}
public int remove(Object object) {
if (object == null)
return -1;
try {
em.remove(this.search(object));
return 0;
} catch (Exception e) {
return -2;
}
}
public int[] removes(Object[] objects) {
if(objects==null)
throw new NullPointerException();
int[] results = new int[objects.length];
for (int i = 0; i < objects.length; i++) {
Object obj = em.find(objects.getClass(), this.getId(objects[i]));
if (obj != null) {
results[i] = this.remove(obj);
}
}
return results;
}
public int update(Object object) {
if (object == null || this.getId(object) == null)
return -1;
Object obj = this.search(object);
if (obj == null)
return -2;
this.updateEntityBean(object, obj);
try {
em.merge(obj.getClass().cast(obj));
return 0;
} catch (Exception e) {
return -1;
}
}
public Object search(Object object) {
if (object == null || this.getId(object) == null)
return -1;
try {
return em.find(object.getClass(), this.getId(object));
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public List searchAll(Object object) {
if (object == null)
return null;
try {
return em.createQuery("from " + object.getClass().getSimpleName()).getResultList();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Long insert(Object object) {
try {
em.persist(object.getClass().cast(object));
return this.getId(object);
} catch (Exception e) {
e.printStackTrace();
return -1l;
}
}
public Long getId(Object object) {
if (object == null)
return -1l;
try {
return (Long) object.getClass().getDeclaredMethod("getId").invoke(
object);
} catch (Exception e) {
e.printStackTrace();
return -5l;
}
}
/**
* obj is update Object
*
* @param object
* @param obj
*/
public void updateEntityBean(Object object, Object obj) {
try {
Method[] methods = object.getClass().getDeclaredMethods();
Method method;
Object objectMethodValue;
Object objMethodValue;
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().trim().startsWith("get")) {
if (methods[i].invoke(object) == null) {
System.out
.println("----------this field is null--------------");
continue;
}
objectMethodValue = methods[i].invoke(object);
objMethodValue = methods[i].invoke(obj);
if (objectMethodValue.equals(objMethodValue)) {
System.out
.println("----------field is equals--------------");
continue;
}
method = obj.getClass().getDeclaredMethod(
methods[i].getName().trim().replaceFirst("g", "s"),
methods[i].getReturnType());
method.invoke(obj, objectMethodValue);
System.out.println("==============="
+ methods[i].invoke(obj));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}