/*
 * 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.Transfer;
import com.arrebol.apc.model.admin.constance.TransferCfg;
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.Calendar;
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 TransferController implements Serializable{
    
    /**
     *
     * Searching all transfer by office.
     *
     * @param officeId
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Transfer> fillTransferDatatable(String officeId, Date startDate, Date endDate) {
        logger.debug("fillTransferDatatable");
        List<ModelParameter> parameters = new ArrayList<>();
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        startDate = calendar.getTime();

        Calendar calendarF = Calendar.getInstance();
        calendarF.setTime(endDate);
        calendarF.add(Calendar.DAY_OF_YEAR, 1);
        endDate = calendarF.getTime();
        
        parameters.add(new ModelParameter(TransferCfg.FIELD_OFFICE, new Office(officeId)));
        parameters.add(new ModelParameter(TransferCfg.PARAM_START_DATE, startDate));
        parameters.add(new ModelParameter(TransferCfg.PARAM_END_DATE, endDate));

        return genericEntityRepository.xmlQueryAPCEntities(Transfer.class, TransferCfg.QUERY_FIND_ALL_TRANFERS_BY_OFFICE_BETWEEN_DATES, parameters);
    }
    
    /**
     *
     * @param transfer
     * @return boolean
     */
    public boolean saveTransfer(Transfer transfer) {
        logger.debug("saveTransfer");
        boolean success = genericEntityRepository.insertAPCEntity(transfer);

        return success;
    }
    
    /**
     *
     * @param transfer
     * @return boolean
     */
    public boolean updateByTransferId(Transfer transfer) {
        logger.debug("updateByTransferId");

        return genericEntityRepository.updateAPCEntity(transfer);
    }
    
    /**
     *
     * @param status
     * @param transferIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateTransferByStatus(ActiveStatus status, String transferIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateTransferByStatus");

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

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

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(TransferCfg.QUERY_UPDATE_TRANSFER_BY_ACTIVE_STATUS, parameters);
    }

    final Logger logger = LogManager.getLogger(TransferController.class);
    private final GenericEntityRepository genericEntityRepository;

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