Наследование: SmartDeviceProject1.SqlCeOfflineEntity
Пример #1
0
        /// <summary>
        /// Adds a new list.  It is important to note that because there is no enforcement of
        /// data consistency in the context, the app must understand the data and enforce it.
        /// </summary>
        /// <param name="modelList">The model list to add</param>
        public void AddList(ModelList modelList)
        {
            // Extract the service entity from the model entity
            DefaultScope.List newList = modelList.List;

            // Simple argument checking
            if (null == newList)
            {
                throw new ArgumentNullException("newList");
            }

            if (newList.ID == null)
            {
                throw new ArgumentNullException("List id is null");
            }

            // Do a LINQ query to verify that the list can be added.  Current constraints
            // are unique id and unique name.
            DefaultScope.List existingList = (from s in context.ListCollection
                                              where s.ID == newList.ID || s.Name == newList.Name
                                              select s).FirstOrDefault();

            if (existingList != default(List))
            {
                throw new ArgumentException("List with the same id already exists");
            }

            // Add the list to the context.
            context.AddList(newList);
        }
Пример #2
0
        /// <summary>
        /// Construct which takes the underlying list and its associated context.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="context"></param>
        public ModelList(DefaultScope.List list, ListSampleOfflineContext context)
        {
            this.list    = list;
            this.context = context;
            items        = null;

            // Register for the property changed event
            list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
        }
Пример #3
0
        /// <summary>
        /// Construct which takes the underlying list and its associated context.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="context"></param>
        public ModelList(DefaultScope.List list, ListSampleOfflineContext context)
        {
            this.list = list;
            this.context = context;
            items = null;

            // Register for the property changed event
            list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
        }
Пример #4
0
        /// <summary>
        /// Navigates to the item's list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemListButtonClick(object sender, RoutedEventArgs e)
        {
            Control control = (Control)sender;

            DefaultScope.List list = (DefaultScope.List)control.Tag;

            ListsListBox.SelectedItem        = list;
            MainPageTabControl.SelectedIndex = 0;
        }
Пример #5
0
        private void AddNewListBtn_Click(object sender, EventArgs e)
        {
            List newList = new List();
            newList.ID = Guid.NewGuid();
            newList.UserID = SettingsViewModel.Instance.UserId;
            newList.Name = "New List";
            newList.Description = "Enter Description.";
            newList.CreatedDate = DateTime.Now;

            NavigationService.Navigate(new Uri("/AddListView.xaml", UriKind.Relative));
            (Application.Current.RootVisual as FrameworkElement).DataContext = newList;
        }
Пример #6
0
        /// <summary>
        /// Deletes the specified list.
        ///
        /// NOTE: Because of the fact that the context doesn't handle referential
        /// integrity, the application must handle the cascading delete
        /// </summary>
        /// <param name="list"></param>
        public void DeleteList(DefaultScope.List list)
        {
            // First find all items that are in the list and delete them
            List <Item> listItems = (from i in context.ItemCollection
                                     where i.ListID == list.ID
                                     select i).ToList();

            foreach (Item i in listItems)
            {
                DeleteItem(i);
            }

            // once all items are deleted, delete the list
            context.DeleteList(list);
        }
Пример #7
0
        /// <summary>
        /// Constructor which takes the context Item that is being wrapped and its associated context.
        /// </summary>
        /// <param name="item"></param>
        /// <param name="context"></param>
        public ModelItem(Item item, ListSampleOfflineContext context)
        {
            this.item    = item;
            this.context = context;
            tags         = null;
            unusedTags   = null;
            usedTags     = new Dictionary <int, int>();
            list         = null;

            // Materialize the priority and status from the ids in the item
            UpdatePriority();
            UpdateStatus();

            // Register for any events that occur on the item
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);

            // Register for any changes to the tag item mappings
            ((INotifyCollectionChanged)context.TagItemMappingCollection).CollectionChanged += new NotifyCollectionChangedEventHandler(ModelItem_CollectionChanged);
        }
Пример #8
0
        public List<Item> GetAllItems(Guid listId)
        {
            var items = new List<Item>();

            using (var command = new SqlCeCommand())
            {
                command.Connection = GetSqlCeConnection();
                command.CommandText = GET_ALL_ITEMS;
                command.Parameters.Add("@ListID", SqlDbType.UniqueIdentifier).Value = listId;

                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var i = new Item
                    {
                        ID = (Guid)reader["ID"],
                        Name = reader["Name"] as string,
                        Description = reader["Description"] as string,
                        ListID = (Guid)reader["ListID"],
                        Priority = reader["Priority"] as int?,
                        Status = reader["Status"] as int?,
                        UserID = (Guid)reader["UserID"],
                        ServiceMetadata = new OfflineEntityMetadata()
                        {
                            IsTombstone = (bool)reader["IsTombstone"],
                            Id = reader["_MetadataID"] as string
                        }
                    };

                    items.Add(i);
                }
            }

            return items;
        }
Пример #9
0
 /// <summary>
 /// Gets the corresponding model list for a context list
 /// </summary>
 /// <param name="list"></param>
 /// <returns></returns>
 public ModelList GetList(DefaultScope.List list)
 {
     return(new ModelList(list, context));
 }
Пример #10
0
        public List<Priority> GetAllPriority()
        {
            var priorities = new List<Priority>();

            using (var command = new SqlCeCommand())
            {
                command.Connection = GetSqlCeConnection();
                command.CommandText = GET_ALL_PRIORITY;

                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var p = new Priority()
                    {
                        ID = (int)reader["ID"],
                        Name = reader["Name"] as string,
                        ServiceMetadata = new OfflineEntityMetadata()
                        {
                            IsTombstone = false,
                            Id = reader["_MetadataID"] as string
                        }
                    };

                    priorities.Add(p);
                }
            }

            return priorities;
        }
Пример #11
0
 public void AddList(List entity) {
     base.AddItem<List>(entity);
 }
Пример #12
0
        public List<Status> GetAllStatus()
        {
            var statuses = new List<Status>();

            using (var command = new SqlCeCommand())
            {
                command.Connection = GetSqlCeConnection();
                command.CommandText = GET_ALL_STATUS;

                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var p = new Status
                                {
                                    ID = (int) reader["ID"],
                                    Name = reader["Name"] as string,
                                    ServiceMetadata = new OfflineEntityMetadata()
                                    {
                                        IsTombstone = false,
                                        Id = reader["_MetadataID"] as string
                                    }
                                };

                    statuses.Add(p);
                }
            }

            return statuses;
        }
Пример #13
0
        public List<Tag> GetAllTags()
        {
            var tags = new List<Tag>();

            using (var command = new SqlCeCommand())
            {
                command.Connection = GetSqlCeConnection();
                command.CommandText = GET_ALL_TAGS;

                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var t = new Tag
                                {
                                    ID = (int) reader["ID"],
                                    Name = reader["Name"] as string,
                                    ServiceMetadata = new OfflineEntityMetadata()
                                    {
                                        IsTombstone = false,
                                        Id = reader["_MetadataID"] as string
                                    }
                                };
                    tags.Add(t);
                }
            }

            return tags;
        }
Пример #14
0
 public void DeleteList(List entity) {
     base.DeleteItem<List>(entity);
 }
Пример #15
0
        void ItemDetailView_Loaded(object sender, RoutedEventArgs e1)
        {
            this.listObj = SyncContextInstance.Context.ListCollection.Where((e) => e.ID.Equals((this.DataContext as Item).ListID)).FirstOrDefault();
            addMode = (this.DataContext as Item).EntityState == Microsoft.Synchronization.ClientServices.IsolatedStorage.OfflineEntityState.Detached;

            // If add mode then only the done button will be visible
            if (addMode)
            {
                FlipToEditMode();
                (this.ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = false;
                (this.ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = false;
                (this.ApplicationBar.Buttons[2] as ApplicationBarIconButton).IsEnabled = true;
                (this.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = false;
            }
        }
Пример #16
0
 /// <summary>
 /// Gets a new materialized list
 /// </summary>
 private void UpdateList()
 {
     list = (from l in context.ListCollection
             where l.ID == item.ListID
             select l).FirstOrDefault();
 }
Пример #17
0
        public object GetAllLists()
        {
            var lists = new List<List>();

            using (var command = new SqlCeCommand())
            {
                command.Connection = GetSqlCeConnection();
                command.CommandText = GET_ALL_LISTS;

                SqlCeDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var l = new List
                                {
                                    ID = (Guid) reader["ID"],
                                    Name = reader["Name"] as string,
                                    Description = reader["Description"] as string,
                                    CreatedDate = (DateTime) reader["CreatedDate"],
                                    UserID = (Guid)reader["UserID"],
                                    ServiceMetadata = new OfflineEntityMetadata()
                                    {
                                        IsTombstone = (bool)reader["IsTombstone"],
                                        Id = reader["_MetadataID"] as string
                                    }
                                };

                    lists.Add(l);
                }
            }

            return lists;
        }
Пример #18
0
        /// <summary>
        /// Gets all entities that were created/modified/deleted locally after the last sync.
        /// </summary>
        /// <param name="state">A unique identifier for the changes that are uploaded</param>
        /// <returns>The set of incremental changes to send to the service.</returns>
        public IEnumerable<SqlCeOfflineEntity> GetChanges(Guid state)
        {
            var changeList = new List<SqlCeOfflineEntity>();

            using (GetSqlCeConnection())
            {
                SqlCeTransaction transaction = _connection.BeginTransaction();

                try
                {
                    using (var command = new SqlCeCommand(SELECT_ITEM_CHANGES, GetSqlCeConnection()))
                    {
                        var reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            var item = new Item
                            {
                                ID = (Guid)reader["ID"],
                                ListID = (Guid)reader["ListID"],
                                UserID = (Guid)reader["UserID"],
                                Name = reader["Name"] as string,
                                Description = reader["Description"] as string,
                                Priority = reader["Priority"] as int?,
                                Status = reader["Status"] as int?,
                                StartDate = reader["StartDate"] as DateTime?,
                                EndDate = reader["EndDate"] as DateTime?,
                                ServiceMetadata = new OfflineEntityMetadata()
                                                      {
                                                          IsTombstone = (bool)reader["IsTombstone"],
                                                          Id = reader["_MetadataID"] as string
                                                      }
                            };

                            changeList.Add(item);
                        }
                    }

                    using (var command = new SqlCeCommand(SELECT_LIST_CHANGES, GetSqlCeConnection()))
                    {
                        var reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            var listItem = new List
                            {
                                ID = (Guid)reader["ID"],
                                Name = reader["Name"] as string,
                                Description = reader["Description"] as string,
                                UserID = (Guid)reader["UserID"],
                                CreatedDate = (DateTime)reader["CreatedDate"],
                                ServiceMetadata = new OfflineEntityMetadata()
                                {
                                    IsTombstone = (bool)reader["IsTombstone"],
                                    Id = reader["_MetadataID"] as string
                                }
                            };

                            changeList.Add(listItem);
                        }
                    }

                    using (var command = new SqlCeCommand(SELECT_TAGITEMMAPPING_CHANGES, GetSqlCeConnection()))
                    {
                        var reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            var tagItemMapping = new TagItemMapping
                            {
                                TagID = (int)reader["TagID"],
                                ItemID = (Guid)reader["ItemID"],
                                UserID = (Guid)reader["UserID"],
                                ServiceMetadata = new OfflineEntityMetadata()
                                {
                                    IsTombstone = (bool)reader["IsTombstone"],
                                    Id = reader["_MetadataID"] as string
                                }
                            };

                            changeList.Add(tagItemMapping);
                        }
                    }

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            return changeList;
        }
Пример #19
0
        private void MenuItemSave_Click(object sender, System.EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBoxName.Text))
            {
                MessageBox.Show("List name cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);

                return;
            }

            try
            {

                if (IsNewItem)
                {
                    var listItem = new List
                                       {
                                           ID = Guid.NewGuid(),
                                           Name = textBoxName.Text,
                                           Description = textBoxDescription.Text,
                                           CreatedDate = DateTime.Now,
                                           UserID = new Guid(Settings.ClientId),
                                           ServiceMetadata = new OfflineEntityMetadata() { IsTombstone = false }
                                       };

                    var storageHandler = new SqlCeStorageHandler();
                    storageHandler.InsertList(listItem, true);
                    MessageBox.Show("List created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None,
                                    MessageBoxDefaultButton.Button1);
                }
                else
                {
                    ListItem.Name = textBoxName.Text.Trim();
                    ListItem.Description = textBoxDescription.Text.Trim();

                    var storageHandler = new SqlCeStorageHandler();
                    storageHandler.UpdateList(ListItem, true);

                    MessageBox.Show("List updated!", "Success", MessageBoxButtons.OK, MessageBoxIcon.None,
                                    MessageBoxDefaultButton.Button1);
                }

                this.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
                                MessageBoxDefaultButton.Button1);
            }
        }