00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Diagnostics;
00014 using System.Net;
00015 using System.Reflection;
00016 using System.Resources;
00017 using System.Security;
00018 using System.Web.Services.Protocols;
00019 using Mcs.Epm.MicrosoftProject.mpFx.Interfaces;
00020 using Mcs.Epm.MicrosoftProject.mpFx.LoginFormsWebService;
00021 using Mcs.Epm.MicrosoftProject.mpFx.LoginWindowsWebService;
00022 using Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService;
00023 using LibraryResources = Mcs.Epm.MicrosoftProject.mpFx.Properties.Resources;
00024
00025 [assembly: CLSCompliant(true)]
00026 namespace Mcs.Epm.MicrosoftProject.mpFx
00027 {
00028
00029
00030
00031 public class ProjectServer : IDisposable
00032 {
00033 #region Static Fields
00034
00035 internal static readonly ResourceManager _ResourceManager = new ResourceManager("Mcs.Epm.MicrosoftProject.mpFx.Properties.Resources",
00036 Assembly.GetAssembly(typeof(ProjectServer)));
00037
00038 #endregion
00039
00040 #region Public Static Methods
00041
00042 public static ProjectProfiles ImportProjectProfiles()
00043 {
00044 return new ProjectProfiles(true);
00045 }
00046
00047 #endregion
00048
00049 #region Instance Data
00050
00051 #region General
00052
00053 private bool _Disposed;
00054 private readonly ILog _Log;
00055 private Guid _CurrentUserGuid;
00056
00057 #endregion
00058
00059 #region Object Model
00060
00061 private Calendars _CalendarsCollection;
00062 private CustomFields _CustomFields;
00063 private LookupTables _LookupTables;
00064 private Timesheets _Timesheet;
00065 private Events _Events;
00066 private ProjectCollection _ProjectCollection;
00067 private Queue _Queue;
00068 private ResourceCollection _ResourceCollection;
00069 private Security _Security;
00070
00071 #endregion
00072
00073 #endregion
00074
00075 #region Constructors
00076
00077
00078
00079
00080
00081
00082
00083 public ProjectServer(string projectServerUrl, DataStoreEnum store, ILog log)
00084 {
00085 try
00086 {
00087 _Log = log;
00088
00089 if (!Utilities.IsValidUrl(projectServerUrl))
00090 {
00091 throw new ArgumentException(LibraryResources.InvalidProjectServerUrl);
00092 }
00093
00094 Settings = new ProjectServerSettings();
00095 WebServices = new WebServices(this);
00096 Site = new Uri(projectServerUrl);
00097 Store = store;
00098
00099 NetworkCredential = CredentialCache.DefaultNetworkCredentials;
00100 AuthenticationType = AuthenticationType.Windows;
00101
00102 WebServices.LoginWindows = new LoginWindows
00103 {
00104 Url = WebServices.AppendPath(projectServerUrl, ServicePaths.WindowsLoginService),
00105 UseDefaultCredentials = true
00106 };
00107
00108 WriteLogEntry(LogArea.Constructor,
00109 LogEntryType.Information,
00110 string.Format(LibraryResources.LogConstructor, AuthenticationType, projectServerUrl));
00111
00112 WebServices.LoginWindows.Login();
00113
00114 Settings.ListSeparator = WebServices.Projects.ReadServerListSeparator();
00115
00116 }
00117 catch (SoapException exception)
00118 {
00119 throw MpFxException.Create(exception, _Log, LogArea.Constructor, LogEntryType.Error);
00120 }
00121 catch (ArgumentException exception)
00122 {
00123 throw MpFxException.Create(exception, _Log, LogArea.Constructor, LogEntryType.Error);
00124 }
00125 catch (Exception exception)
00126 {
00127 throw MpFxException.Create(exception, _Log, LogArea.Constructor, LogEntryType.Error);
00128 }
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 public ProjectServer(string projectServerUrl,
00140 string userName,
00141 string password,
00142 DataStoreEnum store, ILog log)
00143 {
00144 _Log = log;
00145 WebServices = new WebServices(this);
00146 Settings = new ProjectServerSettings();
00147 Store = store;
00148
00149 Site = new Uri(projectServerUrl);
00150
00151 string servicePath = WebServices.AppendPath(projectServerUrl, ServicePaths.FormsLoginService);
00152
00153 AuthenticationType = AuthenticationType.Forms;
00154
00155 WebServices.LoginForms = new LoginForms { Url = servicePath };
00156
00157
00158 NetworkCredential = new NetworkCredential(userName, password);
00159
00160 CookieContainer = new CookieContainer();
00161
00162 WebServices.LoginForms.CookieContainer = CookieContainer;
00163
00164 if (WebServices.LoginForms.Login(userName, password))
00165 {
00166 Settings.ListSeparator = WebServices.Projects.ReadServerListSeparator();
00167 }
00168 else
00169 {
00170 AuthenticationType = AuthenticationType.NotAuthenticated;
00171
00172 throw new SecurityException(_ResourceManager.GetString("FormsAuthenticationFailed"));
00173 }
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 public ProjectServer(string projectServerUrl, string sspBaseUrl, DataStoreEnum store, ILog log)
00184 {
00185 _Log = log;
00186 Site = new Uri(projectServerUrl);
00187 SPPBaseURL = sspBaseUrl;
00188
00189 Store = store;
00190 WebServices = new WebServices(this);
00191 Settings = new ProjectServerSettings();
00192
00193 AuthenticationType = AuthenticationType.Windows;
00194
00195 try
00196 {
00197 string siteURL;
00198 string siteID;
00199
00200 WebServices.SiteData.GetSiteUrl(projectServerUrl, out siteURL, out siteID);
00201 SiteID = new Guid(siteID);
00202
00203 IsImpersonating = true;
00204 }
00205 catch (Exception exception)
00206 {
00207 Debug.WriteLine(exception.Message);
00208 throw;
00209 }
00210 }
00211
00212 public ProjectServer(string projectServerUrl, string sspBaseUrl, Guid siteGuid, DataStoreEnum store)
00213 {
00214 Site = new Uri(projectServerUrl);
00215 SPPBaseURL = sspBaseUrl;
00216
00217 Store = store;
00218 WebServices = new WebServices(this);
00219 Settings = new ProjectServerSettings();
00220
00221 AuthenticationType = AuthenticationType.Windows;
00222
00223 SiteID = siteGuid;
00224
00225 IsImpersonating = true;
00226 }
00227
00228 internal string SPPBaseURL { get; set; }
00229
00230 public Guid SiteID { get; set; }
00231
00232 #endregion
00233
00234 #region Public Events
00235
00236 #endregion
00237
00238 #region Public Properties
00239
00240 public ProjectServerSettings Settings { get; private set; }
00241
00242 public DataStoreEnum Store { get; private set; }
00243
00244 public AuthenticationType AuthenticationType { get; private set; }
00245
00246 public Uri Site { get; private set; }
00247
00248 public string UserName
00249 {
00250 get
00251 {
00252 switch (AuthenticationType)
00253 {
00254 case AuthenticationType.NotAuthenticated:
00255 return string.Empty;
00256 case AuthenticationType.Windows:
00257 return string.Format(@"{0}\{1}", NetworkCredential.Domain, NetworkCredential.UserName);
00258
00259 case AuthenticationType.Forms:
00260 return NetworkCredential.UserName;
00261
00262 default:
00263 throw new InvalidOperationException();
00264 }
00265 }
00266 }
00267
00268 public Guid CurrentUserGuid
00269 {
00270 get
00271 {
00272 if (_CurrentUserGuid == Guid.Empty)
00273 {
00274 _CurrentUserGuid = WebServices.Resources.GetCurrentUserUid();
00275 }
00276
00277 return _CurrentUserGuid;
00278 }
00279 }
00280
00281 public ProjectCollection Projects
00282 {
00283 get
00284 {
00285 if (_ProjectCollection == null)
00286 {
00287 _ProjectCollection = new ProjectCollection(this);
00288 }
00289
00290 return _ProjectCollection;
00291 }
00292 }
00293
00294 public CustomFields CustomFields
00295 {
00296 get
00297 {
00298 if (_CustomFields == null)
00299 {
00300 _CustomFields = new CustomFields(this);
00301 }
00302
00303 return _CustomFields;
00304 }
00305 }
00306
00307 public ResourceCollection Resources
00308 {
00309 get
00310 {
00311 if (_ResourceCollection == null)
00312 {
00313 _ResourceCollection = new ResourceCollection(this);
00314 }
00315
00316 return _ResourceCollection;
00317 }
00318 }
00319
00320 public LookupTables LookupTables
00321 {
00322 get
00323 {
00324 if (_LookupTables == null)
00325 {
00326 _LookupTables = new LookupTables(this, true);
00327 }
00328
00329 return _LookupTables;
00330 }
00331 }
00332
00333 public Queue Queue
00334 {
00335 get
00336 {
00337 if (_Queue == null)
00338 {
00339 _Queue = new Queue(this);
00340 }
00341
00342 return _Queue;
00343 }
00344 }
00345
00346 public Security Security
00347 {
00348 get
00349 {
00350 if (_Security == null)
00351 {
00352 _Security = new Security(this);
00353 }
00354
00355 return _Security;
00356 }
00357 }
00358
00359 public Calendars Calendars
00360 {
00361 get
00362 {
00363 if (_CalendarsCollection == null)
00364 {
00365 _CalendarsCollection = new Calendars(this);
00366 }
00367
00368 return _CalendarsCollection;
00369 }
00370 }
00371 public Timesheets Timesheets
00372 {
00373 get
00374 {
00375 if (_Timesheet == null)
00376 {
00377 _Timesheet = new Timesheets(this);
00378 }
00379
00380 return _Timesheet;
00381 }
00382 }
00383 public Events Events
00384 {
00385 get
00386 {
00387
00388 if (_Events == null)
00389 {
00390 _Events = new Events(this);
00391 }
00392
00393 return _Events;
00394 }
00395 }
00396
00397 #endregion
00398
00399 #region Internal Properties
00400
00401 public WebServices WebServices { get; private set; }
00402
00403 protected internal NetworkCredential NetworkCredential { get; private set; }
00404
00405 protected internal CookieContainer CookieContainer { get; private set; }
00406
00407 #endregion
00408
00409 #region Public Methods
00410
00411 public bool IsImpersonating { get; private set; }
00412
00413 public ILog Log
00414 {
00415 get { return _Log; }
00416 }
00417
00418
00419
00420
00421 public void Dispose()
00422 {
00423 Dispose(true);
00424 GC.SuppressFinalize(this);
00425 }
00426
00427 public void SetImpersonationContext(bool isWindowsUser,
00428 string userName,
00429 Guid userGuid,
00430 Guid trackingGuid,
00431 Guid siteGuid,
00432 string lcid)
00433 {
00434
00435
00436
00437 WebServices.Calendars.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00438 WebServices.CustomFields.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00439 WebServices.LookupTables.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00440 WebServices.Projects.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00441 WebServices.Resources.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00442 WebServices.Statusing.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00443 WebServices.Queue.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00444 WebServices.Security.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00445 WebServices.Timesheet.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00446 WebServices.Administration.SetImpersonationContext(isWindowsUser, userName, userGuid, trackingGuid, siteGuid, lcid);
00447 }
00448
00449 public void ClearImpersonation()
00450 {
00451 WebServices.Calendars.ContextString = null;
00452 WebServices.CustomFields.ContextString = null;
00453 WebServices.LookupTables.ContextString = null;
00454 WebServices.Projects.ContextString = null;
00455 WebServices.Resources.ContextString = null;
00456 WebServices.Statusing.ContextString = null;
00457 WebServices.Queue.ContextString = null;
00458 WebServices.Security.ContextString = null;
00459 }
00460
00461
00462
00463
00464 public void Refresh()
00465 {
00466 DisposeResourceCollection();
00467
00468 if (_ResourceCollection == null)
00469 {
00470 _ResourceCollection = new ResourceCollection(this);
00471 }
00472
00473 _ResourceCollection.LoadResourceCollection();
00474 _ResourceCollection.LoadResourceCustomFields();
00475
00476 if (_ProjectCollection == null)
00477 {
00478 _ProjectCollection = new ProjectCollection(this);
00479 }
00480
00481 _ProjectCollection.LoadProjectCollection();
00482
00483 if (_CalendarsCollection == null)
00484 {
00485 _CalendarsCollection = new Calendars(this);
00486 }
00487
00488 _CalendarsCollection.LoadCalendars();
00489
00490 _CustomFields = null;
00491 }
00492
00493 public Guid GetSiteGuid()
00494 {
00495 try
00496 {
00497 string siteURL;
00498 string siteID;
00499
00500 WebServices.SiteData.GetSiteUrl(Site.OriginalString, out siteURL, out siteID);
00501
00502 return new Guid(siteID);
00503 }
00504 catch (Exception exception)
00505 {
00506 Debug.WriteLine(exception.Message);
00507 throw;
00508 }
00509 }
00510
00511 #endregion
00512
00513 #region Private Methods
00514
00515 private void Dispose(bool disposing)
00516 {
00517 if (!_Disposed)
00518 {
00519 if (disposing)
00520 {
00521 try
00522 {
00523 switch (AuthenticationType)
00524 {
00525 case AuthenticationType.NotAuthenticated:
00526 break;
00527 case AuthenticationType.Windows:
00528 if (WebServices.LoginWindows != null)
00529 {
00530 WebServices.LoginWindows.Logoff();
00531 }
00532 break;
00533 case AuthenticationType.Forms:
00534 if (WebServices.LoginForms != null)
00535 {
00536 WebServices.LoginForms.Logoff();
00537 }
00538 break;
00539 default:
00540 break;
00541 }
00542
00543 WebServices.Dispose(WebServices.LoginWindows);
00544 WebServices.Dispose(WebServices.LoginForms);
00545 WebServices.Dispose(WebServices.Projects);
00546 WebServices.Dispose(WebServices.Resources);
00547 WebServices.Dispose(WebServices.CustomFields);
00548 WebServices.Dispose(WebServices.LookupTables);
00549
00550 DisposeResourceCollection();
00551 }
00552 catch (Exception exception)
00553 {
00554 Debug.WriteLine(exception.Message);
00555 Debug.WriteLine(exception.StackTrace);
00556
00557
00558 }
00559 }
00560
00561 _Disposed = true;
00562 }
00563 }
00564
00565 private void DisposeResourceCollection()
00566 {
00567 if (_ResourceCollection != null)
00568 {
00569 foreach (Guid index in _ResourceCollection)
00570 {
00571 _ResourceCollection[index].Dispose();
00572 }
00573 }
00574 }
00575
00576 #endregion
00577
00578 internal void WriteLogEntry(LogArea logArea, LogEntryType type, string message)
00579 {
00580 if (_Log != null)
00581 {
00582 if (type == LogEntryType.Information && !_Log.Verbose)
00583 {
00584 return;
00585 }
00586
00587 _Log.WriteEntry(logArea, type, message);
00588 }
00589 }
00590 }
00591 }