/// <summary> /// Initializes a new instance of a CategoryCollection class. /// </summary> /// <param name="parent">The parent of the category node.</param> /// <param name="rootRow">The data model record that provides a context for this Model View.</param> public CategoryCollection(CategoryCollection parent, DataSet.RootRow rootRow) { // Validate the parameters. if (rootRow == null) { throw new ArgumentNullException("rootRow"); } // Extract the values from the record. this.Name = rootRow.Name; this.Parent = parent; this.rootIdField = rootRow.RootId; this.typeIdField = rootRow.TypeId; this.TypeDescription = rootRow.TypeRow.Name; // This will disassemble the icon and give us the basic image sizes supported by the application framework. Dictionary <ImageSize, ImageSource> images = ImageHelper.DecodeIcon(Convert.FromBase64String(rootRow.Image)); this.SmallImageSource = images[ImageSize.Small]; this.MediumImageSource = images[ImageSize.Medium]; this.LargeImageSource = images[ImageSize.Large]; this.ExtraLargeImageSource = images[ImageSize.ExtraLarge]; // These are the metadata properties of the item. foreach (DataSet.PropertyStoreRow propertyStoreRow in rootRow.GetPropertyStoreRows()) { this.PropertyStore.Add(propertyStoreRow.PropertyId, propertyStoreRow.Value); } // This will dig into the relations and recursively construct a hierarchy of items. this.ExpandTree(rootRow); }
/// <summary> /// Expands the tree using the given data model record. /// </summary> /// <param name="currentRootRow">The data model used to provide a context for the model view.</param> void ExpandTree(DataSet.RootRow currentRootRow) { foreach (DataSet.TreeRow treeRow in currentRootRow.GetTreeRowsByFK_Root_Tree_RootIdParentId()) { DataSet.RootRow rootRow = treeRow.RootRowByFK_Root_Tree_RootIdChildId; if (rootRow.TypeId == TypeId.FileFolder) { this.Children.Add(new CategoryCollection(this, rootRow)); } else { this.Leaves.Add(new CategoryCollection(this, rootRow)); } } }
/// <summary> /// Creates a data model entry for the given path element. /// </summary> /// <param name="categoryCollection"></param> /// <param name="path">The full file name of the object to be loaded.</param> /// <param name="externalObject">The properties used to build the object.</param> static void CreateItem(CategoryCollection categoryCollection, String path, ItemProperty externalObject) { // Create a new record for this object. DataSet.RootRow rootRow = DataModel.DataSet.Root.NewRootRow(); rootRow.RootId = Guid.NewGuid(); rootRow.Name = Path.GetFileName(path); rootRow.TypeId = externalObject.typeId; // Create an image for this item. Stream sourceStream = Application.GetResourceStream(new Uri(externalObject.IconUri, UriKind.Relative)).Stream; MemoryStream iconStream = new MemoryStream(); CategoryPage.Copy(sourceStream, iconStream); rootRow.Image = Convert.ToBase64String(iconStream.ToArray()); // Add this new record to the data model. DataModel.DataSet.Root.AddRootRow(rootRow); // If a viewer was provided, then make a property for it. if (String.IsNullOrEmpty(externalObject.ViewerUri)) { DataSet.PropertyStoreRow viewerPropertyRow = DataModel.DataSet.PropertyStore.NewPropertyStoreRow(); viewerPropertyRow.PropertyId = PropertyId.Viewer; viewerPropertyRow.PropertyStoreId = Guid.NewGuid(); viewerPropertyRow.RootId = rootRow.RootId; viewerPropertyRow.Value = externalObject.ViewerUri; DataModel.DataSet.PropertyStore.AddPropertyStoreRow(viewerPropertyRow); } // Create the metadata for the creation date. DataSet.PropertyStoreRow dateCreatedPropertyRow = DataModel.DataSet.PropertyStore.NewPropertyStoreRow(); dateCreatedPropertyRow.PropertyId = PropertyId.DateCreated; dateCreatedPropertyRow.PropertyStoreId = Guid.NewGuid(); dateCreatedPropertyRow.RootId = rootRow.RootId; dateCreatedPropertyRow.Value = DateTime.Now; DataModel.DataSet.PropertyStore.AddPropertyStoreRow(dateCreatedPropertyRow); // Create the metadata for the creation time. DataSet.PropertyStoreRow dateModifiedPropertyRow = DataModel.DataSet.PropertyStore.NewPropertyStoreRow(); dateModifiedPropertyRow.PropertyId = PropertyId.DateModified; dateModifiedPropertyRow.PropertyStoreId = Guid.NewGuid(); dateModifiedPropertyRow.RootId = rootRow.RootId; dateModifiedPropertyRow.Value = DateTime.Now; DataModel.DataSet.PropertyStore.AddPropertyStoreRow(dateModifiedPropertyRow); // This will load a binary version of the file into the metadata. using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { MemoryStream memoryStream = new MemoryStream(); CategoryPage.Copy(fileStream, memoryStream); DataSet.PropertyStoreRow dataPropertyRow = DataModel.DataSet.PropertyStore.NewPropertyStoreRow(); dataPropertyRow.PropertyId = PropertyId.Data; dataPropertyRow.PropertyStoreId = Guid.NewGuid(); dataPropertyRow.RootId = rootRow.RootId; dataPropertyRow.Value = Convert.ToBase64String(memoryStream.GetBuffer(), 0, Convert.ToInt32(memoryStream.Length)); DataModel.DataSet.PropertyStore.AddPropertyStoreRow(dataPropertyRow); } // This will create an association between the parent item and the object just created. DataSet.TreeRow treeRow = DataModel.DataSet.Tree.NewTreeRow(); treeRow.ParentId = categoryCollection.RootId; treeRow.ChildId = rootRow.RootId; treeRow.TreeId = Guid.NewGuid(); DataModel.DataSet.Tree.AddTreeRow(treeRow); // This will insert the new object in the View Model at the given location in the hierarchy. categoryCollection.Leaves.Add(new CategoryCollection(categoryCollection, rootRow)); }