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 Mcs.Epm.MicrosoftProject.mpFx.CustomFieldsWebService;
00016 using Mcs.Epm.MicrosoftProject.mpFx.ResourcesWebService;
00017
00018 namespace Mcs.Epm.MicrosoftProject.mpFx
00019 {
00020
00021
00022
00023 public sealed class ResourceCollection : IEnumerable<Guid>
00024 {
00025 #region Instance Data
00026
00027 private readonly ProjectServer _Parent;
00028 private CustomFieldDataSet _CustomFields;
00029 private Dictionary<Guid, EnterpriseResource> _ResourceCollection;
00030
00031 #endregion
00032
00033 #region Constructors
00034
00035
00036
00037
00038
00039 internal ResourceCollection(ProjectServer parent)
00040 {
00041 if (parent == null)
00042 {
00043 throw new InvalidOperationException( );
00044 }
00045
00046 _Parent = parent;
00047 }
00048
00049 #endregion
00050
00051 #region Public Properties
00052
00053
00054
00055
00056
00057
00058 public EnterpriseResource this[int index]
00059 {
00060 get
00061 {
00062 if (_ResourceCollection == null)
00063 {
00064 LoadResourceCollection();
00065 }
00066
00067 if (index > _ResourceCollection.Keys.Count)
00068 {
00069 throw new ArgumentOutOfRangeException( );
00070 }
00071
00072 Dictionary<Guid, EnterpriseResource>.KeyCollection.Enumerator enumerator = _ResourceCollection.Keys.GetEnumerator();
00073
00074 for (int i = 0; i <= index; i++)
00075 {
00076 enumerator.MoveNext();
00077 }
00078
00079 return _ResourceCollection[enumerator.Current];
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088 public EnterpriseResource this[Guid resourceGuid]
00089 {
00090 get
00091 {
00092 if (_ResourceCollection == null)
00093 {
00094 LoadResourceCollection();
00095 }
00096
00097 if (!_ResourceCollection.ContainsKey(resourceGuid))
00098 {
00099 return null;
00100 }
00101 return _ResourceCollection[resourceGuid];
00102 }
00103 }
00104
00105
00106
00107
00108 public CustomFieldDataSet CustomFieldsDataSet
00109 {
00110 get
00111 {
00112 if (_CustomFields == null)
00113 {
00114 LoadResourceCustomFields();
00115 }
00116 return _CustomFields;
00117 }
00118 }
00119
00120 #endregion
00121
00122 #region Public Methods
00123
00124 public void AddAuthorization(Guid resourceUid, string Account, bool WindowsUser)
00125 {
00126 using (ResourceAuthorizationDataSet resourceAuthDs = new ResourceAuthorizationDataSet())
00127 {
00128 ResourceAuthorizationDataSet.ResourcesRow resourceAuthRow = resourceAuthDs.Resources.NewResourcesRow();
00129
00130 resourceAuthRow.RES_UID = resourceUid;
00131
00132 resourceAuthRow.RES_IS_WINDOWS_USER = WindowsUser;
00133
00134 resourceAuthRow.WRES_ACCOUNT = Account;
00135
00136 resourceAuthDs.Resources.AddResourcesRow(resourceAuthRow);
00137
00138 Parent.WebServices.Resources.SetResourceAuthorization(resourceAuthDs);
00139 }
00140 }
00141
00142
00143 public void Refresh()
00144 {
00145 LoadResourceCollection();
00146 }
00147
00148 public EnterpriseResource FindByName(string resourceName)
00149 {
00150 if (_ResourceCollection == null)
00151 {
00152 LoadResourceCollection();
00153 }
00154
00155 if (_ResourceCollection != null)
00156 {
00157 foreach (KeyValuePair<Guid, EnterpriseResource> resource in _ResourceCollection)
00158 {
00159 if (resource.Value.Name.Equals(resourceName, StringComparison.CurrentCultureIgnoreCase))
00160 {
00161 return resource.Value;
00162 }
00163 }
00164 }
00165
00166 return null;
00167 }
00168
00169 public void Create(ResourceDataSet resourceDataSet, bool validateOnly, bool autoCheckIn)
00170 {
00171 _Parent.WebServices.Resources.CreateResources(resourceDataSet, validateOnly, autoCheckIn);
00172 }
00173
00174
00175
00176
00177
00178
00179 public bool ContainsKey(Guid resourceGuid)
00180 {
00181 if (_ResourceCollection == null)
00182 {
00183 LoadResourceCollection();
00184 }
00185 return _ResourceCollection.ContainsKey(resourceGuid);
00186 }
00187
00188
00189
00190
00191 public void LoadResourceCustomFields()
00192 {
00193 if (_CustomFields != null)
00194 {
00195 _CustomFields.Dispose();
00196 }
00197
00198 _CustomFields = new CustomFieldDataSet();
00199
00200 _CustomFields = Parent.WebServices.CustomFields.ReadCustomFieldsByEntity(new Guid(Utilities.GetEntityGuidStringFromName("Resource")));
00201 }
00202
00203
00204
00205
00206
00207
00208 public EnterpriseResource GetResourceGuidByName(string resourceName)
00209 {
00210 if (_ResourceCollection == null)
00211 {
00212 LoadResourceCollection();
00213 }
00214
00215 foreach (KeyValuePair<Guid, EnterpriseResource> enterpriseResource in _ResourceCollection)
00216 {
00217 if (enterpriseResource.Value.Name.Equals(resourceName, StringComparison.CurrentCultureIgnoreCase))
00218 {
00219 return enterpriseResource.Value;
00220 }
00221 }
00222
00223 return null;
00224 }
00225
00226 public ResourceDataSet.ResourcesRow GetResourceGuidByAccount(string account)
00227 {
00228 using (ResourceDataSet resourceDataSet = _Parent.WebServices.Resources.ReadResources(ResourceFilters.ItemBasicInformationByAccount(account), false))
00229 {
00230 if (resourceDataSet == null || resourceDataSet.Resources.Rows.Count != 1)
00231 {
00232 return null;
00233 }
00234
00235 return (ResourceDataSet.ResourcesRow)resourceDataSet.Resources.Rows[0];
00236 }
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 public ResourceDataSet ReadResources(string xmlFilter, bool autoCheckout)
00247 {
00248 return Parent.WebServices.Resources.ReadResources(xmlFilter, autoCheckout);
00249 }
00250
00251 #endregion
00252
00253 #region Internal Methods
00254
00255
00256
00257
00258 internal void LoadResourceCollection()
00259 {
00260 if (_ResourceCollection == null)
00261 {
00262 _ResourceCollection = new Dictionary<Guid, EnterpriseResource>();
00263 }
00264 else
00265 {
00266 _ResourceCollection.Clear();
00267 }
00268
00269 using (ResourceDataSet resourceDataSet = Parent.WebServices.Resources.ReadResources(ResourceFilters.ListMinimumResourceInformation, false))
00270 {
00271 foreach (ResourceDataSet.ResourcesRow resource in resourceDataSet.Resources.Rows)
00272 {
00273 _ResourceCollection.Add(resource.RES_UID, new EnterpriseResource(this, resource.RES_UID, resource.RES_NAME));
00274 }
00275 }
00276 }
00277
00278 #endregion
00279
00280 #region Internal Properties
00281
00282 internal Resource ResourcesWebService
00283 {
00284 get { return Parent.WebServices.Resources; }
00285 }
00286
00287 internal ProjectServer Parent
00288 {
00289 get { return _Parent; }
00290 }
00291
00292 #endregion
00293
00294 #region IEnumerable Implementation
00295
00296 IEnumerator<Guid> IEnumerable<Guid>.GetEnumerator()
00297 {
00298 if (_ResourceCollection == null)
00299 {
00300 LoadResourceCollection();
00301 }
00302
00303 foreach (KeyValuePair<Guid, EnterpriseResource> pair in _ResourceCollection)
00304 {
00305 yield return pair.Key;
00306 }
00307 }
00308
00309 public IEnumerator GetEnumerator()
00310 {
00311 return ((IEnumerable<Guid>)this).GetEnumerator();
00312 }
00313
00314 #endregion
00315 }
00316 }