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


import com.acme.model.domain.ejf.entity.Role;
import com.acme.structure.BaseFilter;

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

public class RoleFilter extends BaseFilter
{

    public RoleFilter(Object object)
    {
        super(object);
    }

    @Override
    public Query createQuery(EntityManager manager)
    {
        Role role = (Role)object;

        StringBuilder queryString = new StringBuilder("\nFROM \n");
        queryString.append(role.getEntityName()).append(" r WHERE 1=1\n");

        if (!isNullOrEmpty(role.getRoleName()))
            queryString.append("AND lower(r.roleName) LIKE :roleName\n");

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

        if (!isNullOrEmpty(role.getRoleName()))
            query.setParameter("roleName", role.getRoleName().toLowerCase());

        return query;
    }
}
