Skip to content

Commit 0e2c6e0

Browse files
all done, Component calculation: upload direct in module #46
1 parent fa6fcd1 commit 0e2c6e0

4 files changed

Lines changed: 87 additions & 28 deletions

File tree

Controllers/LUIComponentCalculationController.cs

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,38 +85,83 @@ public ActionResult UploadSelectedRows(int[] rowIds)
8585
{
8686
ComponentDataModel compData = Session["ComponentData"] as ComponentDataModel;
8787

88-
DataApiModel model = new DataApiModel();
89-
model.DatasetId = (long)Models.Settings.get("lui:datasetNewComponentsSet");
90-
model.DecimalCharacter = DecimalCharacter.point;
88+
//check for dublicates
89+
int[] noDuplicateIds = CheckDuplicates(compData.Data, rowIds);
9190

92-
//get col names
93-
List<string> cols = new List<string>();
94-
foreach(DataColumn colum in compData.Data.Columns)
91+
string result = "";
92+
93+
if (noDuplicateIds.Length == 0)
9594
{
96-
if (colum.ColumnName != "id")
97-
cols.Add(colum.ColumnName);
95+
result = "No Upload: all selected rows are already uploaded!";
9896
}
97+
else
98+
{
99+
//get duplicated ids for result text
100+
var dups = rowIds.Except(noDuplicateIds);
101+
result += "Duplicated ids not uploaded: <br>";
102+
foreach (int d in dups)
103+
{
104+
result += d.ToString() + "<br>";
105+
}
99106

100-
model.Columns = cols.ToArray();
107+
DataApiModel model = new DataApiModel();
108+
model.DatasetId = Convert.ToInt64(Models.Settings.get("lui:datasetNewComponentsSet"));
109+
model.DecimalCharacter = DecimalCharacter.point;
101110

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)
111+
//get col names
112+
List<string> cols = new List<string>();
113+
foreach (DataColumn colum in compData.Data.Columns)
107114
{
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);
115+
if (colum.ColumnName != "Id")
116+
cols.Add(colum.ColumnName);
111117
}
112118

113-
model.Data = dataArrays.ToArray();
114-
}
119+
model.Columns = cols.ToArray();
115120

116-
//upload
117-
string result = DataAccess.Upload(model);
121+
string[,] dataArray = new string[rowIds.Count(), cols.Count];
122+
123+
List<string[]> dataArrays = new List<string[]>();
124+
if (rowIds != null)
125+
{
126+
foreach (int id in noDuplicateIds)
127+
{
128+
DataTable copy = new DataTable();
129+
copy = compData.Data.Copy();
130+
131+
DataRow row = copy.AsEnumerable().Where(a => a.Field<int>("Id") == id).FirstOrDefault();
132+
row.Table.Columns.Remove("Id");
133+
134+
string[] stringArray = row.ItemArray.Cast<string>().ToArray();
135+
dataArrays.Add(stringArray);
136+
}
137+
138+
model.Data = dataArrays.ToArray();
139+
}
140+
141+
//upload
142+
result = DataAccess.Upload(model);
143+
}
118144

119145
return Content(result);
120146
}
147+
148+
149+
private int[] CheckDuplicates(DataTable newCompData, int[] rowIds)
150+
{
151+
string luiIdNew = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
152+
DataTable allCompData = DataAccess.GetComponentData(luiIdNew);
153+
154+
List<int> noDuplicates = new List<int>();
155+
foreach (int id in rowIds)
156+
{
157+
var row = newCompData.AsEnumerable().Where(a => a.Field<int>("Id") == id).FirstOrDefault();
158+
var duplicate = allCompData.AsEnumerable().Where(c=>c.Field<string>("EP_PlotID") == row.Field<string>("EP_PlotID") && c.Field<DateTime>("Year").ToString("yyyy") == row.Field<string>("Year")).First();
159+
if (duplicate == null)
160+
noDuplicates.Add(id);
161+
162+
}
163+
164+
return noDuplicates.ToArray();
165+
}
121166
}
122167
}

Helper/DataAccess.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ public static DataTable GetComponentData(string datasetId)
150150
request.Headers.Add("Authorization", "Bearer " + serverInformation.Token);
151151

152152
DataTable compData = new DataTable();
153-
compData.Columns.Add("Id");
153+
DataColumn idCol = new DataColumn("Id");
154+
idCol.DataType = System.Type.GetType("System.Int32");
155+
compData.Columns.Add(idCol);
154156
DataColumn year = new DataColumn("Year");
155157
year.DataType = System.Type.GetType("System.DateTime");
156158
compData.Columns.Add(year);

Helper/LUIComponentsCalculation.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public DataTable CalculateComponents()
2323
{
2424
//create result datatable
2525
DataTable luiComponents = new DataTable();
26+
DataColumn idCol = new DataColumn("Id");
27+
idCol.DataType = System.Type.GetType("System.Int32");
28+
luiComponents.Columns.Add(idCol);
29+
2630
luiComponents.Columns.Add("Year");
2731
luiComponents.Columns.Add("Exploratory");
2832
luiComponents.Columns.Add("EP_PlotID");
@@ -43,10 +47,12 @@ public DataTable CalculateComponents()
4347
}
4448

4549
#endregion
46-
50+
int idCounter = 0;
4751
foreach (DataRow row in landuseData.Rows)
4852
{
53+
idCounter++;
4954
DataRow dr = luiComponents.NewRow();
55+
dr["Id"] = idCounter;
5056
dr["Year"] = row.Field<DateTime>("Year").Year;
5157
dr["Exploratory"] = row.Field<string>("Exploratory");
5258
dr["EP_PlotID"] = row.Field<string>("EP_PlotID");

Views/LUIComponentCalculation/ComponentCalculation.cshtml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<tbody>
3131
@for (int i = 0; i < Model.Data.Rows.Count; i++)
3232
{
33-
<tr>
33+
<tr id="@Model.Data.Rows[i]["Id"].ToString()">
3434
<td></td>
3535
@for (int c = 0; c < Model.Data.Columns.Count; c++)
3636
{
@@ -45,8 +45,8 @@
4545
</table>
4646
<br />
4747
@Html.ActionLink("Download (csv)", "Download", "LUIComponentCalculation", new { @class = "bx-button function" })
48-
<button onclick="upload()" class="bx-button function">Upload selected rows</button>
49-
<p id="error"></p>
48+
<button onclick="upload()" class="bx-button function">Upload selected rows</button><br />
49+
<p id="error" style="font-weight:bold;"></p>
5050

5151
}
5252

@@ -68,8 +68,14 @@
6868
});
6969
7070
function upload() {
71-
var selectedIds = tbl.columns().checkboxes.selected()[0];
72-
$.post('@Url.Action("Upload", "LUIComponentCalculation", new RouteValueDictionary { { "area", "RBM" } })', { rowIds: selectedIds }, function (data) {
71+
var selectedIds = [];
72+
$.each(tbl.rows('.selected').nodes(), function (i, item) {
73+
selectedIds.push(item.id);
74+
//var data = tbl.row(this).data();
75+
});
76+
77+
console.log(selectedIds);
78+
$.post('@Url.Action("UploadSelectedRows", "LUIComponentCalculation", new RouteValueDictionary { { "area", "LUI" } })', { rowIds: selectedIds }, function (data) {
7379
document.getElementById("error").textContent += data;
7480
});
7581
}

0 commit comments

Comments
 (0)