/* ============================================================================ Copyright 2014 Hewlett Packard Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ============================================================================ */ using System.Collections.Generic; using System.Linq; using Openstack.Common.Properties; using Openstack.Objects.Domain; using System; using Openstack.Objects.Utility; using Openstack.Objects.DataAccess; using System.Collections.ObjectModel; using System.Collections; using Openstack.Objects.Domain.Compute; namespace Openstack.Client.Powershell.Providers.Common { public class GenericUIContainer : BaseEntityUIContainer { } public enum ObjectType { Entity = 1, Container = 2 } public abstract class BaseUIContainer { private string _id; private string _name; private string _description; private string _path = @"\"; private ObjectType _objectType = ObjectType.Container; protected List _containers = new List(); protected IList _entities = new List(); private BaseEntity _entity; private BaseUIContainer _parentContainer; private bool _isContainerListInitialized = false; private Context _context; private BaseRepositoryFactory _repositoryFactory; private string _displayName = null; #region Ctors //================================================================================ /// /// /// //================================================================================ public BaseUIContainer() { } //================================================================================ /// /// /// /// /// /// /// /// //================================================================================ public BaseUIContainer(BaseUIContainer parentContainer, string name, string description, string path) { _description = description; _parentContainer = parentContainer; _name = name; _path = path; } //================================================================================ /// /// /// /// /// /// /// //================================================================================ public BaseUIContainer(string displayName, BaseUIContainer parentContainer, string name, string path) { _parentContainer = parentContainer; _name = name; _path = path; _displayName = displayName; } #endregion #region Properties //================================================================================ /// /// /// //================================================================================ public BaseRepositoryFactory RepositoryFactory { get { return _repositoryFactory; } set { _repositoryFactory = value; } } //================================================================================ /// /// /// //================================================================================ public Context Context { get { return _context; } set { _context = value; if (_repositoryFactory != null && _repositoryFactory.Context != null) _repositoryFactory.Context = value; } } //================================================================================ /// /// /// /// //================================================================================ protected bool IsMocked { get { if (Settings.Default.IsMocked == true) { return true; } else { return false; } } } //================================================================================ /// /// /// //================================================================================ public IList Entities { get { return _entities; } set { _entities = value; } } //================================================================================ /// /// /// //================================================================================ public BaseEntity Entity { get { return _entity; } set { _entity = value; } } //================================================================================ /// /// /// //================================================================================ public List Containers { get { return _containers; } //set { _containers = value; } } //================================================================================ /// /// /// //================================================================================ public ObjectType ObjectType { get { return _objectType; } set { _objectType = value; } } //================================================================================ /// /// /// //================================================================================ public string Id { get { return _id; } set { _id = value; } } //================================================================================ /// /// /// //================================================================================ public BaseUIContainer Parent { get { return _parentContainer; } set { _parentContainer = value; } } //================================================================================ /// /// /// //================================================================================ public string Path { get { return _path; } set { _path = value; } } //================================================================================ /// /// /// //================================================================================ public string Description { get { return _description; } set { _description = value; } } //================================================================================ /// /// /// //================================================================================ public string DisplayName { get { return _displayName; } set { _displayName = value; } } //================================================================================ /// /// /// //================================================================================ public string Name { get { return _name; } set { _name = value; } } #endregion #region Methods //================================================================================ /// /// /// /// //================================================================================ protected void AddContainer(BaseUIContainer container) { if (container != null) { this.Containers.Add(container); } } //================================================================================ /// /// /// /// /// //================================================================================ protected string BuildChildPath(string name) { return this.Path + "\\" + name; } //================================================================================ /// /// /// /// //================================================================================ public virtual BaseUIContainer CreateContainer(string id) { return null; } //================================================================================ /// /// /// /// /// /// //================================================================================ private bool IsAuthorized(T container) { Type type = container.GetType(); object[] metadata = type.GetCustomAttributes(false); foreach (object attribute in metadata) { RequiredServiceIdentifierAttribute identifier = attribute as RequiredServiceIdentifierAttribute; if (identifier != null) if (this.Context.ServiceCatalog.DoesServiceExist(identifier.ServiceName)) return true; else return false; } return true; } //================================================================================ /// /// /// /// /// /// /// /// //================================================================================ protected BaseUIContainer SetContainer(BaseUIContainer container, string name, string description, string path, string displayName = null) where T : BaseUIContainer { if (this.IsAuthorized(container)) { container.Name = name; container.Description = description; container.Context = this.Context; container.RepositoryFactory = this.RepositoryFactory; container.Parent = this; container.Path = path; container.DisplayName = displayName; container.Id = name; return container; } else { return null; } } //================================================================================ /// /// /// /// /// /// /// /// //================================================================================ protected BaseUIContainer CreateContainer(string name, string description, string path, string displayName = null) where T : BaseUIContainer { T container = Activator.CreateInstance(); if (this.IsAuthorized(container)) { container.Name = name; container.Description = description; container.Context = this.Context; container.RepositoryFactory = this.RepositoryFactory; container.Parent = this; container.Path = path; container.DisplayName = displayName; container.Id = name; return container; } else { return null; } } //================================================================================ /// /// /// /// //================================================================================ public virtual BaseUIContainer CreateEntityContainer(BaseEntity entity) { BaseEntityUIContainer container = new BaseEntityUIContainer(); container.Entity = entity; return container; } //================================================================================ /// /// /// /// /// //================================================================================ public BaseUIContainer GetContainer(string containerName) { return _containers.Where(c => c.Id == containerName).SingleOrDefault(); } public virtual void WriteEntityDetails() { return; } //================================================================================ /// /// /// /// /// //================================================================================ protected void SetUIContainers(Collection entities) where T : BaseUIContainer { List entityContainers = new List(); foreach (BaseEntity entity in entities) { T entityContainer = (T)Activator.CreateInstance(typeof(T)); entityContainer.Entity = entity; entityContainer.Name = entity.Name; entityContainer.ObjectType = Common.ObjectType.Entity; entityContainer.Parent = this; entityContainer.RepositoryFactory = this.RepositoryFactory; entityContainer.Context = this.Context; entityContainer.Id = entity.Id; entityContainers.Add(entityContainer); } this.Containers.Clear(); int count = 0; foreach (BaseUIContainer bc in entityContainers) { bc.Path = @"\" + bc.Parent.Path + @"\-" + Convert.ToString(count); this.Containers.Add(bc); ++count; } } //========================================================================================================= /// /// /// /// //========================================================================================================= protected void SetUIContainers(IList entities) where T : BaseUIContainer { if (entities != null && entities.Count > 0) { List entityContainers = new List(); foreach (BaseEntity entity in entities) { T entityContainer = (T)Activator.CreateInstance(typeof(T)); entityContainer.Entity = entity; entityContainer.Name = entity.Name; entityContainer.ObjectType = Common.ObjectType.Entity; entityContainer.Parent = this; entityContainer.RepositoryFactory = this.RepositoryFactory; entityContainer.Context = this.Context; entityContainer.Id = entity.Id; entityContainers.Add(entityContainer); } this.Containers.Clear(); int count = 0; foreach (BaseUIContainer bc in entityContainers) { bc.Path = @"\" + bc.Parent.Path + @"\-" + Convert.ToString(count); this.Containers.Add(bc); ++count; } } } public abstract void Load(); //================================================================================ /// /// /// /// /// //================================================================================ public virtual bool IsEntityId(string id) { if (this.Id == id) return true; else return false; } //========================================================================================================= /// /// /// /// //========================================================================================================= protected void WriteMetadata(MetaData metadata) { if (metadata != null) { int maxLength = metadata.Max(m => m.Key.Length); foreach (KeyValuePair element in metadata) { if (element.Value != null && element.Key != null) Console.WriteLine(element.Key.PadRight(maxLength) + " : " + element.Value.Trim()); } Console.WriteLine(); } } //================================================================================ /// /// /// /// /// //================================================================================ protected void WriteHeader(string msg, ConsoleColor textColor = ConsoleColor.Yellow) { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); Console.WriteLine(msg ); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; } #endregion } }