Skip to content

Commit bef73d1

Browse files
upload and dupliacted check is in, status messages ok close #46
1 parent 48d4efa commit bef73d1

File tree

2 files changed

+78
-52
lines changed

2 files changed

+78
-52
lines changed

Controllers/LUIComponentCalculationController.cs

Lines changed: 75 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,63 +85,88 @@ public ActionResult Download()
8585

8686
public ActionResult UploadSelectedRows(int[] rowIds)
8787
{
88-
ComponentDataModel compData = Session["ComponentData"] as ComponentDataModel;
89-
90-
//check for dublicates
91-
int[] noDuplicateIds = CheckDuplicates(compData.Data, rowIds);
92-
9388
string result = "";
94-
95-
if (noDuplicateIds.Length == 0)
89+
if (rowIds != null)
9690
{
97-
result = "No Upload: all selected rows are already uploaded!";
98-
}
99-
else
100-
{
101-
//get duplicated ids for result text
102-
var dups = rowIds.Except(noDuplicateIds);
103-
result += "Duplicated ids not uploaded: <br>";
104-
foreach (int d in dups)
105-
{
106-
result += d.ToString() + "<br>";
107-
}
91+
ComponentDataModel compData = Session["ComponentData"] as ComponentDataModel;
10892

109-
DataApiModel model = new DataApiModel();
110-
model.DatasetId = Convert.ToInt64(Models.Settings.get("lui:datasetNewComponentsSet"));
111-
model.DecimalCharacter = DecimalCharacter.point;
93+
//check for dublicates
94+
int[] duplicateIds = CheckDuplicates(compData.Data, rowIds);
11295

113-
//get col names
114-
List<string> cols = new List<string>();
115-
foreach (DataColumn colum in compData.Data.Columns)
96+
if (duplicateIds.Length == rowIds.Length)
11697
{
117-
if (colum.ColumnName != "Id")
118-
cols.Add(colum.ColumnName);
98+
result = "No Upload: all selected rows are already uploaded!";
11999
}
100+
else
101+
{
102+
//get duplicated ids for result text
103+
//var dups = rowIds.Except(noDuplicateIds);
104+
if (duplicateIds.Length > 0)
105+
{
106+
result += "Duplicated ids not uploaded: ";
107+
foreach (int d in duplicateIds)
108+
{
109+
if(duplicateIds.Last() == d)
110+
result += d.ToString() + " ";
111+
else
112+
result += d.ToString() + ", ";
113+
}
114+
115+
result += "<br/>";
116+
}
120117

121-
model.Columns = cols.ToArray();
122118

123-
string[,] dataArray = new string[rowIds.Count(), cols.Count];
119+
DataApiModel model = new DataApiModel();
120+
model.DatasetId = Convert.ToInt64(Models.Settings.get("lui:datasetNewComponentsSet"));
121+
model.DecimalCharacter = DecimalCharacter.point;
124122

125-
List<string[]> dataArrays = new List<string[]>();
126-
if (rowIds != null)
127-
{
128-
foreach (int id in noDuplicateIds)
123+
//get col names
124+
List<string> cols = new List<string>();
125+
foreach (DataColumn colum in compData.Data.Columns)
129126
{
130-
DataTable copy = new DataTable();
131-
copy = compData.Data.Copy();
127+
if (colum.ColumnName != "Id")
128+
cols.Add(colum.ColumnName);
129+
}
132130

133-
DataRow row = copy.AsEnumerable().Where(a => a.Field<int>("Id") == id).FirstOrDefault();
134-
row.Table.Columns.Remove("Id");
131+
model.Columns = cols.ToArray();
135132

136-
string[] stringArray = row.ItemArray.Cast<string>().ToArray();
137-
dataArrays.Add(stringArray);
138-
}
133+
string[,] dataArray = new string[rowIds.Count(), cols.Count];
139134

140-
model.Data = dataArrays.ToArray();
141-
}
135+
List<string[]> dataArrays = new List<string[]>();
136+
137+
//get all not duplicated id
138+
var idsToUpload = rowIds.Except(duplicateIds);
139+
if(idsToUpload.Count() > 0)
140+
result += "Uploaded Ids: ";
141+
142+
foreach (int id in idsToUpload)
143+
{
144+
DataTable copy = new DataTable();
145+
copy = compData.Data.Copy();
146+
147+
DataRow row = copy.AsEnumerable().Where(a => a.Field<int>("Id") == id).FirstOrDefault();
148+
row.Table.Columns.Remove("Id");
149+
150+
string[] stringArray = row.ItemArray.Cast<string>().ToArray();
151+
dataArrays.Add(stringArray);
152+
153+
if (idsToUpload.Last() == id)
154+
result += id.ToString() + " ";
155+
else
156+
result += id.ToString() + ", ";
157+
}
158+
result += "<br/>";
142159

143-
//upload
144-
result = DataAccess.Upload(model, GetServerInformation());
160+
model.Data = dataArrays.ToArray();
161+
162+
//upload
163+
result += "API response: " + DataAccess.Upload(model, GetServerInformation()) + "<br/>";
164+
165+
}
166+
}
167+
else
168+
{
169+
result += "No Upload: no rows selected.";
145170
}
146171

147172
return Content(result);
@@ -193,17 +218,19 @@ private int[] CheckDuplicates(DataTable newCompData, int[] rowIds)
193218
string luiIdNew = Models.Settings.get("lui:datasetNewComponentsSet").ToString();
194219
DataTable allCompData = DataAccess.GetComponentData(luiIdNew, GetServerInformation());
195220

196-
List<int> noDuplicates = new List<int>();
221+
List<int> duplicates = new List<int>();
197222
foreach (int id in rowIds)
198223
{
199224
var row = newCompData.AsEnumerable().Where(a => a.Field<int>("Id") == id).FirstOrDefault();
200-
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();
201-
if (duplicate == null)
202-
noDuplicates.Add(id);
225+
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>(1)).FirstOrDefault();
226+
if (duplicate != null)
227+
duplicates.Add(id);
228+
229+
203230

204231
}
205232

206-
return noDuplicates.ToArray();
233+
return duplicates.ToArray();
207234
}
208235
}
209236
}

Views/LUIComponentCalculation/ComponentCalculation.cshtml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<br />
4747
@Html.ActionLink("Download (csv)", "Download", "LUIComponentCalculation", new { @class = "bx-button function" })
4848
<button onclick="upload()" class="bx-button function">Upload selected rows</button><br />
49-
<p id="error" style="font-weight:bold;"></p>
49+
<div id="error" style="font-weight:bold;"></div>
5050

5151
}
5252

@@ -80,15 +80,14 @@
8080
8181
function upload() {
8282
var selectedIds = [];
83+
document.getElementById("error").textContent = "";
8384
$.each(tbl.rows('.selected').nodes(), function (i, item) {
8485
selectedIds.push(item.id);
8586
//var data = tbl.row(this).data();
8687
});
8788
88-
89-
console.log(selectedIds);
9089
$.post('@Url.Action("UploadSelectedRows", "LUIComponentCalculation", new RouteValueDictionary { { "area", "LUI" } })', { rowIds: selectedIds }, function (data) {
91-
document.getElementById("error").textContent += data;
90+
document.getElementById("error").innerHTML = data;
9291
});
9392
}
9493

0 commit comments

Comments
 (0)