/*
 * 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.catalog;

import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.catalog.RouteCtlg;
import com.arrebol.apc.model.catalog.constance.RouteCfg;
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 RouteController implements Serializable {

    /**
     *
     * Searching all roles.
     *
     * @param officeId
     * @return
     */
    public List<RouteCtlg> fillRoutesDatatable(String officeId) {
        logger.debug("fillRoutesDatatable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(RouteCfg.FIELD_ACTIVE_STATUS, ActiveStatus.ENEBLED));
        parameters.add(new ModelParameter(RouteCfg.FIELD_OFFICE, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(RouteCtlg.class, RouteCfg.QUERY_FIND_ALL_ROUTES, parameters);
    }

    /**
     *
     * @param route
     * @return boolean
     */
    public boolean saveRoute(RouteCtlg route) {
        logger.debug("saveRouteController");
        boolean success = genericEntityRepository.insertAPCEntity(route);

        return success;
    }

    /**
     *
     * @param route
     * @return boolean
     */
    public boolean updateByRouteId(RouteCtlg route) {
        logger.debug("updateByRouteId");

        return genericEntityRepository.updateAPCEntity(route);
    }

    /**
     *
     * @param status
     * @param routeIdToUpdate
     * @param lastUpdatedBy
     * @return
     */
    public boolean updateRouteByStatus(ActiveStatus status, String routeIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateRouteByStatus");

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

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

        return genericEntityRepository.xmlUpdateOrDeleteAPCEntity(RouteCfg.UPDATE_ROUTE_BY_STATUS, parameters);
    }

    private static final long serialVersionUID = -6277126340752974003L;
    final Logger logger = LogManager.getLogger(RoleController.class);
    private final GenericEntityRepository genericEntityRepository;

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