In-Memory Datastore with Swing

It’s the end of my stay on the Sourcing project and I thought it’d be fun to use the in-memory datastore (QLMDatastore) in a little Swing application.

So here goes:

  • Define Book entity.

Book entity is a simple POJO except for getPrimaryKeyHandler(). Here we’re using a numeric key handler so that a numeric id’s will be generated for each book added to the datastore.

public class Book extends BaseEntityData {

	private String authorName;
	private String title;
	private Date publicationDate;
	private String bookId;

	public static final String PROPERTY_BOOK_ID = "bookId";

	public IPrimaryKeyHandler getPrimaryKeyHandler() {
		return new NumericPrimaryKeyHandlerImpl(Book.class, PROPERTY_BOOK_ID);
	}

	//getters and setters here
}
  • Define BookService
public class BookService implements IBookService {

	private IBookDAO bookDAO;

	public List<Book> getBooks() throws BookServiceException {
		try {
			List<Book> books = getBookDAO().getBooks();
			EntityComparator comparator = new EntityComparator(Book.class);
			comparator.addComparison("authorName", EntityComparator.ASCEND);
			comparator.addComparison("title", EntityComparator.ASCEND);
			comparator.addComparison("publicationDate", EntityComparator.ASCEND);
			comparator.sort(books);
			return books;
		} catch (DataStoreException e) {
			throw new BookServiceException(e);
		}
	}

	public IBookDAO getBookDAO() {
		return bookDAO;
	}

	public void setBookDAO(IBookDAO bookDAO) {
		this.bookDAO = bookDAO;
	}

	public Book saveBook(Book book) throws BookServiceException {
		try {
			return getBookDAO().saveBook(book);
		} catch (DataStoreException e) {
			throw new BookServiceException(e);
		}
	}
}
  • Define BookDao
public class BookDAO implements IBookDAO {

	private IDataStore dataStore;

	public IDataStore getDataStore() {
		return dataStore;

	}

	public void setDataStore(IDataStore dataStore) {
		this.dataStore = dataStore;
	}

	public List<Book> getBooks() throws DataStoreException {
		return getDataStore().findAllEntities(Book.class);
	}

	public Book saveBook(Book book) throws DataStoreException {
		return (Book) getDataStore().saveEntity(book);
	}
}
  • Define DataStorageController, BookService and BookDao services in the Spring application context. Configure the transactional boundaries of these services.
  • Load the Spring application context in the application and inject the services

That’s it! To run the application, you have to supply a System property to indicate where the datastore files is to be placed:

-Dservice.datastore.rootDir=c:/

The application took little more than a spare hour (interleaved with a little baking time). Most of the time was really spent editing the applicationContext.xml and fussing with Swing table model and layout.

Except for QLMUtilities and Spring obviously, QLMDataStore does not require any other Sourcing projects. Not surprising since Sourcing consists of 2 web separate applications – QLMSourcing and QLMFormServer; each has its own datastore and run within one JBoss instance.

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply