private void ParseFiles(CompareSets set)
        {
            // scan directories
            var files = ListFiles(set.Path);

            // set files
            set.FileList = files;
            totalFiles  += files.Count;

            int wt, cpt;

            ThreadPool.GetMaxThreads(out wt, out cpt);

            // files file comparision
            var tasks = new List <Task>();

            foreach (var file in set.FileList)
            {
                tasks.Add(Task.Run(() =>
                {
                    var data = FileCompareData.FromFile(file, set.Path);
                    lock (set)
                    {
                        set.AllFiles.Add(data);
                        set.Files[data.absolutePath] = data;
                        Filenames.Add(data.name);
                    }

                    ++fileScanIndex;
                }));
            }

            Task.WaitAll(tasks.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// This will load the results from the completed QC analysis and
        /// display on the SeqcosMainWindow.
        /// </summary>
        /// <param name="dir">Directory name of files</param>
        /// <returns>A collection of Image control objects</returns>
        private void LoadRunResults(string dir, Filenames fileList)
        {
            if (!Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException();
            }

            if (fileList == null)
            {
                fileList = new Filenames(dir, Resource.ChartFormat);
            }

            ClearPreviousRun();

            // Update TabControl panels
            UpdateTabControl(dir, fileList.GetSequenceLevelFilenames(), this.sequenceTab);
            UpdateTabControl(dir, fileList.GetQualityLevelFilenames(), this.qualityTab);

            // Issue a warning if the selected directory doesn't contain any relevant files
            if (this.sequenceTab.Children.Count == 0 && this.qualityTab.Children.Count == 0)
            {
                MessageBox.Show(Resource.LoadRunFail, Resource.LoadRunFailCaption, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            else
            {
                // Update input statistics
                UpdateInputStatistics(dir + @"\" + fileList.CSV);

                if (application != null)
                {
                    OpenInExplorer(application.OutputDirectory);
                }
            }
        }
        private void buttonChooseStack_Click(object sender, EventArgs e)
        {
            if (Filenames.Count > 0)
            {
                Filenames.Clear();
            }

            openFileDialogStack.Filter           = _dialogFilterStack;
            openFileDialogStack.InitialDirectory = _initDir;
            openFileDialogStack.FileName         = "";
            DialogResult result = openFileDialogStack.ShowDialog();

            if (result == DialogResult.OK)
            {
                Stackname = openFileDialogStack.FileName;
                if (Stackname != null)
                {
                    textBox1.Text = Stackname;

                    WSIExtraction.Stack stack = ReadWriteUtils.DeserializeObjectFromJSON <WSIExtraction.Stack>(Stackname);
                    foreach (WSIExtraction.Slice slice in stack.Section)
                    {
                        Filenames.Add(Path.Combine(Path.GetDirectoryName(Stackname), slice.Path));
                    }
                }
                textBox1.Text = Stackname;
                EnableButtons();
            }
        }
Exemple #4
0
        /*
         * public void Reload(Stream fontStream, int faceIndex) {
         *  try {
         *      SharpFont.Face currentFace = Face;
         *      Load(fontStream, faceIndex);
         *
         *      if (currentFace != null) {
         *          currentFace.Dispose();
         *      }
         *  } catch(System.Exception e) {
         *      throw e;
         *  }
         * }
         */

        public Font Clone(float?size = null)
        {
            string[] filenames;

            if (Filenames != null)
            {
                filenames = new string[Filenames.Length];
                Filenames.CopyTo(filenames, 0);
            }
            else
            {
                filenames = null;
            }

            RenderMap?.AddReferenceCount();

            Font font = new Font()
            {
                Name             = Name,
                Filenames        = filenames,
                RenderMap        = RenderMap,
                ShaderParameters = ShaderParameters?.Clone()
            };

            if (size.HasValue)
            {
                font.Size = size.Value;
            }
            else
            {
                font.Size = _size;
            }

            return(font);
        }
Exemple #5
0
 public static void Add(string filename)
 {
     if (!String.IsNullOrEmpty(filename))
     {
         Filenames.Add(filename.ToLower());
     }
 }
Exemple #6
0
 private Comment MapToComment(Filenames rec) => new Comment
 {
     Id = rec.CommentId,
     //Author = rec.CommentAuthor,
     ParentCommentId = (rec.ParentCommentId == string.Empty) ? rec.PostId.Split('_')[1] : rec.ParentCommentId,
     CommentText     = rec.CommentText,
     Children        = new List <Comment>()
 };
Exemple #7
0
        internal void ReadItems()
        {
            if (items.Any())
            {
                throw new InvalidOperationException();
            }

            using (var reader = cache.CreateReader(cache.MetadataTranslator))
            {
                reader.Seek(TagClassDataPointer.Address, SeekOrigin.Begin);
                Classes.AddRange(reader.ReadEnumerable <TagClass>(TagClassCount));

                reader.Seek(TagDataPointer.Address, SeekOrigin.Begin);
                for (int i = 0; i < TagCount; i++)
                {
                    //every Reach map has an empty tag
                    var item = reader.ReadObject(new IndexItem(cache, i));
                    if (item.ClassIndex < 0)
                    {
                        continue;
                    }

                    items.Add(i, item);

                    if (item.ClassCode != CacheFactory.ScenarioClass && CacheFactory.SystemClasses.Contains(item.ClassCode))
                    {
                        sysItems.Add(item.ClassCode, item);
                    }
                }

                reader.Seek(cache.Header.FileTableIndexPointer.Address, SeekOrigin.Begin);
                var indices = reader.ReadEnumerable <int>(TagCount).ToArray();

                reader.Seek(cache.Header.FileTablePointer.Address, SeekOrigin.Begin);
                var decrypted = reader.ReadAesBytes(cache.Header.FileTableSize, cache.CacheType == CacheType.HaloReachBeta ? CacheFile.BetaKey : CacheFile.FileNamesKey);
                using (var ms = new MemoryStream(decrypted))
                    using (var tempReader = new EndianReader(ms))
                    {
                        for (int i = 0; i < TagCount; i++)
                        {
                            if (indices[i] == -1)
                            {
                                Filenames.Add(i, null);
                                continue;
                            }

                            tempReader.Seek(indices[i], SeekOrigin.Begin);
                            Filenames.Add(i, tempReader.ReadNullTerminatedString());
                        }
                    }
            }

            try
            {
                sysItems[CacheFactory.ScenarioClass] = items.Values.Single(i => i.ClassCode == CacheFactory.ScenarioClass && i.FullPath == cache.Header.ScenarioName);
            }
            catch { throw Exceptions.AmbiguousScenarioReference(); }
        }
        /// <summary>
        /// Gets the value.
        /// </summary>
        public override object GetValue()
        {
            if (MultiFile)
            {
                return(Filenames.ToArray());
            }

            return(null);
        }
        public async Task <List <DnBOrgsModel> > LoadIfNewerAsync()
        {
            var dnbOrgsPath = Path.Combine(DataPath, Filenames.DnBOrganisations());
            var fileExists  = await FileRepository.GetFileExistsAsync(dnbOrgsPath);

            //Copy the previous years if no current year
            if (!fileExists)
            {
                var dnbOrgsPathPrevious = Path.Combine(DataPath, Filenames.PreviousDnBOrganisations());
                if (await FileRepository.GetFileExistsAsync(dnbOrgsPathPrevious))
                {
                    await FileRepository.WriteAsync(dnbOrgsPath,
                                                    await FileRepository.ReadBytesAsync(dnbOrgsPathPrevious));

                    fileExists = await FileRepository.GetFileExistsAsync(dnbOrgsPath);
                }
            }

            if (!fileExists)
            {
                return(null);
            }

            var newloadTime = fileExists ? await FileRepository.GetLastWriteTimeAsync(dnbOrgsPath) : DateTime.MinValue;

            if (_DnBOrgsLoaded > DateTime.MinValue && newloadTime <= _DnBOrgsLoaded)
            {
                return(null);
            }

            var orgs = fileExists ? await FileRepository.ReadAsync(dnbOrgsPath) : null;

            if (string.IsNullOrWhiteSpace(orgs))
            {
                throw new Exception($"No content not load '{dnbOrgsPath}'");
            }

            _DnBOrgsLoaded = newloadTime;

            var list = await FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

            if (list.Count < 1)
            {
                throw new Exception($"No records found in '{dnbOrgsPath}'");
            }

            foreach (var org in list.OrderBy(o => o.OrganisationName))
            {
                if (org.CompanyNumber.IsNumber())
                {
                    org.CompanyNumber = org.CompanyNumber.PadLeft(8, '0');
                }
            }

            return(list);
        }
Exemple #10
0
 /// <inheritdoc />
 public void Dispose()
 {
     foreach (var contentsValue in Contents.Values)
     {
         contentsValue?.Dispose();
     }
     Contents.Clear();
     Filenames.Clear();
     Formats.Clear();
     OriginalFormats.Clear();
 }
Exemple #11
0
        public void Read(byte[] data)
        {
            using BinaryReader reader = new(new MemoryStream(data));
            int size = data.Length;

            while (size > 0)
            {
                var filename = reader.ReadCString();
                Filenames.Add(filename.GetPlainName());
                size -= filename.Length + 1;
            }
        }
Exemple #12
0
        public NarcArchive(byte[] data)
        {
            var frames = ReadNitroFrames(data);

            if (!frames.ContainsKey("FATB") || !frames.ContainsKey("FNTB") || !frames.ContainsKey("FIMG"))
            {
                throw new IOException("Not a valid narc file");
            }

            var fatbframe = frames["FATB"];
            var fimgframe = frames["FIMG"];
            var fileCount = ReadLong(fatbframe, 0);

            for (var i = 0; i < fileCount; i++)
            {
                var startOffset = ReadLong(fatbframe, 4 + i * 8);
                var endOffset   = ReadLong(fatbframe, 8 + i * 8);
                var length      = endOffset - startOffset;

                Files.Add(fimgframe.Slice(length, startOffset));
            }

            var fntbframe = frames["FNTB"];
            var unk1      = ReadLong(fntbframe, 0);

            if (unk1 == 8)
            {
                var offset = 8;
                HasFilenames = true;

                for (var i = 0; i < fileCount; i++)
                {
                    var fnLength = fntbframe[offset] & 0xFF;
                    offset++;

                    var array = new byte[fntbframe.Length];
                    fntbframe.CopyTo(array, 0);
                    Filenames.Add(Encoding.ASCII.GetString(array, offset, fnLength));
                }
            }
            else
            {
                HasFilenames = false;

                for (var i = 0; i < fileCount; i++)
                {
                    Filenames.Add(null);
                }
            }
        }
Exemple #13
0
        public override bool isValid()
        {
            // filter if there are no files
            if (!Filenames.Any())
            {
                return(false);
            }

            // filter if there is only one file with no name
            if (Filenames.Count == 1 && Filenames[0] == string.Empty)
            {
                return(false);
            }

            return(true);
        }
        public DatabaseAdminMediaWindow()
        {
            InitializeComponent();

            ServerTextfield.Text         = Properties.Settings.Default.DataServer;
            FolderTextfield.Text         = Properties.Settings.Default.DataServerFolder;
            Connectiontype.SelectedIndex = 0;

            string[] split = Properties.Settings.Default.Filenames.Split(';');

            for (int i = 0; i < split.Length; i++)
            {
                Filenames.AppendText(split[i] + "\r\n");
            }

            //   Filenames.Text = Properties.Settings.Default.Filenames;
        }
        override public void Init()
        {
            DirectoryInfo d = new DirectoryInfo(Misc.Common.SaveFolder); // TODO specified directory

            if (d.Exists == false)
            {
                return;
            }
            ListBox box = FindName("Databases") as ListBox;

            foreach (FileInfo fi in d.GetFiles())
            {
                if (fi.Extension.Equals(Common.DbExt))
                {
                    Filenames.Add(fi);
                }
            }
            box.SelectedIndex = 0;
            OnInfo(string.Format("Found {0} records", Filenames.Count));
        }
Exemple #16
0
        internal void ReadItems()
        {
            if (items.Any())
            {
                throw new InvalidOperationException();
            }

            using (var reader = cache.CreateReader(cache.MetadataTranslator))
            {
                reader.Seek(TagDataAddress.Address, SeekOrigin.Begin);
                for (int i = 0; i < TagCount; i++)
                {
                    //Halo2Vista multiplayer maps have empty tags in them
                    var item = reader.ReadObject(new IndexItem(cache));
                    if (item.Id < 0)
                    {
                        continue;
                    }

                    items.Add(i, item);

                    //Halo2Vista multiplayer maps have two ugh! tags
                    if (CacheFactory.SystemClasses.Contains(item.ClassCode) && !sysItems.ContainsKey(item.ClassCode))
                    {
                        sysItems.Add(item.ClassCode, item);
                    }
                }

                reader.Seek(cache.Header.FileTableIndexOffset, SeekOrigin.Begin);
                var indices = reader.ReadEnumerable <int>(TagCount).ToArray();

                for (int i = 0; i < TagCount; i++)
                {
                    reader.Seek(cache.Header.FileTableAddress + indices[i], SeekOrigin.Begin);
                    Filenames.Add(i, reader.ReadNullTerminatedString());
                }
            }
        }
Exemple #17
0
        internal void ReadItems(DependencyReader reader)
        {
            if (items.Any())
            {
                throw new InvalidOperationException();
            }

            for (int i = 0; i < TagCount; i++)
            {
                reader.Seek(cache.Header.IndexAddress + HeaderSize + i * 32, SeekOrigin.Begin);

                var item = reader.ReadObject(new IndexItem(cache));
                items.Add(item);

                if (CacheFactory.SystemClasses.Contains(item.ClassCode))
                {
                    sysItems.Add(item.ClassCode, item);
                }

                reader.Seek(item.FileNamePointer.Address, SeekOrigin.Begin);
                Filenames.Add(item.Id, reader.ReadNullTerminatedString());
            }
        }
        private void buttonChoosePNGs_Click(object sender, EventArgs e)
        {
            if (Filenames.Count > 0)
            {
                Filenames.Clear();
            }

            openFileDialogImages.Multiselect      = true;
            openFileDialogImages.Filter           = _dialogFilterImages;
            openFileDialogImages.InitialDirectory = _initDir;
            openFileDialogImages.FileName         = "";
            DialogResult result = openFileDialogImages.ShowDialog();

            if (result == DialogResult.OK)
            {
                foreach (string path in openFileDialogImages.FileNames)
                {
                    Filenames.Add(path);
                }
            }
            textBox2.Text = string.Join("; ", Filenames);
            EnableButtons();
        }
Exemple #19
0
        public void RecursiveReplace()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            sl.Statement = statement;

            OtherStatement os = new OtherStatement();
            os.Mode = OtherStatementMode.RecursiveReplace;
            statement.OtherStatement = os;

            Regex regex = new Regex();
            regex.Pattern = "abc";
            os.Regex = regex;

            Filenames filenames = new Filenames();
            os.Filenames = filenames;

            Filename source = new Filename();
            source.Path = "../../../TestFiles/abc.txt";
            filenames.Filename = source;

            Filename dest = new Filename();
            dest.Path = "../../../TestFiles/abc_output.txt";
            filenames.Destimation = dest;

            miniRe.Execute(table);

            String expected = "bbc  a  bc  a";
            StringBuilder actual = new StringBuilder();
            using (FileStream fs = new FileStream("../../../TestFiles/abc_output.txt", FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    actual.Append(sr.ReadToEnd());
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }
Exemple #20
0
        internal void ReadItems()
        {
            if (items.Any())
            {
                throw new InvalidOperationException();
            }

            using (var reader = cache.CreateReader(cache.MetadataTranslator, cache.PointerExpander))
            {
                reader.Seek(TagClassDataPointer.Address, SeekOrigin.Begin);
                Classes.AddRange(reader.ReadEnumerable <TagClass>(TagClassCount));

                reader.Seek(TagDataPointer.Address, SeekOrigin.Begin);
                for (int i = 0; i < TagCount; i++)
                {
                    var item = reader.ReadObject(new IndexItem(cache, i));
                    if (item.ClassIndex < 0)
                    {
                        continue;
                    }

                    items.Add(i, item);

                    if (item.ClassCode != CacheFactory.ScenarioClass && CacheFactory.SystemClasses.Contains(item.ClassCode))
                    {
                        sysItems.Add(item.ClassCode, item);
                    }
                }

                var play = sysItems["play"];
                if (play.MetaPointer.Value == 0)
                {
                    play.MetaPointer = new Pointer(sysItems["zone"].MetaPointer.Value + 28, cache.MetadataTranslator);
                }

                reader.Seek(cache.Header.FileTableIndexPointer.Address, SeekOrigin.Begin);
                var indices = reader.ReadEnumerable <int>(TagCount).ToArray();

                reader.Seek(cache.Header.FileTablePointer.Address, SeekOrigin.Begin);
                using (var tempReader = reader.CreateVirtualReader())
                {
                    for (int i = 0; i < TagCount; i++)
                    {
                        if (indices[i] == -1)
                        {
                            Filenames.Add(i, null);
                            continue;
                        }

                        tempReader.Seek(indices[i], SeekOrigin.Begin);
                        Filenames.Add(i, tempReader.ReadNullTerminatedString());
                    }
                }
            }

            try
            {
                sysItems[CacheFactory.ScenarioClass] = items.Values.Single(i => i.ClassCode == CacheFactory.ScenarioClass && i.FullPath == cache.Header.ScenarioName);
            }
            catch { throw Exceptions.AmbiguousScenarioReference(); }
        }
        public async Task DnBImportAsync(ILogger log, long currentUserId)
        {
            if (RunningJobs.Contains(nameof(CompaniesHouseCheck)) ||
                RunningJobs.Contains(nameof(DnBImportAsync)))
            {
                return;
            }

            RunningJobs.Add(nameof(DnBImportAsync));
            string userEmail    = null;
            string error        = null;
            var    startTime    = VirtualDateTime.Now;
            var    totalChanges = 0;
            var    totalInserts = 0;

            try
            {
                #region Load and Prechecks

                //Load the D&B records
                var dnbOrgsPaths =
                    await _SharedBusinessLogic.FileRepository.GetFilesAsync(_SharedBusinessLogic.SharedOptions.DataPath,
                                                                            Filenames.DnBOrganisations());

                var dnbOrgsPath = dnbOrgsPaths.OrderByDescending(f => f).FirstOrDefault();
                if (string.IsNullOrEmpty(dnbOrgsPath))
                {
                    return;
                }

                if (!await _SharedBusinessLogic.FileRepository.GetFileExistsAsync(dnbOrgsPath))
                {
                    throw new Exception("Could not find " + dnbOrgsPath);
                }

                var AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                if (!AllDnBOrgs.Any())
                {
                    log.LogWarning($"No records found in '{dnbOrgsPath}'");
                    return;
                }

                AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();

                //Check for duplicate DUNS
                var count = AllDnBOrgs.Count() - AllDnBOrgs.Select(o => o.DUNSNumber).Distinct().Count();
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate DUNS numbers detected");
                }

                //Check for no addresses
                count = AllDnBOrgs.Count(o => !o.IsValidAddress());
                if (count > 0)
                {
                    throw new Exception(
                              $"There are {count} organisations with no address detected (i.e., no AddressLine1, AddressLine2, PostalCode, and PoBox).");
                }

                //Check for no organisation name
                count = AllDnBOrgs.Count(o => string.IsNullOrWhiteSpace(o.OrganisationName));
                if (count > 0)
                {
                    throw new Exception($"There are {count} organisations with no OrganisationName detected.");
                }

                //Check for duplicate employer references
                var allEmployerReferenceCount = AllDnBOrgs.Count(o => !string.IsNullOrWhiteSpace(o.EmployerReference));
                var employerReferences        = new SortedSet <string>(
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.EmployerReference))
                    .Select(o => o.EmployerReference).Distinct());
                count = allEmployerReferenceCount - employerReferences.Count;
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate EmployerReferences detected");
                }

                //Check companies have been updated
                count = AllDnBOrgs.Count(
                    o => !string.IsNullOrWhiteSpace(o.CompanyNumber) &&
                    o.DateOfCessation == null &&
                    (o.StatusCheckedDate == null ||
                     o.StatusCheckedDate.Value.AddMonths(1) < VirtualDateTime.Now));
                if (count > 0)
                {
                    throw new Exception(
                              $"There are {count} active companies who have not been checked with companies house within the last month");
                }

                //Fix Company Number
                Parallel.ForEach(
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.CompanyNumber)),
                    dnbOrg =>
                {
                    if (dnbOrg.CompanyNumber.IsNumber())
                    {
                        dnbOrg.CompanyNumber = dnbOrg.CompanyNumber.PadLeft(8, '0');
                    }
                });

                //Check for duplicate company numbers
                var companyNumbers =
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.CompanyNumber)).Select(o => o.CompanyNumber);
                count = companyNumbers.Count() - companyNumbers.Distinct().Count();
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate CompanyNumbers detected");
                }

                //Get the current users email address
                var user = await _SharedBusinessLogic.DataRepository.GetAll <User>()
                           .FirstOrDefaultAsync(u => u.UserId == currentUserId);

                userEmail = user?.EmailAddress;

                //Count records requiring import
                count = AllDnBOrgs.Count(
                    o => !o.GetIsDissolved() &&
                    (o.ImportedDate == null || string.IsNullOrWhiteSpace(o.CompanyNumber) ||
                     o.ImportedDate < o.StatusCheckedDate));
                if (count == 0)
                {
                    return;
                }

                var dbOrgs = _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToList();

                #endregion

                //Set all existing org employer references
                await ReferenceEmployersAsync();

                #region Set all existing org DUNS

                var dnbOrgs = AllDnBOrgs
                              .Where(o => o.OrganisationId > 0 && string.IsNullOrWhiteSpace(o.EmployerReference))
                              .ToList();
                if (dnbOrgs.Count > 0)
                {
                    foreach (var dnbOrg in dnbOrgs)
                    {
                        var org = dbOrgs.FirstOrDefault(o => o.OrganisationId == dnbOrg.OrganisationId);
                        if (org == null)
                        {
                            if (!_SharedBusinessLogic.SharedOptions.IsProduction())
                            {
                                continue;
                            }

                            throw new Exception($"OrganisationId:{dnbOrg.OrganisationId} does not exist in database");
                        }

                        if (!string.IsNullOrWhiteSpace(org.DUNSNumber))
                        {
                            continue;
                        }

                        org.DUNSNumber        = dnbOrg.DUNSNumber;
                        dnbOrg.OrganisationId = null;
                    }

                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();

                    dbOrgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToListAsync();

                    await _SharedBusinessLogic.FileRepository.SaveCSVAsync(AllDnBOrgs, dnbOrgsPath);

                    AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                    AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();
                }

                #endregion

                var allSicCodes = await _SharedBusinessLogic.DataRepository.GetAll <SicCode>().ToListAsync();

                dnbOrgs = AllDnBOrgs.Where(o => o.ImportedDate == null || o.ImportedDate < o.StatusCheckedDate)
                          .ToList();
                while (dnbOrgs.Count > 0)
                {
                    var allBadSicCodes = new ConcurrentBag <OrganisationSicCode>();

                    var c          = 0;
                    var dbChanges  = 0;
                    var dnbChanges = 0;

                    foreach (var dnbOrg in dnbOrgs)
                    {
                        //Only do 100 records at a time
                        if (c > 100)
                        {
                            break;
                        }

                        var dbChanged = false;
                        var dbOrg     = dbOrgs.FirstOrDefault(o => o.DUNSNumber == dnbOrg.DUNSNumber);

                        var dataSource = string.IsNullOrWhiteSpace(dnbOrg.NameSource) ? "D&B" : dnbOrg.NameSource;
                        var orgName    = new OrganisationName
                        {
                            Name = dnbOrg.OrganisationName.Left(100), Source = dataSource
                        };

                        if (dbOrg == null)
                        {
                            dbOrg = string.IsNullOrWhiteSpace(dnbOrg.CompanyNumber)
                                ? null
                                : dbOrgs.FirstOrDefault(o => o.CompanyNumber == dnbOrg.CompanyNumber);
                            if (dbOrg != null)
                            {
                                dbOrg.DUNSNumber = dnbOrg.DUNSNumber;
                            }
                            else
                            {
                                dbOrg = new Organisation
                                {
                                    DUNSNumber        = dnbOrg.DUNSNumber,
                                    EmployerReference = dnbOrg.EmployerReference,
                                    OrganisationName  = orgName.Name,
                                    CompanyNumber     = string.IsNullOrWhiteSpace(dnbOrg.CompanyNumber)
                                        ? null
                                        : dnbOrg.CompanyNumber,
                                    SectorType      = dnbOrg.SectorType,
                                    DateOfCessation = dnbOrg.DateOfCessation
                                };
                                dbOrg.OrganisationNames.Add(orgName);

                                //Create a presumed in-scope for current year
                                var newScope = new OrganisationScope
                                {
                                    Organisation    = dbOrg,
                                    ScopeStatus     = ScopeStatuses.PresumedInScope,
                                    ScopeStatusDate = VirtualDateTime.Now,
                                    Status          = ScopeRowStatuses.Active,
                                    SnapshotDate    = _snapshotDateHelper.GetSnapshotDate(dbOrg.SectorType)
                                };
                                _SharedBusinessLogic.DataRepository.Insert(newScope);
                                dbOrg.OrganisationScopes.Add(newScope);

                                //Create a presumed out-of-scope for previous year
                                var oldScope = new OrganisationScope
                                {
                                    Organisation    = dbOrg,
                                    ScopeStatus     = ScopeStatuses.PresumedOutOfScope,
                                    ScopeStatusDate = VirtualDateTime.Now,
                                    Status          = ScopeRowStatuses.Active,
                                    SnapshotDate    = newScope.SnapshotDate.AddYears(-1)
                                };
                                _SharedBusinessLogic.DataRepository.Insert(oldScope);
                                dbOrg.OrganisationScopes.Add(oldScope);

                                dbOrg.SetStatus(OrganisationStatuses.Active, currentUserId, "Imported from D&B");
                            }
                        }
                        //Skip dissolved companies
                        else if (_OrganisationBusinessLogic.GetOrganisationWasDissolvedBeforeCurrentAccountingYear(dbOrg))
                        {
                            dnbOrg.ImportedDate = VirtualDateTime.Now;
                            dnbChanges++;
                            continue;
                        }
                        else if (dbOrg.OrganisationName != orgName.Name)
                        {
                            var oldOrgName = dbOrg.GetLatestName();
                            if (oldOrgName == null ||
                                _SharedBusinessLogic.SourceComparer.CanReplace(orgName.Source, oldOrgName.Source))
                            {
                                dbOrg.OrganisationName = orgName.Name;
                                dbOrg.OrganisationNames.Add(orgName);
                                dbChanged = true;
                            }
                        }

                        //Ensure D&B gas an organisationID
                        if (dnbOrg.OrganisationId == null || dnbOrg.OrganisationId.Value == 0)
                        {
                            dnbOrg.OrganisationId = dbOrg.OrganisationId;
                            dnbChanges++;
                        }

                        //Add the cessation date
                        if (dbOrg.DateOfCessation == null && dbOrg.DateOfCessation != dnbOrg.DateOfCessation)
                        {
                            dbOrg.DateOfCessation = dnbOrg.DateOfCessation;
                            dbChanged             = true;
                        }

                        //Set the employer reference
                        if (string.IsNullOrWhiteSpace(dbOrg.EmployerReference))
                        {
                            string employerReference;
                            do
                            {
                                employerReference = _OrganisationBusinessLogic.GenerateEmployerReference();
                            } while (employerReferences.Contains(employerReference));

                            dbOrg.EmployerReference = employerReference;
                            employerReferences.Add(employerReference);
                            dbChanged = true;
                        }

                        if (dnbOrg.EmployerReference != dbOrg.EmployerReference)
                        {
                            dnbOrg.EmployerReference = dbOrg.EmployerReference;
                            dnbChanges++;
                        }

                        //Add the new address
                        var fullAddress = dnbOrg.GetAddress();
                        var newAddress  = dbOrg.LatestAddress;

                        //add the address if there isnt one
                        dataSource = string.IsNullOrWhiteSpace(dnbOrg.AddressSource) ? "D&B" : dnbOrg.AddressSource;
                        if (newAddress == null ||
                            !newAddress.GetAddressString().EqualsI(fullAddress) &&
                            _SharedBusinessLogic.SourceComparer.CanReplace(dataSource, newAddress.Source))
                        {
                            var statusDate = VirtualDateTime.Now;

                            newAddress = new OrganisationAddress();
                            newAddress.Organisation    = dbOrg;
                            newAddress.CreatedByUserId = currentUserId;
                            newAddress.Address1        = dnbOrg.AddressLine1;
                            newAddress.Address2        = dnbOrg.AddressLine2;
                            newAddress.Address3        = dnbOrg.AddressLine3;
                            newAddress.County          = dnbOrg.County;
                            newAddress.Country         = dnbOrg.Country;
                            newAddress.TownCity        = dnbOrg.City;
                            newAddress.PostCode        = dnbOrg.PostalCode;
                            newAddress.PoBox           = dnbOrg.PoBox;
                            newAddress.Source          = dataSource;
                            newAddress.SetStatus(AddressStatuses.Active, currentUserId, "Imported from D&B");
                            if (dbOrg.LatestAddress != null)
                            {
                                dbOrg.LatestAddress.SetStatus(AddressStatuses.Retired, currentUserId,
                                                              $"Replaced by {newAddress.Source}");
                            }
                        }

                        //Update the sic codes
                        var newCodeIds   = dnbOrg.GetSicCodesIds();
                        var newCodesList = dnbOrg.GetSicCodesIds().ToList();
                        for (var i = 0; i < newCodesList.Count; i++)
                        {
                            var code = newCodesList[i];
                            if (code <= 0)
                            {
                                continue;
                            }

                            var sicCode = allSicCodes.FirstOrDefault(sic => sic.SicCodeId == code);
                            if (sicCode != null)
                            {
                                continue;
                            }

                            sicCode = allSicCodes.FirstOrDefault(
                                sic => sic.SicCodeId == code * 10 && sic.Description.EqualsI(dnbOrg.SicDescription));
                            if (sicCode != null)
                            {
                                newCodesList[i] = sicCode.SicCodeId;
                            }
                        }

                        newCodeIds = new SortedSet <int>(newCodesList);

                        var newCodes     = new List <OrganisationSicCode>();
                        var oldCodes     = dbOrg.GetLatestSicCodes().ToList();
                        var oldSicSource = dbOrg.GetLatestSicSource();
                        var oldCodeIds   = oldCodes.Select(s => s.SicCodeId);
                        if (dbOrg.SectorType == SectorTypes.Public)
                        {
                            newCodeIds.Add(1);
                        }

                        if (!_SharedBusinessLogic.SharedOptions.IsProduction())
                        {
                            Debug.WriteLine(
                                $"OLD:{oldCodes.Select(s => s.SicCodeId).ToDelimitedString()} NEW:{newCodeIds.ToDelimitedString()}");
                        }

                        dataSource = string.IsNullOrWhiteSpace(dnbOrg.SicSource) ? "D&B" : dnbOrg.SicSource;
                        if (!newCodeIds.SetEquals(oldCodeIds) &&
                            _SharedBusinessLogic.SourceComparer.CanReplace(dataSource, oldSicSource))
                        {
                            foreach (var code in newCodeIds)
                            {
                                if (code <= 0)
                                {
                                    continue;
                                }

                                var sicCode = allSicCodes.FirstOrDefault(sic => sic.SicCodeId == code);
                                var newSic  = new OrganisationSicCode
                                {
                                    Organisation = dbOrg, SicCodeId = code, Source = dataSource
                                };
                                if (sicCode == null)
                                {
                                    allBadSicCodes.Add(newSic);
                                    continue;
                                }

                                newCodes.Add(newSic);
                            }

                            if (newCodes.Any())
                            {
                                //Add new codes only
                                foreach (var newSic in newCodes)
                                {
                                    dbOrg.OrganisationSicCodes.Add(newSic);
                                    dbChanged = true;
                                }

                                //Retire the old codes
                                foreach (var oldSic in oldCodes)
                                {
                                    oldSic.Retired = VirtualDateTime.Now;
                                    dbChanged      = true;
                                }
                            }
                        }

                        await _SharedBusinessLogic.DataRepository.BeginTransactionAsync(
                            async() =>
                        {
                            try
                            {
                                //Save the name, Sic, EmployerReference, DateOfCessasion changes
                                if (dbChanged)
                                {
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                }

                                //Save the changes
                                dnbOrg.ImportedDate = VirtualDateTime.Now;
                                dnbChanges++;
                                var insert = false;
                                if (dbOrg.OrganisationId == 0)
                                {
                                    _SharedBusinessLogic.DataRepository.Insert(dbOrg);
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                    dbChanged = true;
                                    insert    = true;
                                }

                                if (newAddress != null && newAddress.AddressId == 0)
                                {
                                    dbOrg.OrganisationAddresses.Add(newAddress);
                                    dbOrg.LatestAddress = newAddress;
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                    dbChanged = true;
                                }

                                if (dbChanged)
                                {
                                    dbChanges++;
                                    _SharedBusinessLogic.DataRepository.CommitTransaction();
                                    totalChanges++;
                                    if (insert)
                                    {
                                        totalInserts++;
                                    }

                                    //Add or remove this organisation to/from the search index
                                    await SearchBusinessLogic.UpdateSearchIndexAsync(dbOrg);
                                }
                            }
                            catch
                            {
                                _SharedBusinessLogic.DataRepository.RollbackTransaction();
                            }
                        });

                        c++;
                    }

                    //Reload all the changes
                    if (dbChanges > 0)
                    {
                        dbOrgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToListAsync();
                    }

                    //Save the D&B records
                    if (dnbChanges > 0)
                    {
                        await _SharedBusinessLogic.FileRepository.SaveCSVAsync(AllDnBOrgs, dnbOrgsPath);

                        AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                        AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();
                        dnbOrgs    = AllDnBOrgs.Where(o => o.ImportedDate == null || o.ImportedDate < o.StatusCheckedDate)
                                     .ToList();
                    }

                    //Save the bad sic codes
                    if (allBadSicCodes.Count > 0)
                    {
                        //Create the logging tasks
                        var badSicLoggingtasks = new List <Task>();
                        allBadSicCodes.ForEach(
                            bsc => badSicLoggingtasks.Add(
                                _BadSicLog.WriteAsync(
                                    new BadSicLogModel
                        {
                            OrganisationId   = bsc.Organisation.OrganisationId,
                            OrganisationName = bsc.Organisation.OrganisationName,
                            SicCode          = bsc.SicCodeId,
                            Source           = bsc.Source
                        })));

                        //Wait for all the logging tasks to complete
                        await Task.WhenAll(badSicLoggingtasks);
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                throw;
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(userEmail))
                {
                    var endTime  = VirtualDateTime.Now;
                    var duration = endTime - startTime;
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(error))
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Failed",
                                userEmail,
                                $"The D&B import failed at {endTime} after {duration.ToFriendly()}.\nChanged {totalChanges} organisations including {totalInserts} new.\n\nERROR:{error}");
                        }
                        else if (totalChanges == 0)
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Complete",
                                userEmail,
                                "The D&B import process completed successfully with no records requiring import.");
                        }
                        else
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Complete",
                                userEmail,
                                $"The D&B import process completed successfully at {endTime} after {duration.ToFriendly()}.\nChanged {totalChanges} organisations including {totalInserts} new.");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, ex.Message);
                    }
                }

                RunningJobs.Remove(nameof(DnBImportAsync));
            }
        }
Exemple #22
0
 public override bool isValid()
 {
     return(Filenames.Any(str => !String.IsNullOrWhiteSpace(str)));
 }
Exemple #23
0
        public async Task <List <RegistrationAddressesFileModel> > GetLatestRegistrationAddressesAsync()
        {
            // Load the DnBOrgs file from storage"
            var dnbOrgsPath = Path.Combine(_SharedBusinessLogic.SharedOptions.DataPath, Filenames.DnBOrganisations());
            var AllDnBOrgs  = await _SharedBusinessLogic.FileRepository.GetFileExistsAsync(dnbOrgsPath)
                ? await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath)
                : new List <DnBOrgsModel>();

            AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();

            // Extract the DUNSNumber, JobTitle and FullName from the DnBOrgs
            var dnbUserOrgs = AllDnBOrgs
                              .Select(dnbOrg => new
                                      { dnbOrg.DUNSNumber, DnBJobTitle = dnbOrg.ContactJobtitle, DnBFullName = dnbOrg.GetContactName() })
                              .ToList();

            // Get all the latest verified organisation registrations
            var verifiedOrgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>()
                               .Where(uo => uo.LatestRegistration != null)
                               .Include(uo => uo.LatestRegistration)
                               .Include(uo => uo.LatestAddress)
                               .Include(uo => uo.LatestReturn)
                               .Include(uo => uo.LatestScope)
                               .ToListAsync();

            return(verifiedOrgs.Select(
                       vo =>
            {
                // Read the latest address for the organisation
                var latestAddress = vo.LatestAddress;
                if (latestAddress == null)
                {
                    throw new Exception(
                        $"Organisation {vo.OrganisationId} has a latest registration with no Organisation Address associated");
                }

                // Get the latest user for the organisation
                var latestRegistrationUser = vo.LatestRegistration?.User;
                if (latestAddress == null)
                {
                    throw new Exception(
                        $"Organisation {vo.OrganisationId} has a latest registration with no User associated");
                }

                // Ensure the address lines don't start with null or whitespaces
                var addressLines = new List <string>();
                foreach (var line in new[]
                         { latestAddress.Address1, latestAddress.Address2, latestAddress.Address3 })
                {
                    if (string.IsNullOrWhiteSpace(line) == false)
                    {
                        addressLines.Add(line);
                    }
                }

                for (var i = addressLines.Count; i < 3; i++)
                {
                    addressLines.Add(string.Empty);
                }

                // Format post code with the po boxes
                var postCode = latestAddress.PostCode;
                if (!string.IsNullOrWhiteSpace(postCode) && !string.IsNullOrWhiteSpace(latestAddress.PoBox))
                {
                    postCode = latestAddress.PoBox + ", " + postCode;
                }
                else if (!string.IsNullOrWhiteSpace(latestAddress.PoBox) && string.IsNullOrWhiteSpace(postCode))
                {
                    postCode = latestAddress.PoBox;
                }

                // Convert two letter country codes to full country names
                var countryCode = Country.FindTwoLetterCode(latestAddress.Country);

                // Get the linked dnb record using the DUNSNumber
                var dnbOrg = dnbUserOrgs.FirstOrDefault(dnbo => dnbo.DUNSNumber == vo.DUNSNumber);

                // If (DnBJobTile and DnBFullName) is null or empty then DnbFullName = "Chief Executive"
                var dnbJobTitle = dnbOrg?.DnBJobTitle;
                var dnbFullName = dnbOrg?.DnBFullName;
                if (string.IsNullOrWhiteSpace(dnbJobTitle) && string.IsNullOrWhiteSpace(dnbFullName))
                {
                    dnbFullName = "Chief Executive";
                }

                // Retrieve the SectorType reporting snapshot date (d MMMM yyyy)
                var expires = _snapshotDateHelper.GetSnapshotDate(vo.SectorType).AddYears(1).AddDays(-1)
                              .ToString("d MMMM yyyy");

                // Generate csv row
                return new RegistrationAddressesFileModel
                {
                    OrganisationId = vo.OrganisationId,
                    DUNSNumber = vo.DUNSNumber,
                    EmployerReference = vo.EmployerReference,
                    Sector = vo.SectorType,
                    DnBJobTitle = dnbJobTitle,
                    DnBFullName = dnbFullName,
                    LatestUserJobTitle = latestRegistrationUser.JobTitle,
                    LatestUserFullName = latestRegistrationUser.Fullname,
                    LatestUserStatus = latestRegistrationUser.Status.ToString(),
                    Company = vo.OrganisationName,
                    Address1 = addressLines[0],
                    Address2 = addressLines[1],
                    Address3 = addressLines[2],
                    City = latestAddress.TownCity,
                    Postcode = postCode,
                    County = latestAddress.County,
                    Country = string.IsNullOrWhiteSpace(countryCode) || countryCode.EqualsI("GB")
                                ? latestAddress.Country
                                : null,
                    CreatedByUserId = latestAddress.CreatedByUserId,
                    Expires = expires
                };
            })
                   .OrderBy(model => model.Company)
                   .ToList());
        }
Exemple #24
0
        internal void ReadItems()
        {
            if (items.Any())
            {
                throw new InvalidOperationException();
            }

            using (var reader = cache.CreateReader(cache.MetadataTranslator))
            {
                reader.Seek(TagClassDataPointer.Address, SeekOrigin.Begin);
                Classes.AddRange(reader.ReadEnumerable <TagClass>(TagClassCount));

                reader.Seek(TagDataPointer.Address, SeekOrigin.Begin);
                for (int i = 0; i < TagCount; i++)
                {
                    var item = reader.ReadObject(new IndexItem(cache, i));
                    if (item.ClassIndex < 0)
                    {
                        continue;
                    }

                    items.Add(i, item);

                    if (item.ClassCode != CacheFactory.ScenarioClass && CacheFactory.SystemClasses.Contains(item.ClassCode))
                    {
                        sysItems.Add(item.ClassCode, item);
                    }
                }

                // hack to redirect any play tag requests to the start of the zone tag when there is no play tag
                var play = sysItems["play"];
                if (play.MetaPointer.Value == 0)
                {
                    play.MetaPointer = new Pointer(sysItems["zone"].MetaPointer.Value + 28, cache.MetadataTranslator);
                }

                reader.Seek(cache.Header.FileTableIndexPointer.Address, SeekOrigin.Begin);
                var indices = reader.ReadEnumerable <int>(TagCount).ToArray();

                reader.Seek(cache.Header.FileTablePointer.Address, SeekOrigin.Begin);
                var decrypted = reader.ReadAesBytes(cache.Header.FileTableSize, CacheFile.FileNamesKey);
                using (var ms = new MemoryStream(decrypted))
                    using (var tempReader = new EndianReader(ms))
                    {
                        for (int i = 0; i < TagCount; i++)
                        {
                            if (indices[i] == -1)
                            {
                                Filenames.Add(i, null);
                                continue;
                            }

                            tempReader.Seek(indices[i], SeekOrigin.Begin);
                            Filenames.Add(i, tempReader.ReadNullTerminatedString());
                        }
                    }
            }

            try
            {
                sysItems[CacheFactory.ScenarioClass] = items.Values.Single(i => i.ClassCode == CacheFactory.ScenarioClass && i.FullPath == cache.Header.ScenarioName);
            }
            catch { throw Exceptions.AmbiguousScenarioReference(); }
        }
Exemple #25
0
        //Remove any unverified users their addresses, UserOrgs, Org and addresses and archive to zip
        public async Task PurgeOrganisations([TimerTrigger("01:00:00:00", RunOnStartup = true)]
                                             TimerInfo timer,
                                             ILogger log)
        {
            try
            {
                var deadline =
                    VirtualDateTime.Now.AddDays(0 - _SharedBusinessLogic.SharedOptions.PurgeUnusedOrganisationDays);
                var orgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>()
                           .Where(
                    o => o.Created < deadline &&
                    !o.Returns.Any() &&
                    !o.OrganisationScopes.Any(
                        sc => sc.ScopeStatus == ScopeStatuses.InScope ||
                        sc.ScopeStatus == ScopeStatuses.OutOfScope) &&
                    !o.UserOrganisations.Any(
                        uo => uo.Method == RegistrationMethods.Manual || uo.PINConfirmedDate != null ||
                        uo.PINSentDate > deadline) &&
                    !o.OrganisationAddresses.Any(a => a.CreatedByUserId == -1 || a.Source == "D&B"))
                           .ToListAsync();

                if (orgs.Any())
                {
                    //Remove D&B orgs
                    var filePath = Path.Combine(_SharedBusinessLogic.SharedOptions.DataPath,
                                                Filenames.DnBOrganisations());
                    var exists = await _SharedBusinessLogic.FileRepository.GetFileExistsAsync(filePath);

                    if (exists)
                    {
                        var allDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(filePath);

                        allDnBOrgs = allDnBOrgs.OrderBy(o => o.OrganisationId).ToList();
                        orgs.RemoveAll(
                            o => allDnBOrgs.Any(
                                dnbo => dnbo.OrganisationId == o.OrganisationId ||
                                !string.IsNullOrWhiteSpace(dnbo.DUNSNumber) &&
                                dnbo.DUNSNumber == o.DUNSNumber));
                    }


                    var count = 0;
                    foreach (var org in orgs)
                    {
                        var logItem = new ManualChangeLogModel(
                            nameof(PurgeOrganisations),
                            ManualActions.Delete,
                            AppDomain.CurrentDomain.FriendlyName,
                            nameof(org.OrganisationId),
                            org.OrganisationId.ToString(),
                            null,
                            JsonConvert.SerializeObject(
                                new
                        {
                            org.OrganisationId,
                            Address = org.GetLatestAddress()?.GetAddressString(),
                            org.EmployerReference,
                            org.DUNSNumber,
                            org.CompanyNumber,
                            org.OrganisationName,
                            org.SectorType,
                            org.Status,
                            SicCodes  = org.GetLatestSicCodeIdsString(),
                            SicSource = org.GetLatestSicSource(),
                            org.DateOfCessation
                        }),
                            null);
                        var searchRecord = _OrganisationBusinessLogic.CreateEmployerSearchModel(org, true);

                        await _SharedBusinessLogic.DataRepository.BeginTransactionAsync(
                            async() =>
                        {
                            try
                            {
                                org.LatestAddress      = null;
                                org.LatestRegistration = null;
                                org.LatestReturn       = null;
                                org.LatestScope        = null;
                                org.UserOrganisations.ForEach(uo => _SharedBusinessLogic.DataRepository.Delete(uo));
                                await _SharedBusinessLogic.DataRepository.SaveChangesAsync();

                                _SharedBusinessLogic.DataRepository.Delete(org);
                                await _SharedBusinessLogic.DataRepository.SaveChangesAsync();

                                _SharedBusinessLogic.DataRepository.CommitTransaction();
                            }
                            catch (Exception ex)
                            {
                                _SharedBusinessLogic.DataRepository.RollbackTransaction();
                                log.LogError(
                                    ex,
                                    $"{nameof(PurgeOrganisations)}: Failed to purge organisation {org.OrganisationId} '{org.OrganisationName}' ERROR: {ex.Message}:{ex.GetDetailsText()}");
                            }
                        });

                        //Remove this organisation from the search index
                        await _EmployerSearchRepository.RemoveFromIndexAsync(new[] { searchRecord });

                        await _ManualChangeLog.WriteAsync(logItem);

                        count++;
                    }

                    log.LogDebug($"Executed {nameof(PurgeOrganisations)} successfully: {count} deleted");
                }
            }
            catch (Exception ex)
            {
                var message = $"Failed webjob ({nameof(PurgeOrganisations)}):{ex.Message}:{ex.GetDetailsText()}";

                //Send Email to GEO reporting errors
                await _Messenger.SendGeoMessageAsync("GPG - WEBJOBS ERROR", message);

                //Rethrow the error
                throw;
            }
        }
        public Assembly CompileAssembly()
        {
            IEnumerable <SyntaxTree> syntaxTrees = Filenames
                                                   // Read in code to compile
                                                   .Select(filename => File.ReadAllText(filename, Encoding.UTF8))
                                                   // Generate syntax tree (Roslyn, see: https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/get-started/syntax-analysis)
                                                   .Union(CodeBodies)
                                                   .Select(body => CSharpSyntaxTree.ParseText(body));

            // Generate a random assembly name to avoid collisions
            string assemblyName = Path.GetRandomFileName();

            // The C# program probably needs references (ie. System.IO)
            // The compiled program may want to utilise the compiling program's namespaces
            //  including all the references used by the original program makes sense
            var trustedAssembliesPaths = ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")).Split(Path.PathSeparator);

            // var references = AppDomain.CurrentDomain.GetAssemblies()
            //                     // Dynamic assemblies cannot be loaded this way
            //                     .Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
            //                         .Select(a => a.Location)
            //                     // Sometimes those are not enough, in which case we should load the entire SDK just in case.
            //                     .Union(SDKPath != null ? Directory.GetFileSystemEntries(SDKPath, "*.dll") : Enumerable.Empty<string>())
            //                     .Union(AssemblyPaths)
            //                     // Create references to all the DLLs
            //                     .Select(path => MetadataReference.CreateFromFile(path));

            var references = trustedAssembliesPaths.Select(path => MetadataReference.CreateFromFile(path));

            // TODO: Allow including multiple directories or specific files

            // Run compilation
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                // Include syntax tree. TODO: Allow for multiple programs
                syntaxTrees: syntaxTrees,
                // Include all references
                references: references,
                // Generate DLL (Static linking would result if a very large file, including the entire SDK)
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                // Emit result into memory stream
                EmitResult result = compilation.Emit(ms);
                if (!result.Success)
                {
                    // Compilation failed. Display all errors.
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);
                    foreach (Diagnostic diagnostic in failures)
                    {
                        Console.Error.WriteLine("\t{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
                    }
                    throw new InvalidProgramException("Compilation failed. See errors above.");
                }
                else
                {
                    // Compilation succeeded
                    // Rewind MemoryStream to beginning (ms.Position = size of DDL in bytes)
                    ms.Position = 0;
                    // Load DDL into memory
                    return(AssemblyLoadContext.Default.LoadFromStream(ms));
                }
            }
        }
 public void AddFile(string path)
 {
     Filenames.Add(path);
 }
Exemple #28
0
        public async Task UpdateFileAsync(ILogger log, string filePath, string action)
        {
            var fileName = Path.GetFileName(filePath);

            switch (Filenames.GetRootFilename(fileName))
            {
            case Filenames.Organisations:
                await UpdateOrganisationsAsync(filePath);

                break;

            case Filenames.Users:
                await UpdateUsersAsync(filePath);

                break;

            case Filenames.Registrations:
                await UpdateRegistrationsAsync(log, filePath);

                break;

            case Filenames.RegistrationAddresses:
                await UpdateRegistrationAddressesAsync(filePath, log);

                break;

            case Filenames.UnverifiedRegistrations:
                await UpdateUnverifiedRegistrationsAsync(log, filePath);

                break;

            case Filenames.SendInfo:
                await UpdateUsersToSendInfoAsync(filePath);

                break;

            case Filenames.AllowFeedback:
                await UpdateUsersToContactForFeedbackAsync(filePath);

                break;

            case Filenames.OrganisationScopes:
                await UpdateScopesAsync(filePath);

                break;

            case Filenames.OrganisationSubmissions:
                await UpdateSubmissionsAsync(filePath);

                break;

            case Filenames.OrganisationLateSubmissions:
                await UpdateOrganisationLateSubmissionsAsync(filePath, log);

                break;

            case Filenames.OrphanOrganisations:
                await UpdateOrphanOrganisationsAsync(filePath, log);

                break;
            }
        }
Exemple #29
0
        public void Replace()
        {
            SymbolTable table = new SymbolTable();

            MiniRE miniRe = new MiniRE();

            StatementList sl = new StatementList();
            miniRe.StatementList = sl;

            Statement statement = new Statement();
            statement.Id = new StringNode("x");
            sl.Statement = statement;

            StatementListTail tail = new StatementListTail();
            sl.Tail = tail;

            Statement s2 = new Statement();
            s2.Id = new StringNode("y");
            tail.Statement = s2;

            StatementListTail tail2 = new StatementListTail();
            tail.Tail = tail2;

            #region Statement 1
            AssignmentStatement asStmt = new AssignmentStatement();
            statement.AssignmentStatement = asStmt;

            Exp exp = new Exp();
            asStmt.Exp = exp;

            Term term = new Term();
            exp.Term = term;

            Regex regex = new Regex();
            regex.Pattern = "a";
            term.Regex = regex;

            Filename filename = new Filename();
            filename.Path = "../../../TestFiles/file1.txt";
            term.Filename = filename;
            #endregion

            #region Statement 2

            AssignmentStatement as2 = new AssignmentStatement();
            as2.Type = AssignmentStatementType.Length;
            s2.AssignmentStatement = as2;

            Exp exp2 = new Exp();
            exp2.Id = new StringNode("x");
            as2.Exp = exp2;

            #endregion

            #region Statement 3

            Statement s3 = new Statement();
            tail2.Statement = s3;

            OtherStatement os = new OtherStatement();
            os.Mode = OtherStatementMode.Replace;
            os.ReplaceText = "b";
            s3.OtherStatement = os;

            Regex regex3 = new Regex();
            regex3.Pattern = "(a|A)";
            os.Regex = regex3;

            Filenames filenames = new Filenames();
            os.Filenames = filenames;

            Filename filename2 = new Filename();
            filename2.Path = "../../../TestFiles/file2.txt";
            filenames.Filename = filename2;

            Filename destimation = new Filename();
            destimation.Path = "../../../TestFiles/file3.txt";
            filenames.Destimation = destimation;

            #endregion

            miniRe.Execute(table);

            String expected = "brgument brgumentbtive predicbment mentoring bpple";
            StringBuilder actual = new StringBuilder();
            using(FileStream fs = new FileStream("../../../TestFiles/file3.txt", FileMode.Open))
            {
                using(StreamReader sr = new StreamReader(fs))
                {
                    actual.Append(sr.ReadToEnd());
                }
            }
            Assert.AreEqual(expected, actual.ToString());
        }