/**
   * Strip missing monitors.
   *
   * @param chartMapping the chart mapping
   * @return true, if successful
   */
  private boolean stripMissingMonitors(ChartMappings chartMapping) {
    boolean isDirty = false;
    ArrayList<String> monitorIDs = chartMapping.getMonitorIDs();
    for (String monitorID : monitorIDs.toArray(new String[monitorIDs.size()])) {
      if (!Monitors.getMonitorsList().containsKey(monitorID)) {
        monitorIDs.remove(monitorID);
        isDirty |= true;
      }
    }

    return isDirty;
  }
  /**
   * Refresh code.
   *
   * @param chartButton the chart button
   * @param updaterThread the updater thread
   */
  public void refreshCode(ChartButton chartButton, UpdaterThread updaterThread) {
    String systemID, nodeID;

    VaadinSession session = getSession();
    if (session == null) {
      session = VaadinSession.getCurrent();
    }
    ManagerUI managerUI = session.getAttribute(ManagerUI.class);
    ClusterComponent componentInfo = session.getAttribute(ClusterComponent.class);

    switch (componentInfo.getType()) {
      case system:
        systemID = componentInfo.getID();
        nodeID = SystemInfo.SYSTEM_NODEID;
        break;

      case node:
        systemID = componentInfo.getParentID();
        nodeID = componentInfo.getID();
        break;

      default:
        return;
    }

    final UserChart userChart = (UserChart) chartButton.getData();
    final Chart chart = chartButton.getChart();
    boolean needsRedraw = false;
    String[] timeStamps = null;
    final Configuration configuration = chart.getConfiguration();

    for (String monitorID : userChart.getMonitorIDs()) {

      if (updaterThread != null && updaterThread.flagged) {
        ManagerUI.log(this.getClass().getName() + " - flagged is set before API call");
        return;
      }

      ManagerUI.log("ChartsLayout - redraw loop MonitorID: " + monitorID);
      final MonitorRecord monitor = Monitors.getMonitor(monitorID);
      if (monitor == null) {
        // monitor was removed from the system: skip
        ManagerUI.log("monitor was removed from the system");
        continue;
      }

      MonitorData monitorData = (MonitorData) userChart.getMonitorData(monitor.getID());
      if (monitorData == null) {
        String method;
        if (UserChart.ChartType.LineChart.name().equals(userChart.getType())) {
          method = MonitorData.METHOD_AVG;
        } else if (UserChart.ChartType.AreaChart.name().equals(userChart.getType())) {
          method = MonitorData.METHOD_MINMAX;
        } else {
          continue; // unknown chart type, skip
        }
        monitorData =
            new MonitorData(
                monitor, systemID, nodeID, time, interval, userChart.getPoints(), method);
        needsRedraw = true;
      } else if (monitorData.update(systemID, nodeID, time, interval, userChart.getPoints())
          == true) {
        // data in chart needs to be updated
        needsRedraw = true;
      } else {
        continue; // no update needed
      }

      if (timeStamps == null) {
        DateConversion dateConversion = session.getAttribute(DateConversion.class);

        ArrayList<Long> unixTimes = monitorData.getTimeStamps();
        if (unixTimes != null) {
          timeStamps = new String[unixTimes.size()];
          int timeSpacer = unixTimes.size() / 15;
          for (int x = 0; x < unixTimes.size(); x++) {
            timeStamps[x] =
                (x % timeSpacer != 0) ? "\u00A0" : dateConversion.stampToString(unixTimes.get(x));
          }
        }
      }

      if (updaterThread != null && updaterThread.flagged) {
        ManagerUI.log("ChartsLayout - flagged is set before UI redraw");
        return;
      }

      final MonitorData finalMonitorData = monitorData;
      final String finalMonitorID = monitorID;

      managerUI.access(
          new Runnable() {
            @Override
            public void run() {
              // Here the UI is locked and can be updated

              ManagerUI.log("ChartsLayout access run() monitorID: " + finalMonitorID);

              if (UserChart.ChartType.LineChart.name().equals(userChart.getType())) {

                ListSeries ls = null, testLS;
                List<Series> lsList = configuration.getSeries();
                Iterator seriesIter = lsList.iterator();
                while (seriesIter.hasNext()) {
                  testLS = (ListSeries) seriesIter.next();
                  if (testLS.getName().equals(monitor.getName())) {
                    ls = testLS;
                    break;
                  }
                }
                if (ls == null) {
                  ls = new ListSeries(monitor.getName());
                  configuration.addSeries(ls);
                }

                userChart.setMonitorData(monitor.getID(), finalMonitorData);

                ArrayList<Number> avgList = finalMonitorData.getAvgPoints();
                ls.setData(avgList);

              } else if (UserChart.ChartType.AreaChart.name().equals(userChart.getType())) {

                RangeSeries rs = null, testRS;
                List<Series> lsList = configuration.getSeries();
                Iterator seriesIter = lsList.iterator();
                while (seriesIter.hasNext()) {
                  testRS = (RangeSeries) seriesIter.next();
                  if (testRS.getName().equals(monitor.getName())) {
                    rs = testRS;
                    break;
                  }
                }
                if (rs == null) {
                  rs = new RangeSeries(monitor.getName());
                  configuration.addSeries(rs);
                }

                userChart.setMonitorData(monitor.getID(), finalMonitorData);

                ArrayList<Number> minList = finalMonitorData.getMinPoints();
                ArrayList<Number> maxList = finalMonitorData.getMaxPoints();

                if (minList != null
                    && maxList != null
                    && minList.size() > 0
                    && maxList.size() > 0
                    && minList.size() == maxList.size()) {
                  Object[] minArray = finalMonitorData.getMinPoints().toArray();
                  Object[] maxArray = finalMonitorData.getMaxPoints().toArray();

                  Number[][] dataList = new Number[minList.size()][2];
                  for (int x = 0; x < minList.size(); x++) {
                    dataList[x][0] = (Number) minArray[x];
                    dataList[x][1] = (Number) maxArray[x];
                  }

                  rs.setRangeData(dataList);
                } else {
                  rs.setRangeData(new Number[0][0]);
                }
              }
            }
          });
    } // for

    final boolean finalNeedsRedraw = needsRedraw;
    final String[] finalTimeStamps = timeStamps;
    final ChartButton finalChartButton = chartButton;

    managerUI.access(
        new Runnable() {
          @Override
          public void run() {
            // Here the UI is locked and can be updated

            if (finalNeedsRedraw) {
              ManagerUI.log("ChartsLayout needsRedraw");

              if (finalTimeStamps != null) {
                XAxis xAxis = configuration.getxAxis();
                Labels labels = new Labels();
                labels.setRotation(-45);
                labels.setAlign(HorizontalAlign.RIGHT);
                xAxis.setLabels(labels);
                xAxis.setCategories(finalTimeStamps);
              }

              chart.drawChart(configuration);
              finalChartButton.setVisible(true);
            }
          }
        });
  }