All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.plugin.content.StaticContentManager Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evasion.plugin.content;

import com.evasion.entity.StaticPage;
import com.evasion.entity.security.User;
import com.evasion.exception.PersistenceViolationException;
import com.evasion.plugin.security.UserDetailsService;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author sebastien.glon
 */
public class StaticContentManager {

    private EntityManager em;

    public StaticContentManager(EntityManager em) {
        this.em = em;
    }

    public StaticPage savePage(StaticPage page) throws PersistenceViolationException {
        StaticPage newPage = page;
        page.setDateMaj(new Date());
        try {
            if (page.getId() == null) {
                em.persist(newPage);
            } else {
                newPage = em.merge(newPage);
            }
            em.flush();
        } catch (Exception e) {
            throw new PersistenceViolationException("Erreur dans la validation de la page statique", e.fillInStackTrace());
        }
        return newPage;
    }

    public StaticPage createPage(StaticPage page, String userName) throws PersistenceViolationException {
        if (page.getId() != null) {
            throw new PersistenceViolationException("Cette méthode ne peut être utilisé pour une page déja existente");
        }
        User user = new UserDetailsService(em).findUserByUserName(userName);
        page.setUser(user);
        return savePage(page);
    }

    public void deletePage(StaticPage p) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listAll() {
        List list;
        Query query = em.createNamedQuery(StaticPage.FIND_ALL);
        list = query.getResultList();
//        for (StaticPage page : list) {
//            em.refresh(page);
//        }
//        list.size();
        return list;
    }

    public StaticPage getStaticPage(Long id) {
        StaticPage page = em.find(StaticPage.class, id);
        if (page != null && page.getCorps()!=null) {
            page.getCorps().length();
        }
        return page;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy