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

com.alogient.cameleon.sdk.content.dao.impl.PageDaoImpl Maven / Gradle / Ivy

The newest version!
package com.alogient.cameleon.sdk.content.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.jdbc.Work;

import com.alogient.cameleon.sdk.common.dao.impl.BaseDao;
import com.alogient.cameleon.sdk.content.dao.PageDao;
import com.alogient.cameleon.sdk.content.dao.model.Page;
import com.alogient.cameleon.sdk.content.dao.model.Template;

/**
 * Data access object used to manipulate page related data
 * 
 * @author alord
 */
public class PageDaoImpl extends BaseDao implements PageDao {
    /**
     * {@inheritDoc}
     */
    @Override
    public boolean exist(Integer cultureId, Integer pageId, Boolean isLive) {
        Query query = getCurrentSession().createSQLQuery(
                "SELECT " + "COUNT(*) AS res " + "FROM " + "Page AS p " + "JOIN Culture AS c " + "ON p.CultureCode = c.CultureCode " + "WHERE "
                        + "p.PageID = :pageId AND c.CultureID = :cultureId AND " + "(( :isLive = 1 AND p.PageVisibility = 1) OR (:isPreview = 1 AND p.PageVisibilityPreview = 1))");

        query.setParameter("pageId", pageId);
        query.setParameter("cultureId", cultureId);

        query.setParameter("isLive", Boolean.valueOf(isLive));
        query.setParameter("isPreview", Boolean.valueOf(!isLive));

        Integer cr = (Integer) query.uniqueResult();

        return cr != 0;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Page getPage(Integer pageId) {
        return (Page) getHibernateTemplate().get(Page.class, pageId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Page getPage(Integer cultureId, Integer navId) {
        Query query = getCurrentSession().getNamedQuery("getPageFromNav");

        query.setParameter("navId", navId);
        query.setParameter("cultureId", cultureId);

        Page page = (Page) query.uniqueResult();

        return page;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public List getSubPages(Integer pageId) {
        Query query = getCurrentSession().getNamedQuery("getSubPage");

        query.setParameter("parentPageId", pageId);

        List pages = query.list();

        return pages;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public List getSubPages(Integer cultureId, Integer navId) {
        Query query = getCurrentSession().getNamedQuery("getNavSubPage");

        query.setParameter("parentNavId", navId);
        query.setParameter("cultureId", cultureId);

        List pages = query.list();

        return pages;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public List getTemplatePages(String templateName, Integer cultureId) {
        Query query = getCurrentSession().getNamedQuery("getTemplatePages");

        query.setParameter("templateName", templateName);
        query.setParameter("cultureId", cultureId);

        List pages = query.list();

        return pages;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer createPage(Integer parentNavId, String templateName, String cultureCode, String pageName, String author) {
        Integer templateId = null;
        Template template = (Template) getCurrentSession().getNamedQuery("getTemplate").setParameter("templateName", templateName).uniqueResult();
        if (template != null) {
            templateId = template.getTemplateId();
        }

        PageIns createPage = new PageIns(parentNavId, templateId, cultureCode, pageName, author);
        getCurrentSession().doWork(createPage);

        return createPage.getNavId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void pageUpdate(Page page, String author) {
        getCurrentSession().flush();

        PageUpd updatePage = new PageUpd(page.getNav().getNavId(), page.getCulture().getCultureCode(), page.getPageNamePreview(), page.getPageNavEnd(), author);
        getCurrentSession().doWork(updatePage);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void pageApprove(Integer navId, String cultureCode, String author) {
        PageApprove approvePage = new PageApprove(navId, cultureCode, author);
        getCurrentSession().doWork(approvePage);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void pageVisibilityUpdate(Integer navId, String cultureCode, boolean isLive, boolean isVisible, String author) {
        PageVisibilityUpd pageVisibilityUpdate = new PageVisibilityUpd(navId, cultureCode, isLive, isVisible, author);
        getCurrentSession().doWork(pageVisibilityUpdate);
    }

    /**
     * [dbo].[PageIns] ( @ParentID int, @TemplateID int, @CultureCode varchar(64), @PageName
     * nvarchar(256), @Author varchar(1024) = NULL )
     */
    private class PageIns implements Work {
        private Integer navId;

        private Integer parentNavId;

        private Integer templateId;

        private String cultureCode;

        private String pageName;

        private String author;

        public PageIns(Integer parentNavId, Integer templateId, String cultureCode, String pageName, String author) {
            this.parentNavId = parentNavId;
            this.templateId = templateId;
            this.cultureCode = cultureCode;
            this.pageName = pageName;
            this.author = author;
        }

        @Override
        public void execute(Connection connection) throws SQLException {
            CallableStatement cState = connection.prepareCall("{call [dbo].[PageIns]( ?, ?, ?, ?, ?)}");

            cState.setInt(1, parentNavId);
            cState.setInt(2, templateId);
            cState.setString(3, cultureCode);
            cState.setString(4, pageName);
            cState.setString(5, author);

            cState.execute();

            while (true) {
                cState.getUpdateCount();
                ResultSet res = cState.getResultSet();

                if (res != null) {
                    res.next();
                    navId = res.getInt("ItemID");
                    res.close();
                    break;
                }

                if (cState.getMoreResults() == false && cState.getUpdateCount() == -1) {
                    break;
                }
            }
        }

        protected Integer getNavId() {
            return navId;
        }
    }

    /**
     * [dbo].[PageUpd] ( @NavID int, @CultureCode varchar(64), @PageName nvarchar(256) = NULL,
     * @PageNavEnd bit = 0, @Author varchar(1024) = NULL )
     */
    private class PageUpd implements Work {
        private Integer navId;

        private String cultureCode;

        private String pageName;

        private boolean isNavEnd;

        private String author;

        public PageUpd(Integer navId, String cultureCode, String pageName, boolean isNavEnd, String author) {
            this.navId = navId;
            this.cultureCode = cultureCode;
            this.pageName = pageName;
            this.isNavEnd = isNavEnd;
            this.author = author;
        }

        @Override
        public void execute(Connection connection) throws SQLException {
            CallableStatement cState = connection.prepareCall("{call [dbo].[PageUpd]( ?, ?, ?, ?, ?) }");

            cState.setInt(1, navId);
            cState.setString(2, cultureCode);
            cState.setString(3, pageName);
            cState.setBoolean(4, isNavEnd);
            cState.setString(5, author);

            cState.executeUpdate();
        }
    }

    /**
     * [dbo].[PageApprove] ( @NavID int, @CultureCode varchar(64), @Author varchar(1024) = NULL )
     */
    private class PageApprove implements Work {
        private Integer navId;

        private String cultureCode;

        private String author;

        public PageApprove(Integer navId, String cultureCode, String author) {
            this.navId = navId;
            this.cultureCode = cultureCode;
            this.author = author;
        }

        @Override
        public void execute(Connection connection) throws SQLException {
            CallableStatement cState = connection.prepareCall("{call [dbo].[PageApprove]( ?, ?, ?) }");

            cState.setInt(1, navId);
            cState.setString(2, cultureCode);
            cState.setString(3, author);

            cState.executeUpdate();
        }
    }

    /**
     * [dbo].[PageVisibilityUpd] ( @NavID int, @CultureCode varchar(64), @Live bit, @Visible bit,
     * @Author varchar(1024) = NULL )
     */
    private class PageVisibilityUpd implements Work {
        private Integer navId;

        private String cultureCode;

        private boolean isLive;

        private boolean isVisible;

        private String author;

        public PageVisibilityUpd(Integer navId, String cultureCode, boolean isLive, boolean isVisible, String author) {
            this.navId = navId;
            this.cultureCode = cultureCode;
            this.isLive = isLive;
            this.isVisible = isVisible;
            this.author = author;
        }

        @Override
        public void execute(Connection connection) throws SQLException {
            CallableStatement cState = connection.prepareCall("{call [dbo].[PageVisibilityUpd]( ?, ?, ?, ?, ?) }");

            cState.setInt(1, navId);
            cState.setString(2, cultureCode);
            cState.setBoolean(3, isLive);
            cState.setBoolean(4, isVisible);
            cState.setString(5, author);

            cState.executeUpdate();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy