Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBAdminPlugin/Controllers/AlgorithmClassController.cs @ 5991

Last change on this file since 5991 was 5763, checked in by wtollsch, 14 years ago

#1433 connection with new AdministrationService established

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