• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 11978888584

22 Nov 2024 07:27PM UTC coverage: 57.383% (-0.002%) from 57.385%
11978888584

push

github

jas88
Fix up redundant type inheritance

11206 of 21050 branches covered (53.24%)

Branch coverage included in aggregate %.

65 of 249 new or added lines in 42 files covered. (26.1%)

17 existing lines in 14 files now uncovered.

31718 of 53752 relevant lines covered (59.01%)

8290.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.23
/Rdmp.Core/CommandExecution/AtomicCommands/ExecuteCommandCreateNewFilter.cs
1
// Copyright (c) The University of Dundee 2018-2019
2
// This file is part of the Research Data Management Platform (RDMP).
3
// 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.
4
// 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.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.Linq;
9
using Rdmp.Core.CommandLine.Interactive.Picking;
10
using Rdmp.Core.Curation;
11
using Rdmp.Core.Curation.Data;
12
using Rdmp.Core.Curation.Data.Aggregation;
13
using Rdmp.Core.Curation.FilterImporting;
14
using Rdmp.Core.Curation.FilterImporting.Construction;
15
using Rdmp.Core.Icons.IconProvision;
16
using Rdmp.Core.Repositories.Construction;
17
using Rdmp.Core.ReusableLibraryCode.Icons.IconProvision;
18
using SixLabors.ImageSharp;
19
using SixLabors.ImageSharp.PixelFormats;
20

21
namespace Rdmp.Core.CommandExecution.AtomicCommands;
22

23
public sealed class ExecuteCommandCreateNewFilter : BasicCommandExecution
24
{
25
    private readonly IFilterFactory _factory;
26
    private readonly IContainer _container;
27
    private readonly IRootFilterContainerHost _host;
28
    private const float DefaultWeight = 0.1f;
29

30
    public IFilter BasedOn { get; set; }
6✔
31
    public ExtractionFilterParameterSet ParameterSet { get; set; }
×
32
    public string Name { get; }
8✔
33
    public string WhereSQL { get; }
8✔
34

35
    private IFilter[] _offerFilters;
36
    private bool _offerCatalogueFilters;
37

38
    public bool OfferCatalogueFilters
39
    {
40
        get => _offerCatalogueFilters;
6✔
41
        set
42
        {
43
            if (value)
×
44
            {
45
                var c = GetCatalogue();
×
46
                _offerFilters = c?.GetAllFilters();
×
47

NEW
48
                if (_offerFilters == null || _offerFilters.Length == 0)
×
49
                    SetImpossible($"There are no Filters declared in Catalogue '{c?.ToString() ?? "NULL"}'");
×
50
            }
51

52

NEW
53
            _offerCatalogueFilters = value;
×
54
        }
×
55
    }
56

57

58
    private ExecuteCommandCreateNewFilter(IBasicActivateItems activator) : base(activator)
6✔
59
    {
60
        Weight = DefaultWeight;
6✔
61
    }
6✔
62

63
    [UseWithCommandLine(
64
        ParameterHelpList = "<into> <basedOn> <name> <where>",
65
        ParameterHelpBreakdown =
66
            """
67
            into        A WHERE filter container or IRootFilterContainerHost (e.g. AggregateConfiguration)
68
            basedOn    Optional ExtractionFilter to copy or ExtractionFilterParameterSet
69
            name    Optional name to set for the new filter
70
            where    Optional SQL to set for the filter.  If <basedOn> is not null this will overwrite it
71
            """)]
72
    public ExecuteCommandCreateNewFilter(IBasicActivateItems activator,
73
        CommandLineObjectPicker picker) : this(activator)
6✔
74
    {
75
        if (picker.Length == 0)
6!
76
            throw new ArgumentException(
×
77
                "You must supply at least one argument to this command (where you want to create the filter)");
×
78

79
        if (picker.Length > 0)
6✔
80
        {
81
            if (picker[0].HasValueOfType(typeof(ExtractionInformation)))
6✔
82
            {
83
                // create a top level Catalogue level filter for reuse later on
84
                var ei = (ExtractionInformation)picker[0].GetValueForParameterOfType(typeof(ExtractionInformation));
2✔
85
                _factory = new ExtractionFilterFactory(ei);
2✔
86
            }
87
            else if (picker[0].HasValueOfType(typeof(IContainer)))
4!
88
            {
89
                // create a filter in this container
90
                _container = (IContainer)picker[0].GetValueForParameterOfType(typeof(IContainer));
×
91
                SetImpossibleIfReadonly(_container);
×
92
            }
93
            else if (picker[0].HasValueOfType(typeof(IRootFilterContainerHost)))
4!
94
            {
95
                // create a container (if none) then add filter to root container of the object
96
                _host = (IRootFilterContainerHost)picker[0]
4✔
97
                    .GetValueForParameterOfType(typeof(IRootFilterContainerHost));
4✔
98
                SetImpossibleIfReadonly(_host);
4✔
99
            }
100
            else
101
            {
102
                throw new ArgumentException(
×
103
                    $"First argument must be {nameof(IContainer)} or  {nameof(IRootFilterContainerHost)} but it was '{picker[0].RawValue}'");
×
104
            }
105

106

107
            _factory ??= _container?.GetFilterFactory() ?? _host?.GetFilterFactory();
6!
108

109
            if (_factory == null)
6!
110
                throw new Exception("It was not possible to work out a FilterFactory from the container/host");
×
111
        }
112

113
        // the index that string arguments begin at (Name and WhereSql)
114
        var stringArgsStartAt = 2;
6✔
115

116
        if (picker.Length > 1)
6✔
117
        {
118
            if (IsImpossible)
2!
119
                return;
×
120

121
            if (picker[1].HasValueOfType(typeof(IFilter)))
2!
122
                BasedOn = (IFilter)picker[1].GetValueForParameterOfType(typeof(IFilter));
×
123
            else if (picker[1].HasValueOfType(typeof(ExtractionFilterParameterSet)))
2!
124
                ParameterSet =
×
125
                    (ExtractionFilterParameterSet)picker[1]
×
126
                        .GetValueForParameterOfType(typeof(ExtractionFilterParameterSet));
×
127
            else if (!picker[1].ExplicitNull) stringArgsStartAt = 1;
4✔
128
        }
129

130
        if (picker.Length > stringArgsStartAt) Name = picker[stringArgsStartAt].RawValue;
8✔
131
        if (picker.Length > stringArgsStartAt + 1) WhereSQL = picker[stringArgsStartAt + 1].RawValue;
8✔
132
    }
6✔
133

134

135
    public ExecuteCommandCreateNewFilter(IBasicActivateItems activator, IRootFilterContainerHost host) : this(activator)
×
136
    {
137
        _factory = host.GetFilterFactory();
×
138
        _container = host.RootFilterContainer;
×
139
        _host = host;
×
140

141
        if (_container == null && _host is AggregateConfiguration ac)
×
142
        {
143
            if (ac.Catalogue.IsApiCall())
×
144
                SetImpossible(ExecuteCommandAddNewFilterContainer.FiltersCannotBeAddedToApiCalls);
×
145

146
            if (ac.OverrideFiltersByUsingParentAggregateConfigurationInstead_ID != null)
×
147
                SetImpossible("Aggregate is set to use another's filter container tree");
×
148
        }
149

150
        SetImpossibleIfReadonly(host);
×
151
    }
×
152

153

154
    public ExecuteCommandCreateNewFilter(IBasicActivateItems activator, CatalogueItem ci) : this(activator)
×
155
    {
156
        if (ci.ExtractionInformation == null)
×
157
        {
158
            SetImpossible(
×
159
                "CatalogueItem is not extractable so cannot have filters. Make this CatalogueItem extractable to add filters.");
×
160
            return;
×
161
        }
162

163
        _factory = new ExtractionFilterFactory(ci.ExtractionInformation);
×
164
    }
×
165

166
    public ExecuteCommandCreateNewFilter(IBasicActivateItems activator, IFilterFactory factory,
167
        IContainer container = null)
168
        : this(activator)
×
169
    {
170
        _factory = factory;
×
171
        _container = container;
×
172

173
        SetImpossibleIfReadonly(container);
×
174
    }
×
175

176
    public ExecuteCommandCreateNewFilter(IBasicActivateItems activator, IContainer container, IFilter basedOn) :
177
        this(activator)
×
178
    {
179
        _container = container;
×
180
        BasedOn = basedOn;
×
181

182
        SetImpossibleIfReadonly(container);
×
183
    }
×
184

185

186
    private ICatalogue GetCatalogue() => _host?.GetCatalogue() ?? _container?.GetCatalogueIfAny();
×
187

188
    public override Image<Rgba32> GetImage(IIconProvider iconProvider) =>
189
        OfferCatalogueFilters
×
190
            ? iconProvider.GetImage(RDMPConcept.Filter, OverlayKind.Import)
×
191
            : iconProvider.GetImage(RDMPConcept.Filter, OverlayKind.Add);
×
192

193
    public override void Execute()
194
    {
195
        base.Execute();
6✔
196

197
        IFilter f;
198
        var container = _container;
6✔
199

200
        if (_host != null && container == null)
6✔
201
        {
202
            if (_host.RootFilterContainer_ID == null)
4✔
203
                _host.CreateRootContainerIfNotExists();
4✔
204

205
            container = _host.RootFilterContainer;
4✔
206
        }
207

208
        // if importing an existing filter instead of creating blank
209
        if (BasedOn != null)
6!
210
        {
211
            var wizard = new FilterImportWizard(BasicActivator);
×
212
            f = wizard.Import(container, BasedOn, ParameterSet);
×
213
        }
214
        else if (OfferCatalogueFilters)
6!
215
        {
216
            // we want user to make decision about what to import
217
            ImportExistingFilter(container);
×
218
            return;
×
219
        }
220
        else
221
        {
222
            f = _factory.CreateNewFilter($"New Filter {Guid.NewGuid()}");
6✔
223
        }
224

225
        container?.AddChild(f);
6✔
226

227
        if (!string.IsNullOrWhiteSpace(Name)) f.Name = Name;
8✔
228
        if (!string.IsNullOrWhiteSpace(WhereSQL)) f.WhereSQL = WhereSQL;
8✔
229

230
        f.SaveToDatabase();
6✔
231

232
        if (f is ExtractionFilter ef)
6✔
233
            Publish(ef.ExtractionInformation);
2✔
234
        else
235
            Publish((DatabaseEntity)container ?? (DatabaseEntity)f);
4!
236

237
        Emphasise(f);
6✔
238
        Activate((DatabaseEntity)f);
6✔
239
    }
6✔
240

241
    private void ImportExistingFilter(IContainer container)
242
    {
243
        var wizard = new FilterImportWizard(BasicActivator);
×
244

245
        var import = wizard.ImportManyFromSelection(container, _offerFilters).ToArray();
×
246

247
        foreach (var f in import) container.AddChild(f);
×
248

249
        if (import.Length > 0)
×
250
        {
251
            Publish((DatabaseEntity)container);
×
252
            Emphasise((DatabaseEntity)import.Last());
×
253
        }
254
    }
×
255
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc