Exemplo n.º 1
0
        private SyncLog Upsert(SyncType type, object item, UpsertModel upsertDelegate)
        {
            var log = new SyncLog(SyncAction.Upsert, type, item);

            try
            {
                int?newEntityId = upsertDelegate(item);
                log.SetSuccessful(newEntityId);
            }
            catch (Exception ex)
            {
                log.SetFailed(ex.Message);
            }
            return(log);
        }
Exemplo n.º 2
0
        private SyncLog Delete(SyncType type, int id, DeleteModel deleteDelegate)
        {
            var log = new SyncLog(SyncAction.Delete, type, id);

            try
            {
                deleteDelegate(id);
                log.SetSuccessful(null);
            }
            catch (Exception ex)
            {
                log.SetFailed(ex.Message);
            }
            return(log);
        }
Exemplo n.º 3
0
        public static IEnumerable <SyncLog> SyncChanges(string serializedItems)
        {
            var items    = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(serializedItems);
            var instance = new OnlineSync();
            var logs     = new List <SyncLog>();
            int observationId;

            const string parentObservationErrorMessage = "Parent observation was not saved.";

            // Capture new observation ID's so that child objects can be mapped to them.
            Dictionary <string, int> newObservations = new Dictionary <string, int>();

            foreach (var o in items.Observations.Upserts)
            {
                SyncLog log = instance.Upsert(SyncType.Observation, (object)o, instance.UpsertObservation);
                if (log.NewEntityId.HasValue)
                {
                    newObservations.Add((string)o.ObservationId, log.NewEntityId.Value);
                }
                log.SetObservation(log.NewEntityId ?? (int)o.ObservationId);
                logs.Add(log);
            }

            // Observation Changes
            foreach (var c in items.ObservationChanges.Upserts)
            {
                // If the change belongs to a newly-inserted observation, update ObservationId first.
                if (!Int32.TryParse((string)c.ObservationId, out observationId))
                {
                    if (newObservations.ContainsKey((string)c.ObservationId))
                    {
                        // Set the new ObservationId.
                        c.ObservationId = newObservations[(string)c.ObservationId];
                    }
                    else
                    {
                        // Could not find the ObservationId. Do not import the change.
                        logs.Add(new SyncLog(SyncAction.Upsert, SyncType.Change, c, parentObservationErrorMessage));
                        continue;
                    }
                }

                var log = instance.Upsert(SyncType.Change, (object)c, instance.UpsertChange);
                log.SetObservation((int)c.ObservationId);
                logs.Add(log);
            }
            foreach (var c in items.ObservationChanges.Deletes)
            {
                logs.Add(instance.Delete(SyncType.Change, (int)c, instance.DeleteChange));
            }

            // Observation Attachments
            foreach (var a in items.ObservationAttachments.Upserts)
            {
                // If the attachment belongs to a newly-inserted observation, update ObservationId first.
                if (!Int32.TryParse((string)a.ObservationId, out observationId))
                {
                    if (newObservations.ContainsKey((string)a.ObservationId))
                    {
                        // Set the new ObservationId.
                        a.ObservationId = newObservations[(string)a.ObservationId];
                    }
                    else
                    {
                        // Could not find the ObservationId. Do not import the attachment.
                        logs.Add(new SyncLog(SyncAction.Upsert, SyncType.Attachment, a, parentObservationErrorMessage));
                        continue;
                    }
                }

                var log = instance.Upsert(SyncType.Attachment, (object)a, instance.UpsertAttachment);
                log.SetObservation((int)a.ObservationId);
                logs.Add(log);
            }

            // Observation Comments
            foreach (var c in items.ObservationComments.Upserts)
            {
                // If the comment belongs to a newly-inserted observation, update ObservationId first.
                if (!Int32.TryParse((string)c.ObservationId, out observationId))
                {
                    if (newObservations.ContainsKey((string)c.ObservationId))
                    {
                        // Set the new ObservationId.
                        c.ObservationId = newObservations[(string)c.ObservationId];
                    }
                    else
                    {
                        // Could not find the ObservationId. Do not import the comment.
                        logs.Add(new SyncLog(SyncAction.Upsert, SyncType.Comment, c, parentObservationErrorMessage));
                        continue;
                    }
                }

                var log = instance.Upsert(SyncType.Comment, (object)c, instance.UpsertComment);
                log.SetObservation((int)c.ObservationId);
                logs.Add(log);
            }
            foreach (var c in items.ObservationComments.Deletes)
            {
                logs.Add(instance.Delete(SyncType.Comment, (int)c, instance.DeleteComment));
            }

            // If any item(s) failed, send a copy of the raw data to the system adminsitrator(s).
            if (logs.Any(x => !x.Successful))
            {
                SendLogEmail(logs);
            }

            return(logs);
        }