Skip to content

Commit

Permalink
Added ExecuteCommandRefreshBrokenCohorts
Browse files Browse the repository at this point in the history
Fixes #1094
  • Loading branch information
tznind committed Apr 29, 2022
1 parent 3a44c13 commit 95f51fc
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

...

### Added

- Added new command 'RefreshBrokenCohorts' for clearing the 'forbid list' of unreachable cohort sources.

### Changed

- Dll load warnings must now be enabled otherwise the information is reported as Success (see user settings error codes R008 and R009)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP 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 General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.

using NUnit.Framework;
using Rdmp.Core.CommandExecution.AtomicCommands;
using Rdmp.Core.CommandLine.Interactive;
using Rdmp.Core.DataExport.Data;
using Rdmp.Core.Providers;
using Rdmp.Core.Repositories;
using ReusableLibraryCode.Checks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rdmp.Core.Tests.CommandExecution
{
internal class ExecuteCommandRefreshBrokenCohortsTests
{

[Test]
public void TestBrokenCohort()
{
var repo = new MemoryDataExportRepository();

var ect = new ExternalCohortTable(repo, "yarg", FAnsi.DatabaseType.MicrosoftSQLServer);
ect.Server = "IDontExist";
ect.Database = "fff";
ect.PrivateIdentifierField = "haha";
ect.ReleaseIdentifierField = "haha";
ect.SaveToDatabase();

var cohort = new ExtractableCohort();
cohort.Repository = repo;
cohort.ExternalCohortTable_ID = ect.ID;
cohort.OriginID = 123;
cohort.SaveToDatabase();

var repoLocator = new RepositoryProvider(repo);

var activator = new ConsoleInputManager(repoLocator, new ThrowImmediatelyCheckNotifier()) {
DisallowInput = true
};

Assert.AreEqual(1,((DataExportChildProvider)activator.CoreChildProvider).ForbidListedSources.Count);

var cmd = new ExecuteCommandRefreshBrokenCohorts(activator)
{
// suppress publishing so we don't just go back into a refresh
// and find it missing again
NoPublish = true,
};

Assert.IsFalse(cmd.IsImpossible);
cmd.Execute();

//now no forbidden cohorts
Assert.IsEmpty(((DataExportChildProvider)activator.CoreChildProvider).ForbidListedSources);


cmd = new ExecuteCommandRefreshBrokenCohorts(activator);
Assert.IsTrue(cmd.IsImpossible);
Assert.AreEqual("There are no broken ExternalCohortTable to clear status on", cmd.ReasonCommandImpossible);

cmd = new ExecuteCommandRefreshBrokenCohorts(activator,ect);
Assert.IsTrue(cmd.IsImpossible);
Assert.AreEqual("'yarg' is not broken", cmd.ReasonCommandImpossible);
}
}
}
2 changes: 2 additions & 0 deletions Rdmp.Core/CommandExecution/AtomicCommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ public IEnumerable<IAtomicCommand> CreateCommands(object o)

yield return new ExecuteCommandImportAlreadyExistingCohort(_activator, ect, null)
{ OverrideCommandName = "Existing Cohort", Weight = -4.6f,SuggestedCategory = "Add" };

yield return new ExecuteCommandRefreshBrokenCohorts(_activator, ect) { Weight = 1};
}

if(Is(o,out ExtractableCohort cohort))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP 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 General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.

using Rdmp.Core.Curation.Data;
using Rdmp.Core.DataExport.Data;
using Rdmp.Core.Providers;
using Rdmp.Core.Repositories.Construction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rdmp.Core.CommandExecution.AtomicCommands
{
/// <summary>
/// Clears the <see cref="DataExportChildProvider.ForbidListedSources"/> list and triggers a refresh
/// which results in all previously broken cohort sources to be re-evaluated for existence
/// </summary>
public class ExecuteCommandRefreshBrokenCohorts : BasicCommandExecution
{
private readonly ExternalCohortTable _ect;

[UseWithObjectConstructor]
public ExecuteCommandRefreshBrokenCohorts(IBasicActivateItems activator,
[DemandsInitialization("The specific ExternalCohortTable to attempt to refresh connections to or null to refresh all ExternalCohortTables")]
ExternalCohortTable ect = null):base(activator)
{
_ect = ect;

if (!(activator.CoreChildProvider is DataExportChildProvider dx))
{
SetImpossible($"{nameof(activator.CoreChildProvider)} is not a {nameof(DataExportChildProvider)}");
return;
}

// if we only want to clear one
if(ect != null)
{
if(!dx.ForbidListedSources.Contains(ect))
{
SetImpossible($"'{ect}' is not broken");
}
}
else
{
// we want to clear all of them
if (!dx.ForbidListedSources.Any())
{
SetImpossible("There are no broken ExternalCohortTable to clear status on");
return;
}
}
}

public override void Execute()
{
base.Execute();

var dx = (DataExportChildProvider)BasicActivator.CoreChildProvider;
var toPublish = _ect ?? dx.ForbidListedSources.FirstOrDefault();

// theres nothing to clear now anyway
if (toPublish == null)
return;

if (_ect != null)
{
dx.ForbidListedSources.Remove(_ect);
}
else
{
dx.ForbidListedSources.Clear();
}


Publish(toPublish);
}
}
}

0 comments on commit 95f51fc

Please sign in to comment.