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

HicServices / RDMP / 9757494962

02 Jul 2024 08:23AM UTC coverage: 56.679% (-0.2%) from 56.914%
9757494962

Pull #1867

github

web-flow
Merge branch 'develop' into release/8.2.0
Pull Request #1867: Release/8.2.0

10912 of 20750 branches covered (52.59%)

Branch coverage included in aggregate %.

369 of 831 new or added lines in 38 files covered. (44.4%)

375 existing lines in 25 files now uncovered.

30965 of 53135 relevant lines covered (58.28%)

7847.68 hits per line

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

94.23
/Rdmp.Core/CommandExecution/AtomicCommands/ExecuteCommandUpdateCatalogueDataLocation.cs
1
// Copyright (c) The University of Dundee 2024-2024
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 FAnsi.Discovery;
10
using Rdmp.Core.Curation.Data;
11

12
namespace Rdmp.Core.CommandExecution.AtomicCommands;
13

14
public class ExecuteCommandUpdateCatalogueDataLocation : BasicCommandExecution, IAtomicCommand
15
{
16
    private readonly IBasicActivateItems _activator;
17
    private readonly DiscoveredTable _table;
18
    private readonly CatalogueItem[] _selectedCatalogueItems;
19
    private readonly string _catalogueMapping;
20

21
    public readonly string CatalogueMappingIdentifier = "$column";
18✔
22

23
    private bool _checksPassed;
24

25
    public ExecuteCommandUpdateCatalogueDataLocation(IBasicActivateItems activator,
18✔
26
        CatalogueItem[] selectedCatalogueItems, DiscoveredTable table, string catalogueMapping)
18✔
27
    {
28
        _activator = activator;
18✔
29
        _table = table;
18✔
30
        _selectedCatalogueItems = selectedCatalogueItems;
18✔
31
        _catalogueMapping = catalogueMapping;
18✔
32
    }
18✔
33

34
    public string Check()
35
    {
36
        //check the server is alive
37
        if (_table is null) return "No table has been set";
18!
38
        _table.Database.Server.TestConnection();
18✔
39
        //must modify at least 
40
        if (_selectedCatalogueItems.Length == 0) return "Must select at least one catalogue item to modify";
20✔
41
        // check the mapping isn't junk
42
        if (!string.IsNullOrWhiteSpace(_catalogueMapping) && !_catalogueMapping.Contains(CatalogueMappingIdentifier))
16✔
43
            return "Column Mapping must contain the string '$column'";
4✔
44
        // check the columns actually exist & that the types match
45
        foreach (var item in _selectedCatalogueItems)
56✔
46
        {
47
            string newColumn;
48
            try
49
            {
50
                newColumn = GrabColumnName(item.ColumnInfo.Name);
18✔
51
            }
18✔
NEW
52
            catch (Exception ex)
×
53
            {
NEW
54
                return ex.Message;
×
55
            }
56

57
            var discoveredColumns = _table.DiscoverColumns();
18✔
58

59
            var foundColumn = discoveredColumns.AsEnumerable()
18✔
60
                .Where(dc => dc.GetFullyQualifiedName().Contains(newColumn)).FirstOrDefault();
48✔
61
            if (foundColumn is null) return $"Unable to find column '{newColumn}' in selected table";
20✔
62
            if (foundColumn.DataType.ToString() != item.ColumnInfo.Data_type)
16✔
63
                return
2✔
64
                    $"The data type of column '{newColumn}' is of type '{foundColumn.DataType}'. This does not match the current type of '{item.ColumnInfo.Data_type}'";
2✔
65
        }
66

67
        _checksPassed = true;
8✔
68
        return null;
8✔
NEW
69
    }
×
70

71
    private string GrabTableQualifier(string name)
72
    {
73
        return _table.GetFullyQualifiedName();
20✔
74
    }
75

76
    private string GrabColumnName(string name)
77
    {
78
        return MutilateColumnWithMapping(name.Split('.')[^1]);
38✔
79
    }
80

81
    private string MutilateColumnWithMapping(string columnName)
82
    {
83
        var useParenthesis = false;
38✔
84
        if (columnName.StartsWith('[') && columnName.EndsWith("]"))
38✔
85
        {
86
            useParenthesis = true;
34✔
87
            columnName = columnName.Substring(1, columnName.Length - 2);
34✔
88
        }
89

90
        if (!string.IsNullOrWhiteSpace(_catalogueMapping))
38✔
91
        {
92
            if (!_catalogueMapping.Contains(CatalogueMappingIdentifier))
4!
NEW
93
                throw new Exception("Column Mapping is invalid. Add '$column' to the mapping string to fix this.");
×
94
            columnName = _catalogueMapping.Replace(CatalogueMappingIdentifier, columnName);
4✔
95
        }
96

97
        if (useParenthesis) columnName = '[' + columnName + ']';
72✔
98
        return columnName;
38✔
99
    }
100

101

102
    private string GenerateNewSQLPath(string path)
103
    {
104
        var qualifier = GrabTableQualifier(path);
20✔
105
        var updatedName = qualifier + '.' + GrabColumnName(path);
20✔
106
        return updatedName;
20✔
107
    }
108

109
    private TableInfo TableIsAlreadyKnown()
110
    {
111
        return _activator.RepositoryLocator.CatalogueRepository.GetAllObjects<TableInfo>().Where(ti =>
10✔
112
        {
10✔
113
            return ti.Name == _table.GetFullyQualifiedName() &&
12✔
114
                   ti.Server == _table.Database.Server.Name &&
12✔
115
                   ti.Database == _table.Database.GetRuntimeName();
12✔
116
        }).FirstOrDefault();
10✔
117
    }
118

119

120
    public override void Execute()
121
    {
122
        if (!_checksPassed)
8✔
123
        {
124
            var checkResults = Check();
8✔
125
            if (checkResults != null)
8✔
126
                throw new Exception(
2✔
127
                    $"Unable to execute ExecuteCommandUpdateCatalogueDataLocation as the checks returned: {checkResults}");
2✔
128
        }
129

130
        foreach (var selectedCatalogueItem in _selectedCatalogueItems)
32✔
131
        {
132
            var existingTable = TableIsAlreadyKnown();
10✔
133
            if (existingTable is not null)
10✔
134
            {
135
                selectedCatalogueItem.ColumnInfo.TableInfo_ID = existingTable.ID;
8✔
136
            }
137
            else
138
            {
139
                var tblInfo = new TableInfo(_activator.RepositoryLocator.CatalogueRepository,
2✔
140
                    _table.GetFullyQualifiedName());
2✔
141
                tblInfo.Server = _table.Database.Server.Name;
2✔
142
                tblInfo.Database = _table.Database.GetRuntimeName();
2✔
143
                tblInfo.SaveToDatabase();
2✔
144
                selectedCatalogueItem.ColumnInfo.TableInfo_ID = tblInfo.ID;
2✔
145
            }
146

147
            selectedCatalogueItem.ColumnInfo.Name = GenerateNewSQLPath(selectedCatalogueItem.ColumnInfo.Name);
10✔
148
            selectedCatalogueItem.ColumnInfo.SaveToDatabase();
10✔
149
            foreach (var ei in selectedCatalogueItem.ColumnInfo.ExtractionInformations)
40✔
150
            {
151
                ei.SelectSQL = GenerateNewSQLPath(selectedCatalogueItem.ExtractionInformation.SelectSQL);
10✔
152
                ei.SaveToDatabase();
10✔
153
            }
154
        }
155
    }
6✔
156
}
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