/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller.admin;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.Goal;
import com.arrebol.apc.model.admin.constance.GoalCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class GoalController implements Serializable{
    
       /**
     *
     * Searching all goals by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Goal> fillGoalDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillGoalDatatable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(GoalCfg.FIELD_OFFICE, new Office(officeId)));
        parameters.add(new ModelParameter(GoalCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(GoalCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(GoalCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(Goal.class, GoalCfg.QUERY_FIND_ALL_GOALS_BY_OFFICE_BETWEEN_DATES, parameters);
    }
    
    /**
     *
     * @param goal
     * @return boolean
     */
    public boolean saveGoal(Goal goal) {
        logger.debug("saveGoal");
        boolean success = genericEntityRepository.insertAPCEntity(goal);

        return success;
    
    }    
        /**
     *
     * @param status
     * @param goalIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateGoalByStatus(ActiveStatus status, String goalIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateGoalByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(GoalCfg.FIELD_ACTIVE_STATUS, status));
        parameters.add(new ModelParameter(GoalCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(GoalCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(GoalCfg.FIELD_ID, goalIdToUpdate));

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(GoalCfg.QUERY_UPDATE_GOAL_BY_STATUS, parameters);
    }
    
    /**
     *
     * @param goal
     * @return boolean
     */
    public boolean updateByGoalId(Goal goal) {
        logger.debug("updateByGoalId");

        return genericEntityRepository.updateAPCEntity(goal);
    }
    
    final Logger logger = LogManager.getLogger(GoalController.class);
    private final GenericEntityRepository genericEntityRepository;

    public GoalController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }
    
}
