Skip to content

Commit

Permalink
Add comments; add filter by metadata structure id BEXIS2#991 BEXIS2#986
Browse files Browse the repository at this point in the history
  • Loading branch information
geofranzi committed Oct 24, 2022
1 parent e7e6ac8 commit a2d9eeb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
Expand Up @@ -20,16 +20,18 @@ public class MetadataStatisticOutController : ApiController
/// <summary>
/// Get a list of unique metadata values, count and list of occurrence
/// </summary>
/// <remarks>Data is directly returned from the database without (currently) any further permission check. Nested nodes are cuurently not supported.</remarks>
/// <returns>JSON object</returns>
[BExISApiAuthorize]
[PostRoute("api/MetadataStatistic")]
public HttpResponseMessage Post([FromBody] PostApiMetadataStatisticModel data)
{
{

// #TODO add security
// string token = this.Request.Headers.Authorization?.Parameter;

var result = UniqueValuesByXPATH(data.xpath);
if (data.xpath.Length == 0) return Request.CreateResponse(HttpStatusCode.PreconditionFailed, "xpath is empty, but required");

var result = UniqueValuesByXPATH(data.xpath, data.datasetIds, data.metadatastructureIds);

// JSON is returned, but not correctly detected within the repsonse function.
// Workaround: Deserialized and Serialize from and to JSON before
Expand All @@ -43,18 +45,24 @@ public HttpResponseMessage Post([FromBody] PostApiMetadataStatisticModel data)
return response;
}

private object UniqueValuesByXPATH(string xpath)
private object UniqueValuesByXPATH(string xpath, string[] datatsetIDs = null, string[] metadatastructureIds = null)
{
StringBuilder mvBuilder = new StringBuilder();
string whereNotIn = "";
/* if (notIn.Count > 0)
{
whereNotIn = "and datasetref no in(" + string.Join(",", notIn) + ") ";
}
*/

string datasetIDsIN = "";
if (datatsetIDs != null )
{
datasetIDsIN = "and datasetref in(" + String.Join(",", datatsetIDs) + ")";
}

string metadatastructureIdsIN = "";
if (metadatastructureIds != null)
{
metadatastructureIdsIN = "and (xpath('/Metadata/@id', metadata))[1]::text::integer in(" + String.Join(",", metadatastructureIds) + ")";
}

// example xpath: " /Metadata/contacts/contactsType/contactPerson/contactType/institute/instituteType/"
mvBuilder.AppendLine(string.Format("with result as (select(xpath(\'{0}/text()\', metadata))[1]::text as xpath,json_build_object(\'data\', json_agg(to_jsonb(row(b.datasetref, b.title))), \'count\', count(id)) as json from datasetversions as b where status = 2 {1} group by xpath order by xpath asc) select json_object_agg(coalesce(xpath, \'null\'), result.json) from result; ", xpath, whereNotIn));
mvBuilder.AppendLine(string.Format("with result as (select(xpath(\'{0}/text()\', metadata))[1]::text as xpath,json_build_object(\'data\', json_agg(to_jsonb(row(b.datasetref, b.title))), \'count\', count(id)) as json from datasetversions as b where status = 2 {1} {2} group by xpath order by xpath asc) select json_object_agg(coalesce(xpath, \'null\'), result.json) from result; ", xpath, datasetIDsIN, metadatastructureIdsIN));
// execute the statement
try
{
Expand Down
8 changes: 7 additions & 1 deletion Console/BExIS.Web.Shell/Areas/DIM/Models/API/ApiModels.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class ApiDatasetModel
public string VersionPublicAccessDate { get; set; }
public Dictionary<string, string> AdditionalInformations { get; set; }
public Dictionary<string, Dictionary<string, string>> Parties { get; set; }
public DateTime VersionDate { get; set; }
public string VersionDate { get; set; }
public object Names { get; internal set; }

public ApiDatasetModel()
Expand Down Expand Up @@ -87,8 +88,13 @@ public class ApiDataStatisticModel
public DataTable missingValues { get; set; }

}

public class PostApiMetadataStatisticModel
{
public string xpath { get; set; }
public string[] datasetIds { get; set; }
public string[] metadatastructureIds { get; set; }
public string regex { get; set; }

}
}

0 comments on commit a2d9eeb

Please sign in to comment.