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 | if (Session["pw"] == null)
|
---|
25 | Response.Redirect("/Account/Logon");
|
---|
26 |
|
---|
27 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
28 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
29 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
30 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
31 | var controllerProxy = cf.CreateChannel();
|
---|
32 | return del(controllerProxy);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public void withControllerService(ControllerServiceDelegate del) {
|
---|
37 | if (Session["pw"] == null)
|
---|
38 | Response.Redirect("/Account/Logon");
|
---|
39 |
|
---|
40 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
41 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
42 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
43 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
44 | var controllerProxy = cf.CreateChannel();
|
---|
45 | del(controllerProxy);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | //
|
---|
49 | // GET: /Optimization/
|
---|
50 |
|
---|
51 | public ActionResult Index()
|
---|
52 | {
|
---|
53 | var optModel = withControllerService<OptimizationModel>((service) => {
|
---|
54 | OptimizationModel model = new OptimizationModel();
|
---|
55 | model.Scenarios = service.GetOptimizationScenarios();
|
---|
56 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
57 | model.Jobs = service.GetJobs(u);
|
---|
58 | return model;
|
---|
59 | });
|
---|
60 | return View(optModel);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public ActionResult ProblemParameters(string scenario) {
|
---|
64 | var optScenario = withControllerService<OptimizationScenario>((service) => {
|
---|
65 | return service.GetOptimizationScenarioByName(scenario);
|
---|
66 | });
|
---|
67 | Session["scenario"] = optScenario;
|
---|
68 | return View(optScenario.InputParameters);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public ActionResult JobDetails(string jobId) {
|
---|
72 | Job job = withControllerService<Job>((service) => {
|
---|
73 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
74 | return service.GetJob(u, jobId);
|
---|
75 | });
|
---|
76 | var runs = withControllerService<IList<Run>>((service) => {
|
---|
77 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
78 | return service.GetJobResults(u, jobId);
|
---|
79 | });
|
---|
80 | JobDetailsModel jdm = new JobDetailsModel() { Job = job, Runs = runs };
|
---|
81 | Session["jobDetails"] = jdm;
|
---|
82 | return View(jdm);
|
---|
83 | }
|
---|
84 |
|
---|
85 | [HttpPost]
|
---|
86 | public ActionResult JobDetails(Job job) {
|
---|
87 | withControllerService((service) => {
|
---|
88 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
89 | service.DeleteJob(u, job.Id);
|
---|
90 | });
|
---|
91 | return RedirectToAction("Index");
|
---|
92 | }
|
---|
93 |
|
---|
94 | [HttpPost]
|
---|
95 | public ActionResult ProblemParameters(InputParameters parameters) {
|
---|
96 | if (ModelState.IsValid) {
|
---|
97 | return RedirectToAction("AlgorithmParameters", parameters);
|
---|
98 | }
|
---|
99 | return View(parameters);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public ActionResult AlgorithmParameters() {
|
---|
103 | return View((Session["scenario"] as OptimizationScenario).AlgorithmParameters);
|
---|
104 | }
|
---|
105 |
|
---|
106 | [HttpPost]
|
---|
107 | public ActionResult AlgorithmParameters(AlgorithmParameters parameters) {
|
---|
108 | if (ModelState.IsValid) {
|
---|
109 | return RedirectToAction("ScheduleJob");
|
---|
110 | }
|
---|
111 | return View(parameters);
|
---|
112 | }
|
---|
113 |
|
---|
114 | public ActionResult ScheduleJob() {
|
---|
115 | return View(Session["scenario"]);
|
---|
116 | }
|
---|
117 |
|
---|
118 | [HttpPost]
|
---|
119 | public ActionResult ScheduleJob(OptimizationScenario scenario) {
|
---|
120 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
121 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
122 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
123 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
124 | var controllerProxy = cf.CreateChannel();
|
---|
125 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
126 | controllerProxy.ScheduleOptimizationScenario(u, Session["scenario"] as OptimizationScenario);
|
---|
127 | }
|
---|
128 | return RedirectToAction("Index");
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|