Skip to content

Commit 962ccfa

Browse files
committedFeb 21, 2025·
Change so that selectiongroupings looks more like a valueset.
1 parent e51ba2f commit 962ccfa

File tree

8 files changed

+273
-67
lines changed

8 files changed

+273
-67
lines changed
 

‎ManualTests/CodelistsTest.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ public void TestOkValueSet()
116116
[TestMethod]
117117
public void TestGetGrouping()
118118
{
119-
//string getId = "AldGrupp19Grp5";
119+
//string getId = "AlleAldre48e3"; // pres I 1 mother 5 children
120120
//string getId = "GrkretsBydel2002";
121-
string getId = _okGrouping;
121+
//string getId = "KommSummer"; //tab 07459 pres A
122+
string getId = "AlleVUtd"; // pres I 12 mother 118 children
123+
//string getId = _okGrouping;
124+
125+
122126

123127
PCAxis.Sql.Models.Grouping actual_data = ApiUtilStatic.GetGrouping(getId, _mainLanguage);
124128

@@ -127,7 +131,6 @@ public void TestGetGrouping()
127131
Assert.IsTrue(actual_data.AvailableLanguages.Contains(_mainLanguage));
128132
Assert.IsTrue(actual_data.AvailableLanguages.Contains("en"));
129133

130-
131134
PCAxis.Sql.Models.Grouping data_en = ApiUtilStatic.GetGrouping(getId, "en");
132135
}
133136

‎PCAxis.Sql/Models/Grouping.cs

+33
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,38 @@ public Grouping()
2020
AvailableLanguages = new List<string>();
2121
}
2222

23+
24+
/*
25+
26+
valueset:
27+
{
28+
"id": "vs_FylkerSvalbardIalt",
29+
"label": "Alle fylker ",
30+
"values": [
31+
{ "code": "Ialt", "label": "I alt", "valueMap": [ "Ialt" ] },
32+
{ "code": "30", "label": "Viken", "valueMap": [ "30" ] },
33+
....
34+
35+
grouping:
36+
Aggregation :
37+
{
38+
"id": "agg_FylkerSvIaltB",
39+
"label": "Fylker 1972-2019",
40+
"values": [
41+
{ "code": "FylkerSvIaltB", "label": "Fylker 1972-2019", "valueMap": ["01", "Ialt", "03", "04",
42+
.......
43+
44+
grouping:
45+
selection:
46+
{
47+
"id": "agg_FylkerSvIaltB",
48+
"label": "Fylker 1972-2019",
49+
"values": [
50+
{ "code": "Ialt", "label": "I alt", "valueMap": ["Ialt"] },
51+
{ "code": "30","label": "Viken", "valueMap": ["30"] },
52+
....
53+
54+
*/
55+
2356
}
2457
}

‎PCAxis.Sql/QueryLib_21/Queries.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23

34
using PCAxis.Sql.DbConfig;
45
using PCAxis.Sql.Repositories;
@@ -114,6 +115,10 @@ internal override string GetGroupingExistsIn(string lang, PxSqlCommand sqlComman
114115
{_db.GroupingLang2.GroupingCol.Id(lang)} = {sqlCommand.GetParameterRef("aGrouping")}";
115116
}
116117

118+
internal override Models.Grouping FixGrouping(string lang, string mGroupingId, List<Models.GroupedValue> groupedValues)
119+
{
120+
throw new NotImplementedException();
121+
}
117122

118123
internal override string GetGroupingValuesQuery(string lang, PxSqlCommand sqlCommand)
119124
{

‎PCAxis.Sql/QueryLib_22/Queries.cs

+67
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
5+
using Microsoft.IdentityModel.Tokens;
26

37
using PCAxis.Sql.DbConfig;
48
using PCAxis.Sql.Repositories;
@@ -112,6 +116,69 @@ internal override string GetGroupingExistsIn(string lang, PxSqlCommand sqlComman
112116
{_db.GroupingLang2.GroupingCol.Id(lang)} = {sqlCommand.GetParameterRef("aGrouping")}";
113117
}
114118

119+
internal override Models.Grouping FixGrouping(string lang, string mGroupingId, List<Models.GroupedValue> groupedValues)
120+
{
121+
SetLang(lang);
122+
var cnmmRow = _metaQuery.GetGroupingRow(mGroupingId);
123+
Models.Grouping myOut = new Models.Grouping();
124+
myOut.Id = mGroupingId;
125+
myOut.Label = cnmmRow.texts[lang].PresText;
126+
127+
bool isSelectionGrouping = cnmmRow.GroupPres.Equals(_db.Codes.GroupPresI);
128+
129+
if (isSelectionGrouping)
130+
{
131+
// remaking the groupedValues
132+
myOut.Values = RemakeValues(lang, cnmmRow.ValuePool, groupedValues);
133+
}
134+
else
135+
{
136+
myOut.Values = groupedValues;
137+
}
138+
return myOut;
139+
}
140+
141+
private List<Models.GroupedValue> RemakeValues(string lang, string valuePoolId, List<Models.GroupedValue> groupedValues)
142+
{
143+
List<Models.GroupedValue> myOut = new List<Models.GroupedValue>();
144+
145+
//the Codes is needed as a StringCollection since GetValueRowsByValuePool is old :-)
146+
StringCollection stringCollection = new StringCollection();
147+
foreach (Models.GroupedValue gvalue in groupedValues)
148+
{
149+
foreach (string item in gvalue.Codes)
150+
{
151+
stringCollection.Add(item);
152+
}
153+
}
154+
Dictionary<string, Models.GroupedValue> groupsByCode = new Dictionary<string, Models.GroupedValue>();
155+
156+
//hack: 3. parameter , should be lookup in data.
157+
string valueExtraExists = "not " + _db.Codes.ValueTextExistsX;
158+
159+
160+
foreach (ValueRow row in _metaQuery.GetValueRowsByValuePool(valuePoolId, stringCollection, valueExtraExists))
161+
{
162+
Models.GroupedValue tmp = new Models.GroupedValue();
163+
164+
tmp.Text = row.texts[lang].ValueTextL;
165+
if (tmp.Text.IsNullOrEmpty())
166+
{
167+
tmp.Text = row.texts[lang].ValueTextS;
168+
}
169+
tmp.Code = row.ValueCode;
170+
tmp.Codes.Add(tmp.Code);
171+
groupsByCode.Add(tmp.Code, tmp);
172+
}
173+
174+
175+
foreach (string item in stringCollection)
176+
{
177+
myOut.Add(groupsByCode[item]);
178+
}
179+
return myOut;
180+
}
181+
115182
internal override string GetGroupingValuesQuery(string lang, PxSqlCommand sqlCommand)
116183
{
117184
//This differ from 23 and 24 in the use of lang(2). Unsure why.

‎PCAxis.Sql/QueryLib_23/Queries.cs

+63
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
5+
using Microsoft.IdentityModel.Tokens;
26

37
using PCAxis.Sql.DbConfig;
48
using PCAxis.Sql.Repositories;
@@ -112,6 +116,65 @@ internal override string GetGroupingExistsIn(string lang, PxSqlCommand sqlComman
112116
{_db.GroupingLang2.GroupingCol.Id(lang)} = {sqlCommand.GetParameterRef("aGrouping")}";
113117
}
114118

119+
internal override Models.Grouping FixGrouping(string lang, string mGroupingId, List<Models.GroupedValue> groupedValues)
120+
{
121+
SetLang(lang);
122+
var cnmmRow = _metaQuery.GetGroupingRow(mGroupingId);
123+
Models.Grouping myOut = new Models.Grouping();
124+
myOut.Id = mGroupingId;
125+
myOut.Label = cnmmRow.texts[lang].PresText;
126+
127+
bool isSelectionGrouping = cnmmRow.GroupPres.Equals(_db.Codes.GroupPresI);
128+
129+
if (isSelectionGrouping)
130+
{
131+
// remaking the groupedValues
132+
myOut.Values = RemakeValues(lang, cnmmRow.ValuePool, groupedValues);
133+
}
134+
else
135+
{
136+
myOut.Values = groupedValues;
137+
}
138+
return myOut;
139+
}
140+
141+
private List<Models.GroupedValue> RemakeValues(string lang, string valuePoolId, List<Models.GroupedValue> groupedValues)
142+
{
143+
List<Models.GroupedValue> myOut = new List<Models.GroupedValue>();
144+
145+
//the Codes is needed as a StringCollection since GetValueRowsByValuePool is old :-)
146+
StringCollection stringCollection = new StringCollection();
147+
foreach (Models.GroupedValue gvalue in groupedValues)
148+
{
149+
foreach (string item in gvalue.Codes)
150+
{
151+
stringCollection.Add(item);
152+
}
153+
}
154+
Dictionary<string, Models.GroupedValue> groupsByCode = new Dictionary<string, Models.GroupedValue>();
155+
156+
foreach (ValueRowHM row in _metaQuery.GetValueRowsByValuePool(valuePoolId, stringCollection, "this param is not used by method only 2.2"))
157+
{
158+
Models.GroupedValue tmp = new Models.GroupedValue();
159+
160+
tmp.Text = row.texts[lang].ValueTextL;
161+
if (tmp.Text.IsNullOrEmpty())
162+
{
163+
tmp.Text = row.texts[lang].ValueTextS;
164+
}
165+
tmp.Code = row.ValueCode;
166+
tmp.Codes.Add(tmp.Code);
167+
groupsByCode.Add(tmp.Code, tmp);
168+
}
169+
170+
171+
foreach (string item in stringCollection)
172+
{
173+
myOut.Add(groupsByCode[item]);
174+
}
175+
return myOut;
176+
}
177+
115178

116179
internal override string GetGroupingValuesQuery(string lang, PxSqlCommand sqlCommand)
117180
{

‎PCAxis.Sql/QueryLib_24/Queries.cs

+57-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Specialized;
4+
5+
using Microsoft.IdentityModel.Tokens;
26

37
using PCAxis.Sql.DbConfig;
48
using PCAxis.Sql.Repositories;
@@ -89,17 +93,66 @@ ORDER BY
8993

9094
}
9195

92-
/*
93-
internal override Models.Grouping GetPartialGrouping(string lang, string myGroupingId)
96+
97+
internal override Models.Grouping FixGrouping(string lang, string mGroupingId, List<Models.GroupedValue> groupedValues)
9498
{
99+
SetLang(lang);
100+
var cnmmRow = _metaQuery.GetGroupingRow(mGroupingId);
95101
Models.Grouping myOut = new Models.Grouping();
96-
var cnmmRow = _metaQuery.GetGroupingRow(myGroupingId);
102+
myOut.Id = mGroupingId;
103+
myOut.Label = cnmmRow.texts[lang].PresText;
97104

105+
bool isSelectionGrouping = cnmmRow.GroupPres.Equals(_db.Codes.GroupPresI);
106+
107+
if (isSelectionGrouping)
108+
{
109+
// remaking the groupedValues
110+
myOut.Values = RemakeValues(lang, cnmmRow.ValuePool, groupedValues);
111+
}
112+
else
113+
{
114+
myOut.Values = groupedValues;
115+
}
98116
return myOut;
99117
}
100-
*/
101118

102119

120+
private List<Models.GroupedValue> RemakeValues(string lang, string valuePoolId, List<Models.GroupedValue> groupedValues)
121+
{
122+
List<Models.GroupedValue> myOut = new List<Models.GroupedValue>();
123+
124+
//the Codes is needed as a StringCollection since GetValueRowsByValuePool is old :-)
125+
StringCollection stringCollection = new StringCollection();
126+
foreach (Models.GroupedValue gvalue in groupedValues)
127+
{
128+
foreach (string item in gvalue.Codes)
129+
{
130+
stringCollection.Add(item);
131+
}
132+
}
133+
Dictionary<string, Models.GroupedValue> groupsByCode = new Dictionary<string, Models.GroupedValue>();
134+
135+
foreach (ValueRowHM row in _metaQuery.GetValueRowsByValuePool(valuePoolId, stringCollection, "this param is not used by method"))
136+
{
137+
Models.GroupedValue tmp = new Models.GroupedValue();
138+
139+
tmp.Text = row.texts[lang].ValueTextL;
140+
if (tmp.Text.IsNullOrEmpty())
141+
{
142+
tmp.Text = row.texts[lang].ValueTextS;
143+
}
144+
tmp.Code = row.ValueCode;
145+
tmp.Codes.Add(tmp.Code);
146+
groupsByCode.Add(tmp.Code, tmp);
147+
}
148+
149+
150+
foreach (string item in stringCollection)
151+
{
152+
myOut.Add(groupsByCode[item]);
153+
}
154+
return myOut;
155+
}
103156

104157
internal override string GetGroupingQuery(string lang, PxSqlCommand sqlCommand)
105158
{

‎PCAxis.Sql/Repositories/AbstractQueries.cs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ internal abstract class AbstractQueries
2020

2121
internal abstract string GetGroupingQuery(string lang, PxSqlCommand sqlCommand);
2222

23+
internal abstract Models.Grouping FixGrouping(string lang, string mGroupingId, List<Models.GroupedValue> groupedValues);
24+
2325
internal abstract string GetGroupingValuesQuery(string lang, PxSqlCommand sqlCommand);
2426

2527

‎PCAxis.Sql/Repositories/GroupingRepositoryStatic.cs

+40-60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Data.Common;
45

56
using PCAxis.Sql.DbClient;
67
using PCAxis.Sql.DbConfig;
@@ -12,27 +13,27 @@ internal static class GroupingRepositoryStatic
1213
{
1314
private static readonly List<string> LanguagesInDbConfig;
1415

15-
private static readonly Dictionary<string, List<string>> SqlquerysByLanguage;
16+
private static readonly Dictionary<string, string> ValuesQueryByLanguage = new Dictionary<string, string>();
17+
private static readonly string ExistsInLangQuery;
18+
private static readonly AbstractQueries Queries;
19+
20+
21+
1622
static GroupingRepositoryStatic()
1723
{
1824
LanguagesInDbConfig = SqlDbConfigsStatic.DefaultDatabase.ListAllLanguages();
1925

20-
//Prepares the sqls. 3 per language.
21-
SqlquerysByLanguage = new Dictionary<string, List<string>>();
22-
2326
var config = SqlDbConfigsStatic.DefaultDatabase;
24-
AbstractQueries queries = AbstractQueries.GetSqlqueries(config);
27+
Queries = AbstractQueries.GetSqlqueries(config);
2528

2629
InfoForDbConnection info = config.GetInfoForDbConnection(config.GetDefaultConnString());
2730
var sqlCommand = new PxSqlCommandForTempTables(info.DataBaseType, info.DataProvider, info.ConnectionString);
2831

32+
ExistsInLangQuery = GetValuesetExistsInLangSql(Queries, sqlCommand);
33+
2934
foreach (var language in LanguagesInDbConfig)
3035
{
31-
string sqlGrouping = queries.GetGroupingQuery(language, sqlCommand);
32-
string sqlValues = queries.GetGroupingValuesQuery(language, sqlCommand); ;
33-
string sqlGroupingExistsInLang = GetOtherLanguagesSql(queries, language, config, sqlCommand);
34-
List<string> sqlquerys = new List<string> { sqlGrouping, sqlValues, sqlGroupingExistsInLang };
35-
SqlquerysByLanguage[language] = sqlquerys;
36+
ValuesQueryByLanguage[language] = Queries.GetGroupingValuesQuery(language, sqlCommand);
3637
}
3738

3839
}
@@ -45,77 +46,64 @@ internal static Models.Grouping GetGrouping(string groupingId, string language)
4546
return null;
4647
}
4748

48-
string sqlGrouping = SqlquerysByLanguage[language][0];
49-
string sqlValues = SqlquerysByLanguage[language][1];
50-
string sqlGroupingExistsInLang = SqlquerysByLanguage[language][2];
51-
5249
var config = SqlDbConfigsStatic.DefaultDatabase;
5350
InfoForDbConnection info = config.GetInfoForDbConnection(config.GetDefaultConnString());
5451
var cmd = new PxSqlCommandForTempTables(info.DataBaseType, info.DataProvider, info.ConnectionString);
5552

56-
System.Data.Common.DbParameter[] parameters = new System.Data.Common.DbParameter[1];
57-
parameters[0] = cmd.GetStringParameter("aGrouping", groupingId);
53+
DbParameter[] parameters = GetParameters(groupingId, cmd);
54+
DataSet ExistsInLangDS = cmd.ExecuteSelect(ExistsInLangQuery, parameters);
55+
var availableLanguages = AbstractQueries.ParseLangs(ExistsInLangDS);
5856

59-
var groupingDS = cmd.ExecuteSelect(sqlGrouping, parameters);
60-
61-
//Oracle works without the 2 next lines, but mssql does not.
62-
parameters = new System.Data.Common.DbParameter[1];
63-
parameters[0] = cmd.GetStringParameter("aGrouping", groupingId);
57+
if (!availableLanguages.Contains(language))
58+
{
59+
throw new ApplicationException("Grouping " + groupingId + " not found for language " + language);
60+
}
6461

62+
string sqlValues = ValuesQueryByLanguage[language];
63+
parameters = GetParameters(groupingId, cmd);
6564
var valuesDS = cmd.ExecuteSelect(sqlValues, parameters);
6665

67-
//Oracle works without the 2 next lines, but mssql does not.
68-
parameters = new System.Data.Common.DbParameter[1];
69-
parameters[0] = cmd.GetStringParameter("aGrouping", groupingId);
70-
71-
DataSet extraLangsDS = String.IsNullOrEmpty(sqlGroupingExistsInLang) ? null : cmd.ExecuteSelect(sqlGroupingExistsInLang, parameters);
66+
List<GroupedValue> groupedValuesMaybe = ParseValues(groupingId, valuesDS);
7267

68+
Models.Grouping myOut = Queries.FixGrouping(language, groupingId, groupedValuesMaybe);
7369

74-
Models.Grouping grouping = Parse(groupingId, groupingDS, valuesDS, extraLangsDS);
70+
myOut.AvailableLanguages.AddRange(availableLanguages);
7571

76-
//Adding langs we know exists without checking the DB
77-
grouping.AvailableLanguages.Add(config.MainLanguage.code);
78-
if (!config.MainLanguage.code.Equals(language))
79-
{
80-
grouping.AvailableLanguages.Add(language);
81-
}
72+
return myOut;
73+
}
8274

83-
return grouping;
75+
private static DbParameter[] GetParameters(string groupingId, PxSqlCommandForTempTables cmd)
76+
{
77+
System.Data.Common.DbParameter[] parameters = new System.Data.Common.DbParameter[1];
78+
parameters[0] = cmd.GetStringParameter("aGrouping", groupingId);
79+
return parameters;
8480
}
8581

86-
private static string GetOtherLanguagesSql(AbstractQueries queries, string language, SqlDbConfig config, PxSqlCommand sqlCommand)
82+
private static string GetValuesetExistsInLangSql(AbstractQueries queries, PxSqlCommand sqlCommand)
8783
{
8884
string sqlGroupingExistsInLang = String.Empty;
8985
string glue = String.Empty;
9086
foreach (var lang in LanguagesInDbConfig)
9187
{
92-
if (!lang.Equals(config.MainLanguage.code) && !lang.Equals(language))
93-
{
94-
//skips: config.MainLanguage has to exist and language will fail in GetValueSetQuery if vaulset is not translated
88+
sqlGroupingExistsInLang += glue + queries.GetGroupingExistsIn(lang, sqlCommand);
89+
glue = " UNION ";
9590

96-
sqlGroupingExistsInLang += glue + queries.GetGroupingExistsIn(lang, sqlCommand);
97-
glue = " UNION ";
98-
}
9991
}
10092
return sqlGroupingExistsInLang;
101-
102-
10393
}
10494

105-
private static Models.Grouping Parse(string groupingId, DataSet groupingDS, DataSet valuesDS, DataSet extraLangsDS)
95+
private static List<GroupedValue> ParseValues(string groupingId, DataSet valuesDS)
10696
{
10797
//Make sure we have a grouping
108-
if (groupingDS.Tables.Count == 0 || groupingDS.Tables[0].Rows.Count < 1 || valuesDS.Tables.Count == 0)
98+
if (valuesDS.Tables.Count == 0)
10999
{
110-
throw new ApplicationException("Grouping " + groupingId + " not found or empty");
100+
throw new ApplicationException("Grouping " + groupingId + " empty");
111101
}
112102

113-
var grouping = new PCAxis.Sql.Models.Grouping();
114-
grouping.Id = groupingDS.Tables[0].Rows[0][0].ToString();
115-
grouping.Label = groupingDS.Tables[0].Rows[0][1].ToString();
116-
103+
List<GroupedValue> myOut = new List<GroupedValue>();
117104

118105
var values = new Dictionary<string, GroupedValue>();
106+
119107
for (int i = 0; i < valuesDS.Tables[0].Rows.Count; i++)
120108
{
121109
string groupCode = valuesDS.Tables[0].Rows[i][0].ToString();
@@ -130,24 +118,16 @@ private static Models.Grouping Parse(string groupingId, DataSet groupingDS, Data
130118
gValue = new GroupedValue();
131119
gValue.Code = groupCode;
132120
gValue.Text = valuesDS.Tables[0].Rows[i][2] == DBNull.Value ? valuesDS.Tables[0].Rows[i][3].ToString() : valuesDS.Tables[0].Rows[i][2].ToString();
133-
grouping.Values.Add(gValue);
121+
myOut.Add(gValue);
134122
values.Add(groupCode, gValue);
135123
}
136124
gValue.Codes.Add(valuesDS.Tables[0].Rows[i][1].ToString());
137125
}
138126

139-
if (extraLangsDS != null)
140-
{
141127

142-
for (int i = 0; i < extraLangsDS.Tables[0].Rows.Count; i++)
143-
{
144-
var lang = extraLangsDS.Tables[0].Rows[i][0].ToString();
145-
grouping.AvailableLanguages.Add(lang);
146-
}
147-
}
148128

149129

150-
return grouping;
130+
return myOut;
151131
}
152132

153133

0 commit comments

Comments
 (0)
Please sign in to comment.