00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Collections;
00014 using System.Collections.Generic;
00015 using System.Diagnostics;
00016 using System.Web.Services.Protocols;
00017 using Mcs.Epm.MicrosoftProject.mpFx.CustomFieldsWebService;
00018 using Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService;
00019 using Mcs.Epm.MicrosoftProject.mpFx.QueueWebService;
00020 using Project = Microsoft.Office.Project.Server.Library.Project;
00021
00022 namespace Mcs.Epm.MicrosoftProject.mpFx
00023 {
00024
00025
00026
00027 public class ProjectCollection : IEnumerable<EnterpriseProject>, IEnumerable<Guid>
00028 {
00029 #region Instance Data
00030
00031 private Dictionary<Guid, EnterpriseProject> _projectsCollection;
00032 private CustomFieldDataSet _CustomFields;
00033
00034 #endregion
00035
00036 #region Internal Constructor
00037
00038
00039
00040
00041
00042 internal ProjectCollection(ProjectServer parent)
00043 {
00044 if (parent == null)
00045 {
00046 throw new InvalidOperationException( );
00047 }
00048
00049 Parent = parent;
00050
00051 }
00052
00053 #endregion
00054
00055 #region Public Events
00056
00057 public event EventHandler<ProjectReadCompleteArgs> OnProjectReadComplete;
00058 public event EventHandler<ProjectTeamReadArgs> OnProjectTeamReadComplete;
00059
00060 #endregion
00061
00062 #region Public Properties
00063
00064
00065
00066
00067
00068
00069 public EnterpriseProject this[int index]
00070 {
00071 get
00072 {
00073 if (_projectsCollection == null)
00074 {
00075 LoadProjectCollection();
00076 }
00077
00078 if (index > _projectsCollection.Keys.Count)
00079 {
00080 throw new ArgumentOutOfRangeException( );
00081 }
00082
00083 Dictionary<Guid, EnterpriseProject>.KeyCollection.Enumerator enumerator = _projectsCollection.Keys.GetEnumerator();
00084
00085 for (int i = 0; i <= index; i++)
00086 {
00087 enumerator.MoveNext();
00088 }
00089
00090 return _projectsCollection[enumerator.Current];
00091 }
00092 }
00093
00094
00095
00096
00097
00098
00099 public EnterpriseProject this[Guid projectGuid]
00100 {
00101 get
00102 {
00103 if (_projectsCollection == null)
00104 {
00105 LoadProjectCollection();
00106 }
00107
00108 return _projectsCollection[projectGuid];
00109 }
00110 }
00111
00112
00113
00114 public CustomFieldDataSet CustomFieldsDataSet
00115 {
00116 get
00117 {
00118 if (_CustomFields == null)
00119 {
00120 LoadProjectCustomFields();
00121 }
00122 return _CustomFields;
00123 }
00124 }
00125
00126 #endregion
00127
00128 #region Public Methods
00129
00130 public bool TryGetProjectGuidByName(string name, ref Guid projectGuid)
00131 {
00132 if (_projectsCollection == null)
00133 {
00134 LoadProjectCollection();
00135 }
00136
00137 foreach (KeyValuePair<Guid, EnterpriseProject> project in _projectsCollection)
00138 {
00139 if (project.Value.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
00140 {
00141 projectGuid = project.Value.ProjectGuid;
00142 return true;
00143 }
00144 }
00145
00146 return false;
00147 }
00148
00149 public ProjectDataSet ReadProject(Guid guid,
00150 DataStoreEnum store,
00151 bool asynch)
00152 {
00153 if (asynch)
00154 {
00155 Parent.WebServices.Projects.ReadProjectCompleted += ProjectsReadProjectCompleted;
00156 Parent.WebServices.Projects.ReadProjectAsync(guid, store);
00157 return null;
00158 }
00159
00160 ProjectDataSet projectDataSet = Parent.WebServices.Projects.ReadProject(guid, store);
00161
00162 _projectsCollection[guid].CustomFieldsDataSet = projectDataSet.ProjectCustomFields;
00163
00164 return projectDataSet;
00165 }
00166
00167
00168
00169
00170
00171
00172 private void ProjectsReadProjectCompleted(object sender,
00173 ReadProjectCompletedEventArgs e)
00174 {
00175 if (OnProjectReadComplete != null)
00176 {
00177 if (e.Cancelled == false && e.Error == null && e.Result != null)
00178 {
00179 Debug.Assert(e.Result.Project.Count == 1);
00180
00181 _projectsCollection[e.Result.Project[0].PROJ_UID].CustomFieldsDataSet = e.Result.ProjectCustomFields;
00182 }
00183
00184 OnProjectReadComplete(this, new ProjectReadCompleteArgs(e.Cancelled, e.Error, e.Result));
00185 }
00186 else
00187 {
00188 if (e.Result != null)
00189 {
00190 e.Result.Dispose();
00191 }
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 public ProjectDataSet ReadProject(Guid projectGuid,
00205 DataStoreEnum store,
00206 bool asynch,
00207 string sessionDescription,
00208 out Guid sessionGuid)
00209 {
00210 sessionGuid = Guid.NewGuid();
00211
00212 Parent.WebServices.Projects.CheckOutProject(projectGuid, sessionGuid, sessionDescription);
00213
00214 return ReadProject(projectGuid, store, asynch);
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224 public Guid Create(ProjectDataSet project,
00225 bool validateOnly,
00226 bool wait)
00227 {
00228 try
00229 {
00230 Guid jobGuid = Guid.NewGuid();
00231
00232 Parent.WebServices.Projects.QueueCreateProject(jobGuid, project, validateOnly);
00233
00234 if (wait)
00235 {
00236 string errorMessage;
00237
00238 Parent.Queue.WaitOnJobStatus(jobGuid,
00239 JobState.Success,
00240 Parent.Settings.QueueStatusRetryCount,
00241 Parent.Settings.QueueStatusSleepDuration,
00242 out errorMessage);
00243
00244 MpFxException.ThrowIfError(errorMessage, Parent.Log, LogArea.CreateProject, LogEntryType.Error);
00245 }
00246
00247 return jobGuid;
00248
00249 }
00250 catch (SoapException exception)
00251 {
00252 throw MpFxException.Create(exception, Parent.Log, LogArea.CreateProject, LogEntryType.Error);
00253 }
00254 catch (Exception exception)
00255 {
00256 throw MpFxException.Create(exception, Parent.Log, LogArea.CreateProject, LogEntryType.Error);
00257 }
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 public bool Delete(Guid projectGuid,
00270 bool deleteWSS,
00271 bool deleteBoth,
00272 bool wait,
00273 out string errorString)
00274 {
00275 Guid jobGuid = Guid.NewGuid();
00276
00277 errorString = string.Empty;
00278
00279 Parent.WebServices.Projects.QueueDeleteProjects(jobGuid, deleteWSS, new[] { projectGuid }, deleteBoth);
00280
00281 _projectsCollection.Remove(projectGuid);
00282
00283 if (wait)
00284 {
00285 return Parent.Queue.WaitOnJobStatus(jobGuid, JobState.Success, Parent.Settings.QueueStatusRetryCount, Parent.Settings.QueueStatusSleepDuration, out errorString);
00286 }
00287
00288 errorString = "";
00289 return true;
00290 }
00291
00292
00293
00294
00295
00296
00297
00298 public ProjectTeamDataSet ReadTeam(Guid projectGuid,
00299 bool asynch)
00300 {
00301 if (asynch)
00302 {
00303 Parent.WebServices.Projects.ReadProjectTeamCompleted += ProjectCollection_OnProjectTeamReadComplete;
00304 Parent.WebServices.Projects.ReadProjectTeamAsync(projectGuid);
00305 return null;
00306 }
00307 return Parent.WebServices.Projects.ReadProjectTeam(projectGuid);
00308 }
00309
00310
00311
00312
00313
00314
00315 private void ProjectCollection_OnProjectTeamReadComplete(object sender,
00316 ReadProjectTeamCompletedEventArgs e)
00317 {
00318 if (OnProjectTeamReadComplete != null)
00319 {
00320 if (e.Cancelled == false && e.Error == null && e.Result != null)
00321 {
00322 _projectsCollection[e.Result.ProjectTeam[0].PROJ_UID].ProjectTeamDataSet = e.Result;
00323 }
00324
00325 if (e.Error == null)
00326 {
00327 OnProjectTeamReadComplete(this, new ProjectTeamReadArgs(e.Cancelled, e.Error, e.Result));
00328 }
00329 else
00330 {
00331 OnProjectTeamReadComplete(this, new ProjectTeamReadArgs(e.Cancelled, e.Error, null));
00332 }
00333
00334 Parent.WebServices.Projects.ReadProjectTeamCompleted -= ProjectCollection_OnProjectTeamReadComplete;
00335 }
00336 else
00337 {
00338 if (e.Result != null)
00339 {
00340 e.Result.Dispose();
00341 }
00342 }
00343 }
00344
00345
00346
00347
00348
00349
00350 public bool ContainsKey(Guid projectGuid)
00351 {
00352 if (_projectsCollection == null)
00353 {
00354 LoadProjectCollection();
00355 }
00356
00357 return _projectsCollection.ContainsKey(projectGuid);
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 public bool CheckIn(Guid projectGuid,
00370 Guid sessionGuid,
00371 bool force,
00372 bool wait,
00373 out string errorString)
00374 {
00375 Guid jobGuid = Guid.NewGuid();
00376 errorString = string.Empty;
00377
00378 Parent.WebServices.Projects.QueueCheckInProject(jobGuid, projectGuid, force, sessionGuid, Parent.Settings.DefaultSessionDescription);
00379
00380 if (wait)
00381 {
00382 return Parent.Queue.WaitOnJobStatus(jobGuid, JobState.Success, Parent.Settings.QueueStatusRetryCount, Parent.Settings.QueueStatusSleepDuration, out errorString);
00383 }
00384
00385 errorString = string.Empty;
00386 return true;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395 public bool IsProjectCheckedOutByCurrentUser(Guid projectGuid,
00396 DataStoreEnum store)
00397 {
00398 using (ProjectDataSet project = Parent.WebServices.Projects.ReadProjectStatus(projectGuid, store, string.Empty, (int)Project.ProjectType.Project))
00399 {
00400 Debug.Assert(project.Project.Rows.Count == 1);
00401
00402 if (project.Project[0].IsPROJ_CHECKOUTBYNull())
00403 {
00404 return false;
00405 }
00406
00407 return project.Project[0].PROJ_CHECKOUTBY == Parent.CurrentUserGuid;
00408 }
00409 }
00410
00411 #endregion
00412
00413 #region Private Methods
00414
00415 private void LoadProjectCustomFields()
00416 {
00417 if (_CustomFields != null)
00418 {
00419 _CustomFields.Dispose();
00420 }
00421
00422 _CustomFields = Parent.WebServices.CustomFields.ReadCustomFieldsByEntity(new Guid(Utilities.GetEntityGuidStringFromName("Project")));
00423 }
00424
00425 #endregion
00426
00427 #region Internal Methods
00428
00429
00430
00431
00432 internal void LoadProjectCollection()
00433 {
00434 if (_projectsCollection == null)
00435 {
00436 _projectsCollection = new Dictionary<Guid, EnterpriseProject>();
00437 }
00438 else
00439 {
00440 _projectsCollection.Clear();
00441 }
00442
00443 using (ProjectDataSet projectDataSet = Parent.WebServices.Projects.ReadProjectStatus(Guid.Empty, Parent.Store, string.Empty, (int)Project.ProjectType.Project))
00444 {
00445 foreach (ProjectDataSet.ProjectRow project in projectDataSet.Project.Rows)
00446 {
00447 if (project.PROJ_UID != Guid.Empty)
00448 {
00449 _projectsCollection.Add(project.PROJ_UID, new EnterpriseProject(this, project.PROJ_UID, project.PROJ_NAME, Parent.Store));
00450 }
00451 }
00452 }
00453 }
00454
00455 #endregion
00456
00457 #region Internal Properties
00458
00459
00460
00461
00462 protected internal ProjectServer Parent { get; private set; }
00463
00464 #endregion
00465
00466 #region IEnumerable Implementation
00467
00468 IEnumerator IEnumerable.GetEnumerator()
00469 {
00470 return ((IEnumerable<Guid>)this).GetEnumerator();
00471 }
00472
00473 IEnumerator<Guid> IEnumerable<Guid>.GetEnumerator()
00474 {
00475 if (_projectsCollection == null)
00476 {
00477 LoadProjectCollection();
00478 }
00479
00480 foreach (KeyValuePair<Guid, EnterpriseProject> pair in _projectsCollection)
00481 {
00482 yield return pair.Key;
00483 }
00484 }
00485
00486 IEnumerator<EnterpriseProject> IEnumerable<EnterpriseProject>.GetEnumerator()
00487 {
00488 if (_projectsCollection == null)
00489 {
00490 LoadProjectCollection();
00491 }
00492
00493 foreach (KeyValuePair<Guid, EnterpriseProject> pair in _projectsCollection)
00494 {
00495 yield return pair.Value;
00496 }
00497 }
00498
00499 #endregion
00500
00501 public void Refresh()
00502 {
00503 LoadProjectCollection();
00504 }
00505
00506 public void CheckOut(Guid projectGuid, Guid sessionGuid, string sessionDescription)
00507 {
00508 Parent.WebServices.Projects.CheckOutProject(projectGuid, sessionGuid, sessionDescription);
00509 }
00510
00511 public bool Publish(Guid guid, bool fullPublish, string wssUrl, bool wait, out string errorMessage)
00512 {
00513 Guid jobGuid = Guid.NewGuid();
00514 errorMessage = string.Empty;
00515
00516 Parent.WebServices.Projects.QueuePublish(jobGuid, guid, fullPublish, wssUrl);
00517
00518 if (wait)
00519 {
00520 return Parent.Queue.WaitOnJobStatus(jobGuid, JobState.Success, Parent.Settings.QueueStatusRetryCount, Parent.Settings.QueueStatusSleepDuration, out errorMessage);
00521 }
00522
00523 return true;
00524 }
00525
00526 public IEnumerator<Guid> GetEnumerator()
00527 {
00528 return ((IEnumerable<Guid>)this).GetEnumerator();
00529 }
00530
00531 }
00532 }