1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HLWebOKBAdminPlugin.Models;
|
---|
7 | using HLWebOKBAdminPlugin.OKBAdministrationService;
|
---|
8 | using System.IO;
|
---|
9 |
|
---|
10 | namespace HLWebOKBAdminPlugin.Controllers {
|
---|
11 | /// <summary>
|
---|
12 | /// Controller for Algorithm Submenu
|
---|
13 | /// </summary>
|
---|
14 | public class AlgorithmController : Controller {
|
---|
15 | /// <summary>
|
---|
16 | /// Controller for Index View
|
---|
17 | /// </summary>
|
---|
18 | /// <returns></returns>
|
---|
19 | public ActionResult Index() {
|
---|
20 | Session["SelectedSubMenu"] = "Algorithm";
|
---|
21 | AlgorithmModel pm = new AlgorithmModel();
|
---|
22 | return View(pm);
|
---|
23 | }
|
---|
24 |
|
---|
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 | }
|
---|
35 |
|
---|
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 | }
|
---|
62 |
|
---|
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 | }
|
---|
77 |
|
---|
78 | public ActionResult UploadFile(FormCollection collection) {
|
---|
79 | Session["SelectedSubMenu"] = "Algorithm";
|
---|
80 |
|
---|
81 | long algorithmId = long.Parse(collection.Get("AlgorithmId"));
|
---|
82 |
|
---|
83 | AlgorithmModel pm = new AlgorithmModel();
|
---|
84 |
|
---|
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;
|
---|
93 |
|
---|
94 | //bytearray
|
---|
95 | byte[] data = new byte[fileLength];
|
---|
96 |
|
---|
97 | //initialize input stream
|
---|
98 | Stream dataStream = file.InputStream;
|
---|
99 |
|
---|
100 | //read file into byte array
|
---|
101 | dataStream.Read(data, 0, fileLength);
|
---|
102 |
|
---|
103 | //save data
|
---|
104 | pm.UpdateAlgorithmData(algorithmId, data);
|
---|
105 | }//if
|
---|
106 | }
|
---|
107 | }//foreach
|
---|
108 |
|
---|
109 | return View("Index", pm);
|
---|
110 | }//UploadFile
|
---|
111 |
|
---|
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"));
|
---|
123 |
|
---|
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();
|
---|
128 |
|
---|
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;
|
---|
135 |
|
---|
136 | pm.SaveAlgorithm(pm.Algorithm);
|
---|
137 |
|
---|
138 | return View("Index", pm);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|