@@ -19,7 +19,6 @@ namespace BExIS.Modules.Lui.UI.Helper
1919{
2020 public class DataAccess
2121 {
22-
2322 /// <summary>
2423 /// Get metadata
2524 /// </summary>
@@ -55,10 +54,12 @@ public static DataTable GetData(string datasetId, long structureId, ServerInform
5554 string link = serverInformation . ServerName + "/api/data/" + datasetId ;
5655 HttpWebRequest request = WebRequest . Create ( link ) as HttpWebRequest ;
5756 request . Headers . Add ( "Authorization" , "Bearer " + serverInformation . Token ) ;
58- // request.ContentType = "application/json";
57+
58+ List < ApiDataStatisticModel > statistics = GetMissingValues ( datasetId , serverInformation ) ;
5959
6060 DataStructureObject dataStructureObject = GetDataStructure ( structureId , serverInformation ) ;
6161
62+ //create datatable using data structure info
6263 DataTable data = new DataTable ( ) ;
6364 foreach ( var variable in dataStructureObject . Variables )
6465 {
@@ -68,7 +69,6 @@ public static DataTable GetData(string datasetId, long structureId, ServerInform
6869 data . Columns . Add ( col ) ;
6970 }
7071
71- string a = "" ;
7272 try
7373 {
7474 // Get response
@@ -78,124 +78,89 @@ public static DataTable GetData(string datasetId, long structureId, ServerInform
7878 using ( StreamReader reader = new StreamReader ( response . GetResponseStream ( ) ) )
7979 using ( var csvReader = new CsvReader ( reader , CultureInfo . InvariantCulture ) )
8080 {
81-
8281 var records = csvReader . GetRecords < dynamic > ( ) ;
8382
8483 foreach ( var r in records )
8584 {
86-
8785 var l = Enumerable . ToList ( r ) ;
8886
8987 DataRow dr = data . NewRow ( ) ;
9088 String [ ] row = new String [ 4 ] ;
89+
9190 for ( int j = 0 ; j < data . Columns . Count ; j ++ )
9291 {
93- a = l [ j ] . Value ;
9492 if ( String . IsNullOrEmpty ( l [ j ] . Value ) )
9593 dr [ data . Columns [ j ] . ColumnName ] = DBNull . Value ;
9694 else
97- dr [ data . Columns [ j ] . ColumnName ] = l [ j ] . Value ;
98-
95+ {
96+ //get missing values from data structure to replace display name with placeholder
97+ DataTable missingValues = statistics . Where ( a => a . VariableName == data . Columns [ j ] . ColumnName ) . Select ( a => a . missingValues ) . FirstOrDefault ( ) ;
98+ dynamic value = null ;
99+ if ( missingValues != null )
100+ {
101+ var displayNames = missingValues . AsEnumerable ( ) . Select ( a => a . Field < string > ( "displayName" ) ) . ToList ( ) ;
102+ if ( displayNames . Contains ( l [ j ] . Value ) )
103+ value = missingValues . AsEnumerable ( ) . Where ( a => a . Field < string > ( "displayName" ) == l [ j ] . Value ) . Select ( a => a . Field < string > ( "placeholder" ) ) . FirstOrDefault ( ) ;
104+ else
105+ value = l [ j ] . Value ;
106+
107+ }
108+ else
109+ value = l [ j ] . Value ;
110+
111+ if ( data . Columns [ j ] . DataType == typeof ( DateTime ) )
112+ {
113+ var format = dataStructureObject . Variables . Where ( e => e . Label == data . Columns [ j ] . ColumnName ) . FirstOrDefault ( ) . DataType ;
114+ format = format . Split ( '-' ) . ToArray ( ) [ 1 ] ;
115+ dr [ data . Columns [ j ] . ColumnName ] = ParseValue ( data . Columns [ j ] . DataType . ToString ( ) , value , format ) ;
116+ }
117+ else
118+ dr [ data . Columns [ j ] . ColumnName ] = value ;
119+ }
99120 }
100121
101122 data . Rows . Add ( dr ) ;
102123 }
103124
104- //JavaScriptSerializer js = new JavaScriptSerializer();
105- //var objText = reader.ReadToEnd();
106- //lanuData = (DataTable)JsonConvert.DeserializeObject(objText, (typeof(DataTable)));
107-
108125 response . Close ( ) ;
109126 }
110127 }
111128 }
112129 catch ( Exception e )
113130 {
114- string t = a ;
131+
115132 }
116133
117134
118135 return data ;
119136 }
120137
121-
122- /// <summary>
123- /// Get comp data
124- ///
125- /// </summary>
126- /// <param name="datasetId"></param>
127- /// <returns>Data table with comp dataset depents on dataset id.</returns>
128- public static DataTable GetComponentData ( string datasetId , ServerInformation serverInformation )
138+ public static List < ApiDataStatisticModel > GetMissingValues ( string datasetId , ServerInformation serverInformation )
129139 {
130- string link = serverInformation . ServerName + "/api/data /" + datasetId ;
140+ string link = serverInformation . ServerName + "/api/datastatistic /" + datasetId ;
131141 HttpWebRequest request = WebRequest . Create ( link ) as HttpWebRequest ;
132142 request . Headers . Add ( "Authorization" , "Bearer " + serverInformation . Token ) ;
133143
134- DataTable compData = new DataTable ( ) ;
135- DataColumn idCol = new DataColumn ( "Id" ) ;
136- idCol . DataType = System . Type . GetType ( "System.Int32" ) ;
137- compData . Columns . Add ( idCol ) ;
138- DataColumn year = new DataColumn ( "Year" ) ;
139- year . DataType = System . Type . GetType ( "System.DateTime" ) ;
140- compData . Columns . Add ( year ) ;
141- compData . Columns . Add ( "Exploratory" ) ;
142- compData . Columns . Add ( "EP_PlotID" ) ;
143- compData . Columns . Add ( "isVIP" ) ;
144- compData . Columns . Add ( "isMIP" ) ;
145- DataColumn totalGrazing = new DataColumn ( "TotalGrazing" ) ;
146- totalGrazing . DataType = System . Type . GetType ( "System.Decimal" ) ;
147- compData . Columns . Add ( totalGrazing ) ;
148- DataColumn totalMowing = new DataColumn ( "TotalMowing" ) ;
149- totalMowing . DataType = System . Type . GetType ( "System.Decimal" ) ;
150- compData . Columns . Add ( totalMowing ) ;
151- DataColumn totalFertilization = new DataColumn ( "TotalFertilization" ) ;
152- totalFertilization . DataType = System . Type . GetType ( "System.Decimal" ) ;
153- compData . Columns . Add ( totalFertilization ) ;
144+ List < ApiDataStatisticModel > data = new List < ApiDataStatisticModel > ( ) ;
154145
155146 try
156147 {
157148 // Get response
158149 using ( HttpWebResponse response = request . GetResponse ( ) as HttpWebResponse )
159150 {
160- // Get the response stream
161151 using ( StreamReader reader = new StreamReader ( response . GetResponseStream ( ) ) )
162152 {
163- string line = String . Empty ;
164- string sep = "\t " ;
165- String [ ] row = new String [ 4 ] ;
166- int count = 0 ;
167- int id = 0 ;
168- while ( ( line = reader . ReadLine ( ) ) != null )
169- {
170- count ++ ;
171- id ++ ;
172- if ( count > 1 )
173- {
174- row = line . Split ( ',' ) ;
175- DataRow dr = compData . NewRow ( ) ;
176- //dr["Year"] = DateTime.Parse(row[0]).ToString("yyyy");
177- dr [ "id" ] = id ;
178- dr [ "Year" ] = row [ 0 ] ;
179- dr [ "Exploratory" ] = row [ 1 ] ;
180- dr [ "EP_PlotID" ] = row [ 2 ] ;
181- dr [ "isVIP" ] = row [ 3 ] ;
182- dr [ "isMIP" ] = row [ 4 ] ;
183- dr [ "TotalGrazing" ] = row [ 5 ] ;
184- dr [ "TotalMowing" ] = row [ 6 ] ;
185- dr [ "TotalFertilization" ] = row [ 7 ] ;
186- compData . Rows . Add ( dr ) ;
187- }
188- }
189- response . Close ( ) ;
153+ var objText = reader . ReadToEnd ( ) ;
154+ data = JsonConvert . DeserializeObject < List < ApiDataStatisticModel > > ( objText ) ;
190155 }
191156 }
192157 }
193158 catch ( Exception e )
194159 {
195-
160+ string error = "Not data" + e . InnerException ;
196161 }
197162
198- return compData ;
163+ return data ;
199164 }
200165
201166 /// <summary>
@@ -207,7 +172,8 @@ public static List<MissingComponentData> GetMissingComponentData(ServerInformati
207172 {
208173 List < MissingComponentData > data = new List < MissingComponentData > ( ) ;
209174 string datasetId = Models . Settings . get ( "lui:datasetNewComponentsSet" ) . ToString ( ) ;
210- DataTable compData = GetComponentData ( datasetId , serverInformation ) ;
175+ long structureId = long . Parse ( DataAccess . GetDatasetInfo ( datasetId , serverInformation ) . DataStructureId , CultureInfo . InvariantCulture ) ;
176+ DataTable compData = GetData ( datasetId , structureId , serverInformation ) ;
211177
212178 //get all years where data rows less then 50, that means not all plots has data
213179 var years = compData . AsEnumerable ( ) . GroupBy ( x => x . Field < DateTime > ( "Year" ) ) . Where ( g => g . Count ( ) < 150 ) . ToList ( ) ;
@@ -271,7 +237,11 @@ public static DatasetObject GetDatasetInfo(string datasetId, ServerInformation s
271237
272238 return datasetObject ;
273239 }
274-
240+ /// <summary>
241+ /// Get data structure
242+ ///
243+ /// </summary>
244+ /// <returns> a data structure object</returns>
275245 public static DataStructureObject GetDataStructure ( long structId , ServerInformation serverInformation )
276246 {
277247 string link = serverInformation . ServerName + "/api/structures/" + structId ;
@@ -301,9 +271,6 @@ public static DataStructureObject GetDataStructure(long structId, ServerInformat
301271 return dataStructureObject ;
302272 }
303273
304-
305-
306-
307274 /// <summary>
308275 /// get all ep plot ids from grasland plots
309276 ///
@@ -363,6 +330,11 @@ public static List<string> getAllGrasslandPlots(ServerInformation serverInformat
363330 return graslandPlots ;
364331 }
365332
333+ /// <summary>
334+ /// upload primary data via api
335+ ///
336+ /// </summary>
337+ /// <returns>API response</returns>
366338 public static string Upload ( DataApiModel data , ServerInformation serverInformation )
367339 {
368340 string link = serverInformation . ServerName + "/api/Data/" ;
@@ -386,7 +358,30 @@ public static string Upload(DataApiModel data, ServerInformation serverInformati
386358 }
387359
388360 return result ;
361+ }
389362
363+ /// <summary>
364+ /// convert string to given data type
365+ ///
366+ /// </summary>
367+ /// <returns>type object</returns>
368+ private static object ParseValue ( string type , string value , string format )
369+ {
370+ try
371+ {
372+ switch ( type )
373+ {
374+ case "System.DateTime" :
375+ DateTime . TryParseExact ( value , format , new CultureInfo ( "en-US" ) , DateTimeStyles . None , out DateTime date ) ;
376+ return date ;
377+ default : throw new ArgumentException ( "DataType is not supported" ) ;
378+ }
379+ }
380+ catch ( Exception e )
381+ {
382+ string text = String . Format ( "Type{0}, Value{1}, Format {2}" , type , value , format ) ;
383+ throw new ArgumentException ( text , e ) ;
384+ }
390385 }
391386 }
392387}
0 commit comments