<commit_before>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2012 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.service.common;

import java.util.Date;
import java.util.List;

import org.geomajas.global.ExceptionCode;
import org.geomajas.plugin.deskmanager.client.gwt.geodesk.GeodeskLayout;
import org.geomajas.plugin.deskmanager.domain.Geodesk;
import org.geomajas.plugin.deskmanager.domain.security.Territory;
import org.geomajas.plugin.deskmanager.domain.security.dto.Role;
import org.geomajas.plugin.deskmanager.security.DeskmanagerSecurityContext;
import org.geomajas.security.GeomajasSecurityException;
import org.geomajas.security.SecurityContext;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */
@Repository
@Transactional(rollbackFor = { Exception.class })
public class GeodeskServiceImpl implements GeodeskService {

	@SuppressWarnings("unused")
	private final Logger log = LoggerFactory.getLogger(GeodeskServiceImpl.class);

	@Autowired
	private SessionFactory factory;

	@Autowired
	private SecurityContext securityContext;

	public Geodesk getGeodeskById(String uuid) throws GeomajasSecurityException {
		Geodesk l = (Geodesk) factory.getCurrentSession().get(Geodesk.class, uuid);
		if (l != null) {
			if (((DeskmanagerSecurityContext) securityContext).readAllowed(l)) {
				return l;
			} else {
				throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Read Geodesk",
						securityContext.getUserName());
			}
		} else {
			return null;
		}
	}

	public boolean loketExists(String publicId) {
		Query q = factory.getCurrentSession().createQuery(
				"select id from Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false"
		);
		q.setParameter("id", publicId);
		q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

		Object result = q.uniqueResult();
		return (result != null);
	}

	public boolean geodeskIdExists(String publicId) {
		Query q = factory.getCurrentSession().createQuery("select id from Geodesk l WHERE l.geodeskId = :id");
		q.setParameter("id", publicId);
		q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
		Object result = q.uniqueResult();
		return (result != null);
	}

	/**
	 * Will throw a securityException if no accessrights, or not active.
	 */
	public Geodesk getGeodeskByPublicId(String id) throws GeomajasSecurityException {
		if (loketExists(id)) {
			if (((DeskmanagerSecurityContext) securityContext).isGeodeskUseAllowed(id)) {
				Query q = factory.getCurrentSession().createQuery(
						"FROM Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false AND " 
						+ "l.active = true AND l.blueprint.geodesksActive = true");
				q.setParameter("id", id);
				q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
				Geodesk l = (Geodesk) q.uniqueResult();
				if (l == null) {
					throw new GeomajasSecurityException(GeodeskLayout.EXCEPTIONCODE_LOKETINACTIVE);
				}
				return l;
			} else {
				//FIXME: i18n
				throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Geodesk openen",
						securityContext.getUserName());
			}
		} else {
			throw new GeomajasSecurityException(ExceptionCode.APPLICATION_NOT_FOUND, id);
		}
	}

	public Geodesk getGeodeskByPublicIdInternal(String id) {
		if (loketExists(id)) {
			Query q = factory.getCurrentSession().createQuery(
					"FROM Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false AND " + "l.active = true AND "
							+ "l.blueprint.geodesksActive = true");
			q.setParameter("id", id);
			q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
			return (Geodesk) q.uniqueResult();
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Geodesk> getLoketten() throws GeomajasSecurityException {
		Criteria crit = factory.getCurrentSession().createCriteria(Geodesk.class);
		crit.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
		crit.add(Restrictions.eq("deleted", false));
		Criterion filter = ((DeskmanagerSecurityContext) securityContext).getFilterLoketten();
		if (filter != null) {
			crit.add(filter);
		}

		return crit.list();
	}

	public void deleteLoket(Geodesk l) throws GeomajasSecurityException {
		if (((DeskmanagerSecurityContext) securityContext).deleteAllowed(l)) {
			l.setDeleted(true);
			factory.getCurrentSession().saveOrUpdate(l);
		} else {
			throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Verwijderen Geodesk",
					securityContext.getUserName());
		}
	}

	public void saveOrUpdateGeodesk(Geodesk l) throws GeomajasSecurityException {
		if (((DeskmanagerSecurityContext) securityContext).saveAllowed(l)) {
			Date date = new Date();
			String fullName = ((DeskmanagerSecurityContext) securityContext).getFullName();
			if (l.getCreationBy() == null) {
				l.setCreationBy(fullName);
			}
			if (l.getCreationDate() == null) {
				l.setCreationDate(date);
			}
			l.setLastEditDate(date);
			l.setLastEditBy(fullName);

			factory.getCurrentSession().saveOrUpdate(l);<commit_msg>GDM-11 Don't archive, but delete a geodesk from the database.<commit_after>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2012 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.plugin.deskmanager.service.common;

import java.util.Date;
import java.util.List;

import org.geomajas.global.ExceptionCode;
import org.geomajas.plugin.deskmanager.client.gwt.geodesk.GeodeskLayout;
import org.geomajas.plugin.deskmanager.domain.Geodesk;
import org.geomajas.plugin.deskmanager.domain.security.Territory;
import org.geomajas.plugin.deskmanager.domain.security.dto.Role;
import org.geomajas.plugin.deskmanager.security.DeskmanagerSecurityContext;
import org.geomajas.security.GeomajasSecurityException;
import org.geomajas.security.SecurityContext;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */
@Repository
@Transactional(rollbackFor = { Exception.class })
public class GeodeskServiceImpl implements GeodeskService {

	@SuppressWarnings("unused")
	private final Logger log = LoggerFactory.getLogger(GeodeskServiceImpl.class);

	@Autowired
	private SessionFactory factory;

	@Autowired
	private SecurityContext securityContext;

	public Geodesk getGeodeskById(String uuid) throws GeomajasSecurityException {
		Geodesk l = (Geodesk) factory.getCurrentSession().get(Geodesk.class, uuid);
		if (l != null) {
			if (((DeskmanagerSecurityContext) securityContext).readAllowed(l)) {
				return l;
			} else {
				throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Read Geodesk",
						securityContext.getUserName());
			}
		} else {
			return null;
		}
	}

	public boolean loketExists(String publicId) {
		Query q = factory.getCurrentSession().createQuery(
				"select id from Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false"
		);
		q.setParameter("id", publicId);
		q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

		Object result = q.uniqueResult();
		return (result != null);
	}

	public boolean geodeskIdExists(String publicId) {
		Query q = factory.getCurrentSession().createQuery("select id from Geodesk l WHERE l.geodeskId = :id");
		q.setParameter("id", publicId);
		q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
		Object result = q.uniqueResult();
		return (result != null);
	}

	/**
	 * Will throw a securityException if no accessrights, or not active.
	 */
	public Geodesk getGeodeskByPublicId(String id) throws GeomajasSecurityException {
		if (loketExists(id)) {
			if (((DeskmanagerSecurityContext) securityContext).isGeodeskUseAllowed(id)) {
				Query q = factory.getCurrentSession().createQuery(
						"FROM Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false AND " 
						+ "l.active = true AND l.blueprint.geodesksActive = true");
				q.setParameter("id", id);
				q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
				Geodesk l = (Geodesk) q.uniqueResult();
				if (l == null) {
					throw new GeomajasSecurityException(GeodeskLayout.EXCEPTIONCODE_LOKETINACTIVE);
				}
				return l;
			} else {
				//FIXME: i18n
				throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Geodesk openen",
						securityContext.getUserName());
			}
		} else {
			throw new GeomajasSecurityException(ExceptionCode.APPLICATION_NOT_FOUND, id);
		}
	}

	public Geodesk getGeodeskByPublicIdInternal(String id) {
		if (loketExists(id)) {
			Query q = factory.getCurrentSession().createQuery(
					"FROM Geodesk l WHERE l.geodeskId = :id AND " + "l.deleted = false AND " + "l.active = true AND "
							+ "l.blueprint.geodesksActive = true");
			q.setParameter("id", id);
			q.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
			return (Geodesk) q.uniqueResult();
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Geodesk> getLoketten() throws GeomajasSecurityException {
		Criteria crit = factory.getCurrentSession().createCriteria(Geodesk.class);
		crit.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);
		crit.add(Restrictions.eq("deleted", false));
		Criterion filter = ((DeskmanagerSecurityContext) securityContext).getFilterLoketten();
		if (filter != null) {
			crit.add(filter);
		}

		return crit.list();
	}

	public void deleteGeodesk(Geodesk l) throws GeomajasSecurityException {
		if (((DeskmanagerSecurityContext) securityContext).deleteAllowed(l)) {
			factory.getCurrentSession().delete(l);
		} else {
			throw new GeomajasSecurityException(ExceptionCode.COMMAND_ACCESS_DENIED, "Verwijderen Geodesk",
					securityContext.getUserName());
		}
	}

	public void saveOrUpdateGeodesk(Geodesk l) throws GeomajasSecurityException {
		if (((DeskmanagerSecurityContext) securityContext).saveAllowed(l)) {
			Date date = new Date();
			String fullName = ((DeskmanagerSecurityContext) securityContext).getFullName();
			if (l.getCreationBy() == null) {
				l.setCreationBy(fullName);
			}
			if (l.getCreationDate() == null) {
				l.setCreationDate(date);
			}
			l.setLastEditDate(date);
			l.setLastEditBy(fullName);

			factory.getCurrentSession().saveOrUpdate(l);
{'fraction_non_alphanumeric': 0.10921852263973295, 'fraction_numerical': 0.0022254557904647777, 'mean_word_length': 5.050750906266184, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989665', 'n_tokens_mistral': 4020, 'n_tokens_neox': 3631, 'n_words': 747}