Exemplo n.º 1
0
        public static ThingModel FromNameValueCollection(string partitionKey, NameValueCollection pairs, bool autoSave)
        {
            ThingModel thing;

            if (string.IsNullOrWhiteSpace(pairs["id"]))
            {
                // We did not receive an id in the collection, create a new ThingModel
                thing = new ThingModel(partitionKey);
            }
            else
            {
                // We got an id but we need to make sure it exists. We will try
                // and retrieve it and if it fails we'll create a new thing with the passed id
                thing = FromRowKey(partitionKey, pairs["id"]);
                if (thing == null) thing = new ThingModel(partitionKey, pairs["id"]);
            }

            // Cycle through each of the value pairs we were passed and
            // pass them into SetProperProperty to update the model as
            // needed.
            foreach (var item in pairs.AllKeys)
            {
                thing.SetProperProperty(item, pairs[item]);
            }

            if (autoSave) thing.Save();

            // Return the completed thing (note that we have not saved this thing)
            return thing;
        }
Exemplo n.º 2
0
 public ActionResult UpdateItem(ThingModel model)
 {
     var user = Lib.UserUtils.GetUser(this);
     model.PartitionKey = user.RepoId;
     model.RowKey = model.Id;
     model.Save();
     return RedirectToAction("RepoItems", "Home");
 }
Exemplo n.º 3
0
        public EventModel(ThingModel thing) :this()
        {
            _thing = thing;

            this.PartitionKey = thing.PartitionKey;
            this.RowKey = Guid.NewGuid().ToString("N");

            EventProperties.Add("Id", thing.Id);
            EventProperties.Add("EventType", EventType.Change.ToString()); // Set the default type

            if (!string.IsNullOrWhiteSpace(thing.Agent)) EventProperties.Add("Agent", thing.Agent);
        }
Exemplo n.º 4
0
        private Dictionary <string, Tuple <string, string> > ThingDiff(ThingModel newThing, ThingModel oldThing)
        {
            var results = new Dictionary <string, Tuple <string, string> >();

            if (newThing == null || oldThing == null)
            {
                return(results);
            }

            // Name Change
            if (newThing.Name != oldThing.Name)
            {
                results.Add("Name", new Tuple <string, string>(oldThing.Name, newThing.Name));
            }

            // ID Change
            if (newThing.Id != oldThing.Id)
            {
                results.Add("Id", new Tuple <string, string>(oldThing.Id, newThing.Id));
            }


            // Agent Change
            if (newThing.Agent != oldThing.Agent)
            {
                results.Add("Agent", new Tuple <string, string>(oldThing.Agent, newThing.Agent));
            }

            // Type
            if (newThing.Type != oldThing.Type)
            {
                results.Add("Type", new Tuple <string, string>(oldThing.Type, newThing.Type));
            }

            var comparer = EqualityComparer <string> .Default;

            foreach (KeyValuePair <string, string> kvp in newThing.Properties)
            {
                string secondValue;
                if (!oldThing.Properties.TryGetValue(kvp.Key, out secondValue))
                {
                    results.Add(kvp.Key, new Tuple <string, string>(string.Empty, kvp.Value));
                }

                if (!comparer.Equals(kvp.Value, secondValue))
                {
                    results.Add(kvp.Key, new Tuple <string, string>(secondValue, kvp.Value));
                }
            }
            return(results);
        }
Exemplo n.º 5
0
        private Dictionary<string, Tuple<string, string>> ThingDiff(ThingModel newThing, ThingModel oldThing)
        {
            var results = new Dictionary<string, Tuple<string, string>>();

            if (newThing == null || oldThing == null) return results;

            // Name Change
            if (newThing.Name != oldThing.Name)
            {
                results.Add("Name", new Tuple<string, string>(oldThing.Name, newThing.Name));
            }

            // ID Change
            if (newThing.Id != oldThing.Id)
            {
                results.Add("Id", new Tuple<string, string>(oldThing.Id, newThing.Id));
            }


            // Agent Change
            if (newThing.Agent != oldThing.Agent)
            {
                results.Add("Agent", new Tuple<string, string>(oldThing.Agent, newThing.Agent));
            }

            // Type
            if (newThing.Type != oldThing.Type)
            {
                results.Add("Type", new Tuple<string, string>(oldThing.Type, newThing.Type));
            }

            var comparer = EqualityComparer<string>.Default;

            foreach (KeyValuePair<string, string> kvp in newThing.Properties)
            {
                string secondValue;
                if (!oldThing.Properties.TryGetValue(kvp.Key, out secondValue))
                    results.Add(kvp.Key, new Tuple<string, string>(string.Empty, kvp.Value));

                if (!comparer.Equals(kvp.Value, secondValue))
                    results.Add(kvp.Key, new Tuple<string, string>(secondValue, kvp.Value));
            }
            return results;
        }
Exemplo n.º 6
0
        private void StateChangedEvent(ThingModel current, ThingModel previous)
        {
            var thingDiff = this.ThingDiff(current, previous);

            if (thingDiff.Count == 0)
            {
                // No actual changes
                System.Diagnostics.Debug.WriteLine("No Changes Found: " + current.Id);
                return;
            }

            EventModel eventModel = new EventModel(current);

            foreach (var item in thingDiff)
            {
                eventModel.EventProperties.Add(item.Key, item.Value.Item1);
            }
            eventModel.Send();
        }
Exemplo n.º 7
0
        public EventModel(ThingModel thing, EventType type)
            : this(thing)
        {
            EventProperties["EventType"] = type.ToString();            

            switch (type)
            {
                case EventType.Change:
                    break;
                case EventType.Add:
                    break;
                case EventType.Delete:
                    break;
                case EventType.Message:
                    EventProperties.Add("EventMessage", string.Empty); // Add if this is a message
                    break;
                default:
                    break;
            }
        }
Exemplo n.º 8
0
        public static ThingModel FromNameValueCollection(string partitionKey, NameValueCollection pairs, bool autoSave)
        {
            ThingModel thing;

            if (string.IsNullOrWhiteSpace(pairs["id"]))
            {
                // We did not receive an id in the collection, create a new ThingModel
                thing = new ThingModel(partitionKey);
            }
            else
            {
                // We got an id but we need to make sure it exists. We will try
                // and retrieve it and if it fails we'll create a new thing with the passed id
                thing = FromRowKey(partitionKey, pairs["id"]);
                if (thing == null)
                {
                    thing = new ThingModel(partitionKey, pairs["id"]);
                }
            }

            // Cycle through each of the value pairs we were passed and
            // pass them into SetProperProperty to update the model as
            // needed.
            foreach (var item in pairs.AllKeys)
            {
                thing.SetProperProperty(item, pairs[item]);
            }

            if (autoSave)
            {
                thing.Save();
            }

            // Return the completed thing (note that we have not saved this thing)
            return(thing);
        }
Exemplo n.º 9
0
        private void StateChangedEvent(ThingModel current, ThingModel previous)
        {
            var thingDiff = this.ThingDiff(current, previous);

            if (thingDiff.Count == 0)
            {
                // No actual changes
                System.Diagnostics.Debug.WriteLine("No Changes Found: " + current.Id);
                return;
            }

            EventModel eventModel = new EventModel(current);
            foreach (var item in thingDiff)
            {
                eventModel.EventProperties.Add(item.Key, item.Value.Item1);
            }
            eventModel.Send();
        }