Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6274 was 6246, checked in by wtollsch, 14 years ago

#1433 AlgorithmClass, Algorithm, ProblemClass, Problem create/update appropriate controller/models/views, File upload (Algorithm / Problem)

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