Skip to content

Commit fa6fcd1

Browse files
api access and ui implemented, new class for api models #46
1 parent e305831 commit fa6fcd1

5 files changed

Lines changed: 207 additions & 83 deletions

File tree

BExIS.Modules.Lui.UI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
<Compile Include="Helper\LUISeedDataGenerator.cs" />
193193
<Compile Include="Helper\Settings.cs" />
194194
<Compile Include="LUIModule.cs" />
195+
<Compile Include="Models\ApiModels.cs" />
195196
<Compile Include="Models\ComponentDataModel.cs" />
196197
<Compile Include="Models\DataModel.cs" />
197198
<Compile Include="Models\LUIQueryModel.cs" />

Controllers/LUIComponentCalculationController.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,43 @@ public ActionResult Download()
8080

8181
return File(storePath, MimeMapping.GetMimeMapping("ComponentData" + ".csv"), Path.GetFileName(storePath));
8282
}
83+
84+
public ActionResult UploadSelectedRows(int[] rowIds)
85+
{
86+
ComponentDataModel compData = Session["ComponentData"] as ComponentDataModel;
87+
88+
DataApiModel model = new DataApiModel();
89+
model.DatasetId = (long)Models.Settings.get("lui:datasetNewComponentsSet");
90+
model.DecimalCharacter = DecimalCharacter.point;
91+
92+
//get col names
93+
List<string> cols = new List<string>();
94+
foreach(DataColumn colum in compData.Data.Columns)
95+
{
96+
if (colum.ColumnName != "id")
97+
cols.Add(colum.ColumnName);
98+
}
99+
100+
model.Columns = cols.ToArray();
101+
102+
string[,] dataArray = new string[rowIds.Count(), cols.Count];
103+
List<string[]> dataArrays = new List<string[]>();
104+
if (rowIds != null)
105+
{
106+
foreach (int id in rowIds)
107+
{
108+
DataRow row = compData.Data.AsEnumerable().Where(a => a.Field<int>("id") == id).FirstOrDefault();
109+
string[] stringArray = row.ItemArray.Cast<string>().ToArray();
110+
dataArrays.Add(stringArray);
111+
}
112+
113+
model.Data = dataArrays.ToArray();
114+
}
115+
116+
//upload
117+
string result = DataAccess.Upload(model);
118+
119+
return Content(result);
120+
}
83121
}
84122
}

Helper/DataAccess.cs

Lines changed: 41 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.IO;
1010
using System.Linq;
1111
using System.Net;
12+
using System.Text;
1213
using System.Web;
1314
using System.Web.Script.Serialization;
1415
using System.Xml;
@@ -98,7 +99,7 @@ public static DataTable GetData(string datasetId, long structureId)
9899

99100
foreach (var r in records)
100101
{
101-
102+
102103
var l = Enumerable.ToList(r);
103104

104105
DataRow dr = data.NewRow();
@@ -110,7 +111,7 @@ public static DataTable GetData(string datasetId, long structureId)
110111
dr[data.Columns[j].ColumnName] = DBNull.Value;
111112
else
112113
dr[data.Columns[j].ColumnName] = l[j].Value;
113-
114+
114115
}
115116

116117
data.Rows.Add(dr);
@@ -128,27 +129,28 @@ public static DataTable GetData(string datasetId, long structureId)
128129
{
129130
string t = a;
130131
}
131-
132+
132133

133134
return data;
134135
}
135136

136137

137-
/// <summary>
138-
/// Get comp data
139-
///
140-
/// </summary>
141-
/// <param name="datasetId"></param>
142-
/// <returns>Data table with comp dataset depents on dataset id.</returns>
143-
public static DataTable GetComponentData(string datasetId)
144-
{
138+
/// <summary>
139+
/// Get comp data
140+
///
141+
/// </summary>
142+
/// <param name="datasetId"></param>
143+
/// <returns>Data table with comp dataset depents on dataset id.</returns>
144+
public static DataTable GetComponentData(string datasetId)
145+
{
145146
ServerInformation serverInformation = GetServerInformation();
146147

147148
string link = serverInformation.ServerName + "/api/data/" + datasetId;
148149
HttpWebRequest request = WebRequest.Create(link) as HttpWebRequest;
149150
request.Headers.Add("Authorization", "Bearer " + serverInformation.Token);
150151

151152
DataTable compData = new DataTable();
153+
compData.Columns.Add("Id");
152154
DataColumn year = new DataColumn("Year");
153155
year.DataType = System.Type.GetType("System.DateTime");
154156
compData.Columns.Add(year);
@@ -178,14 +180,17 @@ public static DataTable GetComponentData(string datasetId)
178180
string sep = "\t";
179181
String[] row = new String[4];
180182
int count = 0;
183+
int id = 0;
181184
while ((line = reader.ReadLine()) != null)
182185
{
183186
count++;
187+
id++;
184188
if (count > 1)
185189
{
186190
row = line.Split(',');
187191
DataRow dr = compData.NewRow();
188192
//dr["Year"] = DateTime.Parse(row[0]).ToString("yyyy");
193+
dr["id"] = id;
189194
dr["Year"] = row[0];
190195
dr["Exploratory"] = row[1];
191196
dr["EP_PlotID"] = row[2];
@@ -242,7 +247,7 @@ public static List<MissingComponentData> GetMissingComponentData()
242247
/// <returns>Information like version, title etc</returns>
243248
public static DatasetObject GetDatasetInfo(string datasetId)
244249
{
245-
ServerInformation serverInformation = GetServerInformation();
250+
ServerInformation serverInformation = GetServerInformation();
246251
string link = serverInformation.ServerName + "/api/dataset/" + datasetId;
247252
HttpWebRequest request = WebRequest.Create(link) as HttpWebRequest;
248253
request.Headers.Add("Authorization", "Bearer " + serverInformation.Token);
@@ -270,7 +275,7 @@ public static DatasetObject GetDatasetInfo(string datasetId)
270275
return datasetObject;
271276
}
272277

273-
public static DataStructureObject GetDataStructure( long structId)
278+
public static DataStructureObject GetDataStructure(long structId)
274279
{
275280
ServerInformation serverInformation = GetServerInformation();
276281
string link = serverInformation.ServerName + "/api/structures/" + structId;
@@ -362,80 +367,36 @@ public static List<string> getAllGrasslandPlots()
362367

363368
return graslandPlots;
364369
}
365-
}
366-
367370

371+
public static string Upload(DataApiModel data)
372+
{
373+
ServerInformation serverInformation = GetServerInformation();
374+
string link = serverInformation.ServerName + "/api/Data/";
375+
HttpWebRequest request = WebRequest.Create(link) as HttpWebRequest;
376+
//request.PreAuthenticate = true;
377+
request.Headers.Add("Authorization", "Bearer " + serverInformation.Token);
378+
request.Method = "POST";
379+
request.ContentType = "application/json";
368380

369-
/// <summary>
370-
/// Class to store server information to access data via API
371-
///
372-
/// </summary>
373-
/// <returns></returns>
374-
public class ServerInformation
375-
{
376-
public string ServerName { get; set; }
377-
public string Token { get; set; }
378-
379-
}
381+
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
382+
{
383+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
384+
streamWriter.Write(json);
385+
}
380386

381-
/// <summary>
382-
/// Class to store dataset information receive via api
383-
///
384-
/// </summary>
385-
/// <returns></returns>
386-
public class DataStructureObject
387-
{
388-
public string Id { get; set; }
389-
public string Title { get; set; }
390-
public string Description { get; set; }
391-
public string inUse { get; set; }
392-
public string Structured { get; set; }
393-
public List<Variables> Variables { get; set; }
394-
}
387+
var httpResponse = (HttpWebResponse)request.GetResponse();
388+
string result;
389+
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
390+
{
391+
result = streamReader.ReadToEnd();
392+
}
395393

396-
public class Variables
397-
{
398-
public string Id { get; set; }
399-
public string Label { get; set; }
400-
public string Description { get; set; }
401-
public string isOptional { get; set; }
402-
public string Unit { get; set; }
403-
public string DataType { get; set; }
404-
public string SystemType { get; set; }
405-
public string AttributeName { get; set; }
406-
public string AttributeDescription { get; set; }
407-
}
394+
return result;
408395

409-
/// <summary>
410-
/// Class to store dataset information receive via api
411-
///
412-
/// </summary>
413-
/// <returns></returns>
414-
public class DatasetObject
415-
{
416-
public string Id { get; set; }
417-
public string Version { get; set; }
418-
public string VersionId { get; set; }
419-
public string Title { get; set; }
420-
public string Description { get; set; }
421-
public string DataStructureId { get; set; }
422-
public string MetadataStructureId { get; set; }
423-
public AdditionalInformations AdditionalInformations { get; set; }
424-
public DatasetObject()
425-
{
426-
AdditionalInformations = new AdditionalInformations();
427396
}
428397
}
429-
430398
}
431399

432-
/// <summary>
433-
/// Store AdditionalInformations for Dataset Object
434-
///
435-
/// </summary>
436-
/// <returns></returns>
437-
public class AdditionalInformations
438-
{
439-
public string Title { get; set; }
440400

441-
}
401+
402+

Models/ApiModels.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace BExIS.Modules.Lui.UI.Models
7+
{
8+
/// <summary>
9+
/// Class to store server information to access data via API
10+
///
11+
/// </summary>
12+
/// <returns></returns>
13+
public class ServerInformation
14+
{
15+
public string ServerName { get; set; }
16+
public string Token { get; set; }
17+
18+
}
19+
20+
/// <summary>
21+
/// Class to store dataset information receive via api
22+
///
23+
/// </summary>
24+
/// <returns></returns>
25+
public class DataStructureObject
26+
{
27+
public string Id { get; set; }
28+
public string Title { get; set; }
29+
public string Description { get; set; }
30+
public string inUse { get; set; }
31+
public string Structured { get; set; }
32+
public List<Variables> Variables { get; set; }
33+
}
34+
35+
public class Variables
36+
{
37+
public string Id { get; set; }
38+
public string Label { get; set; }
39+
public string Description { get; set; }
40+
public string isOptional { get; set; }
41+
public string Unit { get; set; }
42+
public string DataType { get; set; }
43+
public string SystemType { get; set; }
44+
public string AttributeName { get; set; }
45+
public string AttributeDescription { get; set; }
46+
}
47+
48+
/// <summary>
49+
/// Class to store dataset information receive via api
50+
///
51+
/// </summary>
52+
/// <returns></returns>
53+
public class DatasetObject
54+
{
55+
public string Id { get; set; }
56+
public string Version { get; set; }
57+
public string VersionId { get; set; }
58+
public string Title { get; set; }
59+
public string Description { get; set; }
60+
public string DataStructureId { get; set; }
61+
public string MetadataStructureId { get; set; }
62+
public AdditionalInformations AdditionalInformations { get; set; }
63+
public DatasetObject()
64+
{
65+
AdditionalInformations = new AdditionalInformations();
66+
}
67+
}
68+
69+
70+
71+
/// <summary>
72+
/// Store AdditionalInformations for Dataset Object
73+
///
74+
/// </summary>
75+
/// <returns></returns>
76+
public class AdditionalInformations
77+
{
78+
public string Title { get; set; }
79+
80+
}
81+
82+
public enum DecimalCharacter
83+
{
84+
point,
85+
comma
86+
}
87+
88+
public class DataApiModel
89+
{
90+
public long DatasetId { get; set; }
91+
public DecimalCharacter DecimalCharacter { get; set; }
92+
public string[] Columns { get; set; }
93+
public string[][] Data { get; set; }
94+
}
95+
}

0 commit comments

Comments
 (0)