/**
  * Determine the text being shown for given panel.
  *
  * @param panel
  * @return The text shown, i18n.
  */
 public Map getHtmlCode(Panel panel) {
   PanelSession pSession = SessionManager.getPanelSession(panel);
   Map m = (Map) pSession.getAttribute(ATTR_TEXT);
   if (m != null) return m;
   HTMLText text = load(panel.getInstance());
   if (text != null) return text.getText();
   try {
     HTMLText textToCreate = new HTMLText();
     textToCreate.setPanelInstance(panel.getInstance());
     Locale[] locales = LocaleManager.lookup().getPlatformAvailableLocales();
     for (int i = 0; i < locales.length; i++) {
       Locale locale = locales[i];
       ResourceBundle i18n =
           localeManager.getBundle("org.jboss.dashboard.ui.panel.advancedHTML.messages", locale);
       textToCreate.setText(locale.getLanguage(), i18n.getString("defaultContent"));
     }
     textToCreate.save();
   } catch (Exception e) {
     log.error("Error creating empty text for panel: ", e);
   }
   text = load(panel.getInstance());
   if (text != null) return text.getText();
   log.error("Current HTML code is null for panel " + panel);
   return null;
 }
  /**
   * Replicates panel data.
   *
   * @param src Source PanelInstance
   * @param dest Destinaton PanelInstance
   */
  public void replicateData(final PanelInstance src, PanelInstance dest) throws Exception {
    super.replicateData(src, dest);
    log.debug(
        "HTMLDriver replicating Data from PanelInstance "
            + src.getDbid()
            + " to "
            + dest.getDbid()
            + ").");
    if (src.equals(dest)) {
      log.debug("Ignoring replication, panel instance is the same.");
      return;
    }

    final HTMLText[] textArray = new HTMLText[1];
    try {
      new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
          log.debug("Getting text to duplicate for instance " + src.getDbid());
          FlushMode oldMode = session.getFlushMode();
          session.setFlushMode(
              FlushMode
                  .COMMIT); // Avoids flushing, as we know the text was not modified in this
                            // transaction.
          textArray[0] = load(src);
          session.setFlushMode(oldMode);
          log.debug("Got text to duplicate for instance " + src.getDbid());
        }
      }.execute();
    } catch (Exception e) {
      log.error("Error loading text for instance. ", e);
    }
    HTMLText text = textArray[0];
    if (text == null) {
      log.debug(
          "Nothing to replicate from PanelInstance " + src.getDbid() + " to " + dest.getDbid());
      return;
    }
    Map htmlSrc = text.getText();

    log.debug("htmlCode to replicate = " + htmlSrc);
    HTMLText htmlDest = new HTMLText();
    htmlDest.setPanelInstance(dest.getInstance());
    for (Iterator it = htmlSrc.keySet().iterator(); it.hasNext(); ) {
      String key = (String) it.next();
      String val = (String) htmlSrc.get(key);
      htmlDest.setText(key, val);
    }
    try {
      log.debug("Updating HTMLText: IDText " + htmlDest.getDbid() + " Text " + htmlDest.getText());
      htmlDest.save();
    } catch (Exception e) {
      log.error("Replicating panel data", e);
    }
  }
 public HTMLText load(final PanelInstance instance) {
   final List results = new ArrayList();
   try {
     new HibernateTxFragment() {
       protected void txFragment(Session session) throws Exception {
         FlushMode oldFlushMode = session.getFlushMode();
         session.setFlushMode(FlushMode.NEVER);
         Query query =
             session.createQuery(
                 " from "
                     + HTMLText.class.getName()
                     + " as text where text.panelInstance = :instance");
         query.setParameter("instance", instance);
         query.setCacheable(true);
         results.addAll(query.list());
         session.setFlushMode(oldFlushMode);
       }
     }.execute();
     HTMLText text = null;
     if (results.size() > 0) text = (HTMLText) results.get(0);
     else log.debug("Does not exist a html_text for HTML panel");
     return text;
   } catch (Exception e) {
     log.error("Can't retrive a data for HTML panel ", e);
     return null;
   }
 }
 /** Write instance content to given OutputStream, which must not be closed. */
 public void exportContent(PanelInstance instance, OutputStream os) throws Exception {
   HTMLText text = load(instance);
   if (text == null) {
     try {
       text = new HTMLText();
       text.setPanelInstance(instance);
       text.save();
     } catch (Exception e) {
       log.error("Error creating empty HTMLText object", e);
     }
   }
   ObjectOutputStream oos = new ObjectOutputStream(os);
   if (log.isDebugEnabled()) log.debug("Exporting content: " + text.getText());
   HashMap h = new HashMap(); // Avoids serializing a hibernate map
   h.putAll(text.getText());
   oos.writeObject(h);
 }