Skip to content

Commit 005e583

Browse files
Use current logged in user and server information for API access done #50
1 parent 5b16222 commit 005e583

4 files changed

Lines changed: 124 additions & 65 deletions

File tree

Controllers/LUICalculationController.cs

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using BExIS.Utils.Extensions;
2121
using Vaiona.Web.Mvc.Modularity;
2222
using System.Web.Routing;
23+
using Microsoft.AspNet.Identity;
2324

2425
namespace BExIS.Modules.Lui.UI.Controllers
2526
{
@@ -47,9 +48,9 @@ public ActionResult Index()
4748

4849
//create model
4950
LUIQueryModel model = new LUIQueryModel();
50-
model.MissingComponentData = DataAccess.GetMissingComponentData();
51+
model.MissingComponentData = DataAccess.GetMissingComponentData(GetServerInformation());
5152
model.NewComponentsSetDatasetId = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
52-
model.NewComponentsSetDatasetVersion = DataAccess.GetDatasetInfo(model.NewComponentsSetDatasetId).Version;
53+
model.NewComponentsSetDatasetVersion = DataAccess.GetDatasetInfo(model.NewComponentsSetDatasetId, GetServerInformation()).Version;
5354
return View("Index", model);
5455

5556
}
@@ -69,7 +70,7 @@ public ActionResult ShowPrimaryData(long datasetID)
6970
Session["LUICalModel"] = lUIQueryModel;
7071

7172
DataModel model = new DataModel();
72-
model.Data = DataAccess.GetComponentData(datasetID.ToString());
73+
model.Data = DataAccess.GetComponentData(datasetID.ToString(), GetServerInformation());
7374

7475
return PartialView("_data", model);
7576
}
@@ -105,7 +106,19 @@ public ActionResult CalculateLUI(LUIQueryModel model)
105106
Session["DataStructureId"] = selectedDataStructureId;
106107

107108
// do the calucaltion
108-
var results = CalculateLui.DoCalc(model);
109+
// source data
110+
string dsId = "";
111+
switch (model.ComponentsSet.SelectedValue)
112+
{
113+
case "old components set":
114+
dsId = Models.Settings.get("lui:datasetOldComponentsSet").ToString();
115+
break;
116+
case "new components set":
117+
dsId = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
118+
break;
119+
}
120+
DataTable dt_sourceData = DataAccess.GetComponentData(dsId, GetServerInformation());
121+
var results = CalculateLui.DoCalc(model, dt_sourceData);
109122

110123
DataModel dataModel = new DataModel();
111124
dataModel.Data = results;
@@ -142,7 +155,7 @@ public ActionResult PrepareDownloadFile(string mimeType)
142155
LUIQueryModel model = (LUIQueryModel)Session["LUICalModel"];
143156

144157
if (model.RawVsCalc.SelectedValue == "unstandardized")
145-
downloadData = DataAccess.GetComponentData(model.DownloadDatasetId);
158+
downloadData = DataAccess.GetComponentData(model.DownloadDatasetId, GetServerInformation());
146159
else
147160
downloadData = Session[SESSION_TABLE] as DataTable;
148161

@@ -177,7 +190,7 @@ public ActionResult PrepareDownloadFile(string mimeType)
177190
//XmlDocument xmlDocument = DataAccess.GetMetadata(model.DownloadDatasetId);
178191

179192
//string htmlPage = PartialView("SimpleMetadata", xmlDocument).RenderToString();
180-
DatasetObject datasetObject = DataAccess.GetDatasetInfo(model.DownloadDatasetId);
193+
DatasetObject datasetObject = DataAccess.GetDatasetInfo(model.DownloadDatasetId, GetServerInformation());
181194
var view = this.Render("DCM", "Form", "LoadMetadataOfflineVersion", new RouteValueDictionary()
182195
{
183196
{ "entityId", long.Parse(model.DownloadDatasetId) },
@@ -192,11 +205,11 @@ public ActionResult PrepareDownloadFile(string mimeType)
192205
string pathHtml = downloadManager.GenerateHtmlFile(view.ToHtmlString(), model.DownloadDatasetId + "_metadata");
193206

194207
//get missing data when relavent
195-
List<MissingComponentData> missingComponentData = DataAccess.GetMissingComponentData();
208+
List<MissingComponentData> missingComponentData = DataAccess.GetMissingComponentData(GetServerInformation());
196209
string pathMissingData = "";
197210
if (missingComponentData.Count > 0)
198211
{
199-
string version = DataAccess.GetDatasetInfo(model.DownloadDatasetId).Version;
212+
string version = DataAccess.GetDatasetInfo(model.DownloadDatasetId, GetServerInformation()).Version;
200213
string filenameMissingdata = model.DownloadDatasetId + "_" + "Version_" + version + "_" + "MissingComponentData";
201214
pathMissingData = downloadManager.GernateMissingDataFile(missingComponentData, filenameMissingdata);
202215
}
@@ -269,7 +282,7 @@ public ActionResult DownloadFile(string mimeType)
269282
else
270283
datasetId = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
271284

272-
string version = DataAccess.GetDatasetInfo(datasetId).Version;
285+
string version = DataAccess.GetDatasetInfo(datasetId, GetServerInformation()).Version;
273286

274287
string text = "LUI Calculation file <b>\"" + Path.GetFileName(pathData) + "\"</b> with id <b>(" + datasetId + ")</b> version <b>(" + version + ")</b> was downloaded by <b>" + user + "</b>";
275288
es.Send("LUI data was downloaded (Id: " + datasetId + ", Version: " + version + ")", text, "bexis-sys@listserv.uni-jena.de");
@@ -300,15 +313,15 @@ private bool checkPreconditions()
300313
// check for LUI new dataset
301314
bool exists = false;
302315
string luiIdNew = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
303-
var dataNew = DataAccess.GetComponentData(luiIdNew);
316+
var dataNew = DataAccess.GetComponentData(luiIdNew, GetServerInformation());
304317
if (dataNew.Rows.Count == 0)
305318
return exists == false;
306319

307320
string dsdId = Models.Settings.get("lui:datastructureNewComponentsSet").ToString();
308321

309322
// check for LUI old dataset
310323
string luiIdOld = Models.Settings.get("lui:datasetOldComponentsSet").ToString();
311-
var dataOld = DataAccess.GetComponentData(luiIdOld);
324+
var dataOld = DataAccess.GetComponentData(luiIdOld, GetServerInformation());
312325
if (dataOld.Rows.Count == 0)
313326
return exists == false;
314327

@@ -317,6 +330,45 @@ private bool checkPreconditions()
317330
// if we came that far, all conditions are met
318331
return true;
319332
}
333+
334+
/// <summary>
335+
/// Get current server und user information
336+
/// </summary>
337+
/// <returns></returns>
338+
public ServerInformation GetServerInformation()
339+
{
340+
//string filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("LUI"), "Credentials.json");
341+
//string text = System.IO.File.ReadAllText(filePath);
342+
ServerInformation serverInformation = new ServerInformation();
343+
var uri = System.Web.HttpContext.Current.Request.Url;
344+
serverInformation.ServerName = uri.GetLeftPart(UriPartial.Authority);
345+
serverInformation.Token = GetUserToken();
346+
347+
348+
return serverInformation;
349+
}
350+
private string GetUserToken()
351+
{
352+
var identityUserService = new IdentityUserService();
353+
var userManager = new UserManager();
354+
355+
try
356+
{
357+
long userId = 0;
358+
long.TryParse(this.User.Identity.GetUserId(), out userId);
359+
360+
var user = identityUserService.FindById(userId);
361+
362+
user = identityUserService.FindById(userId);
363+
var token = userManager.GetTokenAsync(user).Result;
364+
return token;
365+
}
366+
finally
367+
{
368+
identityUserService.Dispose();
369+
userManager.Dispose();
370+
}
371+
}
320372
}
321373

322374
}

Controllers/LUIComponentCalculationController.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using BExIS.Modules.Lui.UI.Helper;
22
using BExIS.Modules.Lui.UI.Models;
3+
using BExIS.Security.Services.Subjects;
4+
using Microsoft.AspNet.Identity;
35
using System;
46
using System.Collections.Generic;
57
using System.Data;
@@ -27,9 +29,9 @@ public ActionResult CalculateCompontents()
2729
Session["ComponentData"] = null;
2830
string datasetId = Models.Settings.get("lui:lanuDataset").ToString();
2931
//get data structureId
30-
long structureId = long.Parse(DataAccess.GetDatasetInfo(datasetId).DataStructureId, CultureInfo.InvariantCulture);
32+
long structureId = long.Parse(DataAccess.GetDatasetInfo(datasetId, GetServerInformation()).DataStructureId, CultureInfo.InvariantCulture);
3133

32-
DataTable lanuFullData = DataAccess.GetData(datasetId, structureId);
34+
DataTable lanuFullData = DataAccess.GetData(datasetId, structureId, GetServerInformation());
3335

3436
//get only last year
3537
var lastYear = lanuFullData.AsEnumerable().Select(a => a.Field<DateTime>("Year")).Distinct().ToList().Max();
@@ -39,8 +41,8 @@ public ActionResult CalculateCompontents()
3941
//get plottype infos
4042
string datasetIdPlots = Models.Settings.get("lui:epPlotsDataset").ToString();
4143
//get data structureId
42-
long structureIdPlots = long.Parse(DataAccess.GetDatasetInfo(datasetIdPlots).DataStructureId, CultureInfo.InvariantCulture);
43-
DataTable plotTypes = DataAccess.GetData(datasetIdPlots, structureIdPlots);
44+
long structureIdPlots = long.Parse(DataAccess.GetDatasetInfo(datasetIdPlots, GetServerInformation()).DataStructureId, CultureInfo.InvariantCulture);
45+
DataTable plotTypes = DataAccess.GetData(datasetIdPlots, structureIdPlots, GetServerInformation());
4446

4547
LUIComponentsCalculation lUIComponentsCalculation = new LUIComponentsCalculation(lanuData, lanuFullData, plotTypes);
4648
DataTable compData = lUIComponentsCalculation.CalculateComponents();
@@ -139,17 +141,57 @@ public ActionResult UploadSelectedRows(int[] rowIds)
139141
}
140142

141143
//upload
142-
result = DataAccess.Upload(model);
144+
result = DataAccess.Upload(model, GetServerInformation());
143145
}
144146

145147
return Content(result);
146148
}
147149

150+
private string GetUserToken()
151+
{
152+
var identityUserService = new IdentityUserService();
153+
var userManager = new UserManager();
154+
155+
try
156+
{
157+
long userId = 0;
158+
long.TryParse(this.User.Identity.GetUserId(), out userId);
159+
160+
var user = identityUserService.FindById(userId);
161+
162+
user = identityUserService.FindById(userId);
163+
var token = userManager.GetTokenAsync(user).Result;
164+
return token;
165+
}
166+
finally
167+
{
168+
identityUserService.Dispose();
169+
userManager.Dispose();
170+
}
171+
}
172+
173+
/// <summary>
174+
/// Get server information form json file in workspace
175+
/// </summary>
176+
/// <returns></returns>
177+
public ServerInformation GetServerInformation()
178+
{
179+
//string filePath = Path.Combine(AppConfiguration.GetModuleWorkspacePath("LUI"), "Credentials.json");
180+
//string text = System.IO.File.ReadAllText(filePath);
181+
ServerInformation serverInformation = new ServerInformation();
182+
var uri = System.Web.HttpContext.Current.Request.Url;
183+
serverInformation.ServerName = uri.GetLeftPart(UriPartial.Authority) + "/";
184+
serverInformation.Token = GetUserToken();
185+
186+
187+
return serverInformation;
188+
}
189+
148190

149191
private int[] CheckDuplicates(DataTable newCompData, int[] rowIds)
150192
{
151193
string luiIdNew = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
152-
DataTable allCompData = DataAccess.GetComponentData(luiIdNew);
194+
DataTable allCompData = DataAccess.GetComponentData(luiIdNew, GetServerInformation());
153195

154196
List<int> noDuplicates = new List<int>();
155197
foreach (int id in rowIds)

Helper/CalculateLui.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,13 @@ public enum ComponentsSet
1515
New
1616
};
1717

18-
public static DataTable DoCalc(LUIQueryModel model)
18+
public static DataTable DoCalc(LUIQueryModel model, DataTable dt_sourceData)
1919
{
2020
// -----------------------------------------------------------------------------------------
2121
// initiate some neede variables
2222
//
2323
//
2424

25-
// source data
26-
string dsId = "";
27-
switch (model.ComponentsSet.SelectedValue)
28-
{
29-
case "old components set":
30-
dsId = Settings.get("lui:datasetOldComponentsSet").ToString();
31-
break;
32-
case "new components set":
33-
dsId = Settings.get("lui:datasetNewComponentsSet").ToString();
34-
break;
35-
}
36-
DataTable dt_sourceData = DataAccess.GetComponentData(dsId);
37-
3825
// result data
3926
DataTable dt_rslts = MakeResultsDT();
4027
// clear result data before filling it again

0 commit comments

Comments
 (0)