<commit_before>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2013 St. Antoniusziekenhuis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.importers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.daos.IAppPropertiesDAO;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.daos.IOrderSyncInfoDAO;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.OrderSyncInfo;
import org.libreplan.business.resources.daos.IWorkerDAO;
import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.workreports.daos.IWorkReportLineDAO;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.libreplan.importers.tim.DurationDTO;
import org.libreplan.importers.tim.PersonDTO;
import org.libreplan.importers.tim.ProductDTO;
import org.libreplan.importers.tim.RegistrationDateDTO;
import org.libreplan.importers.tim.TimOptions;
import org.libreplan.importers.tim.TimeRegistrationDTO;
import org.libreplan.importers.tim.TimeRegistrationRequestDTO;
import org.libreplan.importers.tim.TimeRegistrationResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of export timesheets to tim
 *
 * @author <NAME> <<EMAIL>>
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ExportTimesheetsToTim implements IExportTimesheetsToTim {

    private static final Log LOG = LogFactory
            .getLog(ExportTimesheetsToTim.class);

    @Autowired
    private IWorkerDAO workerDAO;

    @Autowired
    private IWorkReportLineDAO workReportLineDAO;

    @Autowired
    private IConfigurationDAO configurationDAO;

    @Autowired
    IOrderSyncInfoDAO orderSyncInfoDAO;

    @Autowired
    private IAdHocTransactionService adHocTransactionService;

    private List<Worker> workers;

    @Autowired
    private IAppPropertiesDAO appPropertiesDAO;

    @Autowired
    private IOrderDAO orderDAO;

    @Override
    @Transactional(readOnly = true)
    public void exportTimesheets() {
        Map<String, String> prop = appPropertiesDAO.findByMajorId("Tim");
        List<Order> orders = orderDAO.getOrders();
        for (Order order : orders) {
            OrderSyncInfo orderSyncInfo = orderSyncInfoDAO
                    .findByOrderLastSynchronizedInfo(order);
            if (orderSyncInfo == null) {
                LOG.warn("Order '" + order.getName()
                        + "' is not yet synchronized");
            } else {
                boolean result = exportTimesheets(orderSyncInfo.getCode(),
                        orderSyncInfo.getOrder(), prop);
                LOG.info("Export successful: " + result);
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public boolean exportTimesheets(String productCode, Order order) {
        if (productCode == null || productCode.isEmpty()) {
            throw new RuntimeException("Product code should not be empty");
        }
        if (order == null) {
            throw new RuntimeException("Order should not be empty");
        }

        Map<String, String> prop = appPropertiesDAO.findByMajorId("Tim");
        return exportTimesheets(productCode, order, prop);

    }

    /**
     * exports time sheets to Tim
     *
     * @param productCode
     *            the product code
     * @param order
     *            the order
     * @param appProperties
     *            the app properties
     *
     * @return true if export is succeeded, false otherwise
     */
    private boolean exportTimesheets(String productCode, Order order,
            Map<String, String> appProperties) {

        workers = workerDAO.findAll();
        if (workers == null || workers.isEmpty()) {
            LOG.warn("No workers found!");
            return false;
        }

        String url = appProperties.get("Server");
        String userName = appProperties.get("Username");
        String password = <PASSWORD>("Password");
        int nrDaysTimesheetToTim = Integer.parseInt(appProperties
                .get("NrDaysTimesheetToTim"));

        Calendar dateNrOfDaysBack = Calendar.getInstance();
        dateNrOfDaysBack.add(Calendar.DAY_OF_MONTH, -nrDaysTimesheetToTim);

        List<WorkReportLine> workReportLines = order.getWorkReportLines(
                new Date(dateNrOfDaysBack.getTimeInMillis()), new Date(), true);
        if (workReportLines == null || workReportLines.isEmpty()) {
            LOG.warn("No work reportlines are found");
            return false;
        }

        List<TimeRegistrationDTO> timeRegistrationDTOs = new ArrayList<TimeRegistrationDTO>();

        for (WorkReportLine workReportLine : workReportLines) {
            TimeRegistrationDTO timeRegistrationDTO = createExportTimeRegistration(<commit_msg>tim-connector: Use LocalDate API<commit_after>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2013 <NAME>huis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.libreplan.importers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.LocalDate;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.daos.IAppPropertiesDAO;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.daos.IOrderSyncInfoDAO;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.OrderSyncInfo;
import org.libreplan.business.resources.daos.IWorkerDAO;
import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.workreports.daos.IWorkReportLineDAO;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.libreplan.importers.tim.DurationDTO;
import org.libreplan.importers.tim.PersonDTO;
import org.libreplan.importers.tim.ProductDTO;
import org.libreplan.importers.tim.RegistrationDateDTO;
import org.libreplan.importers.tim.TimOptions;
import org.libreplan.importers.tim.TimeRegistrationDTO;
import org.libreplan.importers.tim.TimeRegistrationRequestDTO;
import org.libreplan.importers.tim.TimeRegistrationResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of export timesheets to tim
 *
 * @author <NAME> <<EMAIL>>
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ExportTimesheetsToTim implements IExportTimesheetsToTim {

    private static final Log LOG = LogFactory
            .getLog(ExportTimesheetsToTim.class);

    @Autowired
    private IWorkerDAO workerDAO;

    @Autowired
    private IWorkReportLineDAO workReportLineDAO;

    @Autowired
    private IConfigurationDAO configurationDAO;

    @Autowired
    IOrderSyncInfoDAO orderSyncInfoDAO;

    @Autowired
    private IAdHocTransactionService adHocTransactionService;

    private List<Worker> workers;

    @Autowired
    private IAppPropertiesDAO appPropertiesDAO;

    @Autowired
    private IOrderDAO orderDAO;

    @Override
    @Transactional(readOnly = true)
    public void exportTimesheets() {
        Map<String, String> prop = appPropertiesDAO.findByMajorId("Tim");
        List<Order> orders = orderDAO.getOrders();
        for (Order order : orders) {
            OrderSyncInfo orderSyncInfo = orderSyncInfoDAO
                    .findByOrderLastSynchronizedInfo(order);
            if (orderSyncInfo == null) {
                LOG.warn("Order '" + order.getName()
                        + "' is not yet synchronized");
            } else {
                boolean result = exportTimesheets(orderSyncInfo.getCode(),
                        orderSyncInfo.getOrder(), prop);
                LOG.info("Export successful: " + result);
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public boolean exportTimesheets(String productCode, Order order) {
        if (productCode == null || productCode.isEmpty()) {
            throw new RuntimeException("Product code should not be empty");
        }
        if (order == null) {
            throw new RuntimeException("Order should not be empty");
        }

        Map<String, String> prop = appPropertiesDAO.findByMajorId("Tim");
        return exportTimesheets(productCode, order, prop);

    }

    /**
     * exports time sheets to Tim
     *
     * @param productCode
     *            the product code
     * @param order
     *            the order
     * @param appProperties
     *            the app properties
     *
     * @return true if export is succeeded, false otherwise
     */
    private boolean exportTimesheets(String productCode, Order order,
            Map<String, String> appProperties) {

        workers = workerDAO.findAll();
        if (workers == null || workers.isEmpty()) {
            LOG.warn("No workers found!");
            return false;
        }

        String url = appProperties.get("Server");
        String userName = appProperties.get("Username");
        String password = appProperties.get("Password");
        int nrDaysTimesheetToTim = Integer.parseInt(appProperties
                .get("NrDaysTimesheetToTim"));

        LocalDate dateNrOfDaysBack = new LocalDate()
                .minusDays(nrDaysTimesheetToTim);

        List<WorkReportLine> workReportLines = order.getWorkReportLines(
                dateNrOfDaysBack.toDateTimeAtStartOfDay().toDate(), new Date(),
                true);
        if (workReportLines == null || workReportLines.isEmpty()) {
            LOG.warn("No work reportlines are found");
            return false;
        }

        List<TimeRegistrationDTO> timeRegistrationDTOs = new ArrayList<TimeRegistrationDTO>();

        for (WorkReportLine workReportLine : workReportLines) {
            TimeRegistrationDTO timeRegistrationDTO = createExportTimeRegistration(
{'fraction_non_alphanumeric': 0.08766606527800558, 'fraction_numerical': 0.0008200754469411186, 'mean_word_length': 3.2849613492621224, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1796690', 'n_tokens_mistral': 3366, 'n_tokens_neox': 3296, 'n_words': 816}