Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Console/3.2/AddJobForm.cs @ 2806

Last change on this file since 2806 was 2092, checked in by svonolfe, 15 years ago

Small refactoring (#372)

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Hive.Contracts.Interfaces;
31using HeuristicLab.Hive.Contracts.BusinessObjects;
32using HeuristicLab.Hive.Contracts;
33using HeuristicLab.Hive.JobBase;
34using HeuristicLab.Core;
35using System.ServiceModel;
36
37namespace HeuristicLab.Hive.Server.ServerConsole {
38
39  public delegate void addDelegate();
40
41  public partial class AddJobForm : Form {
42
43    public event addDelegate addJobEvent;
44
45    ResponseList<Project> projects = null;
46    IJobManager jobManager;
47    IClientManager clientManager;
48    ResponseList<ClientGroup> clientGroups;
49
50    Dictionary<Guid, string> clients = null;
51
52    public AddJobForm() {
53      InitializeComponent();
54
55      clients = new Dictionary<Guid, string>();
56      AddJob();
57
58    }
59
60    private void AddJob() {
61      try {
62
63      jobManager =
64        ServiceLocator.GetJobManager();
65      projects = jobManager.GetAllProjects();
66      clientManager =
67        ServiceLocator.GetClientManager();
68      clientGroups = clientManager.GetAllClientGroups();
69      cbProject.Items.Add("none");
70      cbProject.SelectedIndex = 0;
71      foreach (Project project in projects.List) {
72        cbProject.Items.Add(project.Name);
73      }
74
75      AddClientGroups();
76
77      foreach (KeyValuePair<Guid, string> kvp in clients) {
78        lbGroupsOut.Items.Add(kvp.Value + " (" + kvp.Key + ")");
79      }
80      }
81      catch (FaultException fe) {
82        MessageBox.Show(fe.Message);
83      }
84
85    }
86
87    private void AddClientGroups() {
88     foreach (ClientGroup cg in clientGroups.List) {
89       if (cg.Id != Guid.Empty)
90       clients.Add(cg.Id, cg.Name);
91        AddClientOrGroup(cg);
92      }
93    }
94
95    private void AddClientOrGroup(ClientGroup clientGroup) {
96      foreach (Resource resource in clientGroup.Resources) {
97        if (resource is ClientGroup) {
98          if (resource.Id != Guid.Empty)
99          clients.Add(resource.Id, resource.Name);
100          AddClientOrGroup(resource as ClientGroup);
101        }
102      }
103   
104    }
105
106    private void BtnAdd_Click(object sender, EventArgs e) {
107      try {
108        lblError.Text = "";
109        int numJobs = Convert.ToInt32(tbNumJobs.Text);
110        if (numJobs > 0) {
111          for (int i = 0; i < numJobs; i++) {
112            Job job = new Job { State = State.offline, CoresNeeded = 1 };
113           
114            // if project selected (0 -> none)
115            if (cbProject.SelectedIndex != 0) {
116              job.Project = projects.List[cbProject.SelectedIndex - 1];
117            }
118
119            if (!cbAllGroups.Checked) {
120              List<Guid> groupsToCalculate = new List<Guid>();
121              foreach (string item in lbGroupsIn.Items) {
122                int start = item.IndexOf("(");
123                int end = item.IndexOf(")");
124                string substring = item.Substring(start + 1, end - start - 1);
125                Guid guid = new Guid(substring);
126                groupsToCalculate.Add(guid);
127              }
128              job.AssignedResourceIds = groupsToCalculate;
129            }
130
131            SerializedJob computableJob =
132              new SerializedJob();
133            computableJob.JobInfo = job;
134            computableJob.SerializedJobData = PersistenceManager.SaveToGZip(new TestJob());
135            Response resp = jobManager.AddNewJob(computableJob);
136          }
137          if (addJobEvent != null) {
138            addJobEvent();
139          }
140          this.Close();
141        } else {
142          lblError.Text = "Wrong number of Jobs";
143        }
144
145      }
146      catch {
147        lblError.Text = "There should be a number";
148      }
149    }
150
151    private void BtnClose_Click(object sender, EventArgs e) {
152      this.Close();
153    }
154
155    private void cbAllGroups_CheckedChanged(object sender, EventArgs e) {
156      foreach (Control control in gbGroups.Controls) {
157        control.Enabled = !cbAllGroups.Checked;
158      }
159    }
160
161    private void btnAddGroup_Click(object sender, EventArgs e) {
162      AddGroup();
163    }
164
165    private void btnRemoveGroup_Click(object sender, EventArgs e) {
166      RemoveGroup();
167    }
168
169    private void lbGroupsOut_SelectedIndexChanged(object sender, EventArgs e) {
170      AddGroup();
171    }
172
173    private void lbGroupsIn_SelectedIndexChanged(object sender, EventArgs e) {
174      RemoveGroup();
175    }
176
177    private void AddGroup() {
178      if (lbGroupsOut.SelectedItem != null) {
179        lbGroupsIn.Items.Add(lbGroupsOut.SelectedItem);
180        lbGroupsOut.Items.RemoveAt(lbGroupsOut.SelectedIndex);
181      }
182 
183    }
184
185    private void RemoveGroup() {
186      if (lbGroupsIn.SelectedItem != null) {
187        lbGroupsOut.Items.Add(lbGroupsIn.SelectedItem);
188        lbGroupsIn.Items.RemoveAt(lbGroupsIn.SelectedIndex);
189      }
190    }
191
192    private void btnLoad_Click(object sender, EventArgs e) {
193
194    }
195
196
197  }
198}
Note: See TracBrowser for help on using the repository browser.