Skip to content

Commit

Permalink
Merge pull request #68 from statisticssweden/Feature/PBI179793_UseMen…
Browse files Browse the repository at this point in the history
…uTitle

Feature/pbi179793 use menu title
  • Loading branch information
KentMossback authored Mar 7, 2024
2 parents 6dbbc68 + 6f8169e commit b4b1169
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 148 deletions.
128 changes: 22 additions & 106 deletions Px.Dcat/DataCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.IdentityModel.Tokens;

namespace Px.Dcat
{
Expand Down Expand Up @@ -307,122 +308,35 @@ private List<string> getDescriptions(PXMeta meta, List<string> langs)
/// <summary>
/// Gets the title for each language
/// </summary>
/// <param name="Selection"></param>
/// <param name="meta">Metadata of table</param>
/// <param name="langs">List of languages</param>
/// <param name="path">List of pxmenuitems</param>
/// <returns>Return titles in each language for a table</returns>
private List<string> getTitles(PXMeta meta, List<string> langs)
private List<string> getTitles(string Selection, PXMeta meta, List<string> langs, List<PxMenuItem> path)
{
List<string> titles = new List<string>(langs.Count());
PxMenuItem item = path.LastOrDefault();

foreach (string lang in langs)
{
meta.SetLanguage(lang);
titles.Add(getTitleWithInterval(meta));
}
return titles;
}

/// <summary>
/// Adds time interval to the table title
/// </summary>
/// <param name="meta">Metadata of table</param>
/// <returns>Title string with interval</returns>
private string getTitleWithInterval(PXMeta meta)
{
Variable timeVar = meta.Variables.FirstOrDefault(x => x.IsTime);
string startTime = "";
string endTime = "";
StringBuilder sb = new StringBuilder();

if (timeVar != null)
{
startTime = GetFirstTimePeriod(timeVar);
endTime = GetLastTimePeriod(timeVar);
}

sb.Append(meta.Title);

if (IsInteger(meta.Title[meta.Title.Length - 1].ToString())) //Title ends with a number, add nothing
{
return sb.ToString();
}
if (string.IsNullOrEmpty(startTime) || string.IsNullOrEmpty(endTime)) //No starttime or endtime, add nothing
{
return sb.ToString();
}
if (meta.Title.EndsWith("-"))//Title ends with a dash, only endtime should be added
{
sb.Append(endTime);
return sb.ToString();
}
if (startTime == endTime) //Starttime and Endtime are the same, only starttime should be added
{
sb.Append(" ");
sb.Append(startTime);
return sb.ToString();
}

if (startTime.Contains("-"))
{
sb.Append(" (");
sb.Append(startTime);
sb.Append(")-(");
sb.Append(endTime);
sb.Append(")");
}
else
{
sb.Append(" ");
sb.Append(startTime);
sb.Append("-");
sb.Append(endTime);
}

return sb.ToString();
}

private string GetFirstTimePeriod(Variable variable)
{
string first = "";

if (variable.Values.Count > 0)
{
first = variable.Values.First().Text;
string val2 = variable.Values.Last().Text;

if (string.CompareOrdinal(first, val2) > 0)

var titleItem = getMenuInLanguage(item, lang);

var title = titleItem.SubItems.FirstOrDefault(x => x.ID.Selection == Selection);
if (title != null && !title.Text.IsNullOrEmpty() )
{
first = val2;
meta.SetLanguage(lang);
titles.Add(title.Text);
}
}

return first;
}

private string GetLastTimePeriod(Variable variable)
{
string last = "";

if (variable.Values.Count > 0)
{
last = variable.Values.Last().Text;
string val2 = variable.Values.First().Text;

if (string.CompareOrdinal(last, val2) < 0)
else
{
last = val2;
titles.Add("TABLE_HAS_NO_TITLE");
}
}

return last;
}

private static bool IsInteger(string value)
{
int outValue;

return int.TryParse(value, out outValue);
return titles;
}

/// <summary>
/// Gets update frequency
/// </summary>
Expand Down Expand Up @@ -709,7 +623,8 @@ private List<Keyword> getKeywords(List<PxMenuItem> path, string lang)
}
return keywords;
}



/// <summary>
/// Gets each keyword from a specific table
/// </summary>
Expand Down Expand Up @@ -845,12 +760,13 @@ private Dataset getDataset(string selection, PXMeta meta, List<PxMenuItem> path)
dataset.UpdateFrequency = getUpdateFrequency(meta);

List<string> langs = getLanguages(meta).Intersect(_settings.Languages).ToList();

dataset.Languages = langs;
dataset.LanguageURIs = convertLanguages(langs);

dataset.Descriptions = getDescriptions(meta, langs);
dataset.Titles = getTitles(meta, langs);

dataset.Titles = getTitles(selection, meta, langs, path);
dataset.ContactPersons = getContacts(meta);
dataset.Category = getCategory(path);

Expand Down
25 changes: 16 additions & 9 deletions Px.Dcat/DcatWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Px.Dcat.DataClasses;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml;

namespace Px.Dcat
Expand Down Expand Up @@ -200,18 +201,20 @@ public static XmlElement generateDataset(Dataset d)
{
// Dataset
XmlElement dElem = createElem("dcat", "Dataset", "rdf", "about", d.Resource);



// Titles and descriptions
for (int i = 0; i < d.Languages.Count; i++)
{
string title = d.Titles[i];
string lang = d.Languages[i];
string title = d.Titles[i];
string desc = d.Descriptions[i];

XmlElement titleElem = createElem("dcterms", "title", "xml", "lang", lang);
titleElem.InnerText = title;
dElem.AppendChild(titleElem);
if (title != "TABLE_HAS_NO_TITLE")
{
XmlElement titleElem = createElem("dcterms", "title", "xml", "lang", lang);
titleElem.InnerText = title;
dElem.AppendChild(titleElem);
}

XmlElement descElem = createElem("dcterms", "description", "xml", "lang", lang);
descElem.InnerText = desc;
Expand Down Expand Up @@ -342,9 +345,13 @@ public static XmlElement generateContact(ContactPerson cp)

// Email
string trimmedEmail = cp.Email.Replace(" ", "");
XmlElement emailElem = createElem("vcard", "hasEmail", "rdf", "resource", "mailto:" + trimmedEmail);
individual.AppendChild(emailElem);
var isEmail = Regex.IsMatch(trimmedEmail, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);

if (isEmail)
{
XmlElement emailElem = createElem("vcard", "hasEmail", "rdf", "resource", "mailto:" + trimmedEmail);
individual.AppendChild(emailElem);
}
// Phone
//XmlElement phoneElem = createElem("vcard", "hasTelephone");
//XmlElement descElem = createElem("dcterms", "description");
Expand All @@ -356,7 +363,7 @@ public static XmlElement generateContact(ContactPerson cp)
//descElem.AppendChild(phoneVal);
//individual.AppendChild(phoneElem);

return individual;
return individual;
}

/// <summary>
Expand Down
108 changes: 75 additions & 33 deletions Px.Dcat/Fetchers/CNMMFetcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PCAxis.Menu;
using Microsoft.IdentityModel.Tokens;
using PCAxis.Menu;
using PCAxis.Menu.Implementations;
using PCAxis.Paxiom;
using PCAxis.PlugIn.Sql;
Expand All @@ -9,54 +10,95 @@ namespace Px.Dcat.Fetchers
{
public class CNMMFetcher : IFetcher
{

/// <summary>
/// Retrieves the base item from the data model menu based on the provided parameters.
/// </summary>
/// <param name="nodeID">The ID of the node in the menu.</param>
/// <param name="menuID">The ID of the menu.</param>
/// <param name="lang">The language code.</param>
/// <param name="dbid">The ID of the database.</param>
/// <returns>The base item from the data model menu.</returns>
public Item GetBaseItem(string nodeID, string menuID, string lang, string dbid)
{
TableLink tblFix = null;

DatamodelMenu menu = ConfigDatamodelMenu.Create(
lang,
PCAxis.Sql.DbConfig.SqlDbConfigsStatic.DataBases[dbid],
m =>
{
m.NumberOfLevels = 5;
m.RootSelection = nodeID == "" ? new ItemSelection() : new ItemSelection(menuID, nodeID);
m.AlterItemBeforeStorage = item =>
lang,
PCAxis.Sql.DbConfig.SqlDbConfigsStatic.DataBases[dbid],
m =>
{
if (item is TableLink)
m.NumberOfLevels = 5;
m.RootSelection = nodeID == "" ? new ItemSelection() : new ItemSelection(menuID, nodeID);
m.AlterItemBeforeStorage = item =>
{
TableLink tbl = (TableLink)item;
if (string.Compare(tbl.ID.Selection, nodeID, true) == 0)
{
tblFix = tbl;
}
if (tbl.StartTime == tbl.EndTime)
{
tbl.Text = tbl.Text + " " + tbl.StartTime;
}
else
if (item is TableLink)
{
tbl.Text = tbl.Text + " " + tbl.StartTime + " - " + tbl.EndTime;
}
TableLink tbl = (TableLink)item;
if (tbl.Published.HasValue)
{
tbl.SetAttribute("Modified", tbl.Published.Value.ToShortDateString());
if (string.Compare(tbl.ID.Selection, nodeID, true) == 0)
{
tblFix = tbl;
}
if (!tbl.Text.IsNullOrEmpty() && ((!IsInteger(tbl.Text[tbl.Text.Length - 1].ToString()) ||
!string.IsNullOrEmpty(tbl.StartTime) ||
!string.IsNullOrEmpty(
tbl
.EndTime)))) //Title ends with a number, no start- or endtime, add nothing
{
if (tbl.Text.EndsWith("-")) //Title ends with a dash, only endtime should be added
{
tbl.Text = tbl.Text + " " + tbl.EndTime;
}
else if (tbl.StartTime == tbl.EndTime)
{
tbl.Text = tbl.Text + " " + tbl.StartTime;
}
else if (tbl.StartTime.Contains("-"))
{
tbl.Text = $"{tbl.Text} ({tbl.StartTime})-({tbl.EndTime})";
}
else
{
tbl.Text = tbl.Text + " " + tbl.StartTime + " - " + tbl.EndTime;
}
}
if (tbl.Published.HasValue)
{
tbl.SetAttribute("Modified", tbl.Published.Value.ToShortDateString());
}
}
}
if (string.IsNullOrEmpty(item.SortCode))
{
item.SortCode = item.Text;
}
};
});
if (string.IsNullOrEmpty(item.SortCode))
{
item.SortCode = item.Text;
}
};
});
return menu.CurrentItem;
}
/// <summary>
/// Retrieves the builder for the given selection.
/// </summary>
/// <param name="selection">The selection for the builder.</param>
/// <returns>The builder for the given selection.</returns>
public IPXModelBuilder GetBuilder(string selection)
{
IPXModelBuilder builder = new PXSQLBuilder();
builder.SetPath(selection);
return builder;
}

/// <summary>
/// Checks if a given value is an integer.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>True if the value is an integer, false otherwise.</returns>
private static bool IsInteger(string value)
{
int outValue;
return int.TryParse(value, out outValue);
}
}
}

0 comments on commit b4b1169

Please sign in to comment.