May 18 2014
Java EE DAO Pattern
Java EE really makes our life easy when dealing with Database Management. Here is a basic DAO design pattern that I use in most of my projects:
public abstract class AbstractDao, ID extends Serializable> {
protected abstract EntityManager getEntityManager();
private Class entity;
public AbstractDao(Class entity) {
this.entity = entity;
}
public T find(ID id) {
return getEntityManager().find(entity, id);
}
public List findAll() {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));
return getEntityManager().createQuery(cq).getResultList();
}
public List findByPage(int pageNum, int pageSize) {
CriteriaQuery cq = getEntityManager().getCriteriaBuilder()
.createQuery(entity);
cq.select(cq.from(entity));
int firstResult = pageNum * pageSize;
return getEntityManager().createQuery(cq).setFirstResult(firstResult)
.setMaxResults(pageSize).getResultList();
}
public long count() {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Long.class);
Root personRoot = cq.from(entity);
cq.select(cb.count(personRoot));
List list = getEntityManager().createQuery(cq).setMaxResults(1)
.getResultList();
long personsNumber = 0l;
if (!list.isEmpty()) {
personsNumber = list.get(0);
}
return personsNumber;
}
public T save(T entity) {
try {
if (entity.getId() == null) {
getEntityManager().persist(entity);
} else {
entity = getEntityManager().merge(entity);
}
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}
public T saveAndFlush(T entity) {
try {
entity = save(entity);
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return entity;
}
public void flush() {
getEntityManager().flush();
}
public List saveAllAndFlush(Collection collection) {
List list = null;
try {
list = collection.parallelStream()
.map(this::save)
.collect(Collectors.toList());
flush();
} catch (Exception e) {
getLogger().error(e.getMessage());
}
return list;
}
public boolean exists(ID id) {
return find(id) != null;
}
protected abstract Logger getLogger();
}
May 24 2014
Android: Circular ViewPager
Recently I was building an Android Application and needed a circular ViewPager. That is a ViewPager that will automatically move to the first page when you swipe the last. Apparently there is no Native Support for this, neither could I find any good implementation online.
So here is a simple but effective way to do this.
The idea is basically to apply the functionality to the PagerAdapter you will insert in the ViewPager. What you do is create a huge set of Pages (lets say 10000) and set the current page in the middle.
So here it is:
/** * Created by Chris Georgoulis on 8/3/2014. */ public class ImageAdapter extends PagerAdapter { public static final int PAGER_PAGES = 10000; public static final int PAGER_PAGES_MIDDLE = PAGER_PAGES / 2; private List drawables; public ImageAdapter(List drawables) { this.drawables = drawables; }By Chris Georgoulis • Android, Programming 0