com.apstamp.liferay.service.persistence.TemplatePersistenceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.apstamp.liferay.service.persistence.TemplatePersistenceImpl.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.apstamp.liferay.service.persistence;

import com.apstamp.liferay.NoSuchTemplateException;
import com.apstamp.liferay.model.Template;
import com.apstamp.liferay.model.impl.TemplateImpl;
import com.apstamp.liferay.model.impl.TemplateModelImpl;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the template service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Rucheco
 * @see TemplatePersistence
 * @see TemplateUtil
 * @generated
 */
public class TemplatePersistenceImpl extends BasePersistenceImpl<Template> implements TemplatePersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link TemplateUtil} to access the template persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = TemplateImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(
            TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateModelImpl.FINDER_CACHE_ENABLED, TemplateImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(
            TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateModelImpl.FINDER_CACHE_ENABLED, TemplateImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TemplateModelImpl.ENTITY_CACHE_ENABLED,
            TemplateModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_BYID = new FinderPath(
            TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateModelImpl.FINDER_CACHE_ENABLED, TemplateImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findBybyId", new String[] { Long.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BYID = new FinderPath(
            TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateModelImpl.FINDER_CACHE_ENABLED, TemplateImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findBybyId", new String[] { Long.class.getName() },
            TemplateModelImpl.TEMPLATEID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_BYID = new FinderPath(
            TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countBybyId", new String[] { Long.class.getName() });

    /**
     * Returns all the templates where templateId = &#63;.
     *
     * @param templateId the template ID
     * @return the matching templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findBybyId(long templateId) throws SystemException {
        return findBybyId(templateId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the templates where templateId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.apstamp.liferay.model.impl.TemplateModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param templateId the template ID
     * @param start the lower bound of the range of templates
     * @param end the upper bound of the range of templates (not inclusive)
     * @return the range of matching templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findBybyId(long templateId, int start, int end) throws SystemException {
        return findBybyId(templateId, start, end, null);
    }

    /**
     * Returns an ordered range of all the templates where templateId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.apstamp.liferay.model.impl.TemplateModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param templateId the template ID
     * @param start the lower bound of the range of templates
     * @param end the upper bound of the range of templates (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findBybyId(long templateId, int start, int end, OrderByComparator orderByComparator)
            throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BYID;
            finderArgs = new Object[] { templateId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_BYID;
            finderArgs = new Object[] { templateId, start, end, orderByComparator };
        }

        List<Template> list = (List<Template>) FinderCacheUtil.getResult(finderPath, finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (Template template : list) {
                if ((templateId != template.getTemplateId())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_TEMPLATE_WHERE);

            query.append(_FINDER_COLUMN_BYID_TEMPLATEID_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(TemplateModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(templateId);

                if (!pagination) {
                    list = (List<Template>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<Template>(list);
                } else {
                    list = (List<Template>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first template in the ordered set where templateId = &#63;.
     *
     * @param templateId the template ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching template
     * @throws com.apstamp.liferay.NoSuchTemplateException if a matching template could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template findBybyId_First(long templateId, OrderByComparator orderByComparator)
            throws NoSuchTemplateException, SystemException {
        Template template = fetchBybyId_First(templateId, orderByComparator);

        if (template != null) {
            return template;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("templateId=");
        msg.append(templateId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTemplateException(msg.toString());
    }

    /**
     * Returns the first template in the ordered set where templateId = &#63;.
     *
     * @param templateId the template ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching template, or <code>null</code> if a matching template could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template fetchBybyId_First(long templateId, OrderByComparator orderByComparator) throws SystemException {
        List<Template> list = findBybyId(templateId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last template in the ordered set where templateId = &#63;.
     *
     * @param templateId the template ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching template
     * @throws com.apstamp.liferay.NoSuchTemplateException if a matching template could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template findBybyId_Last(long templateId, OrderByComparator orderByComparator)
            throws NoSuchTemplateException, SystemException {
        Template template = fetchBybyId_Last(templateId, orderByComparator);

        if (template != null) {
            return template;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("templateId=");
        msg.append(templateId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchTemplateException(msg.toString());
    }

    /**
     * Returns the last template in the ordered set where templateId = &#63;.
     *
     * @param templateId the template ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching template, or <code>null</code> if a matching template could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template fetchBybyId_Last(long templateId, OrderByComparator orderByComparator) throws SystemException {
        int count = countBybyId(templateId);

        if (count == 0) {
            return null;
        }

        List<Template> list = findBybyId(templateId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Removes all the templates where templateId = &#63; from the database.
     *
     * @param templateId the template ID
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeBybyId(long templateId) throws SystemException {
        for (Template template : findBybyId(templateId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(template);
        }
    }

    /**
     * Returns the number of templates where templateId = &#63;.
     *
     * @param templateId the template ID
     * @return the number of matching templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countBybyId(long templateId) throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_BYID;

        Object[] finderArgs = new Object[] { templateId };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_TEMPLATE_WHERE);

            query.append(_FINDER_COLUMN_BYID_TEMPLATEID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(templateId);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_BYID_TEMPLATEID_2 = "template.templateId = ?";

    public TemplatePersistenceImpl() {
        setModelClass(Template.class);
    }

    /**
     * Caches the template in the entity cache if it is enabled.
     *
     * @param template the template
     */
    @Override
    public void cacheResult(Template template) {
        EntityCacheUtil.putResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                template.getPrimaryKey(), template);

        template.resetOriginalValues();
    }

    /**
     * Caches the templates in the entity cache if it is enabled.
     *
     * @param templates the templates
     */
    @Override
    public void cacheResult(List<Template> templates) {
        for (Template template : templates) {
            if (EntityCacheUtil.getResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                    template.getPrimaryKey()) == null) {
                cacheResult(template);
            } else {
                template.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all templates.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(TemplateImpl.class.getName());
        }

        EntityCacheUtil.clearCache(TemplateImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the template.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(Template template) {
        EntityCacheUtil.removeResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                template.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<Template> templates) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (Template template : templates) {
            EntityCacheUtil.removeResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                    template.getPrimaryKey());
        }
    }

    /**
     * Creates a new template with the primary key. Does not add the template to the database.
     *
     * @param templateId the primary key for the new template
     * @return the new template
     */
    @Override
    public Template create(long templateId) {
        Template template = new TemplateImpl();

        template.setNew(true);
        template.setPrimaryKey(templateId);

        return template;
    }

    /**
     * Removes the template with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param templateId the primary key of the template
     * @return the template that was removed
     * @throws com.apstamp.liferay.NoSuchTemplateException if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template remove(long templateId) throws NoSuchTemplateException, SystemException {
        return remove((Serializable) templateId);
    }

    /**
     * Removes the template with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the template
     * @return the template that was removed
     * @throws com.apstamp.liferay.NoSuchTemplateException if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template remove(Serializable primaryKey) throws NoSuchTemplateException, SystemException {
        Session session = null;

        try {
            session = openSession();

            Template template = (Template) session.get(TemplateImpl.class, primaryKey);

            if (template == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchTemplateException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            return remove(template);
        } catch (NoSuchTemplateException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected Template removeImpl(Template template) throws SystemException {
        template = toUnwrappedModel(template);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(template)) {
                template = (Template) session.get(TemplateImpl.class, template.getPrimaryKeyObj());
            }

            if (template != null) {
                session.delete(template);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (template != null) {
            clearCache(template);
        }

        return template;
    }

    @Override
    public Template updateImpl(com.apstamp.liferay.model.Template template) throws SystemException {
        template = toUnwrappedModel(template);

        boolean isNew = template.isNew();

        TemplateModelImpl templateModelImpl = (TemplateModelImpl) template;

        Session session = null;

        try {
            session = openSession();

            if (template.isNew()) {
                session.save(template);

                template.setNew(false);
            } else {
                session.merge(template);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !TemplateModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        else {
            if ((templateModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BYID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { templateModelImpl.getOriginalTemplateId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_BYID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BYID, args);

                args = new Object[] { templateModelImpl.getTemplateId() };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_BYID, args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BYID, args);
            }
        }

        EntityCacheUtil.putResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                template.getPrimaryKey(), template);

        return template;
    }

    protected Template toUnwrappedModel(Template template) {
        if (template instanceof TemplateImpl) {
            return template;
        }

        TemplateImpl templateImpl = new TemplateImpl();

        templateImpl.setNew(template.isNew());
        templateImpl.setPrimaryKey(template.getPrimaryKey());

        templateImpl.setTemplateId(template.getTemplateId());
        templateImpl.setClienteNome(template.getClienteNome());
        templateImpl.setClienteNomeValido(template.getClienteNomeValido());
        templateImpl.setClienteMorada(template.getClienteMorada());
        templateImpl.setClienteMoradaValido(template.getClienteMoradaValido());
        templateImpl.setClienteNif(template.getClienteNif());
        templateImpl.setClienteNifValido(template.getClienteNifValido());
        templateImpl.setClienteCC(template.getClienteCC());
        templateImpl.setClienteCCValido(template.getClienteCCValido());
        templateImpl.setClienteLocalidade(template.getClienteLocalidade());
        templateImpl.setClienteLocalidadeValido(template.getClienteLocalidadeValido());
        templateImpl.setClienteEmail(template.getClienteEmail());
        templateImpl.setClienteEmpresa(template.getClienteEmpresa());
        templateImpl.setClienteEmpresaValido(template.getClienteEmpresaValido());
        templateImpl.setEmpresaNome(template.getEmpresaNome());
        templateImpl.setEmpresaNomeValido(template.getEmpresaNomeValido());
        templateImpl.setEmpresaRepresentante(template.getEmpresaRepresentante());
        templateImpl.setEmpresaRepresentanteValido(template.getEmpresaRepresentanteValido());
        templateImpl.setEmpresaMorada(template.getEmpresaMorada());
        templateImpl.setEmpresaMoradaValido(template.getEmpresaMoradaValido());
        templateImpl.setEmpresaNif(template.getEmpresaNif());
        templateImpl.setEmpresaNifValido(template.getEmpresaNifValido());
        templateImpl.setEmpresaCC(template.getEmpresaCC());
        templateImpl.setEmpresaCCValido(template.getEmpresaCCValido());
        templateImpl.setEmpresaLocalidade(template.getEmpresaLocalidade());
        templateImpl.setEmpresaLocalidadeValido(template.getEmpresaLocalidadeValido());
        templateImpl.setEmpresaEmail(template.getEmpresaEmail());
        templateImpl.setProjetoNome(template.getProjetoNome());
        templateImpl.setProjetoNomeValido(template.getProjetoNomeValido());
        templateImpl.setProjetoTarefa(template.getProjetoTarefa());
        templateImpl.setProjetoTarefaValido(template.getProjetoTarefaValido());
        templateImpl.setProjetoTecnologia(template.getProjetoTecnologia());
        templateImpl.setProjetoTecnologiaValido(template.getProjetoTecnologiaValido());
        templateImpl.setProjetoEquipa(template.getProjetoEquipa());
        templateImpl.setProjetoEquipaValido(template.getProjetoEquipaValido());
        templateImpl.setProjetoDuracao(template.getProjetoDuracao());
        templateImpl.setProjetoDuracaoValido(template.getProjetoDuracaoValido());
        templateImpl.setProjetoValor(template.getProjetoValor());
        templateImpl.setProjetoValorValido(template.getProjetoValorValido());
        templateImpl.setProjetoTag(template.getProjetoTag());
        templateImpl.setEtiquetaId(template.getEtiquetaId());

        return templateImpl;
    }

    /**
     * Returns the template with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the template
     * @return the template
     * @throws com.apstamp.liferay.NoSuchTemplateException if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template findByPrimaryKey(Serializable primaryKey) throws NoSuchTemplateException, SystemException {
        Template template = fetchByPrimaryKey(primaryKey);

        if (template == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchTemplateException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }

        return template;
    }

    /**
     * Returns the template with the primary key or throws a {@link com.apstamp.liferay.NoSuchTemplateException} if it could not be found.
     *
     * @param templateId the primary key of the template
     * @return the template
     * @throws com.apstamp.liferay.NoSuchTemplateException if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template findByPrimaryKey(long templateId) throws NoSuchTemplateException, SystemException {
        return findByPrimaryKey((Serializable) templateId);
    }

    /**
     * Returns the template with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the template
     * @return the template, or <code>null</code> if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template fetchByPrimaryKey(Serializable primaryKey) throws SystemException {
        Template template = (Template) EntityCacheUtil.getResult(TemplateModelImpl.ENTITY_CACHE_ENABLED,
                TemplateImpl.class, primaryKey);

        if (template == _nullTemplate) {
            return null;
        }

        if (template == null) {
            Session session = null;

            try {
                session = openSession();

                template = (Template) session.get(TemplateImpl.class, primaryKey);

                if (template != null) {
                    cacheResult(template);
                } else {
                    EntityCacheUtil.putResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                            primaryKey, _nullTemplate);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(TemplateModelImpl.ENTITY_CACHE_ENABLED, TemplateImpl.class,
                        primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return template;
    }

    /**
     * Returns the template with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param templateId the primary key of the template
     * @return the template, or <code>null</code> if a template with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public Template fetchByPrimaryKey(long templateId) throws SystemException {
        return fetchByPrimaryKey((Serializable) templateId);
    }

    /**
     * Returns all the templates.
     *
     * @return the templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the templates.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.apstamp.liferay.model.impl.TemplateModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of templates
     * @param end the upper bound of the range of templates (not inclusive)
     * @return the range of templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findAll(int start, int end) throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the templates.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.apstamp.liferay.model.impl.TemplateModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of templates
     * @param end the upper bound of the range of templates (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<Template> findAll(int start, int end, OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<Template> list = (List<Template>) FinderCacheUtil.getResult(finderPath, finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_TEMPLATE);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_TEMPLATE;

                if (pagination) {
                    sql = sql.concat(TemplateModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<Template>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<Template>(list);
                } else {
                    list = (List<Template>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the templates from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (Template template : findAll()) {
            remove(template);
        }
    }

    /**
     * Returns the number of templates.
     *
     * @return the number of templates
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_TEMPLATE);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the template persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(com.liferay.util.service.ServiceProps
                .get("value.object.listener.com.apstamp.liferay.model.Template")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<Template>> listenersList = new ArrayList<ModelListener<Template>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<Template>) InstanceFactory.newInstance(getClassLoader(),
                            listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(TemplateImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    private static final String _SQL_SELECT_TEMPLATE = "SELECT template FROM Template template";
    private static final String _SQL_SELECT_TEMPLATE_WHERE = "SELECT template FROM Template template WHERE ";
    private static final String _SQL_COUNT_TEMPLATE = "SELECT COUNT(template) FROM Template template";
    private static final String _SQL_COUNT_TEMPLATE_WHERE = "SELECT COUNT(template) FROM Template template WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "template.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Template exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Template exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil
            .getBoolean(PropsUtil.get(PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(TemplatePersistenceImpl.class);
    private static Template _nullTemplate = new TemplateImpl() {
        @Override
        public Object clone() {
            return this;
        }

        @Override
        public CacheModel<Template> toCacheModel() {
            return _nullTemplateCacheModel;
        }
    };

    private static CacheModel<Template> _nullTemplateCacheModel = new CacheModel<Template>() {
        @Override
        public Template toEntityModel() {
            return _nullTemplate;
        }
    };
}