99using System . IO ;
1010using System . Linq ;
1111using System . Net ;
12+ using System . Text ;
1213using System . Web ;
1314using System . Web . Script . Serialization ;
1415using 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+
0 commit comments