Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBAdminPlugin/Controllers/AlgorithmController.cs @ 9141

Last change on this file since 9141 was 6317, checked in by jwolfing, 13 years ago

#1433 code formatted

File size: 4.7 KB
RevLine 
[6213]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HLWebOKBAdminPlugin.Models;
7using HLWebOKBAdminPlugin.OKBAdministrationService;
[6246]8using System.IO;
[6213]9
[6317]10namespace HLWebOKBAdminPlugin.Controllers {
11  /// <summary>
12  /// Controller for Algorithm Submenu
13  /// </summary>
14  public class AlgorithmController : Controller {
[6213]15    /// <summary>
[6317]16    /// Controller for Index View
[6213]17    /// </summary>
[6317]18    /// <returns></returns>
19    public ActionResult Index() {
20      Session["SelectedSubMenu"] = "Algorithm";
21      AlgorithmModel pm = new AlgorithmModel();
22      return View(pm);
23    }
[6213]24
[6317]25    /// <summary>
26    /// Controller for Index View
27    /// </summary>
28    /// <returns></returns>
29    public ActionResult SortAsc() {
30      Session["SelectedSubMenu"] = "Algorithm";
31      AlgorithmModel pm = new AlgorithmModel();
32      pm.Algorithms = pm.Algorithms.OrderBy(x => x.Name).ToList<Algorithm>();
33      return View("Index", pm);
34    }
[6213]35
[6317]36    /// <summary>
37    /// Controller for Index View
38    /// </summary>
39    /// <returns></returns>
40    public ActionResult SortDesc() {
41      Session["SelectedSubMenu"] = "Algorithm";
42      AlgorithmModel pm = new AlgorithmModel();
43      pm.Algorithms = pm.Algorithms.OrderByDescending(x => x.Name).ToArray<Algorithm>();
44      return View("Index", pm);
45    }
46    /// <summary>
47    /// Controller for Detail View
48    /// </summary>
49    /// <param name="p"></param>
50    /// <returns></returns>
51    public ActionResult Detail(long? id) {
52      Session["SelectedSubMenu"] = "Algorithm";
53      // We use here the AlgorithmMode, but I think
54      // we can also use the Algorithm class. (?)
55      AlgorithmModel pm = new AlgorithmModel();
56      if (id == null)
57        pm.Algorithm = new Algorithm();
58      else
59        pm.Algorithm = (Algorithm)pm.Algorithms.Where(x => x.Id.Equals((long)id)).FirstOrDefault();
60      return View(pm);
61    }
[6213]62
[6317]63    /// <summary>
64    /// Controller for Index View
65    /// </summary>
66    /// <returns></returns>
67    public ActionResult Delete(long id) {
68      Session["SelectedSubMenu"] = "Algorithm";
69      // We use here the AlgorithmMode, but I think
70      // we can also use the Algorithm class. (?)
71      AlgorithmModel pm = new AlgorithmModel();
72      if (id != 0) {
73        pm.DeleteAlgorithm(id);
74      }
75      return View("Index", pm);
76    }
[6213]77
[6317]78    public ActionResult UploadFile(FormCollection collection) {
79      Session["SelectedSubMenu"] = "Algorithm";
[6246]80
[6317]81      long algorithmId = long.Parse(collection.Get("AlgorithmId"));
[6246]82
[6317]83      AlgorithmModel pm = new AlgorithmModel();
[6246]84
[6317]85      foreach (string inputTagName in Request.Files) {
86        HttpPostedFileBase file = Request.Files[inputTagName];
87        if (file.ContentLength > 0) {
88          // maybe for storage on server
89          //string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName));
90          if (file.InputStream.CanRead) {
91            //file length
92            int fileLength = file.ContentLength;
[6246]93
[6317]94            //bytearray
95            byte[] data = new byte[fileLength];
[6246]96
[6317]97            //initialize input stream
98            Stream dataStream = file.InputStream;
[6246]99
[6317]100            //read file into byte array
101            dataStream.Read(data, 0, fileLength);
[6246]102
[6317]103            //save data
104            pm.UpdateAlgorithmData(algorithmId, data);
105          }//if
106        }
107      }//foreach
[6246]108
[6317]109      return View("Index", pm);
110    }//UploadFile
[6246]111
[6317]112    /// <summary>
113    /// Controller for Detail View
114    /// </summary>
115    /// <returns></returns>
116    public ActionResult SaveChanges(FormCollection collection) {
117      long algorithmId = long.Parse(collection.Get("AlgorithmId"));
118      String algorithmName = collection.Get("AlgorithmName");
119      String algorithmDescription = collection.Get("AlgorithmDescription");
120      String algorithmDataTypeName = collection.Get("AlgorithmDataTypeName");
121      long algorithmClassId = long.Parse(collection.Get("AlgorithmClassId"));
122      long platformId = long.Parse(collection.Get("PlatformId"));
[6213]123
[6317]124      // Later, we will get the runs from the session ...
125      AlgorithmModel pm = new AlgorithmModel();
126      if (algorithmId != 0)
127        pm.Algorithm = (Algorithm)pm.Algorithms.Where(x => x.Id.Equals(algorithmId)).FirstOrDefault();
[6213]128
[6317]129      pm.Algorithm.Name = algorithmName;
130      pm.Algorithm.Description = algorithmDescription;
131      pm.Algorithm.DataTypeName = algorithmDataTypeName;
132      pm.Algorithm.DataTypeTypeName = "";
133      pm.Algorithm.AlgorithmClassId = algorithmClassId;
134      pm.Algorithm.PlatformId = platformId;
[6213]135
[6317]136      pm.SaveAlgorithm(pm.Algorithm);
[6213]137
[6317]138      return View("Index", pm);
139    }
140  }
[6213]141}
Note: See TracBrowser for help on using the repository browser.