Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Now that we know what we are aiming for, let's start thinking about the storage layer of our application. As mentioned earlier, we will use ObjectDB to store our objects. We already have a document class that will hold information about our uploaded documents. So, let's create the Data Access Object to retrieve, store, modify, and delete our documents.

...

ObjectDB supports using JPA (Java Persistence API). Using JPA decouples our data access object we will implement from the concrete implementation, which makes it easier later on to switch to a different storage solution. To use ObjectDB/JPA, we first need to add a file called persistence.xml in which we configure what database to use. Create a new folder in 'src/main/resources' called "META-INF". In there create a file called 'persistence.xml' and paste the following content:

...

For now, let's implement three methods in our document service: store and retrieve a document and get all stored documents. Add an autowired IDocumentDAO field to your DocumentService. Then use the injected instance of IDocumentDAO to implement a getDocuments method that returns all stored documents. Similarly implement a method to get a document by its id. Last but not least, implement a method that takes creates a new document, generates an id for the documentit, set sets the current date as upload date, and then stores it. Note that we will extend this method later to also store the uploaded file.

...

Code Block
languagejava
themeEclipse
titleDocumentService.java
package edu.asu.diging.tutorial.spring.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.asu.diging.tutorial.spring.core.IDocument;
import edu.asu.diging.tutorial.spring.dao.IDocumentDAO;

/**
 * Class providing methods to manage documents.
 * 
 * @author jdamerow
 *
 */
@Service
public class DocumentService implements IDocumentService  {

	@Autowired
	private IDocumentDAO documentDao;
	
	public List<IDocument> getDocuments() {
		return documentDao.getAllDocuments();
	}
	
	public IDocument getDocument(String id) {
		return documentDao.get(id);
	}
	
	/**
	 * Method to store a new document. This method will create a new document, assign a new id to the passedit,
	 * document and set its date to the current date/time.
	 * 
	 * @param doc Document to be stored.
	 */
	public void storeDocument(String title, String username, int version) {
		IDocument doc = new Document();
		doc.setCurrent(true);
		doc.setTitle(title);
		doc.setUploader(username) {;
		doc.setVersion(version);

		String id = "DOC" + generateId();
		while(true) {
			if (documentDao.get(id) == null) {
					break;
			}
			id = "DOC" + generateId();
		}
		doc.setId(id);
		doc.setDate(new Date());
		
		// still need to set path to file
		
		documentDao.store(doc);
	}
	
	/**
	 * This methods generates a new 6 character long id. Note that this method
	 * does not assure that the id isn't in use yet. 
	 * 
	 * @return 6 character id
	 */
	private String generateId() {
		char[] chars = 
		        "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
		
		Random random = new Random();
		StringBuilder builder = new StringBuilder();
		for (int i=0; i<6; i++) {
			builder.append(chars[random.nextInt(62)]);
		}
		
		return builder.toString();
	}
}

...

Now that the backend is set up, let's take a look at the front end again.