00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Collections.Generic;
00014 using System.Web.Services.Protocols;
00015 using System.Windows.Forms;
00016 using System.IO;
00017
00018 namespace Mcs.Epm.MicrosoftProject.mpFx.WinForms
00019 {
00020
00021
00022
00023 public partial class ProjectSelectionForm : Form
00024 {
00025 #region Instance Data
00026
00027 private readonly ProjectServer _ProjectServer;
00028 private readonly Dictionary<Guid, string> _DisplayedCustomFields;
00029 private readonly List<Guid> _OriginallySelectedProjects;
00030 private Dictionary<Guid, string> _SelectedProjects;
00031 private readonly bool _UseCache;
00032
00033 #endregion
00034
00035 #region Constructors
00036
00037 public ProjectSelectionForm(ProjectServer projectServer,
00038 Dictionary<Guid, string> displayedCustomFiels,
00039 List<Guid> SelecteProjects,
00040 bool useCache)
00041 {
00042 InitializeComponent();
00043
00044 _ProjectServer = projectServer;
00045 _DisplayedCustomFields = displayedCustomFiels;
00046 _OriginallySelectedProjects = SelecteProjects;
00047 _UseCache = useCache;
00048 }
00049
00050 #endregion
00051
00052 #region Public Properties
00053
00054 public Dictionary<Guid, string> SelectedProjects
00055 {
00056 get
00057 {
00058 if (_SelectedProjects == null)
00059 {
00060 _SelectedProjects = new Dictionary<Guid, string>();
00061
00062 foreach (DataGridViewRow row in selectedProjectsDataGridView.Rows)
00063 {
00064 if (Convert.ToBoolean(row.Cells[1].Value))
00065 {
00066 _SelectedProjects.Add(new Guid(row.Cells[0].Value.ToString()),
00067 row.Cells[2].Value.ToString());
00068 }
00069 }
00070 }
00071
00072 return _SelectedProjects;
00073 }
00074 }
00075
00076 #endregion
00077
00078 #region Private Methods
00079
00080 private void LoadProjects(bool overrideCacheSettings)
00081 {
00082 Cursor = Cursors.WaitCursor;
00083 okButton.Enabled = false;
00084
00085 selectedProjectsDataGridView.Rows.Clear();
00086
00087 if (_UseCache && !overrideCacheSettings)
00088 {
00089 LoadCachedProjects();
00090 }
00091 else
00092 {
00093 LoadProjectFromServer();
00094 }
00095
00096 okButton.Enabled = true;
00097 Cursor = Cursors.Default;
00098
00099 Application.DoEvents();
00100 }
00101
00102 private void LoadProjectFromServer()
00103 {
00104 string errorMessage = string.Empty;
00105
00106 try
00107 {
00108
00109 for (int i = selectedProjectsDataGridView.Columns.Count - 1; i > 2; i--)
00110 {
00111 selectedProjectsDataGridView.Columns.RemoveAt(i);
00112 }
00113
00114
00115 foreach (KeyValuePair<Guid, string> displayedCustomField in _DisplayedCustomFields)
00116 {
00117 DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
00118
00119 column.Tag = displayedCustomField.Key;
00120 column.HeaderText = displayedCustomField.Value;
00121
00122 selectedProjectsDataGridView.Columns.Add(column);
00123 }
00124
00125 char listSeparator = _ProjectServer.Settings.ListSeparator;
00126
00127
00128 foreach (Guid projectGuid in _ProjectServer.Projects)
00129 {
00130 if (Cancel)
00131 {
00132 return;
00133 }
00134
00135 Application.DoEvents();
00136
00137 List<object> values = new List<object>();
00138
00139 values.Add(projectGuid);
00140
00141
00142 if (_OriginallySelectedProjects == null)
00143 {
00144 values.Add(false);
00145 }
00146 else
00147 {
00148 values.Add(_OriginallySelectedProjects.Contains(projectGuid));
00149 }
00150
00151
00152 values.Add(_ProjectServer.Projects[projectGuid].Name);
00153
00154 if (_DisplayedCustomFields.Count > 0)
00155 {
00156 ProjectCustomFieldsCollection fieldCollection = _ProjectServer.Projects[projectGuid].ProjectCustomFieldsCollection;
00157
00158
00159 foreach (KeyValuePair<Guid, string> displayedCustomField in _DisplayedCustomFields)
00160 {
00161 if (Cancel)
00162 {
00163 return;
00164 }
00165
00166
00167 if (fieldCollection.ContainsKey(displayedCustomField.Key))
00168 {
00169
00170 if (fieldCollection[displayedCustomField.Key].IsList)
00171 {
00172 string concatenatedValue = string.Empty;
00173
00174 foreach (object listItem in fieldCollection[displayedCustomField.Key].List)
00175 {
00176 if (Cancel)
00177 {
00178 return;
00179 }
00180
00181 concatenatedValue = concatenatedValue + listItem + listSeparator;
00182 }
00183
00184 if (concatenatedValue != string.Empty)
00185 {
00186 concatenatedValue = concatenatedValue.Substring(0, concatenatedValue.Length - 1);
00187 }
00188
00189 values.Add(concatenatedValue);
00190 }
00191 else
00192 {
00193
00194 values.Add(fieldCollection[displayedCustomField.Key].Value);
00195 }
00196 }
00197 else
00198 {
00199
00200 values.Add(string.Empty);
00201 }
00202 }
00203 }
00204
00205
00206 selectedProjectsDataGridView.Rows.Add(values.ToArray());
00207 }
00208 }
00209 catch (SoapException soapException)
00210 {
00211 errorMessage = Errors.ProcessMSProjectErrors(soapException);
00212 }
00213 catch (Exception exception)
00214 {
00215 errorMessage = exception.Message;
00216 }
00217
00218 if (!string.IsNullOrEmpty(errorMessage))
00219 {
00220 MessageBox.Show(this,
00221 string.Format("An error has occurred: {0}",
00222 errorMessage),
00223 "Bels",
00224 MessageBoxButtons.OK,
00225 MessageBoxIcon.Error);
00226 }
00227 }
00228
00229 private void LoadCachedProjects()
00230 {
00231 string errorMessage = string.Empty;
00232
00233 try
00234 {
00235 ProjectsCache projectsCache = ProjectsCache.Deserialze(Path.Combine(Application.StartupPath,
00236 "cache"));
00237
00238 bool cacheIsInvalid = false;
00239
00240
00241 if (projectsCache.CachedCustomFields.Length != _DisplayedCustomFields.Count)
00242 {
00243 cacheIsInvalid = true;
00244 }
00245 else
00246 {
00247 foreach (KeyValuePair<Guid, string> displayedCustomField in _DisplayedCustomFields)
00248 {
00249 bool cachedCustomFieldLocated = false;
00250
00251 foreach (string cachedCustomField in projectsCache.CachedCustomFields)
00252 {
00253 string[] cachedCustomFieldLocatedComponents = cachedCustomField.Split('~');
00254
00255 if (cachedCustomFieldLocatedComponents.Length == 2)
00256 {
00257 cachedCustomFieldLocated = cachedCustomFieldLocatedComponents[0].Equals(displayedCustomField.Key.ToString(),
00258 StringComparison.InvariantCultureIgnoreCase);
00259
00260 if (cachedCustomFieldLocated)
00261 {
00262 break;
00263 }
00264 }
00265 }
00266
00267 if (!cachedCustomFieldLocated)
00268 {
00269 break;
00270 }
00271 }
00272 }
00273
00274 if (cacheIsInvalid)
00275 {
00276
00277 LoadProjectFromServer();
00278 return;
00279 }
00280 foreach (string displayedCustomField in projectsCache.CachedCustomFields)
00281 {
00282 string[] cachedCustomFieldLocatedComponents = displayedCustomField.Split('~');
00283
00284 if (cachedCustomFieldLocatedComponents.Length == 2)
00285 {
00286 DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn
00287 {
00288 Tag = cachedCustomFieldLocatedComponents[0],
00289 HeaderText = cachedCustomFieldLocatedComponents[1]
00290 };
00291
00292 selectedProjectsDataGridView.Columns.Add(column);
00293
00294 }
00295 }
00296
00297 List<object> values = new List<object>();
00298
00299 foreach (ProjectsCache.CachedProject cachedProject in projectsCache.CachedProjects)
00300 {
00301 values.Add(cachedProject.Guid);
00302
00303
00304 if (_OriginallySelectedProjects == null)
00305 {
00306 values.Add(false);
00307 }
00308 else
00309 {
00310 values.Add(_OriginallySelectedProjects.Contains(cachedProject.Guid));
00311 }
00312 values.Add(cachedProject.Name);
00313 values.AddRange(cachedProject.CustomFieldValues);
00314
00315 selectedProjectsDataGridView.Rows.Add(values.ToArray());
00316 values.Clear();
00317 }
00318 }
00319 catch (IOException ioException)
00320 {
00321 errorMessage = ioException.Message;
00322 }
00323 catch (Exception exception)
00324 {
00325 errorMessage = exception.Message;
00326 }
00327
00328 if (!string.IsNullOrEmpty(errorMessage))
00329 {
00330 MessageBox.Show(this,
00331 string.Format("An error occurred while loading cached project information: {0}. Loading from server.",
00332 errorMessage),
00333 "Bels",
00334 MessageBoxButtons.OK,
00335 MessageBoxIcon.Error);
00336
00337 LoadProjectFromServer();
00338 }
00339 }
00340
00341 private void ProjectSelectionForm_Shown(object sender, EventArgs e)
00342 {
00343 LoadProjects(false);
00344 }
00345
00346 private bool Cancel { get; set; }
00347
00348 private void SaveCache()
00349 {
00350 string errorMessage = string.Empty;
00351
00352 try
00353 {
00354 ProjectsCache projectsCache = new ProjectsCache(_DisplayedCustomFields, selectedProjectsDataGridView.Rows.Count);
00355
00356 int counter = 0;
00357
00358 foreach (DataGridViewRow row in selectedProjectsDataGridView.Rows)
00359 {
00360 List<string> customFieldValues = new List<string>();
00361
00362 for (int i = 3; i < selectedProjectsDataGridView.Columns.Count; i++)
00363 {
00364 customFieldValues.Add(row.Cells[i].Value.ToString());
00365 }
00366
00367 ProjectsCache.CachedProject cachedProject = new ProjectsCache.CachedProject(new Guid(row.Cells[0].Value.ToString()), row.Cells[2].Value.ToString(),
00368 customFieldValues);
00369
00370 projectsCache.CachedProjects[counter++] = cachedProject;
00371 }
00372
00373 ProjectsCache.Serialze(projectsCache,
00374 Path.Combine(Application.StartupPath,
00375 "cache"));
00376 }
00377 catch (IOException ioException)
00378 {
00379 errorMessage = ioException.Message;
00380 }
00381 catch (Exception exception)
00382 {
00383 errorMessage = exception.Message;
00384 }
00385
00386 if (!string.IsNullOrEmpty(errorMessage))
00387 {
00388 MessageBox.Show(this,
00389 string.Format("An error occurred while caching project information: {0}",
00390 errorMessage),
00391 "Bels",
00392 MessageBoxButtons.OK,
00393 MessageBoxIcon.Error);
00394
00395
00396 return;
00397 }
00398
00399 return;
00400 }
00401
00402 private void SetSelectedState(bool state)
00403 {
00404 foreach (DataGridViewRow row in selectedProjectsDataGridView.Rows)
00405 {
00406 row.Cells[1].Value = state;
00407 }
00408 }
00409
00410 #endregion
00411
00412 #region Event Handlers
00413
00414 private void refreshToolStripButton_Click(object sender, EventArgs e)
00415 {
00416 LoadProjects(true);
00417 }
00418
00419 private void okButton_Click(object sender, EventArgs e)
00420 {
00421 if (_UseCache)
00422 {
00423 SaveCache();
00424 }
00425
00426 Close();
00427 }
00428
00429 private void cancelButton_Click(object sender, EventArgs e)
00430 {
00431 Cancel = true;
00432 }
00433
00434 private void selectAllToolStripButton_Click(object sender, EventArgs e)
00435 {
00436 SetSelectedState(true);
00437 }
00438
00439 private void unselectAllToolStripButton_Click(object sender, EventArgs e)
00440 {
00441 SetSelectedState(false);
00442 }
00443
00444 #endregion
00445 }
00446 }