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


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

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

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

    @Override
    public Query createQuery(EntityManager manager)
    {
        User user = (User)object;
        StringBuilder queryString = new StringBuilder("\nFROM \n");
        queryString.append(user.getEntityName()).append(" u WHERE 1=1\n");

        if (!isNullOrEmpty(user.getUserName()))
            queryString.append("AND u.userName=:userName \n");

        Query query = manager.createQuery(queryString.toString());
        if (!isNullOrEmpty(user.getUserName()))
            query.setParameter("userName", user.getUserName());

        return query;
    }
}
