[8384] | 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;
|
---|
[8545] | 11 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
[8384] | 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";
|
---|
[8545] | 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) {
|
---|
[9062] | 24 | if (Session["pw"] == null)
|
---|
| 25 | Response.Redirect("/Account/Logon");
|
---|
| 26 |
|
---|
[8545] | 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) {
|
---|
[9062] | 37 | if (Session["pw"] == null)
|
---|
| 38 | Response.Redirect("/Account/Logon");
|
---|
| 39 |
|
---|
| 40 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
[8545] | 41 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
| 42 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
[9062] | 43 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
[8545] | 44 | var controllerProxy = cf.CreateChannel();
|
---|
| 45 | del(controllerProxy);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
[8384] | 48 | //
|
---|
| 49 | // GET: /Optimization/
|
---|
| 50 |
|
---|
| 51 | public ActionResult Index()
|
---|
| 52 | {
|
---|
[8545] | 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);
|
---|
[8384] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public ActionResult ProblemParameters(string scenario) {
|
---|
[8545] | 64 | var optScenario = withControllerService<OptimizationScenario>((service) => {
|
---|
| 65 | return service.GetOptimizationScenarioByName(scenario);
|
---|
| 66 | });
|
---|
| 67 | Session["scenario"] = optScenario;
|
---|
[8506] | 68 | return View(optScenario.InputParameters);
|
---|
[8384] | 69 | }
|
---|
| 70 |
|
---|
[8545] | 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 };
|
---|
[9062] | 81 | Session["jobDetails"] = jdm;
|
---|
[8545] | 82 | return View(jdm);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[8384] | 85 | [HttpPost]
|
---|
[8545] | 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]
|
---|
[8506] | 95 | public ActionResult ProblemParameters(InputParameters parameters) {
|
---|
[8384] | 96 | if (ModelState.IsValid) {
|
---|
[8506] | 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");
|
---|
[8384] | 110 | }
|
---|
[8506] | 111 | return View(parameters);
|
---|
[8384] | 112 | }
|
---|
| 113 |
|
---|
[8506] | 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();
|
---|
[8545] | 125 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
| 126 | controllerProxy.ScheduleOptimizationScenario(u, Session["scenario"] as OptimizationScenario);
|
---|
[8506] | 127 | }
|
---|
| 128 | return RedirectToAction("Index");
|
---|
| 129 | }
|
---|
[8384] | 130 | }
|
---|
| 131 | }
|
---|