1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
7 | using System.IO;
|
---|
8 | using System.Text;
|
---|
9 | using System.Xml;
|
---|
10 | using HeuristicLab.Services.Optimization.ControllerService;
|
---|
11 | using System.ServiceModel;
|
---|
12 | using System.ServiceModel.Description;
|
---|
13 | using System.Web.Security;
|
---|
14 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Services.Optimization.Web.Controllers
|
---|
17 | {
|
---|
18 | [Authorize(Roles = "Web User")]
|
---|
19 | public class AdminController : Controller
|
---|
20 | {
|
---|
21 | public delegate T ControllerServiceDelegate<T>(IControllerService service);
|
---|
22 | public delegate void ControllerServiceDelegate(IControllerService service);
|
---|
23 |
|
---|
24 | public T withControllerService<T>(ControllerServiceDelegate<T> del) {
|
---|
25 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
26 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
27 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
28 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
29 | var controllerProxy = cf.CreateChannel();
|
---|
30 | return del(controllerProxy);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void withControllerService(ControllerServiceDelegate del) {
|
---|
35 | using (var cf = new ChannelFactory<IControllerService>(OptimizationController.DEFAULT_CONTROLLER_ENDPOINT)) {
|
---|
36 | var credentialBehaviour = cf.Endpoint.Behaviors.Find<ClientCredentials>();
|
---|
37 | credentialBehaviour.UserName.UserName = Membership.GetUser().UserName;
|
---|
38 | credentialBehaviour.UserName.Password = Session["pw"] as string;
|
---|
39 | var controllerProxy = cf.CreateChannel();
|
---|
40 | del(controllerProxy);
|
---|
41 | }
|
---|
42 | }
|
---|
43 | //
|
---|
44 | // GET: /Admin/
|
---|
45 | [HttpGet]
|
---|
46 | public ActionResult Index()
|
---|
47 | {
|
---|
48 | var names = withControllerService<IEnumerable<string>>((service) => {
|
---|
49 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
50 | return service.GetOptimizationScenarioNames();
|
---|
51 | });
|
---|
52 | return View(names);
|
---|
53 | }
|
---|
54 |
|
---|
55 | [HttpPost]
|
---|
56 | public ActionResult AddScenario(UploadScenario newSecnario) {
|
---|
57 | StreamReader reader = new StreamReader(newSecnario.ScenarioMapper.InputStream);
|
---|
58 | string scenarioMapper = reader.ReadToEnd();
|
---|
59 |
|
---|
60 | XmlDocument doc = new XmlDocument();
|
---|
61 | doc.Load(newSecnario.ScenarioXml.InputStream);
|
---|
62 | string scenarioXml;
|
---|
63 | using (var stringWriter = new StringWriter())
|
---|
64 | using (var xmlTextWriter = XmlWriter.Create(stringWriter)) {
|
---|
65 | doc.WriteTo(xmlTextWriter);
|
---|
66 | xmlTextWriter.Flush();
|
---|
67 | scenarioXml = stringWriter.GetStringBuilder().ToString();
|
---|
68 | }
|
---|
69 |
|
---|
70 | var ok = withControllerService<bool>((service) => {
|
---|
71 | OptimizationModel model = new OptimizationModel();
|
---|
72 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
73 | return service.AddHiveScenario(u, scenarioXml, scenarioMapper);
|
---|
74 | });
|
---|
75 |
|
---|
76 | return RedirectToAction("Index");
|
---|
77 | }
|
---|
78 |
|
---|
79 | public ActionResult DeleteScenario(string id) {
|
---|
80 | withControllerService<bool>((service) => {
|
---|
81 | User u = new User() { Username = Membership.GetUser().UserName, Password = Session["pw"] as string };
|
---|
82 | return service.DeleteHiveScenario(u, id);
|
---|
83 | });
|
---|
84 | return RedirectToAction("Index");
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 | }
|
---|