Skip to content

Commit

Permalink
OIZ-58 enabled the downloading of materials
Browse files Browse the repository at this point in the history
  • Loading branch information
Nityan Khanna committed May 7, 2018
1 parent d69ea76 commit 3dadc9d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 54 deletions.
86 changes: 51 additions & 35 deletions OpenIZAdmin.Services/Dataset/DatasetService.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
/*
* Copyright 2016-2018 Mohawk College of Applied Arts and Technology
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*
* User: khannan
* Date: 2018-5-7
*/

using OpenIZ.Core.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using OpenIZ.Core.Model;
using OpenIZ.Core.Model.Entities;

namespace OpenIZAdmin.Services.Dataset
{
Expand Down Expand Up @@ -57,29 +53,23 @@ public DatasetInstall ConvertToDataset(object instance, Type type)
}

/// <summary>
/// Converts an entity or derived entity to a dataset instance.
/// Gets the dataset as stream.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="type">The type.</param>
/// <returns>Returns the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the type does not inherit from <see cref="IdentifiedData"/>.</exception>
private DatasetInstall ConvertToDatasetInternal(object instance, Type type)
/// <param name="datasetInstall">The dataset install.</param>
/// <returns>Returns the dataset as a <see cref="Stream" />.</returns>
public Stream GetDatasetAsStream(DatasetInstall datasetInstall)
{
if (type != typeof(IdentifiedData))
{
throw new ArgumentException($"The type {type} must be a derived type of {typeof(IdentifiedData)}");
}
var content = this.Serialize(datasetInstall);

var datasetInstall = new DatasetInstall(Guid.NewGuid().ToString());
var stream = new MemoryStream();

datasetInstall.Action.Add(new DataUpdate
{
InsertIfNotExists = true,
Element = (IdentifiedData)instance,
IgnoreErrors = false
});
var streamWriter = new StreamWriter(stream);

return datasetInstall;
streamWriter.Write(content);
streamWriter.Flush();
stream.Position = 0;

return stream;
}

/// <summary>
Expand All @@ -101,5 +91,31 @@ public string Serialize(DatasetInstall datasetInstall)

return result;
}

/// <summary>
/// Converts an entity or derived entity to a dataset instance.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="type">The type.</param>
/// <returns>Returns the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the type does not inherit from <see cref="IdentifiedData"/>.</exception>
private DatasetInstall ConvertToDatasetInternal(object instance, Type type)
{
if (!type.IsSubclassOf(typeof(IdentifiedData)))
{
throw new ArgumentException($"The type {type} must be a derived type of {typeof(IdentifiedData)}");
}

var datasetInstall = new DatasetInstall(Guid.NewGuid().ToString());

datasetInstall.Action.Add(new DataUpdate
{
InsertIfNotExists = true,
Element = (IdentifiedData)instance,
IgnoreErrors = false
});

return datasetInstall;
}
}
}
}
38 changes: 21 additions & 17 deletions OpenIZAdmin.Services/Dataset/IDatasetService.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
/*
* Copyright 2016-2018 Mohawk College of Applied Arts and Technology
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*
* User: khannan
* Date: 2018-5-7
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OpenIZ.Core.Model;
using OpenIZ.Core.Model.Entities;
using System;
using System.IO;

namespace OpenIZAdmin.Services.Dataset
{
Expand All @@ -47,11 +44,18 @@ public interface IDatasetService
/// <returns>Returns the dataset.</returns>
DatasetInstall ConvertToDataset(object instance, Type type);

/// <summary>
/// Gets the dataset as stream.
/// </summary>
/// <param name="datasetInstall">The dataset install.</param>
/// <returns>Returns the dataset as a <see cref="Stream"/>.</returns>
Stream GetDatasetAsStream(DatasetInstall datasetInstall);

/// <summary>
/// Serializes the specified dataset install.
/// </summary>
/// <param name="datasetInstall">The dataset install.</param>
/// <returns>Returns the dataset install as an XML string.</returns>
string Serialize(DatasetInstall datasetInstall);
}
}
}
29 changes: 27 additions & 2 deletions OpenIZAdmin/Controllers/MaterialController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Web.Mvc;
using OpenIZAdmin.Services.Dataset;

Expand Down Expand Up @@ -489,16 +491,39 @@ public ActionResult DownloadAsDataset(Guid id)
return RedirectToAction("ViewMaterial", "Material", id);
}

var dataset = this.datasetService.ConvertToDataset(material);
var stream = this.datasetService.GetDatasetAsStream(this.datasetService.ConvertToDataset(material));
string name;

if (material.Names.Any(n => n.NameUseKey == NameUseKeys.Assigned))
{
name = string.Join(" ", material.Names.Where(n => n.NameUseKey == NameUseKeys.Assigned).SelectMany(n => n.Component).Select(c => c.Value));
}
else if (material.Names.Any(n => n.NameUseKey == NameUseKeys.OfficialRecord))
{
name = string.Join(" ", material.Names.Where(n => n.NameUseKey == NameUseKeys.OfficialRecord).SelectMany(n => n.Component).Select(c => c.Value));
}
else
{
name = string.Join(" ", material.Names.SelectMany(n => n.Component).Select(c => c.Value));
}

var contentDisposition = new ContentDisposition
{
FileName = $"Material-{name} {id}.xml",
Inline = false
};

this.Response.AppendHeader("Content-Disposition", contentDisposition.ToString());

return File(stream, MediaTypeNames.Text.Xml);
}
catch (Exception e)
{
this.TempData["error"] = Locale.UnexpectedErrorMessage;
Trace.TraceError($"Unable to download material: {e}");
}

return RedirectToAction("ViewMaterial", "Material", id);
return RedirectToAction("ViewMaterial", "Material", new { id, versionId = Guid.Empty });
}

/// <summary>
Expand Down

0 comments on commit 3dadc9d

Please sign in to comment.