Package org.spw.controller

Source Code of org.spw.controller.DocumentController

/*
* DocumentController.java
*
* Created on 4 May 2007, 19:56
*
*/

package org.spw.controller;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.spw.model.Document;

/**
* This class manage the Advertisement entities.
* It's a basic CRUD controller
* It add the ability to parse the string representation to obtain
* the associated document.
* @author PSe
*/
public class DocumentController {
   
    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("VolunteerPU");
   
    /** Creates a new instance of DocumentController */
    public DocumentController() {
    }
   
    /**
     * Retrive all the documents
     * @return List of documents
     */
    public List<Document> getDocuments() {
        EntityManager em = emf.createEntityManager();
        List<Document> result = null;
        try {
            Query query = em.createNamedQuery("Document.findAll");
            result = query.getResultList();
        } catch (Exception e) {
            Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, "Error Query all Document", e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
    public void create(Document object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, "Error creating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    public Document read(Long key) {
        EntityManager em = emf.createEntityManager();
        Document retValue = null;
        try {
            retValue = em.find(Document.class, key);
        } catch (Exception e) {
            Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, "Error reading " + key, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return retValue;
    }
   
    public Document update(Document object) {
        Document result = null;
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            result = em.merge(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, "Error updating " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return result;
    }
   
   
    public void delete(Document object) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            Document entity = em.find(Document.class, object.getIdDocument());
            em.remove(entity);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, "Error deleting " + object, e);
            if (em.getTransaction().isActive())
                em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }
   
    /**
     * From the string representation, get the corresponding document
     *
     * @param documentString String representation of the document
     * @return The corresponding document
     */
    public Document parse(String documentString) {
        int begin = documentString.lastIndexOf('[') + 1;
        int last  = documentString.lastIndexOf(']');
        if (begin < 0 || last < 0 || begin > last) return null;

        Document retValue = null;
        String number = documentString.substring(begin, last);
        try {
            retValue = read(Long.parseLong(number));
        } catch (NumberFormatException ex) {
            //incorrect number, could not happen
            Logger.getLogger(DocumentController.class.getName()).severe(documentString +
                    " is incorrect as a string version of a Document to parse. [" +
                    ex.getLocalizedMessage() + "]");
            //TODO: remove all the Logger.GLOBAL_LOGGER_NAME and replace by MyClass.getName()
        }
        return retValue;
    }
}
TOP

Related Classes of org.spw.controller.DocumentController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.