Skip to content

Commit 75e3432

Browse files
Merge pull request #155 from PxTools/feature/metaid-adjustment
Adjustment of metaid feature, including a bugfix
2 parents 31dfb77 + a58b3c2 commit 75e3432

5 files changed

Lines changed: 93 additions & 13 deletions

File tree

PCAxis.Serializers/Util/Metaid/Link.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ public class Link
2525
/// What type of information is the link to: about-statistics, statistics-homepage, definition-classification, definition-value ,,,
2626
/// </summary>
2727
public string Relation { get; set; }
28+
29+
/// <summary>
30+
/// MetaId that was the source for this Link
31+
/// </summary>
32+
public string MetaId { get; set; }
2833
}
2934
}

PCAxis.Serializers/Util/Metaid/LinkTemplate.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using PCAxis.Serializers.Util.MetaId;
55

6+
67
namespace PCAxis.Serializers.Util.Metaid
78
{
89
/// <summary>
@@ -61,15 +62,32 @@ public bool Match(string metaIdString)
6162
/// <param name="textParams"></param>
6263
/// <param name="linkParams"></param>
6364
/// <returns></returns>
64-
public Link GetFormattedLink(string[] textParams, string[] linkParams)
65+
public Link GetFormattedLink(string[] textParams, string[] linkParams, string metaId)
6566
{
6667
Link link = new Link();
6768
link.Relation = this.LinkRelation;
6869
link.Type = this.LinkType;
69-
70-
link.Url = String.Format(this.LinkUrlFormat, linkParams);
71-
link.Label = String.Format(this.LinkTextFormat, textParams);
70+
link.Url = FormatText(this.LinkUrlFormat, linkParams);
71+
link.Label = FormatText(this.LinkTextFormat, textParams);
72+
link.MetaId = metaId;
7273
return link;
7374
}
75+
76+
77+
private static string FormatText(string text, string[] formatParams)
78+
{
79+
if (String.IsNullOrEmpty(text))
80+
{
81+
return text;
82+
}
83+
try
84+
{
85+
return String.Format(text, formatParams);
86+
}
87+
catch (Exception e)
88+
{
89+
return e.Message;
90+
}
91+
}
7492
}
7593
}

PCAxis.Serializers/Util/Metaid/MetaIdResolverStatic.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static class MetaIdResolverStatic
2727
/// <summary>
2828
/// Character that separates the systems within a META-ID
2929
/// </summary>
30-
private static readonly char[] _systemSeparator = { ',', ' ' };
30+
public static readonly char[] _systemSeparator = { ',', ' ' };
3131

3232
/// <summary>
3333
/// Character that separates the parameters within a system META-ID
@@ -142,13 +142,13 @@ private static List<Link> GetLinks(string metaIdList, List<LinkTemplate> templat
142142
{
143143
if (!template.Match(metaId))
144144
{
145-
break;
145+
continue;
146146
}
147147

148148
string rawParamsString = metaId.Replace(template.MetaSysId, "");
149149
string[] linkParams = rawParamsString.Split(_paramSeparator, StringSplitOptions.RemoveEmptyEntries);
150150

151-
myOut.Add(template.GetFormattedLink(textParams, linkParams));
151+
myOut.Add(template.GetFormattedLink(textParams, linkParams, metaId));
152152

153153
}
154154
}

UnitTests/Util/Metaid/UnitTest1.cs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ namespace UnitTests.Util.Metaid
99
[TestClass]
1010
public class UnitTest1
1111
{
12-
13-
1412
public UnitTest1()
1513
{
16-
1714
}
1815

1916
[TestMethod]
@@ -111,16 +108,64 @@ public void TestMissing()
111108
public void TestMulti()
112109
{
113110
// both comma and space as separators
114-
string metaid_raw = "urn:ssb:classification:klass:1 , urn:ssb:classification:klass:2 urn:ssb:classification:klass:3 ";
111+
string metaid_raw = "urn:ssb:classification:klass:1 ,urn:ssb:conceptvariable:vardok:2 urn:ssb:classification:klass:3 ";
115112
string expectedUrl_0 = "https://www.ssb.no/en/klass/klassifikasjoner/1";
116-
string expectedUrl_1 = "https://www.ssb.no/en/klass/klassifikasjoner/2";
113+
string expectedUrl_1 = "https://www.ssb.no/a/metadata/conceptvariable/vardok/2/en";
117114
string expectedUrl_2 = "https://www.ssb.no/en/klass/klassifikasjoner/3";
118115

116+
string expectedMataid_2 = "urn:ssb:classification:klass:3";
117+
119118
List<Link> links = MetaIdResolverStatic.GetVariableLinks(metaid_raw, "en", "region");
120119
Assert.AreEqual(3, links.Count);
121120
Assert.AreEqual(expectedUrl_0, links[0].Url);
122121
Assert.AreEqual(expectedUrl_1, links[1].Url);
123122
Assert.AreEqual(expectedUrl_2, links[2].Url);
123+
Assert.AreEqual(expectedMataid_2, links[2].MetaId);
124+
}
125+
126+
[TestMethod]
127+
[DeploymentItem("Util/Metaid/metaid.config")]
128+
public void TestNoLabel()
129+
{
130+
string metaid_raw = "NO_LABEL:1";
131+
string expectedUrl = "https://www.ssb.no/en/nice-document1";
132+
string expectedLabel = "";
133+
134+
135+
List<Link> links = MetaIdResolverStatic.GetTableLinks(metaid_raw, "en");
136+
Assert.AreEqual(1, links.Count);
137+
Assert.AreEqual(expectedUrl, links[0].Url);
138+
Assert.AreEqual(expectedLabel, links[0].Label);
139+
}
140+
141+
[TestMethod]
142+
[DeploymentItem("Util/Metaid/metaid.config")]
143+
public void TestAnyUrl()
144+
{
145+
// possible but is it a good idea?
146+
string metaid_raw = "ANY_URL://www.ssb.no/en/nice-document2";
147+
string expectedUrl = "https://www.ssb.no/en/nice-document2";
148+
string expectedLabel = "";
149+
150+
List<Link> links = MetaIdResolverStatic.GetTableLinks(metaid_raw, "en");
151+
Assert.AreEqual(1, links.Count);
152+
Assert.AreEqual(expectedUrl, links[0].Url);
153+
Assert.AreEqual(expectedLabel, links[0].Label);
154+
}
155+
156+
157+
[TestMethod]
158+
[DeploymentItem("Util/Metaid/metaid.config")]
159+
public void TestTooFewParams()
160+
{
161+
// exception text is passed when config or metaid dont fit.
162+
// throwing an exception seems to much
163+
string metaid_raw = "urn:ssb:contextvariable:common:3";
164+
string expectedUrl = "Index (zero based) must be greater than or equal to zero and less than the size of the argument list.";
165+
166+
List<Link> links = MetaIdResolverStatic.GetValueLinks(metaid_raw, "en", "region", "some value");
167+
Assert.AreEqual(1, links.Count);
168+
Assert.AreEqual(expectedUrl, links[0].Url);
124169

125170
}
126171
}

UnitTests/Util/Metaid/metaid.config

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@
1010
<link pxLang="no" labelStringFormat="Om statistikken" urlStringFormat="https://www.ssb.no/{0}#om-statistikken" />
1111
<link pxLang="en" labelStringFormat="About the statistics" urlStringFormat="https://www.ssb.no/en/{0}#om-statistikken" />
1212
</relationalGroup>
13-
</metaSystem>
13+
</metaSystem>
14+
<metaSystem id="NO_LABEL">
15+
<relationalGroup relation="test-no-label" type="text/html">
16+
<link pxLang="no" labelStringFormat="" urlStringFormat="https://www.ssb.no/nice-document{0}" />
17+
<link pxLang="en" labelStringFormat="" urlStringFormat="https://www.ssb.no/en/nice-document{0}" />
18+
</relationalGroup>
19+
</metaSystem>
20+
<metaSystem id="ANY_URL">
21+
<relationalGroup relation="any-url" type="text/html">
22+
<link pxLang="no" labelStringFormat="" urlStringFormat="https:{0}" />
23+
<link pxLang="en" labelStringFormat="" urlStringFormat="https:{0}" />
24+
</relationalGroup>
25+
</metaSystem>
1426
</onTable>
1527
<onVariable>
1628
<metaSystem id="urn:ssb:classification:klass">

0 commit comments

Comments
 (0)