SitePageFacade.java 5.34 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.facade;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.PageContent;
import fi.codecrew.moya.model.PageContent_;
import fi.codecrew.moya.model.Role;
import fi.codecrew.moya.model.Role_;
import fi.codecrew.moya.model.SitePage;
import fi.codecrew.moya.model.SitePage_;

@Stateless
public class SitePageFacade extends IntegerPkGenericFacade<SitePage> {

	@EJB
	private EventBeanLocal eventbean;
	private static final Logger logger = LoggerFactory.getLogger(SitePageFacade.class);

	public SitePageFacade() {

		super(SitePage.class);
	}

	// public SitePage find(String siteName) {
	// CriteriaBuilder cb = getEm().getCriteriaBuilder();
	// CriteriaQuery<SitePage> cq = cb.createQuery(SitePage.class);
	// Root<SitePage> root = cq.from(SitePage.class);
	//
	// cq.where(cb.equal(root.get(SitePage_.name), siteName),
	// cb.equal(root.get(SitePage_.event), eventbean.getCurrentEvent())
	//
	// );
	// return getSingleNullableResult(getEm().createQuery(cq));
	// }

	public List<SitePage> findForUser(EventUser user, SitePage siteroot) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<SitePage> cq = cb.createQuery(SitePage.class);
		Root<SitePage> root = cq.from(SitePage.class);

		ListJoin<Role, EventUser> usrpath = root.join(SitePage_.allowedRoles).join(Role_.users);

		Path<SitePage> parentpath = root.get(SitePage_.parent);
		Predicate rootpred = null;

		if (siteroot == null)
		{
			rootpred = cb.isNull(parentpath);
		}
		else
		{
			rootpred = cb.equal(parentpath, siteroot);
		}

		cq.where(
				cb.equal(usrpath, user),
				rootpred,
				cb.equal(root.get(SitePage_.event), eventbean.getCurrentEvent())
				);

		return getEm().createQuery(cq).getResultList();

	}

	public List<SitePage> findAll() {
		return findAll(eventbean.getCurrentEvent());
	}

	public List<SitePage> findAll(LanEvent event)
	{
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<SitePage> cq = cb.createQuery(SitePage.class);
		Root<SitePage> root = cq.from(SitePage.class);

		cq.where(
				cb.isNull(root.get(SitePage_.parent)),
				cb.equal(root.get(SitePage_.event), event)
				);

		return getEm().createQuery(cq).getResultList();

	}

	public SitePage find(Integer id, List<Role> require) {

		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<SitePage> cq = cb.createQuery(SitePage.class);
		Root<SitePage> root = cq.from(SitePage.class);
		cq.where(
				cb.equal(root.get(SitePage_.id), id),
				root.get(SitePage_.allowedRoles).in(require)
				);

		return getSingleNullableResult(getEm().createQuery(cq));

	}

	public List<PageContent> findContents(SitePage page, Date now, Locale locale) {


		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<PageContent> cq = cb.createQuery(PageContent.class);
		Root<PageContent> root = cq.from(PageContent.class);

		Path<SitePage> sitepath = root.get(PageContent_.sitepage);
		Path<Date> expirepath = root.get(PageContent_.expire);
		Path<Date> publishpath = root.get(PageContent_.publish);
		cq.where(
				cb.equal(sitepath, page),
				cb.or(cb.isNull(publishpath), cb.greaterThan(publishpath, now)),
				cb.or(cb.isNull(expirepath), cb.lessThan(expirepath, now))
				);
		cq.orderBy(cb.desc(root.get(PageContent_.sort)));

		ArrayList<PageContent> ret = new ArrayList<>(getEm().createQuery(cq).getResultList());

		if (locale != null) {

			final String localeLang = locale.getLanguage();
			for (Iterator<PageContent> contIter = ret.iterator(); contIter.hasNext();) {
				PageContent content = contIter.next();
				// Show only if language equals or is null
				Locale contLocale = content.getLocaleObject();
				if (contLocale != null && !localeLang.equals(contLocale.getLanguage())) {
					contIter.remove();
				}
			}
		}
		return ret;
	}

	public SitePage find(String name) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<SitePage> cq = cb.createQuery(SitePage.class);
		Root<SitePage> root = cq.from(SitePage.class);
		cq.where(cb.equal(root.get(SitePage_.event), eventbean.getCurrentEvent()),
				cb.equal(root.get(SitePage_.name), name)
				);
		return super.getSingleNullableResult(getEm().createQuery(cq));
	}
}