Esempio n. 1
0
        public JsonResponse Register(User user)
        {
            var result = _userService.RegisterUser(user);

            if (result.Success)
            {
                Log.Activity(HttpContext, string.Format("New user registered - {0}", user.UserName));
            }

            return(new JsonResponse(result));
        }
Esempio n. 2
0
        public JsonResponse ConfirmRegistration(string userName, string confirmationSalt)
        {
            var result = _userService.ConfirmRegistration(userName, confirmationSalt);

            if (result.Success)
            {
                Log.Activity(HttpContext, string.Format("User {0} confirmed his registration", userName));
            }

            return(new JsonResponse(result));
        }
Esempio n. 3
0
        public JsonResponse UploadBuildOrder(string userName)
        {
            if (User == null || User.UserData.UserName != userName)
            {
                return(new JsonResponse(false, "Unauthorized build order upload is not allowed!"));
            }

            var fileContent = Request.Files[0];

            if (fileContent == null || fileContent.ContentLength == 0)
            {
                return(new JsonResponse(false, "File was not added into request!"));
            }

            BuildOrderInfo uploadBuild = null;
            BuildOrder     buildEntity = null;

            try
            {
                using (var inputStream = new StreamReader(fileContent.InputStream))
                {
                    var jsonReader = new JsonTextReader(inputStream);
                    var ser        = new JsonSerializer();
                    uploadBuild = ser.Deserialize <BuildOrderInfo>(jsonReader);
                }

                buildEntity = ConvertBuildInfoToDomainModel(uploadBuild);
            }
            catch (Exception ex)
            {
                return(new JsonResponse(false, "File format is not valid!"));
            }

            var result = _service.AddBuildOrder(buildEntity);

            if (result.Success)
            {
                Log.Activity(HttpContext, string.Format("New build order [{0}] uploaded by {1}", buildEntity.Name, userName));
            }

            return(new JsonResponse(result));
        }
Esempio n. 4
0
        public void parseparameters()
        {
            Workflow.Serializable = true;
            Workflow.Parameters.Clear();
            if (!string.IsNullOrEmpty(Workflow.Xaml))
            {
                var parameters = GetParameters();
                foreach (var prop in parameters)
                {
                    var par = new workflowparameter()
                    {
                        name = prop.Name
                    };
                    string baseTypeName = prop.Type.BaseType.FullName;
                    if (!prop.Type.IsSerializable2())
                    {
                        Log.Activity(string.Format("Name: {0}, Type: {1} is not serializable, therefor saving state will not be supported", prop.Name, prop.Type));
                        Workflow.Serializable = false;
                    }
                    if (baseTypeName == "System.Activities.InArgument")
                    {
                        par.direction = workflowparameterdirection.@in;
                    }
                    if (baseTypeName == "System.Activities.InOutArgument")
                    {
                        par.direction = workflowparameterdirection.inout;
                    }
                    if (baseTypeName == "System.Activities.OutArgument")
                    {
                        par.direction = workflowparameterdirection.@out;
                    }
                    par.type = prop.Type.GenericTypeArguments[0].FullName;
                    Log.Activity(string.Format("Name: '{0}', Type: {1}", prop.Name, prop.Type));
                    Workflow.Parameters.Add(par);
                }
            }
            if (Workflow.Serializable == true)
            {
                ModelTreeManager mtm     = wfDesigner.Context.Services.GetService <ModelTreeManager>();
                bool             canIdle = false;
                foreach (ModelItem item in this.GetWorkflowActivities(null))
                {
                    try
                    {
                        var a    = item.GetCurrentValue();
                        var prop = a.GetType().GetProperty("CanInduceIdle", BindingFlags.NonPublic | BindingFlags.Instance);
                        if (prop != null)
                        {
                            var CanInduceIdle = (bool)prop.GetValue(a);
                            if (CanInduceIdle == true)
                            {
                                Log.Activity(string.Format("Activity: '{0}' Can induce idle, need to check if workflow is serializable", ToString()));
                                canIdle = true;
                            }
                        }

                        //var i = item.GetCurrentValue() as Activity;
                        //var i2 = i;
                        //ModelItemImpl i = item.instan as System.Activities.Presentation.Model.ModelItemImpl;
                        //item.can
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                if (canIdle == true)
                {
                    foreach (ModelItem item in this.GetWorkflowActivities(null))
                    {
                        var vars = item.Properties["Variables"];
                        if (vars != null && vars.Collection != null)
                        {
                            foreach (var v in vars.Collection)
                            {
                                try
                                {
                                    string baseTypeName = v.ItemType.GenericTypeArguments[0].BaseType.FullName;
                                    if (!v.ItemType.GenericTypeArguments[0].IsSerializable2())
                                    {
                                        var _v   = v.GetCurrentValue();
                                        var prop = _v.GetType().GetProperty("Name");
                                        if (prop != null)
                                        {
                                            Log.Activity(string.Format("Variable name: '{0}', Type: {1} is not serializable", (string)prop.GetValue(_v), baseTypeName));
                                        }
                                        else
                                        {
                                            Log.Activity(string.Format("TypeName: '{0}', Type: {1} is not serializable", v.ItemType.GenericTypeArguments[0].Name, baseTypeName));
                                        }
                                        Workflow.Serializable = false;
                                        //throw new NotSerializable("All properties on a workflow needs to be serializable '" + prop.Name + "'");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.Debug(ex.ToString());
                                }
                            }
                        }
                    }
                }
            }
        }
        protected override void Track(TrackingRecord trackRecord, TimeSpan timeStamp)
        {
            try
            {
                string State      = "unknown";
                Guid   InstanceId = trackRecord.InstanceId;
                ActivityStateRecord     activityStateRecord     = trackRecord as ActivityStateRecord;
                ActivityScheduledRecord activityScheduledRecord = trackRecord as ActivityScheduledRecord;
                WorkflowInstanceRecord  workflowInstanceRecord  = trackRecord as WorkflowInstanceRecord;

                if (workflowInstanceRecord != null)
                {
                    Log.Activity(workflowInstanceRecord.ActivityDefinitionId + " " + workflowInstanceRecord.State);
                    var Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
                    if (Instance == null)
                    {
                        return;
                    }
                    lock (Instance)
                    {
                        if (workflowInstanceRecord.State == WorkflowInstanceStates.Started || workflowInstanceRecord.State == WorkflowInstanceStates.Resumed)
                        {
                            lock (timerslock) timers.Add(InstanceId.ToString(), new Dictionary <string, Stopwatch>());
                        }
                        else if (workflowInstanceRecord.State == WorkflowInstanceStates.Aborted || workflowInstanceRecord.State == WorkflowInstanceStates.Canceled ||
                                 workflowInstanceRecord.State == WorkflowInstanceStates.Completed || workflowInstanceRecord.State == WorkflowInstanceStates.Deleted ||
                                 workflowInstanceRecord.State == WorkflowInstanceStates.Suspended || workflowInstanceRecord.State == WorkflowInstanceStates.Terminated ||
                                 workflowInstanceRecord.State == WorkflowInstanceStates.UnhandledException || workflowInstanceRecord.State == WorkflowInstanceStates.UpdateFailed)
                        {
                            if (timers.ContainsKey(InstanceId.ToString()))
                            {
                                lock (timerslock) timers.Remove(InstanceId.ToString());
                            }
                        }
                    }
                }
                if (activityStateRecord != null)
                {
                    string ActivityId = null, name = null;
                    var    Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Id))
                    {
                        ActivityId = activityStateRecord.Activity.Id;
                    }
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Name))
                    {
                        name = activityStateRecord.Activity.Name;
                    }
                    // var sw = new Stopwatch(); sw.Start();
                    Log.Activity(name + " " + activityStateRecord.State);
                    if (timers.ContainsKey(InstanceId.ToString()) && !string.IsNullOrEmpty(ActivityId))
                    {
                        var timer = timers[InstanceId.ToString()];
                        if (activityStateRecord.State == ActivityStates.Executing)
                        {
                            if (!timer.ContainsKey(ActivityId))
                            {
                                Stopwatch sw = new Stopwatch(); sw.Start();
                                timer.Add(ActivityId, sw);
                                var TypeName = activityStateRecord.Activity.TypeName;
                                var Name     = activityStateRecord.Activity.Name;
                                if (String.IsNullOrEmpty(Name))
                                {
                                    Name = TypeName;
                                }
                                if (TypeName.IndexOf("`") > -1)
                                {
                                    TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                                }
                            }
                        }
                        if (activityStateRecord.State != ActivityStates.Executing)
                        {
                            if (timer.ContainsKey(ActivityId))
                            {
                                Stopwatch sw = timer[ActivityId];
                                timer.Remove(ActivityId);
                                var TypeName = activityStateRecord.Activity.TypeName;
                                var Name     = activityStateRecord.Activity.Name;
                                if (String.IsNullOrEmpty(Name))
                                {
                                    Name = TypeName;
                                }
                                if (TypeName.IndexOf("`") > -1)
                                {
                                    TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                                }
                            }
                        }
                    }
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Name) && Instance != null && Instance.Workflow != null)
                    {
                        var TypeName = activityStateRecord.Activity.TypeName;
                        if (TypeName.IndexOf("`") > -1)
                        {
                            TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                        }
                    }
                    foreach (var v in activityStateRecord.Variables)
                    {
                        if (Instance.Variables.ContainsKey(v.Key))
                        {
                            Instance.Variables[v.Key].value = v.Value;
                        }
                        else
                        {
                            if (v.Value != null)
                            {
                                Instance.Variables.Add(v.Key, new WorkflowInstanceValueType(v.Value.GetType(), v.Value));
                            }
                        }
                    }
                }
                if (activityScheduledRecord != null)
                {
                    var Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
                    if (Instance == null || Instance.wfApp == null)
                    {
                        return;
                    }
                    var wfApp     = Instance.wfApp;
                    var executor  = typeof(System.Activities.Hosting.WorkflowInstance).GetField("executor", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(wfApp);
                    var scheduler = executor.GetType().GetField("scheduler", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor);

                    string ActivityId      = null;
                    string ChildActivityId = null;
                    if (activityStateRecord != null)
                    {
                        ActivityId = activityStateRecord.Activity.Id;
                        State      = activityStateRecord.State.ToLower();
                    }
                    if (activityScheduledRecord != null)
                    {
                        State = "Scheduled";
                        if (activityScheduledRecord.Activity != null)
                        {
                            ActivityId = activityScheduledRecord.Activity.Id;
                        }
                        if (activityScheduledRecord.Child != null)
                        {
                            ChildActivityId = activityScheduledRecord.Child.Id;
                        }
                    }
                    if (activityScheduledRecord.Activity == null && activityScheduledRecord.Child != null)
                    {
                        // this will make "1" be handles twice, but "1" is always sendt AFTER being scheduled, but we can catch it here ?
                        ActivityId      = activityScheduledRecord.Child.Id;
                        ChildActivityId = activityScheduledRecord.Child.Id;
                    }
                    if (string.IsNullOrEmpty(ActivityId))
                    {
                        return;
                    }

                    if (activityScheduledRecord.Child.Id == "1.11")
                    {
                        // scheduler.GetType().GetMethod("ClearAllWorkItems", BindingFlags.Public | BindingFlags.Instance).Invoke(scheduler, new object[] { executor });
                        // scheduler.GetType().GetMethod("ScheduleWork", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(scheduler, new object[] { false });
                        //var firstWorkItem = scheduler.GetType().GetField("firstWorkItem", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(scheduler);
                        //firstWorkItem.GetType().GetMethod("Release", BindingFlags.Public | BindingFlags.Instance).Invoke(firstWorkItem, new object[] { executor });
                        //firstWorkItem.GetType().GetMethod("Dispose", BindingFlags.Public | BindingFlags.Instance).Invoke(firstWorkItem, new object[] { executor });

                        //scheduler.GetType().GetMethod("NotifyWorkCompletion", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(scheduler, new object[] { });
                    }

                    if (Instance.Variables == null)
                    {
                        Instance.Variables = new Dictionary <string, WorkflowInstanceValueType>();
                    }
                    if (activityStateRecord != null)
                    {
                        foreach (var v in Instance.Variables.ToList())
                        {
                            if (!activityStateRecord.Variables.ContainsKey(v.Key))
                            {
                                Instance.Variables.Remove(v.Key);
                            }
                        }
                        foreach (var v in activityStateRecord.Variables)
                        {
                            if (Instance.Variables.ContainsKey(v.Key))
                            {
                                Instance.Variables[v.Key].value = v.Value;
                            }
                        }
                    }
                    var instanceMapField = executor.GetType().GetField("instanceMap", BindingFlags.NonPublic | BindingFlags.Instance);

                    // get SerializedProgramMapping to have InstanceMap get filled, needed by SerializedProgramMapping
                    var SerializedProgramMapping      = executor.GetType().GetProperty("SerializedProgramMapping", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor);
                    ActivityInstance activityInstance = executor.GetType().GetField("rootInstance", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor) as ActivityInstance;

                    // Sometimes we can find the ActivityInstance in rootInstance
                    ActivityInstance result = findActivityInstance(executor, activityInstance, ActivityId);

                    // But more often, we find it in InstanceMapping
                    var instanceMap = instanceMapField.GetValue(executor);
                    if (instanceMap != null && result == null)
                    {
                        var _list = SerializedProgramMapping.GetType().GetProperty("InstanceMapping", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(SerializedProgramMapping);
                        foreach (System.Collections.DictionaryEntry kvp in (System.Collections.IDictionary)_list)
                        {
                            var a = kvp.Key as System.Activities.Activity;
                            if (a == null)
                            {
                                continue;
                            }
                            if (result == null && a.Id == ActivityId)
                            {
                                result = findActivityInstance(kvp.Value, ActivityId);
                            }
                        }
                    }
                    if (result != null)
                    {
                        WorkflowDataContext context = null;
                        var             cs          = typeof(WorkflowDataContext).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
                        ConstructorInfo c           = cs.First();

                        try
                        {
                            object o = c.Invoke(new Object[] { executor, result, true });
                            context = o as WorkflowDataContext;
                            var vars = context.GetProperties();
                            foreach (dynamic v in vars)
                            {
                                var value = v.GetValue(context);
                                if (Instance.Variables.ContainsKey(v.DisplayName))
                                {
                                    Instance.Variables[v.DisplayName] = new WorkflowInstanceValueType(v.PropertyType, value);
                                }
                                else
                                {
                                    Instance.Variables.Add(v.DisplayName, new WorkflowInstanceValueType(v.PropertyType, value));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Debug(ex.Message);
                        }
                    }

                    OnVisualTracking?.Invoke(Instance, ActivityId, ChildActivityId, State);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Esempio n. 6
0
        protected override void Track(TrackingRecord trackRecord, TimeSpan timeStamp)
        {
            try
            {
                string State      = "unknown";
                Guid   InstanceId = trackRecord.InstanceId;
                ActivityStateRecord     activityStateRecord     = trackRecord as ActivityStateRecord;
                ActivityScheduledRecord activityScheduledRecord = trackRecord as ActivityScheduledRecord;
                WorkflowInstanceRecord  workflowInstanceRecord  = trackRecord as WorkflowInstanceRecord;

                var Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
                if (Instance == null)
                {
                    if (System.Threading.Monitor.TryEnter(WorkflowInstance.Instances, 1000))
                    {
                        try
                        {
                            Instance = WorkflowInstance.Instances.Where(x => x.InstanceId == InstanceId.ToString()).FirstOrDefault();
                        }
                        finally
                        {
                            System.Threading.Monitor.Exit(WorkflowInstance.Instances);
                        }
                    }
                    else
                    {
                        Log.Error("Failed getting WorkflowInstance in TrackingParticipant");
                    }
                }

                if (workflowInstanceRecord != null && Instance != null)
                {
                    Log.Activity(workflowInstanceRecord.ActivityDefinitionId + " " + workflowInstanceRecord.State);
                    if (Instance == null)
                    {
                        return;
                    }
                    if (workflowInstanceRecord.State == WorkflowInstanceStates.Started || workflowInstanceRecord.State == WorkflowInstanceStates.Resumed)
                    {
                        if (System.Threading.Monitor.TryEnter(timerslock, 1000))
                        {
                            try
                            {
                                timers.Add(InstanceId.ToString(), new Dictionary <string, Stopwatch>());
                            }
                            finally
                            {
                                System.Threading.Monitor.Exit(timerslock);
                            }
                        }


                        System.Diagnostics.Activity.Current = null;
                        try
                        {
                            Instance.RootActivity = Instance.source?.StartActivity(workflowInstanceRecord.State.ToString() + " " + Instance.name, ActivityKind.Consumer, Instance.ParentSpanId);
                        }
                        catch (Exception)
                        {
                            Instance.source = null;
                        }
                        if (Instance.RootActivity != null)
                        {
                            if (!string.IsNullOrEmpty(Instance.ParentSpanId))
                            {
                                Instance.RootActivity?.SetParentId(Instance.ParentSpanId);
                            }
                            Instance.SpanId = Instance.RootActivity.SpanId.ToHexString();
                        }
                        Instance.RootActivity?.SetTag("status.code", 200);
                        Instance.RootActivity?.SetTag("status.state", workflowInstanceRecord.State.ToString());
                        Instance.RootActivity?.SetTag("ofid", Config.local.openflow_uniqueid);
                        try
                        {
                            if (global.webSocketClient != null && global.webSocketClient.user != null && !string.IsNullOrEmpty(global.webSocketClient.user.username))
                            {
                                Instance.RootActivity?.SetTag("username", global.webSocketClient.user.username);
                            }
                            else
                            {
                                Instance.RootActivity?.SetTag("username", System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                            }
                        }
                        catch (Exception)
                        {
                        }
                        try
                        {
                            if (hostname == null)
                            {
                                hostname = System.Net.Dns.GetHostName();
                            }
                            Instance.RootActivity?.SetTag("hostname", hostname);
                        }
                        catch (Exception)
                        {
                            hostname = "";
                        }
                        Instance.Activities.Push(Instance.RootActivity);
                    }
                    else if (workflowInstanceRecord.State == WorkflowInstanceStates.Aborted || workflowInstanceRecord.State == WorkflowInstanceStates.Canceled ||
                             workflowInstanceRecord.State == WorkflowInstanceStates.Completed || workflowInstanceRecord.State == WorkflowInstanceStates.Deleted ||
                             workflowInstanceRecord.State == WorkflowInstanceStates.Suspended || workflowInstanceRecord.State == WorkflowInstanceStates.Terminated ||
                             workflowInstanceRecord.State == WorkflowInstanceStates.UnhandledException || workflowInstanceRecord.State == WorkflowInstanceStates.UpdateFailed)
                    {
                        if (System.Threading.Monitor.TryEnter(timerslock, 1000))
                        {
                            try
                            {
                                if (timers.ContainsKey(InstanceId.ToString()))
                                {
                                    timers.Remove(InstanceId.ToString());
                                }
                            }
                            finally
                            {
                                System.Threading.Monitor.Exit(timerslock);
                            }
                        }
                        if (workflowInstanceRecord.State != WorkflowInstanceStates.Completed)
                        {
                            Instance.RootActivity?.SetTag("status.state", 500);
                        }
                        if (workflowInstanceRecord.State == WorkflowInstanceStates.UnhandledException)
                        {
                            Instance.RootActivity?.SetTag("Exception", ((System.Activities.Tracking.WorkflowInstanceUnhandledExceptionRecord)workflowInstanceRecord).UnhandledException);
                        }
                        if (workflowInstanceRecord.State == WorkflowInstanceStates.Aborted)
                        {
                            Instance.RootActivity?.SetTag("Reason", ((System.Activities.Tracking.WorkflowInstanceAbortedRecord)workflowInstanceRecord).Reason);
                        }
                        if (workflowInstanceRecord.State == WorkflowInstanceStates.Suspended)
                        {
                            Instance.RootActivity?.SetTag("Reason", ((System.Activities.Tracking.WorkflowInstanceSuspendedRecord)workflowInstanceRecord).Reason);
                        }
                        if (workflowInstanceRecord.State == WorkflowInstanceStates.Terminated)
                        {
                            Instance.RootActivity?.SetTag("Reason", ((System.Activities.Tracking.WorkflowInstanceTerminatedRecord)workflowInstanceRecord).Reason);
                        }
                        Instance.RootActivity?.SetTag("status.state", workflowInstanceRecord.State.ToString());
                        if (Instance.source != null)
                        {
                            while (Instance.Activities.Count > 0)
                            {
                                var span = Instance.Activities.Pop();
                                span?.Dispose();
                            }
                            if (Instance.RootActivity != null)
                            {
                                Instance.RootActivity.Dispose();
                            }
                            Instance.RootActivity = null;
                        }
                    }
                }
                if (activityStateRecord != null && Instance != null)
                {
                    string ActivityId = null, name = null;
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Id))
                    {
                        ActivityId = activityStateRecord.Activity.Id;
                    }
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Name))
                    {
                        name = activityStateRecord.Activity.Name;
                    }
                    // var sw = new Stopwatch(); sw.Start();
                    Log.Activity(name + " " + activityStateRecord.State);
                    if (timers.ContainsKey(InstanceId.ToString()) && !string.IsNullOrEmpty(ActivityId))
                    {
                        var timer = timers[InstanceId.ToString()];
                        if (activityStateRecord.State == ActivityStates.Executing)
                        {
                            if (!timer.ContainsKey(ActivityId))
                            {
                                Stopwatch sw = new Stopwatch(); sw.Start();
                                timer.Add(ActivityId, sw);
                                var TypeName = activityStateRecord.Activity.TypeName;
                                var Name     = activityStateRecord.Activity.Name;
                                if (String.IsNullOrEmpty(Name))
                                {
                                    Name = TypeName;
                                }
                                if (TypeName.IndexOf("`") > -1)
                                {
                                    TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                                }

                                System.Diagnostics.Activity.Current = Instance.RootActivity;
                                try
                                {
                                    var span = Instance.source?.StartActivity(Name, ActivityKind.Consumer);
                                    span?.AddTag("type", TypeName);
                                    span?.AddTag("ActivityId", ActivityId);
                                    if (Instance.source != null && span != null)
                                    {
                                        Instance.Activities.Push(span);
                                    }
                                }
                                catch (Exception)
                                {
                                    Instance.source = null;
                                }
                            }
                        }
                        if (activityStateRecord.State != ActivityStates.Executing)
                        {
                            if (timer.ContainsKey(ActivityId))
                            {
                                Stopwatch sw = timer[ActivityId];
                                timer.Remove(ActivityId);
                                var TypeName = activityStateRecord.Activity.TypeName;
                                var Name     = activityStateRecord.Activity.Name;
                                if (String.IsNullOrEmpty(Name))
                                {
                                    Name = TypeName;
                                }
                                if (TypeName.IndexOf("`") > -1)
                                {
                                    TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                                }

                                try
                                {
                                    if (System.Threading.Monitor.TryEnter(Instance.Activities, 1000))
                                    {
                                        try
                                        {
                                            if (Instance.Activities.Count > 0)
                                            {
                                                if (Instance.Activities.First()?.DisplayName == Name)
                                                {
                                                    var span = Instance.Activities.Pop();
                                                    span?.Dispose();
                                                }
                                            }
                                        }
                                        finally
                                        {
                                            System.Threading.Monitor.Exit(Instance.Activities);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(ex.ToString());
                                }
                            }
                        }
                    }
                    if (activityStateRecord.Activity != null && !string.IsNullOrEmpty(activityStateRecord.Activity.Name) && Instance != null && Instance.Workflow != null)
                    {
                        var TypeName = activityStateRecord.Activity.TypeName;
                        if (TypeName.IndexOf("`") > -1)
                        {
                            TypeName = TypeName.Substring(0, TypeName.IndexOf("`"));
                        }
                    }
                    foreach (var v in activityStateRecord.Variables)
                    {
                        if (Instance.Variables.ContainsKey(v.Key))
                        {
                            Instance.Variables[v.Key].value = v.Value;
                        }
                        else
                        {
                            if (v.Value != null)
                            {
                                Instance.Variables.Add(v.Key, new WorkflowInstanceValueType(v.Value.GetType(), v.Value));
                            }
                        }
                    }
                }
                if (activityScheduledRecord != null && Instance != null && Instance.wfApp != null)
                {
                    if (Instance.Activities.Count > 0)
                    {
                        var wfApp     = Instance.wfApp;
                        var executor  = typeof(System.Activities.Hosting.WorkflowInstance).GetField("executor", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(wfApp);
                        var scheduler = executor.GetType().GetField("scheduler", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor);

                        string ActivityId      = null;
                        string ChildActivityId = null;
                        if (activityStateRecord != null)
                        {
                            ActivityId = activityStateRecord.Activity.Id;
                            State      = activityStateRecord.State.ToLower();
                        }
                        if (activityScheduledRecord != null)
                        {
                            State = "Scheduled";
                            if (activityScheduledRecord.Activity != null)
                            {
                                ActivityId = activityScheduledRecord.Activity.Id;
                            }
                            if (activityScheduledRecord.Child != null)
                            {
                                ChildActivityId = activityScheduledRecord.Child.Id;
                            }
                        }
                        if (activityScheduledRecord.Activity == null && activityScheduledRecord.Child != null)
                        {
                            // this will make "1" be handles twice, but "1" is always sendt AFTER being scheduled, but we can catch it here ?
                            ActivityId      = activityScheduledRecord.Child.Id;
                            ChildActivityId = activityScheduledRecord.Child.Id;
                        }
                        if (string.IsNullOrEmpty(ActivityId))
                        {
                            return;
                        }

                        if (activityScheduledRecord.Child.Id == "1.11")
                        {
                            // scheduler.GetType().GetMethod("ClearAllWorkItems", BindingFlags.Public | BindingFlags.Instance).Invoke(scheduler, new object[] { executor });
                            // scheduler.GetType().GetMethod("ScheduleWork", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(scheduler, new object[] { false });
                            //var firstWorkItem = scheduler.GetType().GetField("firstWorkItem", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(scheduler);
                            //firstWorkItem.GetType().GetMethod("Release", BindingFlags.Public | BindingFlags.Instance).Invoke(firstWorkItem, new object[] { executor });
                            //firstWorkItem.GetType().GetMethod("Dispose", BindingFlags.Public | BindingFlags.Instance).Invoke(firstWorkItem, new object[] { executor });

                            //scheduler.GetType().GetMethod("NotifyWorkCompletion", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(scheduler, new object[] { });
                        }

                        if (Instance.Variables == null)
                        {
                            Instance.Variables = new Dictionary <string, WorkflowInstanceValueType>();
                        }
                        if (activityStateRecord != null)
                        {
                            foreach (var v in Instance.Variables.ToList())
                            {
                                if (!activityStateRecord.Variables.ContainsKey(v.Key))
                                {
                                    Instance.Variables.Remove(v.Key);
                                }
                            }
                            foreach (var v in activityStateRecord.Variables)
                            {
                                if (Instance.Variables.ContainsKey(v.Key))
                                {
                                    Instance.Variables[v.Key].value = v.Value;
                                }
                            }
                        }
                        var instanceMapField = executor.GetType().GetField("instanceMap", BindingFlags.NonPublic | BindingFlags.Instance);

                        // get SerializedProgramMapping to have InstanceMap get filled, needed by SerializedProgramMapping
                        var SerializedProgramMapping      = executor.GetType().GetProperty("SerializedProgramMapping", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor);
                        ActivityInstance activityInstance = executor.GetType().GetField("rootInstance", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(executor) as ActivityInstance;

                        // Sometimes we can find the ActivityInstance in rootInstance
                        ActivityInstance result = findActivityInstance(executor, activityInstance, ActivityId);

                        // But more often, we find it in InstanceMapping
                        var instanceMap = instanceMapField.GetValue(executor);
                        if (instanceMap != null && result == null)
                        {
                            var _list = SerializedProgramMapping.GetType().GetProperty("InstanceMapping", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(SerializedProgramMapping);
                            foreach (System.Collections.DictionaryEntry kvp in (System.Collections.IDictionary)_list)
                            {
                                var a = kvp.Key as System.Activities.Activity;
                                if (a == null)
                                {
                                    continue;
                                }
                                if (result == null && a.Id == ActivityId)
                                {
                                    result = findActivityInstance(kvp.Value, ActivityId);
                                }
                            }
                        }
                        if (result != null)
                        {
                            WorkflowDataContext context = null;
                            var             cs          = typeof(WorkflowDataContext).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance);
                            ConstructorInfo c           = cs.First();

                            try
                            {
                                object o = c.Invoke(new Object[] { executor, result, true });
                                context = o as WorkflowDataContext;
                                var vars = context.GetProperties();
                                foreach (dynamic v in vars)
                                {
                                    var value = v.GetValue(context);
                                    if (Instance.Variables.ContainsKey(v.DisplayName))
                                    {
                                        Instance.Variables[v.DisplayName] = new WorkflowInstanceValueType(v.PropertyType, value);
                                    }
                                    else
                                    {
                                        Instance.Variables.Add(v.DisplayName, new WorkflowInstanceValueType(v.PropertyType, value));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.Debug(ex.Message);
                            }
                        }
                        OnVisualTracking?.Invoke(Instance, ActivityId, ChildActivityId, State);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.ToString());
            }
        }
Esempio n. 7
0
        public JsonResponse UploadBuildOrder(string userName, string password,
                                             string name, string versionId, string description,
                                             string race, string vsRace, string buildItems)
        {
            var credResponce = _authService.Login(HttpContext, userName, password, false);

            if (!credResponce.Success)
            {
                return(new JsonResponse(false, "Wrong username or password!"));
            }

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(versionId) || string.IsNullOrEmpty(race) ||
                string.IsNullOrEmpty(vsRace) || string.IsNullOrEmpty(buildItems))
            {
                return(new JsonResponse(false, "Wrong upload parameters"));
            }

            BuildOrder buildEntity;

            try
            {
                buildEntity = new BuildOrder
                {
                    AddedDate     = DateTime.Now,
                    Race          = race,
                    VsRace        = vsRace,
                    Description   = description,
                    Name          = name,
                    SC2VersionID  = versionId,
                    OwnerUserID   = credResponce.Result.ID,
                    OwnerUserName = userName,
                    BuildItems    = new List <string>()
                };

                var decoded = HttpUtility.UrlDecode(buildItems);
                if (string.IsNullOrEmpty(decoded))
                {
                    return(new JsonResponse(false, "Cant decode string " + buildItems));
                }

                var splitted = decoded.Split(',').ToList();

                var boe = new BuildOrderEncoder(race);
                foreach (var item in splitted)
                {
                    var value = boe.getValue(item);
                    if (!string.IsNullOrEmpty(value))
                    {
                        buildEntity.BuildItems.Add(value);
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResponse(false, "Build data is not in valid format! Exception: " + ex.Message));
            }

            GeneralResponse saveResponse;

            var response = _service.GetBuildByName(buildEntity.Name);

            if (response.Result != null && response.Result.OwnerUserName != userName)
            {
                return(new JsonResponse(false,
                                        "Build order with name " + buildEntity.Name + " already exists and was added by " + userName +
                                        "! Only owner can update existing build order."));
            }

            if (response.Result != null)
            {
                BuildOrder build = response.Result;
                build.BuildItems  = buildEntity.BuildItems;
                build.AddedDate   = DateTime.Now;
                build.Description = description;
                build.Race        = race;
                build.VsRace      = vsRace;

                saveResponse = _service.UpdateBuildOrder(build);
            }
            else
            {
                saveResponse = _service.AddBuildOrder(buildEntity);
            }

            if (saveResponse.Success)
            {
                Log.Activity(HttpContext, string.Format("Build order [{0}] uploaded by {1}", buildEntity.Name, userName));
            }

            return(new JsonResponse(saveResponse));
        }