2828using System . IO ;
2929using System . IO . Compression ;
3030using System . Linq ;
31+ using System . Security . Cryptography ;
3132using System . Text ;
3233using System . Web . Mvc ;
3334using System . Web . Routing ;
@@ -203,13 +204,14 @@ public ActionResult GenerateZip(long id, long versionid, string format)
203204 long dataStructureId = 0 ;
204205 int datasetVersionNumber = dm . GetDatasetVersionNr ( datasetVersionId ) ;
205206 DatasetVersion datasetVersion = datasetManager . GetDatasetVersion ( datasetVersionId ) ;
207+ string title = "" ;
206208
207209 #region Metadata
208210
209211 //metadata as XML
210212 XmlDocument document = OutputMetadataManager . GetConvertedMetadata ( id , TransmissionType . mappingFileExport , datasetVersion . Dataset . MetadataStructure . Name ) ;
211213
212- //generate metadata as HTML and store the file locally
214+ //generate data structure as html
213215 generateMetadataAsHtml ( datasetVersion ) ;
214216
215217 #endregion
@@ -222,7 +224,7 @@ public ActionResult GenerateZip(long id, long versionid, string format)
222224 OutputDataManager odm = new OutputDataManager ( ) ;
223225
224226 //check wheter title is empty or not
225- string title = String . IsNullOrEmpty ( datasetVersion . Title ) ? "no title available" : datasetVersion . Title ;
227+ title = String . IsNullOrEmpty ( datasetVersion . Title ) ? "no title available" : datasetVersion . Title ;
226228
227229 switch ( format )
228230 {
@@ -260,16 +262,15 @@ public ActionResult GenerateZip(long id, long versionid, string format)
260262
261263 AsciiWriter . AllTextToFile ( datastructureFilePath , json ) ;
262264
263- //generate data structure as html
264265 generateDataStructureHtml ( datasetVersion ) ;
265266 }
266267
267268 #endregion
268269
269270 #region zip file
270271
271- string zipName = publishingManager . GetZipFileName ( id , datasetVersionNumber ) ;
272- string zipPath = Path . Combine ( publishingManager . GetDirectoryPath ( id , brokerName ) , zipName ) ;
272+ string zipName = IOHelper . GetFileName ( FileType . Bundle , id , datasetVersionNumber , dataStructureId , title ) ; // publishingManager.GetZipFileName(id, datasetVersionNumber);
273+ string zipPath = Path . Combine ( publishingManager . GetDirectoryPath ( id , brokerName ) , zipName + ".zip" ) ;
273274 FileHelper . CreateDicrectoriesIfNotExist ( Path . GetDirectoryName ( zipPath ) ) ;
274275
275276 using ( var zipFileStream = new FileStream ( zipPath , FileMode . Create ) )
@@ -283,13 +284,33 @@ public ActionResult GenerateZip(long id, long versionid, string format)
283284 {
284285 string path = Path . Combine ( AppConfiguration . DataPath , cd . URI ) ;
285286 string name = cd . URI . Split ( '\\ ' ) . Last ( ) ;
287+ string ext = Path . GetExtension ( name ) . ToLower ( ) ;
286288
287289 if ( cd . Name . StartsWith ( "generated" ) && ! cd . Name . Equals ( dataName ) ) continue ;
288290
289291 if ( FileHelper . FileExist ( path ) )
290292 {
291293 if ( ! archive . Entries . Any ( entry => entry . Name . EndsWith ( name ) ) )
294+ {
295+ if ( cd . Name . Equals ( "metadata" ) )
296+ {
297+ name = IOHelper . GetFileName ( FileType . Metadata , id , datasetVersionNumber , dataStructureId ) + ext ;
298+ }
299+ else if ( cd . Name . Equals ( "datastructure" ) )
300+ {
301+ name = IOHelper . GetFileName ( FileType . DataStructure , id , datasetVersionNumber , dataStructureId ) + ext ;
302+ }
303+ else if ( cd . Name . Contains ( "generated" ) )
304+ {
305+ name = IOHelper . GetFileName ( FileType . PrimaryData , id , datasetVersionNumber , dataStructureId ) + ext ;
306+ }
307+ else
308+ {
309+ name = IOHelper . GetFileName ( FileType . None , id , datasetVersionNumber , dataStructureId , cd . Name ) + ext ;
310+ }
311+
292312 archive . AddFileToArchive ( path , name ) ;
313+ }
293314 }
294315 }
295316
@@ -307,16 +328,17 @@ public ActionResult GenerateZip(long id, long versionid, string format)
307328
308329 if ( manifest != null )
309330 {
331+ string manifestFileName = IOHelper . GetFileName ( FileType . Manifest , id , datasetVersionNumber , dataStructureId ) ;
310332 string manifestPath = OutputDatasetManager . GetDynamicDatasetStorePath ( id ,
311- datasetVersionNumber , "manifest" , ".json" ) ;
333+ datasetVersionNumber , manifestFileName , ".json" ) ;
312334 string fullFilePath = Path . Combine ( AppConfiguration . DataPath , manifestPath ) ;
313335 string directory = Path . GetDirectoryName ( fullFilePath ) ;
314336 if ( ! Directory . Exists ( directory ) )
315337 FileHelper . CreateDicrectoriesIfNotExist ( directory ) ;
316338
317339 System . IO . File . WriteAllText ( fullFilePath , manifest , System . Text . Encoding . UTF8 ) ;
318340
319- archive . AddFileToArchive ( fullFilePath , "manifest .json") ;
341+ archive . AddFileToArchive ( fullFilePath , manifestFileName + " .json") ;
320342 }
321343 #endregion
322344
@@ -329,7 +351,7 @@ public ActionResult GenerateZip(long id, long versionid, string format)
329351
330352 #endregion terms and conditions
331353
332- string title = datasetVersion . Title ;
354+ title = datasetVersion . Title ;
333355 title = String . IsNullOrEmpty ( title ) ? "unknown" : title ;
334356
335357 string message = string . Format ( "dataset {0} version {1} was downloaded as zip - {2}." , id ,
@@ -406,6 +428,12 @@ private string storeGeneratedFilePathToContentDiscriptor(long datasetId, Dataset
406428 mimeType = "text/comma-separated-values" ;
407429 }
408430
431+ if ( ext . Contains ( "json" ) )
432+ {
433+ name = "datastructure" ;
434+ mimeType = "application/json" ;
435+ }
436+
409437 if ( ext . Contains ( "html" ) )
410438 {
411439 name = title ;
@@ -436,6 +464,8 @@ private string storeGeneratedFilePathToContentDiscriptor(long datasetId, Dataset
436464 {
437465 if ( cd . Name . Equals ( name ) && cd . MimeType . Equals ( mimeType ) )
438466 {
467+ cd . Name = name ;
468+ cd . MimeType = mimeType ;
439469 cd . URI = dynamicPath ;
440470 dm . UpdateContentDescriptor ( cd ) ;
441471 }
@@ -463,7 +493,7 @@ private void generateMetadataAsHtml(DatasetVersion dsv)
463493
464494 string title = dsv . Title ;
465495 Session [ "ShowDataMetadata" ] = dsv . Metadata ;
466-
496+ int versionNr = 0 ;
467497 var view = this . Render ( "DCM" , "Form" , "LoadMetadataOfflineVersion" , new RouteValueDictionary ( )
468498 {
469499 { "entityId" , datasetId } ,
@@ -477,6 +507,7 @@ private void generateMetadataAsHtml(DatasetVersion dsv)
477507
478508 byte [ ] content = Encoding . ASCII . GetBytes ( view . ToString ( ) ) ;
479509
510+
480511 string dynamicPathOfMD = "" ;
481512 dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor ( datasetId , dsv ,
482513 "metadata" , ".html" ) ;
@@ -493,7 +524,7 @@ private void generateDataStructureHtml(DatasetVersion dsv)
493524 } ) ;
494525
495526 byte [ ] content = Encoding . ASCII . GetBytes ( view . ToString ( ) ) ;
496-
527+
497528 string dynamicPathOfMD = "" ;
498529 dynamicPathOfMD = storeGeneratedFilePathToContentDiscriptor ( dsv . Dataset . Id , dsv ,
499530 "datastructure" , ".html" ) ;
0 commit comments