hvv_admin4.state.HVV_StateKeeperXML.java Source code

Java tutorial

Introduction

Here is the source code for hvv_admin4.state.HVV_StateKeeperXML.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hvv_admin4.state;

import hvv_admin4.HVV_Admin4;
import hvv_admin4.steps.info.TechProcessCommentInfo;
import hvv_admin4.steps.info.TechProcessGetterInfo;
import hvv_admin4.steps.info.TechProcessHFInfo;
import hvv_admin4.steps.info.TechProcessHvProcessInfo;
import hvv_admin4.steps.info.TechProcessIgenIextProcessInfo;
import hvv_admin4.steps.info.TechProcessStepCommon;
import hvv_admin4.steps.info.TechProcessUacProcessInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author yaroslav
 */
public class HVV_StateKeeperXML {

    static Logger logger = Logger.getLogger(HVV_StateKeeperXML.class);

    HVV_Admin4 theApp;
    String m_strStateKeepFileName;

    public boolean m_bDropReadState;

    public HVV_StateKeeperXML(HVV_Admin4 app) {
        theApp = app;
        m_strStateKeepFileName = app.GetAMSRoot() + "/etc/stateKeeper.admin4." + app.GetSelectedArm() + ".xml";
        m_bDropReadState = false;
    }

    public void SaveState() {
        if (theApp.GetCurrentStep() < 20)
            return;

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

        try {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("SaveStateXML");

            // 
            root.addElement("CurrentStep").addText("" + theApp.GetCurrentStep());

            //?   
            root.addElement("SerialNumber").addText(theApp.GetSerial());

            //  
            root.addElement("ProcessedDeviceType").addText("" + theApp.GetProcessedDeviceType());

            // ? 
            if (theApp.GetDtmTOEnd() == null)
                root.addElement("dtmTOEnd").addText("NULL");
            else
                root.addElement("dtmTOEnd").addText(formatter.format(theApp.GetDtmTOEnd()));

            Set set = theApp.SecretSteps().entrySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();

                String strKey = (String) entry.getKey();
                Object objValue = entry.getValue();

                Element innerRoot = root.addElement("Step" + strKey);
                switch (strKey) {
                case "001":
                case "061":
                case "062":
                case "064":
                case "101":
                case "103":
                case "105":
                case "141":
                case "143":
                case "181":
                    ((TechProcessStepCommon) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                case "021":
                case "022":
                case "041":
                case "042":
                case "043":
                case "044":
                case "102":
                case "104":
                case "106":
                case "142":
                case "144":
                    ((TechProcessHvProcessInfo) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                case "063":
                case "121":
                case "161":
                    ((TechProcessGetterInfo) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                case "081":
                case "182":
                    ((TechProcessIgenIextProcessInfo) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                case "082":
                case "183":
                    ((TechProcessUacProcessInfo) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                case "083":
                case "184":
                    ((TechProcessCommentInfo) objValue).SaveItemXML(innerRoot,
                            theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                    break;

                /*
                case "122":
                case "162":
                (( TechProcessHFInfo ) objValue).SaveItemXML( innerRoot); break;
                */
                }
            }
            OutputFormat format = OutputFormat.createPrettyPrint();

            //File fl = new File( m_strStateKeepFileName);
            //fl.createNewFile();
            XMLWriter writer = new XMLWriter(new FileWriter(m_strStateKeepFileName), format);

            writer.write(document);
            writer.close();
        } catch (IOException ex) {
            logger.error("IOException caught while saving state!", ex);
        }
    }

    public void RestoreState() {
        try {
            FileInputStream fis = new FileInputStream(m_strStateKeepFileName);
            ObjectInputStream ois = new ObjectInputStream(fis);

            int nLastWrittenStep = ois.readInt(); // ??? (?)
            String strSerial = ois.readUTF(); //?  
            int nDeviceType = ois.readInt(); //  (: --)

            // ? 
            Date dt = (Date) ois.readObject();
            if (dt.getYear() != 1900)
                theApp.SetDtmTOEnd(dt);
            else
                theApp.SetDtmTOEnd(null);

            String strStepNumber = String.format("%03d", nLastWrittenStep);
            boolean bContinue;
            String strStep;
            do {
                strStep = ois.readUTF();
                switch (strStep) {
                case "001":
                case "061":
                case "062":
                case "064":
                case "101":
                case "103":
                case "105":
                case "141":
                case "143":
                case "181":

                    theApp.SecretSteps().put(strStep, new TechProcessStepCommon(ois));
                    break;

                case "021":
                case "022":
                case "041":
                case "042":
                case "043":
                case "044":
                case "102":
                case "104":
                case "106":
                case "142":
                case "144":
                    theApp.SecretSteps().put(strStep, new TechProcessHvProcessInfo(ois));
                    break;

                case "063":
                case "121":
                case "161":
                    theApp.SecretSteps().put(strStep, new TechProcessGetterInfo(ois));
                    break;

                case "081":
                case "182":
                    theApp.SecretSteps().put(strStep, new TechProcessIgenIextProcessInfo(ois));
                    break;

                case "082":
                case "183":
                    theApp.SecretSteps().put(strStep, new TechProcessUacProcessInfo(ois));
                    break;

                case "083":
                case "184":
                    theApp.SecretSteps().put(strStep, new TechProcessCommentInfo(ois));
                    break;

                case "122":
                case "162":
                    theApp.SecretSteps().put(strStep, new TechProcessHFInfo(ois));
                    break;
                }
                int nAvailable = fis.available();
                bContinue = nAvailable != 0;

            } while (bContinue);

            ois.close();
            fis.close();

            boolean bDrop = false;

            boolean bNext = true;

            boolean bCurrentStepInProgress = false;

            switch (strStepNumber) {
            case "000":
                break;
            case "001":
                break;

            case "021":
            case "022":
            case "042":
            case "044":
            case "102":
            case "104":
            case "106":
            case "142":
            case "144": {
                TechProcessHvProcessInfo stepInfo = (TechProcessHvProcessInfo) theApp.GetHvStep(strStepNumber);
                RestoreHvStep1 dlg = new RestoreHvStep1();
                dlg.configureAsHv(theApp.GetStepNameWithNum(new Integer(strStepNumber)), stepInfo,
                        theApp.GetSettings().GetProcessingTime_2());
                dlg.setVisible(true);

                bDrop = dlg.m_bDrop;
                if (!bDrop) {
                    stepInfo.SetStopDate(dlg.m_gdtmDtmStopActual.getTime());
                    stepInfo.SetAnStart(new Double(dlg.edtValueParam1.getText()));
                    stepInfo.SetTuStart(new Double(dlg.edtValueParam2.getText()));
                    stepInfo.SetAnStop(new Double(dlg.edtValueParam3.getText()));
                    stepInfo.SetTuStop(new Double(dlg.edtValueParam4.getText()));
                }
                stepInfo.SetRestoredAfterFailFlag(true);
            }
                break;

            case "041":
            case "043": {
                if (HVV_Admin4.MessageBoxAskYesNo(
                        "?      ?",
                        "HVV_Admin4") == JOptionPane.YES_OPTION) {
                    //..   ?   ? ??          
                    TechProcessHvProcessInfo stepInfo = (TechProcessHvProcessInfo) theApp.GetHvStep(strStepNumber);
                    RestoreHvStep1 dlg = new RestoreHvStep1();
                    dlg.configureAsHv(theApp.GetStepNameWithNum(new Integer(strStepNumber)), stepInfo,
                            theApp.GetSettings().GetProcessingTime_2());
                    dlg.setVisible(true);

                    bDrop = dlg.m_bDrop;
                    if (!bDrop) {
                        stepInfo.SetStopDate(dlg.m_gdtmDtmStopActual.getTime());
                        stepInfo.SetAnStart(new Double(dlg.edtValueParam1.getText()));
                        stepInfo.SetTuStart(new Double(dlg.edtValueParam2.getText()));
                        stepInfo.SetAnStop(new Double(dlg.edtValueParam3.getText()));
                        stepInfo.SetTuStop(new Double(dlg.edtValueParam4.getText()));
                    }
                    stepInfo.SetRestoredAfterFailFlag(true);
                } else {
                    //..   ?   ? ??    
                    TechProcessHvProcessInfo stepInfo = (TechProcessHvProcessInfo) theApp.GetHvStep(strStepNumber);
                    RestoreHvStep1 dlg = new RestoreHvStep1();
                    dlg.configureAsHv(theApp.GetStepNameWithNum(new Integer(strStepNumber)), stepInfo,
                            theApp.GetSettings().GetProcessingTime_2());
                    dlg.setVisible(true);

                    bDrop = dlg.m_bDrop;
                    if (!bDrop) {
                        stepInfo.SetStopDate(dlg.m_gdtmDtmStopActual.getTime());
                        stepInfo.SetAnStart(new Double(dlg.edtValueParam1.getText()));
                        stepInfo.SetTuStart(new Double(dlg.edtValueParam2.getText()));
                        stepInfo.SetAnStop(new Double(dlg.edtValueParam3.getText()));
                        stepInfo.SetTuStop(new Double(dlg.edtValueParam4.getText()));
                    }
                    stepInfo.SetRestoredAfterFailFlag(true);

                    theApp.SecretSteps().remove(strStepNumber);
                    if (strStepNumber.equals("041"))
                        nLastWrittenStep = 42;
                    if (strStepNumber.equals("043"))
                        nLastWrittenStep = 44;
                    theApp.SaveStepInfo(String.format("%03d", nLastWrittenStep), stepInfo, false);
                }
            }
                break;

            case "061": {
                //     - ? ? ???  ? dtmTOEnd
                bNext = false;

                /*
                TechProcessStepCommon stepInfo = ( TechProcessStepCommon) theApp.GetCommonStep( strStepNumber);
                RestoreHvStep1 dlg = new RestoreHvStep1();
                dlg.configureAsCalcToFinishDtm(theApp.GetStepNameWithNum( new Integer( strStepNumber)), stepInfo, theApp.GetLocalDate());
                dlg.setVisible( true);
                    
                bDrop = dlg.m_bDrop;
                if( !bDrop) {
                    stepInfo.SetStopDateAsCurrent(nDeviceType);
                    theApp.SetDtmTOEnd( dlg.m_gdtmDtmParam1.getTime());
                }
                */
            }
                break;

            case "101":
            case "103":
            case "105":
            case "141":
            case "143":
            case "181": {
                TechProcessStepCommon stepInfo = (TechProcessStepCommon) theApp.GetCommonStep(strStepNumber);
                if (stepInfo != null) {
                    RestoreHvStep1 dlg = new RestoreHvStep1();
                    dlg.configureAsCommon(theApp.GetStepNameWithNum(new Integer(strStepNumber)), stepInfo,
                            theApp.GetSettings().GetExcerptTime());
                    dlg.setVisible(true);

                    bDrop = dlg.m_bDrop;
                    if (!bDrop) {
                        stepInfo.SetStopDate(dlg.m_gdtmDtmStopActual.getTime());
                        stepInfo.SetRestoredAfterFailFlag(true);
                    }
                } else {
                    bNext = false;
                }
            }
                break;

            case "062":
            case "063":
                //     - ? ? ???  ? dtmTOEnd
                bNext = false;
                break;

            case "064": {
                TechProcessStepCommon stepInfo = (TechProcessStepCommon) theApp.GetCommonStep(strStepNumber);
                RestoreHvStep1 dlg = new RestoreHvStep1();
                dlg.configureAsCommon(theApp.GetStepNameWithNum(new Integer(strStepNumber)), stepInfo,
                        theApp.GetSettings().GetExcerptTime());
                dlg.setVisible(true);

                bDrop = dlg.m_bDrop;
                if (!bDrop) {
                    stepInfo.SetStopDate(dlg.m_gdtmDtmStopActual.getTime());
                }

                TechProcessIgenIextProcessInfo info2 = new TechProcessIgenIextProcessInfo();
                info2.SetStartReportTitle(
                        "     ??");
                info2.SetStartDateAsCurrent(theApp.GetSettings().GetTimeZoneShift());
                info2.SetRestoredAfterFailFlag(true);
                theApp.SaveStepInfo("081", info2, false);
            }
                break;

            case "081":
            case "082":
                bNext = false;
                break;

            case "083": {
                TechProcessStepCommon stepInfo = theApp.GetCommonStep("083");
                stepInfo.SetRestoredAfterFailFlag(true);
                bNext = (stepInfo.GetStopDate() != null);
            }
                break;

            case "121":
                bNext = false;
                bCurrentStepInProgress = true;
                break;

            case "122": {
                TechProcessHFInfo step = (TechProcessHFInfo) theApp.GetCommonStep("122");
                RestoreLongStep dlg = new RestoreLongStep();
                dlg.initAsDegasation(step, nDeviceType);
                dlg.setVisible(true);
                bNext = false;
            }
                break;

            case "161":
                break;
            case "162":
                break;

            case "182":
                break;
            case "183":
                break;
            case "184":
                break;
            }

            theApp.SetCurrentStep(nLastWrittenStep);
            theApp.SetCurrentStepInProgress(bCurrentStepInProgress);

            if (bNext)
                theApp.NextCurrentStep();

            if (bDrop == false) {
                theApp.SetSerial(strSerial);
                theApp.SetProcessedDeviceType(nDeviceType);
                //?  ?   app'e HVV_Admin4
            } else {
                m_bDropReadState = true;
            }

        } catch (Exception ex) {
            logger.error(
                    " ? ??? ????  Exception!",
                    ex);
        }

    }

    public void DropState() {
        File file = new File(m_strStateKeepFileName);
        file.deleteOnExit();
    }

    public boolean CheckStateKeeperFileExistance() {
        return (new File(m_strStateKeepFileName).exists());
    }
}