<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Core.DomainModel;
using Core.DomainServices;

namespace Core.ApplicationServices
{
    public class MoxService : IMoxService
    {
        private readonly IGenericRepository<OrganizationUnit> _orgUnitRepository;
        private readonly IGenericRepository<OrganizationRole> _orgRoleRepository;
        private readonly IGenericRepository<TaskRef> _taskRepository;
        private readonly IGenericRepository<User> _userRepository;
        private readonly IExcelHandler _excelHandler;

        public MoxService(IGenericRepository<OrganizationUnit> orgUnitRepository,
            IGenericRepository<OrganizationRole> orgRoleRepository,
            IGenericRepository<TaskRef> taskRepository,
            IGenericRepository<User> userRepository,
            IExcelHandler excelHandler)
        {
            _orgUnitRepository = orgUnitRepository;
            _orgRoleRepository = orgRoleRepository;
            _taskRepository = taskRepository;
            _userRepository = userRepository;
            _excelHandler = excelHandler;
        }

        public Stream Export(Stream stream, int organizationId, User kitosUser)
        {
            var orgUnits = _orgUnitRepository.Get(x => x.OrganizationId == organizationId).ToList();
            dynamic orgRoles = null;
            dynamic orgTasks = null;
            foreach (var orgUnit in orgUnits)
            {
                var unitName = orgUnit.Name;
                orgRoles = orgUnit.Rights.Select(x => new {OrgUnit = unitName, Role = x.Role.Name, User = x.User.Name});
                orgTasks =
                    orgUnit.TaskUsages.Select(x => new { OrgUnit = unitName, Task = x.TaskRefId, Overview = x.Starred });
            }

            var roles = _orgRoleRepository.Get(x => x.IsActive && !x.IsSuggestion);
            var tasks = _taskRepository.Get(x => x.AccessModifier == AccessModifier.Public);
            var users = _userRepository.Get(x => x.CreatedInId == organizationId);

            var set = new DataSet();
            set.Tables.Add(GetOrganizationTable(orgUnits));
            set.Tables.Add(GetOrgRoleTable(orgRoles));
            set.Tables.Add(GetOrgTaskTable(orgTasks));
            set.Tables.Add(GetRoleTable(roles));
            set.Tables.Add(GetTaskTable(tasks));
            set.Tables.Add(GetUserTable(users));

            return _excelHandler.Export(set, stream);
        }

        private static long? ToNullableLong(string s)
        {
            long i;
            if (long.TryParse(s, out i)) return i;
            return null;
        }
        
        /* imports organization units into an organization */
        public void Import(Stream stream, int organizationId, User kitosUser)
        {
            var set = _excelHandler.Import(stream);
            var orgTable = set.Tables[1];

            // existing orgUnits
            var existingDatarows =
                orgTable.AsEnumerable()
                    .Where(x => !String.IsNullOrEmpty(x.Field<string>(1)));

            var existing = existingDatarows.Select(x => new OrgUnit
            {
                Id = Convert.ToInt32(Convert.ToDouble(x.Field<string>(1))), 
                Name = x.Field<string>(2)
            }).ToList();

            // filter (remove) orgunits without an ID and groupby parent
            var newOrgUnitsGrouped =
                orgTable.AsEnumerable()
                    .Where(x => String.IsNullOrEmpty(x.Field<string>(1)))
                    .Select(x => new OrgUnit { Name = x.Field<string>(2), Parent = x.Field<string>(4), Ean = ToNullableLong(x.Field<string>(3)) })
                    .GroupBy(x => x.Parent).ToList();

            var count = newOrgUnitsGrouped.Count();
            for (var i = 0; i < count; i++)
            {
                var current = newOrgUnitsGrouped[i];
                // if parentless (root) or parent already exists
                var existingParent = existing.SingleOrDefault(x => x.Name == current.Key);
                if (string.IsNullOrEmpty(current.Key) || existingParent != null)
                {
                    var proxyOrgUnits = new List<OrganizationUnit>();<commit_msg>Updated the import org unit functionality to refelct the changes to the excel template. Fixed bug in reading EAN number; Added check for parent org unit<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using Core.DomainModel;
using Core.DomainServices;

namespace Core.ApplicationServices
{
    public class MoxService : IMoxService
    {
        private readonly IGenericRepository<OrganizationUnit> _orgUnitRepository;
        private readonly IGenericRepository<OrganizationRole> _orgRoleRepository;
        private readonly IGenericRepository<TaskRef> _taskRepository;
        private readonly IGenericRepository<User> _userRepository;
        private readonly IExcelHandler _excelHandler;

        public MoxService(IGenericRepository<OrganizationUnit> orgUnitRepository,
            IGenericRepository<OrganizationRole> orgRoleRepository,
            IGenericRepository<TaskRef> taskRepository,
            IGenericRepository<User> userRepository,
            IExcelHandler excelHandler)
        {
            _orgUnitRepository = orgUnitRepository;
            _orgRoleRepository = orgRoleRepository;
            _taskRepository = taskRepository;
            _userRepository = userRepository;
            _excelHandler = excelHandler;
        }

        public Stream Export(Stream stream, int organizationId, User kitosUser)
        {
            var orgUnits = _orgUnitRepository.Get(x => x.OrganizationId == organizationId).ToList();
            dynamic orgRoles = null;
            dynamic orgTasks = null;
            foreach (var orgUnit in orgUnits)
            {
                var unitName = orgUnit.Name;
                orgRoles = orgUnit.Rights.Select(x => new {OrgUnit = unitName, Role = x.Role.Name, User = x.User.Name});
                orgTasks =
                    orgUnit.TaskUsages.Select(x => new { OrgUnit = unitName, Task = x.TaskRefId, Overview = x.Starred });
            }

            var roles = _orgRoleRepository.Get(x => x.IsActive && !x.IsSuggestion);
            var tasks = _taskRepository.Get(x => x.AccessModifier == AccessModifier.Public);
            var users = _userRepository.Get(x => x.CreatedInId == organizationId);

            var set = new DataSet();
            set.Tables.Add(GetOrganizationTable(orgUnits));
            set.Tables.Add(GetOrgRoleTable(orgRoles));
            set.Tables.Add(GetOrgTaskTable(orgTasks));
            set.Tables.Add(GetRoleTable(roles));
            set.Tables.Add(GetTaskTable(tasks));
            set.Tables.Add(GetUserTable(users));

            return _excelHandler.Export(set, stream);
        }

        private static long? StringToEAN(string s)
        {
            if (string.IsNullOrEmpty(s)) return null;

            //if the ean was properly entered, excel will treat is as a Number,
            //which is bascially a string in double format i.e "12345678.0"
            //so try to parse as double first
            double dbl;
            if (!double.TryParse(s, out dbl)) return null;

            //then convert to long
            return Convert.ToInt64(dbl);
        }
        
        /* imports organization units into an organization */
        public void Import(Stream stream, int organizationId, User kitosUser)
        {
            var set = _excelHandler.Import(stream);
            var orgTable = set.Tables[0];

            // existing orgUnits
            var existingDatarows =
                orgTable.AsEnumerable()
                    .Where(x => !String.IsNullOrEmpty(x.Field<string>(0)));

            var existing = existingDatarows.Select(x => new OrgUnit
            {
                Id = Convert.ToInt32(Convert.ToDouble(x.Field<string>(0))), 
                Name = x.Field<string>(1)
            }).ToList();

            // filter (remove) orgunits without an ID and groupby parent
            var newOrgUnitsGrouped =
                orgTable.AsEnumerable()
                    .Where(x => String.IsNullOrEmpty(x.Field<string>(0)))
                    .Where(x => !String.IsNullOrEmpty(x.Field<string>(3)))
                    .Select(x => new OrgUnit
                    {
                        Name = x.Field<string>(1), 
                        Parent = x.Field<string>(3), 
                        Ean = StringToEAN(x.Field<string>(2))
                    })
                    .GroupBy(x => x.Parent).ToList();

            var count = newOrgUnitsGrouped.Count();
            for (var i = 0; i < count; i++)
            {
                var current = newOrgUnitsGrouped[i];
                // if parentless (root) or parent already exists
                var existingParent = existing.SingleOrDefault(x => x.Name == current.Key);
                if (string.IsNullOrEmpty(current.Key) || existingParent != null)
                {
                    var proxyOrgUnits = new List<OrganizationUnit>();
{'fraction_non_alphanumeric': 0.09284079764628964, 'fraction_numerical': 0.0037049144600632016, 'mean_word_length': 2.0340495867768595, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6329425', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2342, 'n_words': 552}