using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HLWebOKBAdminPlugin.Models;
using HLWebOKBAdminPlugin.OKBAdministrationService;
using System.IO;
namespace HLWebOKBAdminPlugin.Controllers {
///
/// Controller for Algorithm Submenu
///
public class AlgorithmController : Controller {
///
/// Controller for Index View
///
///
public ActionResult Index() {
Session["SelectedSubMenu"] = "Algorithm";
AlgorithmModel pm = new AlgorithmModel();
return View(pm);
}
///
/// Controller for Index View
///
///
public ActionResult SortAsc() {
Session["SelectedSubMenu"] = "Algorithm";
AlgorithmModel pm = new AlgorithmModel();
pm.Algorithms = pm.Algorithms.OrderBy(x => x.Name).ToList();
return View("Index", pm);
}
///
/// Controller for Index View
///
///
public ActionResult SortDesc() {
Session["SelectedSubMenu"] = "Algorithm";
AlgorithmModel pm = new AlgorithmModel();
pm.Algorithms = pm.Algorithms.OrderByDescending(x => x.Name).ToArray();
return View("Index", pm);
}
///
/// Controller for Detail View
///
///
///
public ActionResult Detail(long? id) {
Session["SelectedSubMenu"] = "Algorithm";
// We use here the AlgorithmMode, but I think
// we can also use the Algorithm class. (?)
AlgorithmModel pm = new AlgorithmModel();
if (id == null)
pm.Algorithm = new Algorithm();
else
pm.Algorithm = (Algorithm)pm.Algorithms.Where(x => x.Id.Equals((long)id)).FirstOrDefault();
return View(pm);
}
///
/// Controller for Index View
///
///
public ActionResult Delete(long id) {
Session["SelectedSubMenu"] = "Algorithm";
// We use here the AlgorithmMode, but I think
// we can also use the Algorithm class. (?)
AlgorithmModel pm = new AlgorithmModel();
if (id != 0) {
pm.DeleteAlgorithm(id);
}
return View("Index", pm);
}
public ActionResult UploadFile(FormCollection collection) {
Session["SelectedSubMenu"] = "Algorithm";
long algorithmId = long.Parse(collection.Get("AlgorithmId"));
AlgorithmModel pm = new AlgorithmModel();
foreach (string inputTagName in Request.Files) {
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0) {
// maybe for storage on server
//string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
if (file.InputStream.CanRead) {
//file length
int fileLength = file.ContentLength;
//bytearray
byte[] data = new byte[fileLength];
//initialize input stream
Stream dataStream = file.InputStream;
//read file into byte array
dataStream.Read(data, 0, fileLength);
//save data
pm.UpdateAlgorithmData(algorithmId, data);
}//if
}
}//foreach
return View("Index", pm);
}//UploadFile
///
/// Controller for Detail View
///
///
public ActionResult SaveChanges(FormCollection collection) {
long algorithmId = long.Parse(collection.Get("AlgorithmId"));
String algorithmName = collection.Get("AlgorithmName");
String algorithmDescription = collection.Get("AlgorithmDescription");
String algorithmDataTypeName = collection.Get("AlgorithmDataTypeName");
long algorithmClassId = long.Parse(collection.Get("AlgorithmClassId"));
long platformId = long.Parse(collection.Get("PlatformId"));
// Later, we will get the runs from the session ...
AlgorithmModel pm = new AlgorithmModel();
if (algorithmId != 0)
pm.Algorithm = (Algorithm)pm.Algorithms.Where(x => x.Id.Equals(algorithmId)).FirstOrDefault();
pm.Algorithm.Name = algorithmName;
pm.Algorithm.Description = algorithmDescription;
pm.Algorithm.DataTypeName = algorithmDataTypeName;
pm.Algorithm.DataTypeTypeName = "";
pm.Algorithm.AlgorithmClassId = algorithmClassId;
pm.Algorithm.PlatformId = platformId;
pm.SaveAlgorithm(pm.Algorithm);
return View("Index", pm);
}
}
}