<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.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.NonUniqueResultException;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.entities.JiraConfiguration;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.costcategories.daos.ITypeOfWorkHoursDAO;
import org.libreplan.business.costcategories.entities.TypeOfWorkHours;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.resources.daos.IWorkerDAO;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.workingday.EffortDuration;
import org.libreplan.business.workreports.daos.IWorkReportDAO;
import org.libreplan.business.workreports.daos.IWorkReportLineDAO;
import org.libreplan.business.workreports.daos.IWorkReportTypeDAO;
import org.libreplan.business.workreports.entities.WorkReport;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.libreplan.business.workreports.entities.WorkReportType;
import org.libreplan.business.workreports.valueobjects.DescriptionField;
import org.libreplan.business.workreports.valueobjects.DescriptionValue;
import org.libreplan.importers.jira.Issue;
import org.libreplan.importers.jira.WorkLog;
import org.libreplan.importers.jira.WorkLogItem;
import org.libreplan.web.workreports.IWorkReportModel;
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;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class JiraTimesheetSynchronizer implements IJiraTimesheetSynchronizer {

    private JiraSyncInfo jiraSyncInfo;

    private List<Worker> workers;

    private WorkReportType workReportType;

    private TypeOfWorkHours typeOfWorkHours;

    @Autowired
    private IWorkerDAO workerDAO;

    @Autowired
    private IWorkReportTypeDAO workReportTypeDAO;

    @Autowired
    private IWorkReportDAO workReportDAO;

    @Autowired
    private IWorkReportLineDAO workReportLineDAO;

    @Autowired
    private IWorkReportModel workReportModel;

    @Autowired
    private ITypeOfWorkHoursDAO typeOfWorkHoursDAO;

    @Autowired
    private IConfigurationDAO configurationDAO;

    @Autowired
    private IAdHocTransactionService adHocTransactionService;

    @Override
    @Transactional
    public void syncJiraTimesheetWithJiraIssues(List<Issue> issues, Order order) {
        jiraSyncInfo = new JiraSyncInfo();

        workReportType = findWorkReportType("Jira-connector");
        if (workReportType == null) {
            jiraSyncInfo
                    .addSyncFailedReason("Work repor type \"Jira-connector\" not found");
            return;
        }

        typeOfWorkHours = getTypeOfWorkHours();

        workers = getWorkers();
        if (workers == null && workers.isEmpty()) {
            jiraSyncInfo.addSyncFailedReason("No workers found");
            return;
        }

        String code = order.getCode() + " " + order.getImportedLabel();

        WorkReport workReport = updateOrCreateWorkReport(code);

        for (Issue issue : issues) {
            WorkLog worklog = issue.getFields().getWorklog();
            if (worklog == null) {
                jiraSyncInfo.addSyncFailedReason("No worklogs found for '"
                        + issue.getKey() + "'");
            } else {
                List<WorkLogItem> workLogItems = worklog.getWorklogs();
                if (workLogItems == null || workLogItems.isEmpty()) {
                    jiraSyncInfo
                            .addSyncFailedReason("No worklog items found for '"
                                    + issue.getKey() + "' issue");
                } else {

                    String codeOrderElement = JiraConfiguration.CODE_PREFIX
                            + order.getCode() + "-" + issue.getKey();

                    OrderElement orderElement = order.getOrderElement(codeOrderElement);<commit_msg>jira-integration: Avoid whitespace in code use "-" instead<commit_after>/*
 * This file is part of LibrePlan
 *
 * Copyright (C) 2013 <NAME>
 *
 * 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.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.NonUniqueResultException;
import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.entities.JiraConfiguration;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.costcategories.daos.ITypeOfWorkHoursDAO;
import org.libreplan.business.costcategories.entities.TypeOfWorkHours;
import org.libreplan.business.orders.entities.Order;
import org.libreplan.business.orders.entities.OrderElement;
import org.libreplan.business.resources.daos.IWorkerDAO;
import org.libreplan.business.resources.entities.Resource;
import org.libreplan.business.resources.entities.Worker;
import org.libreplan.business.workingday.EffortDuration;
import org.libreplan.business.workreports.daos.IWorkReportDAO;
import org.libreplan.business.workreports.daos.IWorkReportLineDAO;
import org.libreplan.business.workreports.daos.IWorkReportTypeDAO;
import org.libreplan.business.workreports.entities.WorkReport;
import org.libreplan.business.workreports.entities.WorkReportLine;
import org.libreplan.business.workreports.entities.WorkReportType;
import org.libreplan.business.workreports.valueobjects.DescriptionField;
import org.libreplan.business.workreports.valueobjects.DescriptionValue;
import org.libreplan.importers.jira.Issue;
import org.libreplan.importers.jira.WorkLog;
import org.libreplan.importers.jira.WorkLogItem;
import org.libreplan.web.workreports.IWorkReportModel;
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;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class JiraTimesheetSynchronizer implements IJiraTimesheetSynchronizer {

    private JiraSyncInfo jiraSyncInfo;

    private List<Worker> workers;

    private WorkReportType workReportType;

    private TypeOfWorkHours typeOfWorkHours;

    @Autowired
    private IWorkerDAO workerDAO;

    @Autowired
    private IWorkReportTypeDAO workReportTypeDAO;

    @Autowired
    private IWorkReportDAO workReportDAO;

    @Autowired
    private IWorkReportLineDAO workReportLineDAO;

    @Autowired
    private IWorkReportModel workReportModel;

    @Autowired
    private ITypeOfWorkHoursDAO typeOfWorkHoursDAO;

    @Autowired
    private IConfigurationDAO configurationDAO;

    @Autowired
    private IAdHocTransactionService adHocTransactionService;

    @Override
    @Transactional
    public void syncJiraTimesheetWithJiraIssues(List<Issue> issues, Order order) {
        jiraSyncInfo = new JiraSyncInfo();

        workReportType = findWorkReportType("Jira-connector");
        if (workReportType == null) {
            jiraSyncInfo
                    .addSyncFailedReason("Work repor type \"Jira-connector\" not found");
            return;
        }

        typeOfWorkHours = getTypeOfWorkHours();

        workers = getWorkers();
        if (workers == null && workers.isEmpty()) {
            jiraSyncInfo.addSyncFailedReason("No workers found");
            return;
        }

        String code = order.getCode() + "-" + order.getImportedLabel();

        WorkReport workReport = updateOrCreateWorkReport(code);

        for (Issue issue : issues) {
            WorkLog worklog = issue.getFields().getWorklog();
            if (worklog == null) {
                jiraSyncInfo.addSyncFailedReason("No worklogs found for '"
                        + issue.getKey() + "'");
            } else {
                List<WorkLogItem> workLogItems = worklog.getWorklogs();
                if (workLogItems == null || workLogItems.isEmpty()) {
                    jiraSyncInfo
                            .addSyncFailedReason("No worklog items found for '"
                                    + issue.getKey() + "' issue");
                } else {

                    String codeOrderElement = JiraConfiguration.CODE_PREFIX
                            + order.getCode() + "-" + issue.getKey();

                    OrderElement orderElement = order.getOrderElement(codeOrderElement);
{'fraction_non_alphanumeric': 0.08242342255290366, 'fraction_numerical': 0.0009662769349695622, 'mean_word_length': 3.5675198587819947, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1120339', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2840, 'n_words': 636}