Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1198 MVC2 plugin template added for HL OKBWebClient

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