package com.acme.model.domain.ejf.filter;


import com.acme.model.domain.ejf.entity.Log;
import com.acme.model.domain.ejf.vo.LogFilterTemplate;
import com.acme.structure.BaseFilter;

import javax.persistence.EntityManager;
import javax.persistence.Query;


public class LogFilter extends BaseFilter
{
    public LogFilter(Object object)
    {
        super(object);
    }

    @Override
    public Query createQuery(EntityManager manager)
    {
        Log log = (Log)object;
        StringBuilder queryString = new StringBuilder("\nFROM Log log WHERE 1=1\n");

        if (!isNullOrEmpty(log.getEntityClass()))
            queryString.append("AND log.entityClass=:entityClass \n");
        if (log.getEntityId() != 0)
            queryString.append("AND log.entityId=:entityId \n");
        if (!isNullOrEmpty(log.getUserName()))
            queryString.append("AND log.userName=:userName \n");
        if (!isNull(log.getProcess()) && log.getProcess() != '\0')
            queryString.append("AND log.process=:process \n");

        Query query = manager.createQuery(queryString.toString());
        if (!isNullOrEmpty(log.getEntityClass()))
            query.setParameter("entityClass", log.getEntityClass());
        if (log.getEntityId() != 0)
            query.setParameter("entityId", log.getEntityId());
        if (!isNullOrEmpty(log.getUserName()))
            query.setParameter("userName", log.getUserName());
        if (!isNull(log.getProcess()) && log.getProcess() != '\0')
            query.setParameter("process", log.getProcess());

        return query;
    }

    public Query findByfilterByTemplate(EntityManager manager)
    {
        LogFilterTemplate filterTemplate = (LogFilterTemplate)object;
        StringBuilder queryString = new StringBuilder("\nFROM Log log WHERE 1=1\n");

        if (!isNull(filterTemplate.getFromDate()))
            queryString.append("AND log.dateOfProcess>=:fromDate \n");
        if (!isNull(filterTemplate.getToDate()))
            queryString.append("AND log.dateOfProcess<:toDate \n");

        if (!isNullOrEmpty(filterTemplate.getLog().getEntityClass()))
            queryString.append("AND log.entityClass=:entityClass \n");
        if (filterTemplate.getLog().getEntityId() != 0)
            queryString.append("AND log.entityId=:entityId \n");
        if (!isNullOrEmpty(filterTemplate.getLog().getUserName()))
            queryString.append("AND log.userName=:userName \n");
        if (!isNull(filterTemplate.getLog().getProcess()) && filterTemplate.getLog().getProcess() != '\0')
            queryString.append("AND log.process=:process \n");
        if (!isNullOrEmpty(filterTemplate.getLog().getEntityValue()))
            queryString.append("AND log.entityValue=:entityValue \n");

        Query query = manager.createQuery(queryString.toString());

        if (!isNull(filterTemplate.getFromDate()))
            query.setParameter("fromDate", filterTemplate.getFromDate());
        if (!isNull(filterTemplate.getToDate()))
            query.setParameter("toDate", filterTemplate.getToDate());

        if (!isNullOrEmpty(filterTemplate.getLog().getEntityClass()))
            query.setParameter("entityClass", filterTemplate.getLog().getEntityClass());
        if (filterTemplate.getLog().getEntityId() != 0)
            query.setParameter("entityId", filterTemplate.getLog().getEntityId());
        if (!isNullOrEmpty(filterTemplate.getLog().getUserName()))
            query.setParameter("userName", filterTemplate.getLog().getUserName());
        if (!isNull(filterTemplate.getLog().getProcess()) && filterTemplate.getLog().getProcess() != '\0')
            query.setParameter("process", Character.toUpperCase(filterTemplate.getLog().getProcess()));
        if (!isNullOrEmpty(filterTemplate.getLog().getEntityValue()))
            query.setParameter("entityValue", filterTemplate.getLog().getEntityValue());

        return query;
    }


}
