/// <summary> /// Loads the part from the specified path. /// </summary> /// <param name="partPath">The full path to the part to load.</param> /// <returns>The part deserialized from the specified file path.</returns> public PartInfo LoadFromFile(string partPath) { PartInfo info = null; //using (LogGroup logGroup = LogGroup.Start("Loading the part from the specified path.", NLog.LogLevel.Debug)) //{ if (!File.Exists(partPath)) { throw new ArgumentException("The specified file does not exist."); } // LogWriter.Debug("Path: " + partPath); using (StreamReader reader = new StreamReader(File.OpenRead(partPath))) { XmlSerializer serializer = new XmlSerializer(typeof(PartInfo)); info = (PartInfo)serializer.Deserialize(reader); reader.Close(); } //} return(info); }
/// <summary> /// Loads all the parts found in the parts file. /// </summary> /// <param name="includeDisabled">A value indicating whether or not to include disabled parts.</param> /// <returns>An array of the the parts found.</returns> public PartInfo[] LoadInfoFromFile(bool includeDisabled) { // Logging disabled to boost performance //using (LogGroup logGroup = LogGroup.StartDebug("Loading the parts from the XML file.")) //{ if (Parts == null) { List <PartInfo> validParts = new List <PartInfo>(); PartInfo[] parts = new PartInfo[] {}; using (StreamReader reader = new StreamReader(File.OpenRead(FileNamer.PartsInfoFilePath))) { XmlSerializer serializer = new XmlSerializer(typeof(PartInfo[])); parts = (PartInfo[])serializer.Deserialize(reader); } foreach (PartInfo part in parts) { if (part.Enabled || includeDisabled) { validParts.Add(part); } } Parts = validParts.ToArray(); } //} return(Parts); }
/// <summary> /// Locates the part info for performing the specified action with the specified type. /// </summary> /// <param name="action">The action that is to be performed by the part.</param> /// <param name="typeName">The short type name of the entity that is involved in the action.</param> /// <returns>The part info for the specified scenario.</returns> public PartInfo Locate(string action, string typeName) { // Get the specified type Type type = null; if (Entities.EntityState.Entities.Contains(typeName)) { type = Entities.EntityState.Entities[typeName].GetEntityType(); } // Create a direct part key for the specified type string key = Parts.GetPartKey(action, typeName); // Create the part info variable to hold the return value PartInfo partInfo = null; // Check the direct key to see if a part exists if (Parts.PartExists(key)) { partInfo = Parts[key]; } // If not then navigate up the heirarchy looking for a matching part else if (type != null) { partInfo = LocateFromHeirarchy(action, type); } return(partInfo); }
/// <summary> /// Creates the full file path for the provided part. /// </summary> /// <param name="part">The part to create the file path for.</param> /// <returns>The full file path for the provided part.</returns> public string CreatePartFilePath(PartInfo part) { if (part.PartFilePath == null || part.PartFilePath == String.Empty) { throw new ArgumentException("The part.PartFilePath property hasn't been set."); } return(FileMapper.MapApplicationRelativePath(part.PartFilePath)); }
/// <summary> /// Locates the part info for performing the specified action with the specified type by looking at the base types and interfaces of the provided type. /// </summary> /// <param name="action">The action that is to be performed by the part.</param> /// <param name="type">The type that is involved in the action.</param> /// <returns>The part info for the specified scenario.</returns> public PartInfo LocateFromHeirarchy(string action, Type type) { PartInfo partInfo = LocateFromInterfaces(action, type); if (partInfo == null) { partInfo = LocateFromBaseTypes(action, type); } return(partInfo); }
/// <summary> /// Extracts the part infos from the provided file path. /// </summary> /// <param name="filePath">The full path to the part (.ascx) file.</param> /// <returns></returns> public PartInfo[] ExtractPartInfo(string filePath) { string shortName = Path.GetFileNameWithoutExtension(filePath); string extension = Path.GetExtension(filePath).Trim('.'); string[] actions = ExtractActions(shortName); string typeName = ExtractType(shortName); List <PartInfo> parts = new List <PartInfo>(); string relativeFilePath = filePath.Replace(StateAccess.State.PhysicalApplicationPath, "") .Replace(@"\", "/") .Trim('/'); foreach (string action in actions) { PartInfo info = new PartInfo(); info.Action = action; info.TypeName = typeName; info.PartFilePath = relativeFilePath; BasePart p = (BasePart)ControlLoader.LoadControl(filePath); if (p == null) { throw new ArgumentException("Cannot find part file at path: " + filePath); } // Ensure that the info properties have been set p.InitializeInfo(); info.MenuTitle = p.MenuTitle; info.MenuCategory = p.MenuCategory; parts.Add(info); } return(parts.ToArray()); }
/// <summary> /// Creates the file name for the serialized info for the provided part. /// </summary> /// <param name="part">The part to create the file name for.</param> /// <returns>The full file name for the serialized info for the provided part.</returns> public virtual string CreateInfoFileName(PartInfo part) { if (part == null) { throw new ArgumentNullException("part"); } if (part.Action == null) { throw new ArgumentNullException("part.Action", "No action has been set to the Action property."); } string name = String.Empty; if (part.TypeName != String.Empty) { name += part.TypeName + "-"; } name += part.Action + ".part"; return(name); }
/// <summary> /// Locates the part info for performing the specified action with the specified type by looking at the base types of the provided type. /// </summary> /// <param name="action">The action that is to be performed by the part.</param> /// <param name="type">The type that is involved in the action.</param> /// <param name="format">The output format of the part to locate.</param> /// <returns>The part info for the specified scenario.</returns> public PartInfo LocateFromBaseTypes(string action, Type type) { PartInfo partInfo = null; TypeNavigator navigator = new TypeNavigator(type); while (navigator.HasNext && partInfo == null) { Type nextType = navigator.Next(); string key = Parts.GetPartKey(action, nextType.Name); // If a part exists for the base type then use it if (Parts.PartExists(key)) { partInfo = Parts[key]; break; } } return(partInfo); }
/// <summary> /// Locates the part info for performing the specified action with the specified type by looking at the interfaces of the provided type. /// </summary> /// <param name="action">The action that is to be performed by the part.</param> /// <param name="type">The type that is involved in the action.</param> /// <returns>The part info for the specified scenario.</returns> public PartInfo LocateFromInterfaces(string action, Type type) { PartInfo partInfo = null; Type[] interfaceTypes = type.GetInterfaces(); // Loop backwards through the interface types for (int i = interfaceTypes.Length - 1; i >= 0; i--) { Type interfaceType = interfaceTypes[i]; string key = Parts.GetPartKey(action, interfaceType.Name); if (Parts.PartExists(key)) { partInfo = Parts[key]; break; } } return(partInfo); }
/// <summary> /// Initializes the parts and loads all parts to state. /// </summary> public void Initialize() { using (LogGroup logGroup = LogGroup.StartDebug("Initializing the web parts.")) { if (StateAccess.IsInitialized && !PartState.IsInitialized) { PartInfo[] parts = new PartInfo[] {}; bool pageIsAccessible = Page != null; // Only scan for parts if the page component is accessible (otherwise they can't be loaded through LoadControl) // and when the parts have NOT yet been mapped if (pageIsAccessible && !IsCached) { LogWriter.Debug("Is not cached. Scanning from type attributes."); parts = FindParts(); Saver.SaveInfoToFile(parts); Initialize(parts); } else if (IsCached) { LogWriter.Debug("Is cached. Loading from XML."); parts = LoadParts(); Initialize(parts); } } else { LogWriter.Debug("State is not initialized. Skipping."); } } }