1919using BExIS . Security . Entities . Subjects ;
2020using BExIS . Security . Entities . Versions ;
2121using BExIS . Security . Services . Utilities ;
22+ using BExIS . UI . Helpers ;
2223using BExIS . Utils . Config ;
2324using BExIS . Utils . Extensions ;
2425using BExIS . Utils . Models ;
26+ using BExIS . Utils . NH . Querying ;
2527using BExIS . Xml . Helpers ;
2628using Newtonsoft . Json ;
2729using System ;
3537using System . Web . Mvc ;
3638using System . Web . Routing ;
3739using System . Xml ;
40+ using Telerik . Web . Mvc ;
41+ using Vaelastrasz . Library . Models ;
3842using Vaiona . Logging ;
3943using Vaiona . Persistence . Api ;
4044using Vaiona . Utils . Cfg ;
@@ -196,6 +200,9 @@ public ActionResult GenerateZip(long id, long versionid, string format)
196200 PublicationManager publicationManager = new PublicationManager ( ) ;
197201 SubmissionManager publishingManager = new SubmissionManager ( ) ;
198202
203+ bool isFilterInUse = filterInUse ( ) ;
204+ string filteredFilePath = "" ;
205+
199206 string brokerName = "generic" ;
200207
201208 try
@@ -220,6 +227,8 @@ public ActionResult GenerateZip(long id, long versionid, string format)
220227
221228 #region primary data
222229
230+
231+
223232 // check the data sturcture type ...
224233 if ( format != null && datasetVersion . Dataset . DataStructure . Self is StructuredDataStructure && dm . GetDataTuplesCount ( datasetVersion . Id ) > 0 )
225234 {
@@ -228,19 +237,28 @@ public ActionResult GenerateZip(long id, long versionid, string format)
228237 //check wheter title is empty or not
229238 title = String . IsNullOrEmpty ( datasetVersion . Title ) ? "no title available" : datasetVersion . Title ;
230239
231- switch ( format )
240+ if ( isFilterInUse )
241+ {
242+ DataTable filteredData = getFilteredData ( id ) ;
243+ filteredFilePath = odm . GenerateAsciiFile ( "temp" , filteredData , title , format , datasetVersion . Dataset . DataStructure . Id , false ) ;
244+ }
245+ else
232246 {
233- case "application/xlsx" :
234- odm . GenerateExcelFile ( id , datasetVersion . Id , false ) ;
235- break ;
236247
237- case "application/xlsm" :
238- odm . GenerateExcelFile ( id , datasetVersion . Id , true ) ;
239- break ;
248+ switch ( format )
249+ {
250+ case "application/xlsx" :
251+ odm . GenerateExcelFile ( id , datasetVersion . Id , false ) ;
252+ break ;
240253
241- default :
242- odm . GenerateAsciiFile ( id , datasetVersion . Id , format , false ) ;
243- break ;
254+ case "application/xlsm" :
255+ odm . GenerateExcelFile ( id , datasetVersion . Id , true ) ;
256+ break ;
257+
258+ default :
259+ odm . GenerateAsciiFile ( id , datasetVersion . Id , format , false ) ;
260+ break ;
261+ }
244262 }
245263 }
246264
@@ -272,6 +290,9 @@ public ActionResult GenerateZip(long id, long versionid, string format)
272290 #region zip file
273291
274292 string zipName = IOHelper . GetFileName ( FileType . Bundle , id , datasetVersionNumber , dataStructureId , title ) ; //publishingManager.GetZipFileName(id, datasetVersionNumber);
293+ // add suffix if filter is in use
294+ if ( isFilterInUse ) zipName += "_filtered" ;
295+
275296 string zipPath = Path . Combine ( publishingManager . GetDirectoryPath ( id , brokerName ) , zipName + ".zip" ) ;
276297 FileHelper . CreateDicrectoriesIfNotExist ( Path . GetDirectoryName ( zipPath ) ) ;
277298
@@ -316,6 +337,18 @@ public ActionResult GenerateZip(long id, long versionid, string format)
316337 }
317338 }
318339
340+ // if data was filtered add a text file with information about filtering
341+ if ( isFilterInUse )
342+ {
343+ string path = Path . Combine ( AppConfiguration . DataPath , filteredFilePath ) ;
344+ string ext = Path . GetExtension ( filteredFilePath ) . ToLower ( ) ;
345+ string name = IOHelper . GetFileName ( FileType . PrimaryData , id , datasetVersionNumber , dataStructureId ) + "filtered" + ext ;
346+
347+ archive . AddFileToArchive ( filteredFilePath , name ) ;
348+ }
349+
350+
351+
319352 // xml schema
320353 string xsdPath = OutputMetadataManager . GetSchemaDirectoryPath ( id ) ;
321354 if ( Directory . Exists ( xsdPath ) )
@@ -332,6 +365,10 @@ public ActionResult GenerateZip(long id, long versionid, string format)
332365 datasetModel . DownloadInformation . DownloadSource = Request . Url . Host ;
333366 datasetModel . DownloadInformation . DownloadedBy = getPartyNameOrDefault ( ) ;
334367
368+ // set filter info
369+ if ( isFilterInUse )
370+ datasetModel . DownloadInformation . DownloadedFilter = getFilterQuery ( ) ;
371+
335372 if ( datasetModel . DownloadInformation . DownloadedBy == "DEFAULT" ) datasetModel . DownloadInformation . DownloadedBy = "ANONYMOUS" ;
336373
337374 string manifest = JsonConvert . SerializeObject ( datasetModel ) ;
@@ -572,5 +609,62 @@ private string getPartyNameOrDefault()
572609 }
573610 return ! string . IsNullOrWhiteSpace ( userName ) ? userName : "DEFAULT" ;
574611 }
612+
613+ private bool filterInUse ( )
614+ {
615+ if ( Session [ "DataFilter" ] != null || Session [ "DataOrderBy" ] != null || Session [ "DataProjection" ] != null )
616+ {
617+ return true ;
618+ }
619+
620+ return false ;
621+ }
622+
623+ private DataTable getFilteredData ( long datasetId )
624+ {
625+ DatasetManager datasetManager = new DatasetManager ( ) ;
626+ try
627+ {
628+ GridCommand command = null ;
629+ FilterExpression filter = null ;
630+ OrderByExpression orderBy = null ;
631+ ProjectionExpression projection = null ;
632+ string [ ] columns = null ;
633+
634+ if ( Session [ "DataFilter" ] != null ) filter = ( FilterExpression ) Session [ "DataFilter" ] ;
635+ if ( Session [ "DataOrderBy" ] != null ) orderBy = ( OrderByExpression ) Session [ "DataOrderBy" ] ;
636+ if ( Session [ "DataProjection" ] != null ) projection = ( ProjectionExpression ) Session [ "DataProjection" ] ;
637+
638+
639+ long count = datasetManager . RowCount ( datasetId , filter ) ;
640+
641+ DataTable table = datasetManager . GetLatestDatasetVersionTuples ( datasetId , filter , orderBy , projection , "" , 0 , ( int ) count ) ;
642+
643+ if ( projection == null ) table . Strip ( ) ;
644+
645+ return table ;
646+ }
647+ finally
648+ {
649+ datasetManager . Dispose ( ) ;
650+ }
651+ }
652+
653+ private string getFilterQuery ( )
654+ {
655+ FilterExpression filter = null ;
656+ OrderByExpression orderBy = null ;
657+ ProjectionExpression projection = null ;
658+ string [ ] columns = null ;
659+
660+ if ( Session [ "DataFilter" ] != null ) filter = ( FilterExpression ) Session [ "DataFilter" ] ;
661+ if ( Session [ "DataOrderBy" ] != null ) orderBy = ( OrderByExpression ) Session [ "DataOrderBy" ] ;
662+ if ( Session [ "DataProjection" ] != null ) projection = ( ProjectionExpression ) Session [ "DataProjection" ] ;
663+
664+ string query = filter ? . ToSQL ( ) + orderBy ? . ToSQL ( ) + projection ? . ToSQL ( ) ;
665+
666+ return query ;
667+ }
668+
575669 }
576670}
0 commit comments