Package com.evasion.plugin.content

Source Code of com.evasion.plugin.content.StaticContentManager

/*
* 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<StaticPage> listAll() {
        List<StaticPage> 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;
    }

}
TOP

Related Classes of com.evasion.plugin.content.StaticContentManager

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.