1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HLWebOKBAdminPlugin.ViewModels;
|
---|
7 | using System.Web.Security;
|
---|
8 | using System.ServiceModel.Security;
|
---|
9 | //using HLWebPluginHost.OKBService;
|
---|
10 | using System.Diagnostics;
|
---|
11 | using HLWebOKBAdminPlugin.OKBAdministrationService;
|
---|
12 | using HLWebOKBAdminPlugin.OKBAuthenticationService;
|
---|
13 |
|
---|
14 | namespace HLWebOKBAdminPlugin.Controllers
|
---|
15 | {
|
---|
16 | public class AlgorithmClassController : Controller
|
---|
17 | {
|
---|
18 | //BASICS: controllers respond to input from the browser, decide what to do and return
|
---|
19 | // response to the user (Views)
|
---|
20 |
|
---|
21 | // for edit form 2 controller actions are needed
|
---|
22 | // first controller action if form is first loaded to display algorithm class data
|
---|
23 | // second controller handles the post action
|
---|
24 |
|
---|
25 | //returns a certificated service client for current user .... call this function in every method
|
---|
26 |
|
---|
27 | //private OKBService.OKBServiceClient CertificateValidator() {
|
---|
28 | // OKBService.OKBServiceClient sc = new OKBService.OKBServiceClient();
|
---|
29 | // sc.ClientCredentials.UserName.UserName = Membership.GetUser().UserName;
|
---|
30 | // //sc.ClientCredentials.UserName.Password = HttpContext.Session["pwd"].ToString();
|
---|
31 | // sc.ClientCredentials.UserName.Password = "Gerhard";
|
---|
32 | // sc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
33 |
|
---|
34 | // return sc;
|
---|
35 | //}//CertificateValidator
|
---|
36 |
|
---|
37 | private AdministrationServiceClient CertificateValidator() {
|
---|
38 | AdministrationServiceClient asc = new AdministrationServiceClient();
|
---|
39 |
|
---|
40 | asc.ClientCredentials.UserName.UserName = Membership.GetUser().UserName;
|
---|
41 | //asc.ClientCredentials.UserName.Password = HttpContext.Session["pwd"].ToString();
|
---|
42 | asc.ClientCredentials.UserName.Password = "Gerhard";
|
---|
43 | asc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
|
---|
44 |
|
---|
45 | return asc;
|
---|
46 | }//CertificateValidator
|
---|
47 |
|
---|
48 | //
|
---|
49 | // GET: /AlgorithmClass/
|
---|
50 |
|
---|
51 | public ActionResult Index() {
|
---|
52 | //the index action is the homepage of the site (AlgorithmClass)
|
---|
53 | //what happens: generate list of AlgorithmClass and commit the list to the index view
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | //get certificated service client
|
---|
58 | AdministrationServiceClient asc = CertificateValidator();
|
---|
59 |
|
---|
60 | var tstModel = new AlgorithmClassViewModel {
|
---|
61 | AlgorithmClass = new AlgorithmClass()
|
---|
62 | };
|
---|
63 |
|
---|
64 | Debug.WriteLine("tstModel created");
|
---|
65 |
|
---|
66 | //get list of Algorithm Classes
|
---|
67 | var algorithmClasses = new List<AlgorithmClass>();
|
---|
68 | AlgorithmClass[] ac = asc.GetAlgorithmClasses();
|
---|
69 | foreach (AlgorithmClass a in ac) {
|
---|
70 | algorithmClasses.Add(a);
|
---|
71 | }//foreach
|
---|
72 |
|
---|
73 | //return Index View that contains a list of all AlgorithmClasses (Views/Index.aspx)
|
---|
74 | return View(algorithmClasses);
|
---|
75 | }//Index
|
---|
76 |
|
---|
77 | //
|
---|
78 | // GET: /AlgorithmClass/Details/5
|
---|
79 |
|
---|
80 | public ActionResult Details(int id) {
|
---|
81 | //what happens: get AlgorithmClass object , create a viewmodel which contains the data
|
---|
82 | // for the view and commit the viewmodel to the view
|
---|
83 | // => correlates to a HTTP GET request
|
---|
84 |
|
---|
85 | //get certificated service client
|
---|
86 | AdministrationServiceClient asc = CertificateValidator();
|
---|
87 |
|
---|
88 | //the viewmodel contains data for the view
|
---|
89 | var viewModel = new AlgorithmClassViewModel {
|
---|
90 | AlgorithmClass = asc.GetAlgorithmClass(id)
|
---|
91 | };
|
---|
92 |
|
---|
93 | ViewData["AlgorithmClass"] = viewModel;
|
---|
94 |
|
---|
95 | return View();
|
---|
96 | }//Details
|
---|
97 |
|
---|
98 | //
|
---|
99 | // GET: /AlgorithmClass/Create
|
---|
100 |
|
---|
101 | public ActionResult Create() {
|
---|
102 | //what happens: create empty AlgorithmClass object , create a viewmodel which contains the data
|
---|
103 | // for the view and commit the viewmodel to the view
|
---|
104 | // this Create action is needed for first time call of the page to display content
|
---|
105 |
|
---|
106 | //get certificated service client
|
---|
107 | AdministrationServiceClient asc = CertificateValidator();
|
---|
108 |
|
---|
109 | //the viewmodel with empty AlgorithmClass object
|
---|
110 | var viewModel = new AlgorithmClassViewModel {
|
---|
111 | AlgorithmClass = new AlgorithmClass()
|
---|
112 | };
|
---|
113 |
|
---|
114 | ViewData["AlgorithmClass"] = viewModel;
|
---|
115 |
|
---|
116 | return View();
|
---|
117 | }//Create
|
---|
118 |
|
---|
119 | //
|
---|
120 | // POST: /AlgorithmClass/Create
|
---|
121 |
|
---|
122 | [HttpPost]
|
---|
123 | public ActionResult Create(AlgorithmClass algorithmClass) {
|
---|
124 | //what happens: the 2nd Create controller handles the POST action from the form
|
---|
125 | // an AlgorithmClass object is passed from the Create view by submit
|
---|
126 |
|
---|
127 | try {
|
---|
128 | //get certificated service client
|
---|
129 | AdministrationServiceClient asc = CertificateValidator();
|
---|
130 |
|
---|
131 | //create new AlgorithmClass object
|
---|
132 | AlgorithmClass ac = new AlgorithmClass();
|
---|
133 | ac.Id = algorithmClass.Id;
|
---|
134 | ac.Name = algorithmClass.Name;
|
---|
135 | ac.Description = algorithmClass.Description;
|
---|
136 |
|
---|
137 | //add new AlgorithmClass
|
---|
138 | asc.AddAlgorithmClass(ac);
|
---|
139 |
|
---|
140 | //redirect to the home page
|
---|
141 | return RedirectToAction("Index");
|
---|
142 | }
|
---|
143 | catch {
|
---|
144 | //invalid - redisplay with errors
|
---|
145 | var viewModel = new AlgorithmClassViewModel {
|
---|
146 | AlgorithmClass = algorithmClass
|
---|
147 | };
|
---|
148 |
|
---|
149 | ViewData["AlgorithmClass"] = viewModel;
|
---|
150 |
|
---|
151 | return View();
|
---|
152 | }
|
---|
153 | }//Create - post
|
---|
154 |
|
---|
155 | //
|
---|
156 | // GET: /AlgorithmClass/Edit/5
|
---|
157 |
|
---|
158 | public ActionResult Edit(int id) {
|
---|
159 | //what happens: get AlgorithmClass object , create a viewmodel which contains the data
|
---|
160 | // for the view and commit the viewmodel to the view
|
---|
161 | // => correlates to a HTTP GET request
|
---|
162 |
|
---|
163 | //get certificated service client
|
---|
164 | AdministrationServiceClient asc = CertificateValidator();
|
---|
165 |
|
---|
166 | //the viewmodel contains data for the view
|
---|
167 | var viewModel = new AlgorithmClassViewModel {
|
---|
168 | AlgorithmClass = asc.GetAlgorithmClass(id)
|
---|
169 | };
|
---|
170 |
|
---|
171 | ViewData["AlgorithmClass"] = viewModel;
|
---|
172 |
|
---|
173 | return View();
|
---|
174 | }//Edit
|
---|
175 |
|
---|
176 | //
|
---|
177 | // POST: /AlgorithmClass/Edit/5
|
---|
178 |
|
---|
179 | [HttpPost]
|
---|
180 | public ActionResult Edit(int id, FormCollection collection) {
|
---|
181 | //what happens: the 2nd Edit controller handles the POST action from the form
|
---|
182 | // an AlgorithmClass object is passed from the Edit view by submit
|
---|
183 |
|
---|
184 | //get certificated service client
|
---|
185 | AdministrationServiceClient asc = CertificateValidator();
|
---|
186 |
|
---|
187 | //get AlgorithmClass object from DB
|
---|
188 | var algorithmClass = asc.GetAlgorithmClass(id);
|
---|
189 |
|
---|
190 | try {
|
---|
191 | // update AlgorithmClass
|
---|
192 | UpdateModel(algorithmClass, "AlgorithmClass");
|
---|
193 |
|
---|
194 | AlgorithmClass ac = new AlgorithmClass();
|
---|
195 | ac.Id = algorithmClass.Id;
|
---|
196 | ac.Name = algorithmClass.Name;
|
---|
197 | ac.Description = algorithmClass.Description;
|
---|
198 |
|
---|
199 | asc.UpdateAlgorithmClass(ac);
|
---|
200 |
|
---|
201 | return RedirectToAction("Index");
|
---|
202 | }
|
---|
203 | catch {
|
---|
204 | var viewModel = new AlgorithmClassViewModel {
|
---|
205 | AlgorithmClass = algorithmClass
|
---|
206 | };
|
---|
207 |
|
---|
208 | ViewData["AlgorithmClass"] = viewModel;
|
---|
209 |
|
---|
210 | return View();
|
---|
211 | }
|
---|
212 | }//Edit
|
---|
213 |
|
---|
214 | //
|
---|
215 | // GET: /AlgorithmClass/Delete/5
|
---|
216 |
|
---|
217 | public ActionResult Delete(int id) {
|
---|
218 | //what happens: get AlgorithmClass object , create a viewmodel which contains the data
|
---|
219 | // for the view and commit the viewmodel to the view
|
---|
220 | // => correlates to a HTTP GET request
|
---|
221 |
|
---|
222 | //get certificated service client
|
---|
223 | AdministrationServiceClient asc = CertificateValidator();
|
---|
224 |
|
---|
225 | //the viewmodel contains data for the view
|
---|
226 | var viewModel = new AlgorithmClassViewModel {
|
---|
227 | AlgorithmClass = asc.GetAlgorithmClass(id)
|
---|
228 | };
|
---|
229 |
|
---|
230 | ViewData["AlgorithmClass"] = viewModel;
|
---|
231 |
|
---|
232 | return View();
|
---|
233 | }//Delete
|
---|
234 |
|
---|
235 | //
|
---|
236 | // POST: /AlgorithmClass/Delete/5
|
---|
237 |
|
---|
238 | [HttpPost]
|
---|
239 | public ActionResult Delete(int id, FormCollection collection) {
|
---|
240 | //what happens: the 2nd Delete controller handles the POST action from the form
|
---|
241 | // an AlgorithmClass object is passed from the Delete view by submit
|
---|
242 |
|
---|
243 | //get certificated service client
|
---|
244 | AdministrationServiceClient asc = CertificateValidator();
|
---|
245 |
|
---|
246 | //load object from db
|
---|
247 | var algorithmClass = asc.GetAlgorithmClass(id);
|
---|
248 |
|
---|
249 | try {
|
---|
250 | //delete object
|
---|
251 | asc.DeleteAlgorithmClass(id);
|
---|
252 |
|
---|
253 | return RedirectToAction("Index");
|
---|
254 | }
|
---|
255 | catch {
|
---|
256 | var viewModel = new AlgorithmClassViewModel {
|
---|
257 | AlgorithmClass = algorithmClass
|
---|
258 | };
|
---|
259 |
|
---|
260 | ViewData["AlgorithmClass"] = viewModel;
|
---|
261 |
|
---|
262 | return View();
|
---|
263 | }
|
---|
264 | }//Delete
|
---|
265 |
|
---|
266 | }
|
---|
267 | }
|
---|