/// <summary> /// Load first entity from file with specific template /// </summary> /// <remarks>This method is usually used to load form data</remarks> /// <typeparam name="T">Entity class</typeparam> /// <param name="filePath">Path of file that contains entity data</param> /// <param name="templateName">Name of template used to parse data from file</param> /// <returns>The entity data</returns> public T LoadOne <T>(String filePath, String templateName) where T : class, new() { // Find parser TemplateConfigElement templateCfg = _cfg.Templates[templateName]; if (templateCfg == null) { throw new ConfigurationErrorsException(String.Format("Template {0} not exist!", templateName)); } var parserCfg = _cfg.Parsers[templateCfg.Parser] as ParserConfigElement; if (parserCfg == null) { throw new ConfigurationErrorsException(String.Format("Parser {0} not configured!", templateCfg.Parser)); } Type parserType = Type.GetType(parserCfg.Type); if (parserType == null) { throw new ConfigurationErrorsException(String.Format("Parser {0} not exist!", parserCfg.Type)); } // Create parser var parser = parserType.Assembly.CreateInstance(parserType.FullName) as IDataParser; if (parser == null) { throw new ConfigurationErrorsException(String.Format("Cannot create instance of parser {0}!", parserType.FullName)); } // Open file if (!parser.Open(filePath)) { throw new InvalidOperationException(String.Format("Cannot open file {0} with parser {1}!", filePath, parserType.FullName)); } // Parse file and get all entities T ent = null; try { IEnumerable <T> set = parser.Read <T>(templateCfg); ent = set.FirstOrDefault(); } finally { parser.Close(); } return(ent); }
/// <summary> /// Load data object from file with specific configuration and template /// </summary> /// <param name="config">Configuration used to define behavior of data loading</param> /// <returns></returns> IEnumerable <T> IDataParser.Read <T>(TemplateConfigElement config) { // Go throw template and parse data from excel file if (config.Collections == null || config.Collections.Count < 1) { throw new InvalidOperationException( "No valid collection found in TemplateConfigElement for ExcelParser!"); } // This method only handle one collection CollectionConfigElement colCfg = config.Collections[0]; // Create enumerator for this type of entities var parser = new ExcelEntityEnumerable <T>(_doc, colCfg); return(parser); }
/// <summary> /// Write one object to file with specific configuration and template /// </summary> /// <typeparam name="T"></typeparam> /// <param name="config">Configuration used to define behavior of data writting</param> /// <param name="obj"></param> void IDataParser.Write <T>(TemplateConfigElement config, T obj) { // Go throw template and parse data from excel file if (config.Collections == null || config.Collections.Count < 1) { throw new InvalidOperationException( "No valid collection found in TemplateConfigElement for ExcelParser!"); } // This method only handle one collection CollectionConfigElement colCfg = config.Collections[0]; var writter = new ExcelEntityWriter <T>(_doc, colCfg); writter.Write(obj); _changed = true; }
/// <summary> /// Save data entity to file with specific template /// </summary> /// <typeparam name="T">Entity class</typeparam> /// <param name="filePath">Path of file to be save to</param> /// <param name="templateName">Name of template used to write data to file</param> /// <param name="entity">Entity to be saved</param> public void SaveOne <T>(String filePath, String templateName, T entity) where T : class { // Find parser TemplateConfigElement templateCfg = _cfg.Templates[templateName]; if (templateCfg == null) { throw new ConfigurationErrorsException(String.Format("Template {0} not exist!", templateName)); } var parserCfg = _cfg.Parsers[templateCfg.Parser] as ParserConfigElement; if (parserCfg == null) { throw new ConfigurationErrorsException(String.Format("Parser {0} not configured!", templateCfg.Parser)); } Type parserType = Type.GetType(parserCfg.Type); if (parserType == null) { throw new ConfigurationErrorsException(String.Format("Parser {0} not exist!", parserCfg.Type)); } // Create parser var parser = parserType.Assembly.CreateInstance(parserType.FullName) as IDataParser; if (parser == null) { throw new ConfigurationErrorsException(String.Format("Cannot create instance of parser {0}!", parserType.FullName)); } // Open file if (!parser.Open(filePath, false)) { throw new InvalidOperationException(String.Format("Cannot open file {0} with parser {1}!", filePath, parserType.FullName)); } // Parse file and get all entities try { parser.Write(templateCfg, entity); } finally { parser.Close(); } }