@@ -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}
0 commit comments