示例#1
0
        public Task loadTask()
        {
            string     taskFilePath = string.Empty;
            FileStream stream       = null;

            try
            {
                taskFilePath = getNextTaskFile();
                if (taskFilePath == string.Empty)
                {
                    return(null);
                }

                //XmlSerializer serializer = new XmlSerializer(typeof(TaskXmlNode));
                //stream = new FileStream(taskFilePath, FileMode.Open);
                //TaskXmlNode taskXmlNode = serializer.Deserialize(stream) as TaskXmlNode;
                //stream.Close();

                TaskXmlNode taskXmlNode = XmlDeserializer.deserialize <TaskXmlNode>(taskFilePath);

                Task task = new Task();
                task.ReportName = taskXmlNode.ReportSetting.ReportName;
                taskXmlNode.ReportSetting.ReportFilePath = Application.StartupPath + @"\Report" + taskXmlNode.ReportSetting.ReportFilePath;
                task.ReportSetting = taskXmlNode.ReportSetting;

                foreach (DataSourceXmlNode dataSourceXmlNode in taskXmlNode.DataSourceList)
                {
                    List <object> dataSourceValue = new List <object>();
                    string        prefix          = Application.StartupPath + @"\Assembly";
                    Type          type            = ReflectionHelper.getType(prefix + dataSourceXmlNode.AssemblyPath, dataSourceXmlNode.TypeFullName);

                    foreach (RecordXmlNode recordXmlNode in dataSourceXmlNode.RecordList)
                    {
                        Object obj = createEntity(type);
                        initEntity(type, obj, recordXmlNode.PropertyList);
                        dataSourceValue.Add(obj);
                    }
                    string     dataSourceName = generateDataSourceName(dataSourceXmlNode.TypeFullName);
                    DataSource dataSource     = new DataSource(dataSourceName, dataSourceValue);
                    task.addDataSource(dataSource);
                }

                task.CurrentFilePath = handleProcessedTask(taskFilePath);
                return(task);
            }
            catch (Exception ex)
            {
                if (stream != null)
                {
                    stream.Close();
                }

                if (File.Exists(taskFilePath))
                {
                    FileInfo info         = new FileInfo(taskFilePath);
                    string   dir          = GlobalVariable.getDeletedDirectory();
                    string   destFilePath = dir + @"\" + info.Name;
                    if (File.Exists(destFilePath))
                    {
                        File.Delete(destFilePath);
                    }
                    File.Move(taskFilePath, destFilePath);
                    throw new Exception(ex.Message + string.Format(" Failed to load task. Task file '{0}' has been moved to directory '{1}'.", new FileInfo(taskFilePath).Name, dir));
                }
                else
                {
                    throw new Exception(ex.Message + " Failed to load task.");
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }