public static int rollbackGUIConfigurationTransaction(String sConfigurationFileName) {

    String sConfigurationLockFile;
    File fFile;

    sConfigurationLockFile = new String(sConfigurationFileName + ".lck");

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.delete() == false) {
      JOptionPane.showMessageDialog(
          null,
          "fFile.delete on " + sConfigurationLockFile + " failed",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 3;
    }

    return 0;
  }
  public static int beginGUIConfigurationTransaction(String sConfigurationFileName, String sUser) {

    String sConfigurationLockFile;
    BufferedWriter bwBufferedWriter;
    File fFile;

    sConfigurationLockFile = new String(sConfigurationFileName + ".lck");

    System.out.println("beginGUIConfigurationTransaction: " + sConfigurationFileName);

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.exists()) {
      BufferedReader brBufferedReader;
      char cLastConfigurationUser[];

      cLastConfigurationUser = new char[((int) fFile.length())];

      try {
        brBufferedReader =
            new BufferedReader(new FileReader(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
        brBufferedReader.read(cLastConfigurationUser, 0, (int) (fFile.length()));
        brBufferedReader.close();

        System.out.println(
            "Last configuration user: "******"Operation on BufferedWriter failed (1)",
            "ConfigurationTransaction",
            JOptionPane.ERROR_MESSAGE);

        return 1;
      }

      if (String.copyValueOf(cLastConfigurationUser).compareTo(sUser) == 0) {
        System.out.println("File. delete: " + sConfigurationFileName);

        if (fFile.delete() == false) {
          JOptionPane.showMessageDialog(
              null, "fFile.delete failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

          return 2;
        }
      } else {
        JOptionPane.showMessageDialog(
            null,
            "The configuration is locked by "
                + String.copyValueOf(cLastConfigurationUser)
                + ".\nYou cannot change the configuration.",
            "ConfigurationTransaction",
            JOptionPane.PLAIN_MESSAGE);

        return 3;
      }
    }

    try {
      bwBufferedWriter =
          new BufferedWriter(new FileWriter(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
      bwBufferedWriter.write(sUser, 0, sUser.length());
      bwBufferedWriter.close();

      System.out.println("Configuration user written: " + sUser);
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null,
          "Operation on BufferedWriter failed (" + e + ")",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 4;
    }

    return 0;
  }
  public static int commitGUIConfigurationTransaction(
      XMLTreeForConfiguration xtfcXMLTreeForConfiguration, Vector vFunctionalities) {

    TreeVisit tvConfigurationTreeVisit;
    TreeVisitToGetConfigurationFile tvgcTreeVisitToGetConfigurationFile;
    String sConfigurationFile;
    URL uXML;
    String sConfigurationLockFile;
    BufferedWriter bwBufferedWriter;
    File fFile;

    tvgcTreeVisitToGetConfigurationFile = new TreeVisitToGetConfigurationFile(vFunctionalities);
    tvConfigurationTreeVisit = new TreeVisit(tvgcTreeVisitToGetConfigurationFile);

    if (tvConfigurationTreeVisit.inOrderVisit(xtfcXMLTreeForConfiguration.getXMLTopTreeComponent())
        != 0) {
      JOptionPane.showMessageDialog(
          null, "inOrderVisit failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    sConfigurationFile = tvgcTreeVisitToGetConfigurationFile.getConfigurationFile();

    System.out.println("commitGUIConfigurationTransaction: " + sConfigurationFile);

    uXML = xtfcXMLTreeForConfiguration.getXML();

    try {
      bwBufferedWriter =
          new BufferedWriter(new FileWriter(URLDecoder.decode(uXML.getFile(), "UTF-8")));
      bwBufferedWriter.write(sConfigurationFile, 0, sConfigurationFile.length());
      bwBufferedWriter.close();

      System.out.println(
          "commitGUIConfigurationTransaction: file written ("
              + URLDecoder.decode(uXML.getFile(), "UTF-8")
              + ")");
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null,
          "Operation on BufferedWriter failed (3)",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 2;
    }

    sConfigurationLockFile = new String(uXML.getFile() + ".lck");

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));

      System.out.println("File. delete: " + URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.delete() == false) {
      JOptionPane.showMessageDialog(
          null,
          "fFile.delete on " + sConfigurationLockFile + " failed",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 3;
    }

    return 0;
  }
예제 #4
0
  public void realize(XMLTree xtXMLTree) {
    try {
      TreeComponent tcSiteTreeComponent;
      TreeComponent tcSiteTreeComponentParent;
      TreeComponent tcXMLTopTreeComponent;
      TreeVisit tvTreeVisit;
      TreeVisitToFindTreeComponent tvftcTreeVisitToFindTreeComponent;
      TreeComponent tcSiteToRemoveTreeComponent;

      tcSiteTreeComponent = xtXMLTree.getSelectedTreeComponent();

      if (tcSiteTreeComponent.getChildCount() != 0) {
        JOptionPane.showMessageDialog(
            null, "The site is not empty", "DeleteSite", JOptionPane.ERROR_MESSAGE);

        return;
      }

      if (JOptionPane.showOptionDialog(
              null,
              "Are you sure?",
              "DeleteSite",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.QUESTION_MESSAGE,
              null,
              null,
              null)
          == 1) return;

      if (ConfigurationTransaction.beginGUIConfigurationTransaction(
              _xtfcXMLTreeForConfiguration.getXML().getFile(), _pProperties.getProperty("login"))
          != 0) {
        JOptionPane.showMessageDialog(
            null,
            "beginGUIConfigurationTransaction failed",
            "DeleteSite",
            JOptionPane.ERROR_MESSAGE);

        return;
      }

      tcSiteTreeComponentParent = (TreeComponent) tcSiteTreeComponent.getParent();

      xtXMLTree.setSelectedTreeComponent(tcSiteTreeComponentParent);
      tcSiteTreeComponentParent.remove(tcSiteTreeComponent);
      tcSiteTreeComponentParent.refreshSubTree();

      // XMLTreeForContents
      tcXMLTopTreeComponent = _xtfcXMLTreeForContents.getXMLTopTreeComponent();
      tvftcTreeVisitToFindTreeComponent =
          new TreeVisitToFindTreeComponent(
              "Site", tcSiteTreeComponent.getAttributeValue("Identifier"));
      tvTreeVisit = new TreeVisit(tvftcTreeVisitToFindTreeComponent);

      if (tvTreeVisit.preOrderVisit(tcXMLTopTreeComponent) != 0) {
        JOptionPane.showMessageDialog(
            null, "preOrderVisit failed", "DeleteSite", JOptionPane.ERROR_MESSAGE);

        if (ConfigurationTransaction.rollbackGUIConfigurationTransaction(
                _xtfcXMLTreeForConfiguration.getXML().getFile())
            != 0) {
          JOptionPane.showMessageDialog(
              null,
              "rollbackGUIConfigurationTransaction failed",
              "DeleteSite",
              JOptionPane.ERROR_MESSAGE);
        }

        return;
      }

      if ((tcSiteToRemoveTreeComponent = tvftcTreeVisitToFindTreeComponent.getTreeComponentFound())
          == null) {
        JOptionPane.showMessageDialog(
            null,
            "TreeComponent not found (TagName: Site, Identifier: "
                + tcSiteTreeComponent.getAttributeValue("Identifier")
                + ")",
            "DeleteSite",
            JOptionPane.ERROR_MESSAGE);

        if (ConfigurationTransaction.rollbackGUIConfigurationTransaction(
                _xtfcXMLTreeForConfiguration.getXML().getFile())
            != 0) {
          JOptionPane.showMessageDialog(
              null,
              "rollbackGUIConfigurationTransaction failed",
              "DeleteSite",
              JOptionPane.ERROR_MESSAGE);
        }

        return;
      }
      tcXMLTopTreeComponent.remove(tcSiteToRemoveTreeComponent);
      tcXMLTopTreeComponent.refreshSubTree();

      // XMLTreeForMonitor
      tcXMLTopTreeComponent = _xtfmXMLTreeForMonitor.getXMLTopTreeComponent();
      tvftcTreeVisitToFindTreeComponent =
          new TreeVisitToFindTreeComponent(
              "Site", tcSiteTreeComponent.getAttributeValue("Identifier"));
      tvTreeVisit = new TreeVisit(tvftcTreeVisitToFindTreeComponent);

      if (tvTreeVisit.preOrderVisit(tcXMLTopTreeComponent) != 0) {
        JOptionPane.showMessageDialog(
            null, "preOrderVisit failed", "DeleteSite", JOptionPane.ERROR_MESSAGE);

        if (ConfigurationTransaction.rollbackGUIConfigurationTransaction(
                _xtfcXMLTreeForConfiguration.getXML().getFile())
            != 0) {
          JOptionPane.showMessageDialog(
              null,
              "rollbackGUIConfigurationTransaction failed",
              "DeleteSite",
              JOptionPane.ERROR_MESSAGE);
        }

        return;
      }

      if ((tcSiteToRemoveTreeComponent = tvftcTreeVisitToFindTreeComponent.getTreeComponentFound())
          == null) {
        JOptionPane.showMessageDialog(
            null,
            "TreeComponent not found (TagName: Site, Identifier: "
                + tcSiteTreeComponent.getAttributeValue("Identifier")
                + ")",
            "DeleteSite",
            JOptionPane.ERROR_MESSAGE);

        if (ConfigurationTransaction.rollbackGUIConfigurationTransaction(
                _xtfcXMLTreeForConfiguration.getXML().getFile())
            != 0) {
          JOptionPane.showMessageDialog(
              null,
              "rollbackGUIConfigurationTransaction failed",
              "DeleteSite",
              JOptionPane.ERROR_MESSAGE);
        }

        return;
      }
      tcXMLTopTreeComponent.remove(tcSiteToRemoveTreeComponent);
      tcXMLTopTreeComponent.refreshSubTree();

      if (ConfigurationTransaction.commitGUIConfigurationTransaction(
              _xtfcXMLTreeForConfiguration, _xtfcXMLTreeForConfiguration.getFunctionalities())
          != 0) {
        JOptionPane.showMessageDialog(
            null,
            "commitGUIConfigurationTransaction failed",
            "DeleteSite",
            JOptionPane.ERROR_MESSAGE);

        return;
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(
          null, "DeleteSite ERROR: " + e, "DeleteSite", JOptionPane.ERROR_MESSAGE);

      if (ConfigurationTransaction.rollbackGUIConfigurationTransaction(
              _xtfcXMLTreeForConfiguration.getXML().getFile())
          != 0) {
        JOptionPane.showMessageDialog(
            null,
            "rollbackGUIConfigurationTransaction failed",
            "DeleteSite",
            JOptionPane.ERROR_MESSAGE);
      }

      return;
    }
  }