00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 using System;
00013 using System.Data;
00014 using System.Reflection;
00015 using Mcs.Epm.MicrosoftProject.mpFx.EventsWebService;
00016
00017 namespace Mcs.Epm.MicrosoftProject.mpFx
00018 {
00019
00020
00021
00022 public class Events
00023 {
00024 #region Public Properties
00025
00026 public ProjectServer Parent { get; set; }
00027
00028 #endregion
00029
00030 #region Constructors
00031
00032 public Events(ProjectServer parent)
00033 {
00034 if (parent == null)
00035 {
00036 throw new ArgumentNullException("parent");
00037 }
00038
00039 Parent = parent;
00040 }
00041
00042 #endregion
00043
00044 #region Public Methods
00045
00046 public EventHandlersDataSet ReadEventHandlerAssociationsForEvent(PSEventID eventId)
00047 {
00048 return Parent.WebServices.Events.ReadEventHandlerAssociationsForEvent(eventId);
00049 }
00050
00051 public bool IsEventHandlerRegisteredForClass(PSEventID eventId, string className)
00052 {
00053 using (EventHandlersDataSet eventHandlers = Parent.Events.ReadEventHandlerAssociationsForEvent(eventId))
00054 {
00055 if (eventHandlers.EventHandlers.Count == 0)
00056 {
00057 return false;
00058 }
00059
00060 foreach (EventHandlersDataSet.EventHandlersRow eventHandler in eventHandlers.EventHandlers)
00061 {
00062 if (eventHandler.ClassName.Equals(className))
00063 {
00064 return true;
00065 }
00066 }
00067 }
00068
00069 return false;
00070 }
00071
00072 public PSEventID? GetEventID(string sourceName, string eventName)
00073 {
00074 using (EventsDataSet eventsDataSet = Parent.WebServices.Events.ReadEventsList())
00075 {
00076 DataView dataView = new DataView(eventsDataSet.Event);
00077
00078 dataView.RowFilter = string.Format("{0} = '{1}'",
00079 eventsDataSet.Event.Columns["SourceName"].ColumnName,
00080 sourceName);
00081
00082 dataView.RowFilter = string.Format("{0} AND {1} = '{2}'",
00083 dataView.RowFilter,
00084 eventsDataSet.Event.Columns["EventName"].ColumnName,
00085 eventName);
00086
00087 if (dataView.Count == 0)
00088 {
00089 return null;
00090 }
00091
00092 DataRowView dataRowView = dataView[0];
00093
00094 return (PSEventID) dataRowView["EventId"];
00095
00096 }
00097 }
00098
00099 public Guid? Install(PSEventID eventID,
00100 string assemblyName,
00101 string sourceName,
00102 string eventName,
00103 string eventMethod,
00104 string className,
00105 string description)
00106 {
00107 if (IsEventHandlerRegisteredForClass(eventID, className))
00108 {
00109 return null;
00110 }
00111
00112 using (EventHandlersDataSet eventHandlerDataSet = Parent.WebServices.Events.ReadEventHandlerAssociationsForEvent(eventID))
00113 {
00114 int order = 0;
00115
00116 foreach (EventHandlersDataSet.EventHandlersRow eventHandlersRow in eventHandlerDataSet.EventHandlers.Rows)
00117 {
00118 order = Math.Max(order, eventHandlersRow.Order);
00119 }
00120
00121 order++;
00122
00123 EventHandlersDataSet.EventHandlersRow newHandler = eventHandlerDataSet.EventHandlers.NewEventHandlersRow();
00124
00125 newHandler.Order = order;
00126 newHandler.AssemblyName = assemblyName;
00127 newHandler.ClassName = className;
00128 newHandler.Description = description;
00129 newHandler.EventHandlerUid = Guid.NewGuid();
00130 newHandler.EventId = (int) eventID;
00131 newHandler.Name = string.Format("{0} {1} {2}", className, sourceName, eventName);
00132
00133 eventHandlerDataSet.EventHandlers.AddEventHandlersRow(newHandler);
00134
00135 Parent.WebServices.Events.CreateEventHandlerAssociations(eventHandlerDataSet);
00136
00137 return newHandler.EventHandlerUid;
00138 }
00139 }
00140
00141 public EventHandlersDataSet Read()
00142 {
00143 return Parent.WebServices.Events.ReadEventHandlerAssociations();
00144 }
00145
00146 #endregion
00147
00148 #region Private Methods
00149
00150 private void AddAssemblyEventHandlers(Assembly eventHandlerAssembly)
00151 {
00152 foreach (Type type in eventHandlerAssembly.GetTypes())
00153 {
00154
00155 if (type.IsClass && type.BaseType.Module.Name.EndsWith("Microsoft.Office.Project.Server.Events.Receivers.dll"))
00156 {
00157 foreach (MethodInfo method in type.GetMethods())
00158 {
00159
00160 if (method.DeclaringType.FullName ==
00161 type.FullName)
00162 {
00163 int index = type.BaseType.Name.LastIndexOf("EventReceiver");
00164 string sourceName = type.BaseType.Name.Substring(0, index);
00165 string eventName = method.Name.Substring(2);
00166 string eventMethod = method.Name;
00167 string className = type.FullName;
00168
00169 PSEventID? eventId = GetEventID(sourceName, eventName);
00170
00171 if (eventId == null)
00172 {
00173 throw new ArgumentOutOfRangeException("eventHandlerAssembly");
00174 }
00175
00176 Install((PSEventID) eventId,
00177 eventHandlerAssembly.FullName,
00178 sourceName,
00179 eventName,
00180 eventMethod,
00181 className,
00182 string.Empty);
00183 }
00184 }
00185 }
00186 }
00187 }
00188
00189 #endregion
00190 }
00191 }