Chris Georgoulis

About Chris Georgoulis

Summary: - Primary programming language: Java SE (SWING, JDBC), Java EE, (JSF - PrimeFaces, JPA - EclipseLink/Hibernate, EJB, CDI ), - Secondary languages: C/C++, Web Technologies(HTML, XHTML, XML, CSS, JavaScript, jQuery, AngularJS). - Frameworks: Android SDK, Global Sensors Network (GSN), Open Computer Vision (OpenCV). - IDE's: Eclipse, Visual Studio - Other Apps: MS Office, Photoshop, Dreamweaver - Database Design - SQL(MySQL), NoSQL(MongoDB) - Modeling Tools: UML

Posts by Chris Georgoulis:

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;
	}

More

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

Mozilla has disallowed javascript: execution via the address bar

If you try running javascript code via the address bar (not not from a bookmark) it no longer works as mozilla has disabled it for 2 years now.

Trying the following:

javascript:alert("hello");

Causes the following error to appear in the error console:

Error: uncaught exception: ReferenceError: alert is not defined

More

Why Java passes objects as references passed by value

I came across a question recently on stackoverflow that was very enlightening and proves how objects are actually passed in java. It was quite an interesting discovery!

I am quoting from here

 

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d = new Dog("Fifi");
  d.name.equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Max"); // true

In this example aDog.name will still be “Max”. “d” is not overwritten in the function as the object reference is passed by value.

Likewise:

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d.setname("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Fifi"); // true

Add new JSON document using plain text string with LightCouch

Hey,

Small post outlining a small modification to the 0.4 lightcouch source to allow direct plain text json insertion. A colleague and I had a JSON string which we wanted to insert into lightcouch without having to create a JSON object – only to have .toString() be called during the final POST request.

More

Killing CouchDB on Ubuntu 10.04

I Wanted to do some maintenance and move my couchdb data files to another partition but couchdb would not stop when I called the usual stop commands:

1)sudo /etc/init.d/couchdb stop
2)sudo service couchdb stop

Found a lot of posts and out of all of them found this nice tidbit below:

ps -U couchdb -o pid= | xargs kill -9

 

Note: It’s not the best thing to viciously kill a process. I imagine databases usually have shutdown tasks to do, must end with current insertions, etc. I’ll post if I find a bug fix.