1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using System.ServiceModel;
|
---|
7 | using HeuristicLab.Services.Optimization.ControllerService;
|
---|
8 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
9 | using System.ServiceModel.Description;
|
---|
10 | using System.Web.Security;
|
---|
11 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Services.Optimization.Web.Controllers
|
---|
14 | {
|
---|
15 | [Authorize(Roles="Web User")]
|
---|
16 | public class OptimizationController : Controller
|
---|
17 | {
|
---|
18 | public const string DEFAULT_CONTROLLER_ENDPOINT = "WSHttpBinding_IControllerService";
|
---|
19 |
|
---|
20 | public delegate T ControllerServiceDelegate<T>(IControllerService service);
|
---|
21 | public delegate void ControllerServiceDelegate(IControllerService service);
|
---|
22 |
|
---|
23 | public T withControllerService<T>(ControllerServiceDelegate<T> del) {
|
---|
24 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
25 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
26 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
27 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
28 | var controllerProxy = cf.CreateChannel();
|
---|
29 | return del(controllerProxy);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void withControllerService(ControllerServiceDelegate del) {
|
---|
34 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
35 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
36 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
37 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
38 | var controllerProxy = cf.CreateChannel();
|
---|
39 | del(controllerProxy);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | //
|
---|
43 | // GET: /Optimization/
|
---|
44 |
|
---|
45 | public ActionResult Index()
|
---|
46 | {
|
---|
47 | var optModel = withControllerService<OptimizationModel>((service) => {
|
---|
48 | OptimizationModel model = new OptimizationModel();
|
---|
49 | model.Scenarios = service.GetOptimizationScenarios();
|
---|
50 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
51 | model.Jobs = service.GetJobs(u);
|
---|
52 | return model;
|
---|
53 | });
|
---|
54 | return View(optModel);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ActionResult ProblemParameters(string scenario) {
|
---|
58 | var optScenario = withControllerService<OptimizationScenario>((service) => {
|
---|
59 | return service.GetOptimizationScenarioByName(scenario);
|
---|
60 | });
|
---|
61 | Session["scenario"] = optScenario;
|
---|
62 | return View(optScenario.InputParameters);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ActionResult JobDetails(string jobId) {
|
---|
66 | Job job = withControllerService<Job>((service) => {
|
---|
67 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
68 | return service.GetJob(u, jobId);
|
---|
69 | });
|
---|
70 | var runs = withControllerService<IList<Run>>((service) => {
|
---|
71 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
72 | return service.GetJobResults(u, jobId);
|
---|
73 | });
|
---|
74 | JobDetailsModel jdm = new JobDetailsModel() { Job = job, Runs = runs };
|
---|
75 | return View(jdm);
|
---|
76 | }
|
---|
77 |
|
---|
78 | [HttpPost]
|
---|
79 | public ActionResult JobDetails(Job job) {
|
---|
80 | withControllerService((service) => {
|
---|
81 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
82 | service.DeleteJob(u, job.Id);
|
---|
83 | });
|
---|
84 | return RedirectToAction("Index");
|
---|
85 | }
|
---|
86 |
|
---|
87 | [HttpPost]
|
---|
88 | public ActionResult ProblemParameters(InputParameters parameters) {
|
---|
89 | if (ModelState.IsValid) {
|
---|
90 | return RedirectToAction("AlgorithmParameters", parameters);
|
---|
91 | }
|
---|
92 | return View(parameters);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public ActionResult AlgorithmParameters() {
|
---|
96 | return View((Session["scenario"] as OptimizationScenario).AlgorithmParameters);
|
---|
97 | }
|
---|
98 |
|
---|
99 | [HttpPost]
|
---|
100 | public ActionResult AlgorithmParameters(AlgorithmParameters parameters) {
|
---|
101 | if (ModelState.IsValid) {
|
---|
102 | return RedirectToAction("ScheduleJob");
|
---|
103 | }
|
---|
104 | return View(parameters);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public ActionResult ScheduleJob() {
|
---|
108 | return View(Session["scenario"]);
|
---|
109 | }
|
---|
110 |
|
---|
111 | [HttpPost]
|
---|
112 | public ActionResult ScheduleJob(OptimizationScenario scenario) {
|
---|
113 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
114 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
115 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
116 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
117 | var controllerProxy = cf.CreateChannel();
|
---|
118 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
119 | controllerProxy.ScheduleOptimizationScenario(u, Session["scenario"] as OptimizationScenario);
|
---|
120 | }
|
---|
121 | return RedirectToAction("Index");
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|