Skip to content

Commit

Permalink
adding eventtags also to odhtags
Browse files Browse the repository at this point in the history
  • Loading branch information
RudiThoeni committed Nov 19, 2024
1 parent 9749372 commit 2ec0275
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 1 deletion.
29 changes: 29 additions & 0 deletions OdhApiImporter/Controllers/UpdateApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,35 @@ public async Task<IActionResult> ImportLTSEventTags(
}
}

//Imports all Event Tags
[Authorize(Roles = "DataPush")]
[HttpGet, Route("LTS/Event/Update/Tags/ToODHTags")]
public async Task<IActionResult> ImportLTSEventTagsToODHTags(
string id = null,
CancellationToken cancellationToken = default)
{
UpdateDetail updatedetail = default(UpdateDetail);
string operation = "Import LTS Events Tags";
string updatetype = GetUpdateType(null);
string source = "lts";
string otherinfo = "events.tags";

try
{
LTSApiEventTagsToODHTagImportHelper importhelper = new LTSApiEventTagsToODHTagImportHelper(settings, QueryFactory, "smgtags", UrlGeneratorStatic("LTS/Events/Tags"));

updatedetail = await importhelper.SaveDataToODH(null, null, cancellationToken);
var updateResult = GenericResultsHelper.GetSuccessUpdateResult(null, source, operation, updatetype, "Import LTS Events Tags succeeded", otherinfo, updatedetail, true);

return Ok(updateResult);
}
catch (Exception ex)
{
var updateResult = GenericResultsHelper.GetErrorUpdateResult(null, source, operation, updatetype, "Import LTS Events Tags data failed", otherinfo, updatedetail, ex, true);
return BadRequest(updateResult);
}
}

//Imports all Event Categories
[Authorize(Roles = "DataPush")]
[HttpGet, Route("LTS/Event/Update/Categories")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<UpdateDetail> SaveDataToODH(DateTime? lastchanged = null, List
//Import the List
var eventtags = await GetEventTagsFromLTSV2();
//Import Single Data & Deactivate Data
var result = await SaveEventTagsToPG(eventtags);
var result = await SaveEventTagsToPG(eventtags);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// SPDX-FileCopyrightText: NOI Techpark <digital@noi.bz.it>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

using Amazon.Auth.AccessControlPolicy;
using DataModel;
using Helper;
using Helper.Generic;
using LTSAPI;
using Microsoft.AspNetCore.Components.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NINJA.Parser;
using ServiceReferenceLCS;
using SqlKata.Execution;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace OdhApiImporter.Helpers.LTSAPI
{
public class LTSApiEventTagsToODHTagImportHelper : ImportHelper, IImportHelper
{
public LTSApiEventTagsToODHTagImportHelper(ISettings settings, QueryFactory queryfactory, string table, string importerURL) : base(settings, queryfactory, table, importerURL)
{

}

private async Task<List<JObject>> GetEventTagsFromLTSV2()
{
try
{
LtsApi ltsapi = new LtsApi(settings.LtsCredentials.serviceurl, settings.LtsCredentials.username, settings.LtsCredentials.password, settings.LtsCredentials.ltsclientid, false);
var qs = new LTSQueryStrings() { page_size = 100 };
var dict = ltsapi.GetLTSQSDictionary(qs);

var ltsdata = await ltsapi.EventTagRequest(dict, true);

return ltsdata;
}
catch (Exception ex)
{
WriteLog.LogToConsole("", "dataimport", "list.events.tags", new ImportLog() { sourceid = "", sourceinterface = "lts.events.tags", success = false, error = ex.Message });
return null;
}
}

public async Task<UpdateDetail> SaveDataToODH(DateTime? lastchanged = null, List<string>? idlist = null, CancellationToken cancellationToken = default)
{
//Import the List
var eventtags = await GetEventTagsFromLTSV2();
//Import Single Data & Deactivate Data
var result = await SaveEventTagsToPG(eventtags);

return result;
}

private async Task<UpdateDetail> SaveEventTagsToPG(List<JObject> ltsdata)
{
var newimportcounter = 0;
var updateimportcounter = 0;
var errorimportcounter = 0;
var deleteimportcounter = 0;

if (ltsdata != null)
{
List<string> idlistlts = new List<string>();

List<LTSEventTag> eventtagdata = new List<LTSEventTag>();

foreach (var ltsdatasingle in ltsdata)
{
eventtagdata.AddRange(ltsdatasingle["data"].ToObject<IList<LTSEventTag>>());
}

foreach (var data in eventtagdata)
{
string id = data.rid;

//See if data exists
var query = QueryFactory.Query("smgtags")
.Select("data")
.Where("id", id);

var objecttosave = await query.GetObjectSingleAsync<ODHTagLinked>();

if (objecttosave == null)
objecttosave = new ODHTagLinked();

objecttosave.Id = data.rid;
objecttosave.DisplayAsCategory = false;
objecttosave.FirstImport = objecttosave.FirstImport == null ? DateTime.Now : objecttosave.FirstImport;
objecttosave.LastChange = data.lastUpdate;

objecttosave.Source = new List<string>() { "LTSTag" };
objecttosave.TagName = data.name;

objecttosave.MainEntity = "event";
objecttosave.ValidForEntity = new List<string>() { "event" };
objecttosave.Shortname = objecttosave.TagName.ContainsKey("en") ? objecttosave.TagName["en"] : objecttosave.TagName.FirstOrDefault().Value;

objecttosave.IDMCategoryMapping = null;
objecttosave.PublishDataWithTagOn = null;
objecttosave.Mapping = new Dictionary<string, IDictionary<string, string>>() { { "lts", new Dictionary<string, string>() { { "rid", data.rid }, { "code", data.code } } } };
objecttosave.LTSTaggingInfo = null;
objecttosave.PublishedOn = null;
objecttosave.MappedTagIds = null;


var result = await InsertDataToDB(objecttosave, data);

newimportcounter = newimportcounter + result.created ?? 0;
updateimportcounter = updateimportcounter + result.updated ?? 0;
errorimportcounter = errorimportcounter + result.error ?? 0;

idlistlts.Add(id);

WriteLog.LogToConsole(id, "dataimport", "single.events.tags", new ImportLog() { sourceid = id, sourceinterface = "lts.events.tags", success = true, error = "" });
}

if (idlistlts.Count > 0)
{

// var query =
// QueryFactory.Query("smgtags")
//.Select("id")
//.ODHTagSourcesFilter_GeneratedColumn(new List<string>() { "LTSTag" });

// var idlistdb = await query.GetAsync<string>();

// var idstodelete = idlistdb.Where(p => !idlistlts.Any(p2 => p2 == p));

// foreach (var idtodelete in idstodelete)
// {
// var deletedisableresult = await DeleteOrDisableData<ODHTagLinked>(idtodelete, false);

// if (deletedisableresult.Item1 > 0)
// WriteLog.LogToConsole(idtodelete, "dataimport", "single.events.tags.deactivate", new ImportLog() { sourceid = idtodelete, sourceinterface = "lts.events.tags", success = true, error = "" });
// else if (deletedisableresult.Item2 > 0)
// WriteLog.LogToConsole(idtodelete, "dataimport", "single.events.tags.delete", new ImportLog() { sourceid = idtodelete, sourceinterface = "lts.events.tags", success = true, error = "" });


// deleteimportcounter = deleteimportcounter + deletedisableresult.Item1 + deletedisableresult.Item2;
// }
}
}
else
errorimportcounter = 1;

return new UpdateDetail() { updated = updateimportcounter, created = newimportcounter, deleted = deleteimportcounter, error = errorimportcounter };
}

private async Task<PGCRUDResult> InsertDataToDB(ODHTagLinked objecttosave, LTSEventTag eventtag)
{
try
{
//Set LicenseInfo
objecttosave.LicenseInfo = Helper.LicenseHelper.GetLicenseInfoobject(objecttosave, Helper.LicenseHelper.GetLicenseforODHTag);

//Setting MetaInfo (we need the MetaData Object in the PublishedOnList Creator)
objecttosave._Meta = MetadataHelper.GetMetadataobject(objecttosave);

//Set PublishedOn
objecttosave.PublishedOn = new List<string>() { "idm-marketplace" };

return await QueryFactory.UpsertData<ODHTagLinked>(objecttosave, new DataInfo("smgtags", CRUDOperation.Create), new EditInfo("importer", "lts"), new CRUDConstraints(), new CompareConfig(false, false));
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
6 changes: 6 additions & 0 deletions RAVEN/TransformToPGObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ public static EventLinked GetEventPGObject(EventRaven data)
if (eventlinked.Tags == null)
eventlinked.Tags = new List<Tags>();

//Compatibility
if(eventlinked.SmgTags == null)
eventlinked.SmgTags = new List<string>();

//New Add Topics to Tags
foreach (var topic in data.Topics)
{
Expand All @@ -593,6 +597,8 @@ public static EventLinked GetEventPGObject(EventRaven data)
foreach (var ltstag in data.LTSTags)
{
eventlinked.TagIds.Add(ltstag.LTSRID);
//Compatibility
eventlinked.SmgTags.Add(ltstag.LTSRID);

Tags tag = new Tags();
tag.Source = "lts";
Expand Down

0 comments on commit 2ec0275

Please sign in to comment.