/*
 * 
 */
package com.fp.bpmlib.task;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jbpm.services.task.impl.model.UserImpl;
import org.kie.api.task.model.Group;
import org.kie.api.task.model.OrganizationalEntity;
import org.kie.api.task.model.User;
import org.kie.internal.task.api.UserInfo;

import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.PersistenceManager;
import com.fp.persistence.commondb.data.SessionData;
import com.fp.persistence.commondb.data.ThreadFacade;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pbpm.gene.TbpmGroupsUsers;
import com.fp.persistence.pcustomer.gene.TcustPersonAddress;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.gene.TcustPersonDetailKey;
import com.fp.persistence.pgeneral.safe.TsafeUserDetail;

// TODO: Auto-generated Javadoc
/**
 * Class TaskUser encargada de.
 * 
 * @author gfiallos
 */
public class TaskUser implements UserInfo {

    /**
     * Entrega el nombre de un uaurio.
     * 
     * @param entity OBjeto que contiene datos de un usuario asociado a la ejecucion de una actividad.
     * @return String
     */
    @Override
    public String getDisplayName(OrganizationalEntity entity) {
        try {
            this.prepareSession();
            TsafeUserDetail tsud = TsafeUserDetail.find(PersistenceHelper.getEntityManager(), entity.getId());
            TcustPersonDetailKey key = new TcustPersonDetailKey(tsud.getPk().getPersoncode(), FormatDates.getDefaultExpiryTimestamp());
            TcustPersonDetail pd = TcustPersonDetail.find(PersistenceHelper.getEntityManager(), key);
            return pd.getName();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            PersistenceHelper.closeSession();
        }

    }

    /**
     * Prepare session.
     */
    private void prepareSession() {
        SessionData s = new SessionData();
        s.setCompany(1);
        ThreadFacade.setSaveRequest(s);
        PersistenceManager persistenceManager = PersistenceManager.getInstanceTest();
        PersistenceHelper.setEntityManager(persistenceManager.createEntityManagerLocal());
    }

    /**
     * Obtiene el valor de email for entity.
     * 
     * @param entity the entity
     * @return Valor de email for entity
     */
    @Override
    public String getEmailForEntity(OrganizationalEntity entity) {
        try {
            this.prepareSession();
            TsafeUserDetail tsud = TsafeUserDetail.find(PersistenceHelper.getEntityManager(), entity.getId());
            TcustPersonDetailKey key = new TcustPersonDetailKey(tsud.getPk().getPersoncode(), FormatDates.getDefaultExpiryTimestamp());
            TcustPersonDetail pd = TcustPersonDetail.find(PersistenceHelper.getEntityManager(), key);

            return pd.getLanguagecode();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            PersistenceHelper.closeSession();
        }
    }

    /**
     * Entrega el lenguaje preferido de un usuario.
     * 
     * @param entity OBjeto que contiene datos de un usuario asociado a la ejecucion de una actividad.
     * @return String
     */
    @Override
    public String getLanguageForEntity(OrganizationalEntity entity) {

        try {
            this.prepareSession();
            TsafeUserDetail tsud = TsafeUserDetail.find(PersistenceHelper.getEntityManager(), entity.getId());
            return TcustPersonAddress.getEmail(PersistenceHelper.getEntityManager(), tsud.getPk().getPersoncode());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            PersistenceHelper.closeSession();
        }
    }

    /**
     * Obtiene el valor de members for group.
     * 
     * @param group the group
     * @return Valor de members for group
     */
    @Override
    public Iterator<OrganizationalEntity> getMembersForGroup(Group group) {
        List<OrganizationalEntity> l = new ArrayList<OrganizationalEntity>();
        try {
            this.prepareSession();
            List<TbpmGroupsUsers> ldata = TbpmGroupsUsers.find(PersistenceHelper.getEntityManager(), group.getId());
            for (TbpmGroupsUsers obj : ldata) {
                User user = new UserImpl(obj.getPk().getUsercode());
                l.add(user);
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            PersistenceHelper.closeSession();
        }
        return l.iterator();
    }

    /**
     * Checks for email.
     * 
     * @param group the group
     * @return true, si es satisfactorio
     */
    @Override
    public boolean hasEmail(Group group) {
        return false;
    }

}
