示例#1
0
        /// <summary>
        /// Find previous session and if it was broken restore it.
        /// </summary>
        /// <returns>false if session was not restored due to any reason</returns>
        bool restore(ref DateTime start_time)
        {
            lock (this)
            {
                try
                {
                    switch (Session.This.SourceType)
                    {
                    case ItemSourceType.DB:
                        return(false);

                    case ItemSourceType.FILE:

                        DirectoryInfo   work_di     = new DirectoryInfo(Log.WorkDir);
                        DirectoryInfo[] session_dis = work_di.GetDirectories("Session*", SearchOption.TopDirectoryOnly);
                        Array.Sort(session_dis, new CompareDirectoryInfo());
                        for (int i = session_dis.Length - 2; i >= 0; i--)    //pass own session
                        {
                            //if (dis[i].FullName == Log.SessionDir)//pass own session
                            //    continue;

                            FileInfo ssfi = new FileInfo(session_dis[i].FullName + "\\" + STATES_FILE_NAME);
                            if (!ssfi.Exists)
                            {
                                continue;
                            }

                            XmlTextReader xtr        = new XmlTextReader(ssfi.FullName);
                            SessionState  last_state = SessionState.EMPTY;
                            start_time = DateTime.Now;
                            while (xtr.Move2Element("State", true) != null)
                            {
                                string s = xtr.Move2Attribute("value", true);
                                last_state = (SessionState)Enum.Parse(typeof(SessionState), s, true);
                                if (last_state == SessionState.STARTED)
                                {
                                    start_time = DateTime.Parse(xtr.Move2Attribute("session_start_time", true));
                                }
                            }
                            switch (last_state)
                            {
                            case SessionState.EMPTY:
                            case SessionState.RESTORING:
                                continue;

                            case SessionState.COMPLETED:
                                Log.Main.Write("Previous session was completed successfully.");
                                return(false);
                            }

                            //Log.Main.Write("Previous session was broken.");

                            string previous_broken_session_dir = session_dis[i].FullName;

                            if (!LogMessage.AskYesNo("Previous session " + previous_broken_session_dir + " was not completed. Restore it?", true))
                            {
                                return(false);
                            }

                            FileInfo broken_session_items_fi = new FileInfo(previous_broken_session_dir + "\\" + ITEMS_FILE_NAME);
                            if (!broken_session_items_fi.Exists)
                            {
                                string str = "Could not find " + broken_session_items_fi.Name + " in " + previous_broken_session_dir + "!";
                                if (LogMessage.AskYesNo(str + " Proceed without session restoring?", true))
                                {
                                    return(false);
                                }
                                else
                                {
                                    Close();
                                    return(false);
                                }
                            }

                            Log.Main.Write("Restoring session " + previous_broken_session_dir);

                            set_session_state(SessionState.RESTORING, "source_session", previous_broken_session_dir);
                            Log.Main.Write("Restoring session from " + previous_broken_session_dir);
                            restore_session_from_xml_file(broken_session_items_fi.FullName);

                            //string previous_broken_session_output_file = previous_broken_session_dir + "\\" + Log.OutputDirName + "\\" + Properties.Output.Default.OutputFile;

                            //if (File.Exists(previous_broken_session_output_file))
                            //{
                            //    File.Copy(previous_broken_session_output_file, Log.OutputDir + "\\" + Properties.Output.Default.OutputFile);
                            //    Log.Main.Write("Output file of previous broken session was copied to the current session and will be appended if has the name name: " + previous_broken_session_output_file);
                            //}
                            //else
                            //    Log.Main.Write("Output file of previous broken session does not exists: " + previous_broken_session_output_file);

                            DirectoryInfo output_di = new DirectoryInfo(previous_broken_session_dir + "\\" + Log.OutputDirName);
                            if (output_di.Exists)
                            {
                                foreach (FileInfo fi in output_di.GetFiles())
                                {
                                    string old_output_file2;
                                    if (!Properties.Output.Default.WriteOutputFile2CommonFolder)
                                    {
                                        old_output_file2 = Log.OutputDir + "\\" + fi.Name;
                                    }
                                    else
                                    {
                                        old_output_file2 = Log.WorkDir + "\\" + fi.Name;
                                    }
                                    if (!File.Exists(old_output_file2))
                                    {
                                        File.Copy(fi.FullName, old_output_file2);
                                        Log.Main.Write("Output file of previous broken session was copied to the current output folder: " + old_output_file2);
                                    }
                                    else
                                    {
                                        Log.Main.Warning("Output file of previous broken session was not copied because the destination one exists already: " + old_output_file2);
                                    }
                                }
                            }
                            return(true);
                        }
                        return(false);

                    default:
                        throw new Exception("Undefined SourceType: " + Session.This.SourceType.ToString());
                    }
                }
                catch (Exception e)
                {
                    LogMessage.Exit(e);
                }
                return(false);
            }
        }