/* (non-Javadoc)
   * @see edu.ku.brc.af.ui.forms.validation.TypeSearchForQueryFactory#getDOMFromResource(java.lang.String, java.lang.String)
   */
  public Element getDOMFromResource(final String name, final String localPath) {
    SpecifyAppContextMgr acMgr = (SpecifyAppContextMgr) AppContextMgr.getInstance();
    DataProviderSessionIFace session = null;
    try {
      session = DataProviderFactory.getInstance().createSession();

      String[] dirs = {SpecifyAppContextMgr.COMMONDIR, SpecifyAppContextMgr.BACKSTOPDIR};
      for (String dirName : dirs) {
        SpAppResourceDir appResDir = acMgr.getSpAppResourceDirByName(dirName);
        if (appResDir != null) {
          SpAppResource appRes = appResDir.getResourceByName(name);
          if (appRes != null) {
            session.close();
            session = null;

            return AppContextMgr.getInstance().getResourceAsDOM(appRes);
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
      edu.ku.brc.exceptions.ExceptionTracker.getInstance()
          .capture(SpecifyUIFieldFormatterMgr.class, ex);

    } finally {
      if (session != null) {
        session.close();
      }
    }

    return XMLHelper.readDOMFromConfigDir(localPath); // $NON-NLS-1$
  }
  /**
   * Saves an Discipline level XML document to a Database Resource.
   *
   * @param resName the name of the resource
   * @param xml the XML to be saved.
   */
  public void saveResource(final String resName, final String xml) {
    // System.out.println("Saved");
    // System.out.println(xml);
    // System.out.println();

    SpecifyAppContextMgr acMgr = (SpecifyAppContextMgr) AppContextMgr.getInstance();
    DataProviderSessionIFace session = null;
    try {
      session = DataProviderFactory.getInstance().createSession();

      SpAppResource appRes = null;
      SpAppResourceDir commonResDir =
          acMgr.getSpAppResourceDirByName(SpecifyAppContextMgr.COMMONDIR);
      if (commonResDir != null) {
        appRes = commonResDir.getResourceByName(resName);
        if (appRes == null) {
          SpAppResourceDir backStopResDir =
              acMgr.getSpAppResourceDirByName(SpecifyAppContextMgr.BACKSTOPDIR);
          appRes = backStopResDir.getResourceByName(resName);
          if (appRes != null) {
            backStopResDir.getSpAppResources().remove(appRes);
            commonResDir.getSpAppResources().add(appRes);
            appRes.setSpAppResourceDir(commonResDir);
          } else {
            // major error
            UIRegistry.showError("Major Error");
          }
        }

        if (appRes != null) {
          session.close();
          session = null;

          appRes.setDataAsString(xml);
          AppContextMgr.getInstance().saveResource(appRes);

        } else {
          UIRegistry.showError("Major Error #2");
        }
      }

      /*if (appRes == null)
      {
          SpAppResource backStopDir = acMgr.getSpAppResourceDirByName(BACKSTOPDIR).getResourceByName(resName);
          if (appResDir != null)
          {
              SpAppResource newAppRes  = new SpAppResource();
              newAppRes.initialize();
              if (diskAppRes != null)
              {
                  newAppRes.setMetaData(diskAppRes.getMetaData());
                  newAppRes.setDescription(diskAppRes.getDescription());
                  newAppRes.setFileName(diskAppRes.getFileName());
                  newAppRes.setMimeType(diskAppRes.getMimeType());
                  newAppRes.setName(diskAppRes.getName());

                  newAppRes.setLevel(diskAppRes.getLevel());
              } else
              {
                  newAppRes.setName(resName);
                  newAppRes.setLevel((short)0);
                  newAppRes.setMimeType("text/xml");
              }

              Agent agent = AppContextMgr.getInstance().getClassObject(Agent.class);
              newAppRes.setCreatedByAgent(agent);
              newAppRes.setSpecifyUser(user);

              newAppRes.setSpAppResourceDir(appResDir);
              appResDir.getSpAppResources().add(newAppRes);
              newAppRes.setDataAsString(xml);

              session.close();
              session = null;
              ((SpecifyAppContextMgr) AppContextMgr.getInstance()).saveResource(newAppRes);

          } else
          {
              AppContextMgr.getInstance().putResourceAsXML(resName, xml); //$NON-NLS-1$
          }
      }*/

    } catch (Exception ex) {
      ex.printStackTrace();
      edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
      edu.ku.brc.exceptions.ExceptionTracker.getInstance()
          .capture(SpecifyUIFieldFormatterMgr.class, ex);

    } finally {
      if (session != null) {
        session.close();
      }
    }
  }