00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Diagnostics;
00014 using System.Web.Services.Protocols;
00015 using Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService;
00016 using Mcs.Epm.MicrosoftProject.mpFx.QueueWebService;
00017 using Microsoft.Office.Project.Server.Library;
00018 using DataStoreEnum = Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService.DataStoreEnum;
00019 using Project = Microsoft.Office.Project.Server.Library.Project;
00020
00021 namespace Mcs.Epm.MicrosoftProject.mpFx
00022 {
00023
00024
00025
00026 public class EnterpriseProject
00027 {
00028 #region Instance Data
00029
00030 private ProjectCustomFieldsCollection _ProjectCustomFieldsCollection;
00031 private ProjectDataSet.ProjectCustomFieldsDataTable _CustomFields;
00032
00033 private string _Filter;
00034
00035 #endregion
00036
00037 #region Constructors
00038
00039
00040
00041
00042
00043
00044
00045
00046 protected internal EnterpriseProject(ProjectCollection parent,
00047 Guid projectGuid,
00048 string name,
00049 DataStoreEnum store)
00050 {
00051 ProjectGuid = projectGuid;
00052 Parent = parent;
00053 Name = name;
00054 DataStore = store;
00055 }
00056
00057 #endregion
00058
00059 #region Public Properties
00060
00061
00062
00063
00064 public string Name { get; private set; }
00065
00066
00067
00068
00069 public Guid ProjectGuid { get; private set; }
00070
00071
00072
00073
00074 public ProjectDataSet.ProjectCustomFieldsDataTable CustomFieldsDataSet
00075 {
00076 get
00077 {
00078 if (_CustomFields == null)
00079 {
00080 LoadProjectCustomFields();
00081 }
00082
00083 return _CustomFields;
00084 }
00085 protected internal set
00086 {
00087 if (_CustomFields != null)
00088 {
00089 _CustomFields.Dispose();
00090 }
00091 _CustomFields = value;
00092 }
00093 }
00094
00095
00096
00097
00098 public ProjectTeamDataSet ProjectTeamDataSet { get; protected internal set; }
00099
00100
00101
00102
00103 public DataStoreEnum DataStore { get; private set; }
00104
00105
00106
00107
00108 public ProjectCustomFieldsCollection ProjectCustomFieldsCollection
00109 {
00110 get
00111 {
00112 if (_ProjectCustomFieldsCollection == null)
00113 {
00114 _ProjectCustomFieldsCollection = new ProjectCustomFieldsCollection(this);
00115 }
00116
00117 return _ProjectCustomFieldsCollection;
00118 }
00119 }
00120
00121
00122
00123
00124 public ProjectDataSet.ProjectRow StatusInformation
00125 {
00126 get
00127 {
00128 using (ProjectDataSet projectDataSet = Parent.Parent.WebServices.Projects.ReadProjectStatus(ProjectGuid, Parent.Parent.Store, string.Empty, (int)Project.ProjectType.Project))
00129 {
00130 if (projectDataSet != null)
00131 {
00132 Trace.Assert(projectDataSet.Project.Rows.Count == 1);
00133
00134 return (ProjectDataSet.ProjectRow)projectDataSet.Project.Rows[0];
00135 }
00136 }
00137
00138 return null;
00139 }
00140 }
00141
00142
00143 public ProjectDataSet AllInformation
00144 {
00145 get
00146 {
00147 return Parent.Parent.WebServices.Projects.ReadProject(ProjectGuid, Parent.Parent.Store);
00148 }
00149 }
00150
00151 public ProjectDataSet StandardInformation
00152 {
00153 get
00154 {
00155 return Parent.Parent.WebServices.Projects.ReadProjectEntities(ProjectGuid, (int)ProjectEntityType.Project, Parent.Parent.Store);
00156 }
00157 }
00158
00159 #endregion
00160
00161 #region Protected Properties
00162
00163
00164
00165
00166 protected internal ProjectCollection Parent { get; private set; }
00167
00168 #endregion
00169
00170 #region Public Methods
00171
00172
00173
00174
00175 public void LoadProjectCustomFields()
00176 {
00177 using (ProjectDataSet projectDataSet = Parent.Parent.WebServices.Projects.ReadProjectEntities(ProjectGuid, (int) ProjectEntityType.ProjectCustomFields, Parent.Parent.Store))
00178 {
00179 _CustomFields = projectDataSet.ProjectCustomFields;
00180 }
00181 }
00182
00183
00184
00185
00186
00187 public void SetFilter(string filter)
00188 {
00189 if (!filter.Equals(_Filter, StringComparison.CurrentCultureIgnoreCase))
00190 {
00191 _Filter = filter;
00192 }
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 public bool CreateTask(string taskName,
00208 DateTime startDate,
00209 int duration,
00210 bool checkOutProject,
00211 bool autoCheckIn,
00212 Guid sessionGuid,
00213 out Guid taskGuid,
00214 out string errorMessage)
00215 {
00216 errorMessage = string.Empty;
00217 taskGuid = Guid.Empty;
00218 bool checkedOut = false;
00219
00220 try
00221 {
00222 if (checkOutProject)
00223 {
00224 Parent.Parent.Projects.CheckOut(ProjectGuid, sessionGuid, Parent.Parent.Settings.DefaultSessionDescription);
00225 checkedOut = true;
00226 }
00227
00228 using (ProjectDataSet projectDataSet = new ProjectDataSet())
00229 {
00230 ProjectDataSet.TaskRow task = projectDataSet.Task.NewTaskRow();
00231
00232 taskGuid = Guid.NewGuid();
00233
00234 task.PROJ_UID = ProjectGuid;
00235 task.TASK_UID = taskGuid;
00236 task.TASK_NAME = taskName;
00237 task.TASK_START_DATE = startDate;
00238 task.TASK_CONSTRAINT_DATE = startDate;
00239 task.TASK_CONSTRAINT_TYPE = (short)Task.ConstraintType.MustStartOn;
00240 task.TASK_DUR_IS_EST = false;
00241 task.TASK_IS_EFFORT_DRIVEN = false;
00242 task.TASK_IS_MILESTONE = false;
00243 task.TASK_WORK = duration;
00244 task.TASK_DUR = duration;
00245
00246 task.AddPosition = (int)Task.AddPositionType.Last;
00247
00248 projectDataSet.Task.AddTaskRow(task);
00249
00250 Guid jobGuid = Guid.NewGuid();
00251
00252 Parent.Parent.WebServices.Projects.QueueAddToProject(jobGuid, sessionGuid, projectDataSet, false);
00253
00254 if (!Parent.Parent.Queue.WaitOnJobStatus(jobGuid,
00255 JobState.Success,
00256 Parent.Parent.Settings.QueueStatusRetryCount,
00257 Parent.Parent.Settings.QueueStatusSleepDuration,
00258 out errorMessage))
00259 {
00260
00261 return false;
00262 }
00263 }
00264
00265 return true;
00266 }
00267 finally
00268 {
00269 if (checkedOut && autoCheckIn)
00270 {
00271 Parent.Parent.Projects.CheckIn(ProjectGuid, sessionGuid, false, true, out errorMessage);
00272 }
00273 }
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 public bool CreateAssignment(Guid assignmentGuid,
00290 Guid taskGuid,
00291 Guid resourceGuid,
00292 bool autoAddResourceToTeam,
00293 bool checkOutProject,
00294 bool autoCheckIn,
00295 Guid sessionGuid,
00296 out string errorMessage)
00297 {
00298 bool checkedOut = false;
00299 bool forceCheckIn = false;
00300 errorMessage = string.Empty;
00301
00302 try
00303 {
00304 if (checkOutProject)
00305 {
00306 Parent.Parent.Projects.CheckOut(ProjectGuid, sessionGuid, Parent.Parent.Settings.DefaultSessionDescription);
00307 checkedOut = true;
00308 }
00309
00310 using (ProjectDataSet projectDataSet = new ProjectDataSet())
00311 using (ProjectTeamDataSet projectTeamDataSet = Parent.Parent.Projects.ReadTeam(ProjectGuid, false))
00312 {
00313 if (projectTeamDataSet.ProjectTeam.FindByRES_UIDPROJ_UID(resourceGuid, ProjectGuid) == null)
00314 {
00315 if (autoAddResourceToTeam)
00316 {
00317 if (!AddResourceToTeam(sessionGuid, projectTeamDataSet, resourceGuid, true, out errorMessage))
00318 {
00319 return false;
00320 }
00321 }
00322 else
00323 {
00324 errorMessage = "Resource is not assigned to the project team";
00325 return false;
00326 }
00327 }
00328
00329 ProjectDataSet.AssignmentRow assignment = projectDataSet.Assignment.NewAssignmentRow();
00330
00331 assignment.PROJ_UID = ProjectGuid;
00332 assignment.TASK_UID = taskGuid;
00333 assignment.RES_UID = resourceGuid;
00334 assignment.ASSN_UID = assignmentGuid;
00335
00336 projectDataSet.Assignment.AddAssignmentRow(assignment);
00337
00338 Guid jobGuid = Guid.NewGuid();
00339
00340 Parent.Parent.WebServices.Projects.QueueAddToProject(jobGuid, sessionGuid, projectDataSet, false);
00341
00342 if (!Parent.Parent.Queue.WaitOnJobStatus(jobGuid,
00343 JobState.Success,
00344 Parent.Parent.Settings.QueueStatusRetryCount,
00345 Parent.Parent.Settings.QueueStatusSleepDuration,
00346 out errorMessage))
00347 {
00348
00349 forceCheckIn = errorMessage.Contains("AssignmentAlreadyExists");
00350
00351 if (forceCheckIn)
00352 {
00353 Parent.Parent.WebServices.Queue.CancelJob(jobGuid, true, true);
00354 }
00355
00356 return false;
00357 }
00358 }
00359 return true;
00360 }
00361 finally
00362 {
00363 if (checkedOut && autoCheckIn)
00364 {
00365 try
00366 {
00367 string checkinError;
00368 Parent.Parent.Projects.CheckIn(ProjectGuid, sessionGuid, forceCheckIn, true, out checkinError);
00369 }
00370 catch (SoapException)
00371 {
00372
00373 }
00374 }
00375 }
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 public void SetTaskCustomFieldValue(Guid sessionGuid, Guid taskGuid, Guid customFieldGuid, Guid lookupTableValueGuid)
00386 {
00387 using (ProjectDataSet projectDataSet = new ProjectDataSet())
00388 {
00389 ProjectDataSet.TaskCustomFieldsRow customField =
00390 projectDataSet.TaskCustomFields.NewTaskCustomFieldsRow();
00391
00392 customField.CODE_VALUE = lookupTableValueGuid;
00393 customField.CUSTOM_FIELD_UID = Guid.NewGuid();
00394 customField.MD_PROP_UID = customFieldGuid;
00395 customField.PROJ_UID = ProjectGuid;
00396 customField.TASK_UID = taskGuid;
00397 customField.FIELD_TYPE_ENUM = (int)PSDataType.STRING;
00398
00399 projectDataSet.TaskCustomFields.AddTaskCustomFieldsRow(customField);
00400
00401 Guid jobGuid = Guid.NewGuid();
00402
00403 Parent.Parent.WebServices.Projects.QueueAddToProject(jobGuid, sessionGuid, projectDataSet, false);
00404 }
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 public bool SetTaskComplete(Guid taskGuid, int durationInSeconds, Guid sessionGuid, bool wait, out string errorMessage)
00417 {
00418 errorMessage = string.Empty;
00419
00420 using (ProjectDataSet projectDataSet = Parent.Parent.WebServices.Projects.ReadProjectEntities(ProjectGuid, (int)ProjectEntityType.Task, DataStoreEnum.WorkingStore))
00421 {
00422 ProjectDataSet.TaskRow task = projectDataSet.Task.FindByTASK_UIDPROJ_UID(taskGuid, ProjectGuid);
00423
00424 task.TASK_DUR = task.TASK_DUR + durationInSeconds;
00425 task.TASK_DUR_IS_EST = false;
00426 task.TASK_PCT_WORK_COMP = 100;
00427
00428 Guid jobGuid = Guid.NewGuid();
00429
00430 Parent.Parent.WebServices.Projects.QueueUpdateProject(jobGuid, sessionGuid, projectDataSet, false);
00431
00432 if (wait)
00433 {
00434 return Parent.Parent.Queue.WaitOnJobStatus(jobGuid,
00435 JobState.Success,
00436 Parent.Parent.Settings.QueueStatusRetryCount,
00437 Parent.Parent.Settings.QueueStatusSleepDuration,
00438 out errorMessage);
00439 }
00440
00441 }
00442
00443 return true;
00444 }
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 public bool UpdateTaskWorkToActualWorkWithDuration(Guid taskGuid, double duration, Guid sessionGuid, bool wait, out string errorMessage)
00456 {
00457 errorMessage = string.Empty;
00458
00459 using (ProjectDataSet projectDataSet = Parent.Parent.WebServices.Projects.ReadProjectEntities(ProjectGuid, (int)ProjectEntityType.Task, DataStoreEnum.WorkingStore))
00460 {
00461 ProjectDataSet.TaskRow task = projectDataSet.Task.FindByTASK_UIDPROJ_UID(taskGuid, ProjectGuid);
00462
00463 task.TASK_WORK = task.TASK_ACT_WORK + duration;
00464
00465 Guid jobGuid = Guid.NewGuid();
00466
00467 Parent.Parent.WebServices.Projects.QueueUpdateProject(jobGuid, sessionGuid, projectDataSet, false);
00468
00469 if (wait)
00470 {
00471 return Parent.Parent.Queue.WaitOnJobStatus(jobGuid,
00472 JobState.Success,
00473 Parent.Parent.Settings.QueueStatusRetryCount,
00474 Parent.Parent.Settings.QueueStatusSleepDuration,
00475 out errorMessage);
00476 }
00477
00478 }
00479
00480 return true;
00481 }
00482
00483 #endregion
00484
00485 #region Private Methods
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 private bool AddResourceToTeam(Guid sessionGuid, ProjectTeamDataSet projectTeamDataSet, Guid resourceGuid, bool wait, out string errorMessage)
00497 {
00498 errorMessage = string.Empty;
00499
00500 ProjectTeamDataSet.ProjectTeamRow teamMember = projectTeamDataSet.ProjectTeam.NewProjectTeamRow();
00501
00502 teamMember.RES_UID = resourceGuid;
00503 teamMember.PROJ_UID = ProjectGuid;
00504 teamMember.NEW_RES_UID = resourceGuid;
00505 teamMember.RES_IS_ENTERPRISE_RESOURCE = true;
00506 teamMember.RES_TYPE = (int)Resource.Type.WorkResource;
00507
00508 projectTeamDataSet.ProjectTeam.AddProjectTeamRow(teamMember);
00509
00510 Guid jobGuid = Guid.NewGuid();
00511 Parent.Parent.WebServices.Projects.QueueUpdateProjectTeam(jobGuid, sessionGuid, ProjectGuid, projectTeamDataSet);
00512
00513 if (wait)
00514 {
00515 if (!Parent.Parent.Queue.WaitOnJobStatus(jobGuid,
00516 JobState.Success,
00517 Parent.Parent.Settings.QueueStatusRetryCount,
00518 Parent.Parent.Settings.QueueStatusSleepDuration,
00519 out errorMessage))
00520 {
00521
00522 return false;
00523 }
00524 }
00525 return true;
00526 }
00527
00528 #endregion
00529
00530 }
00531 }