/**
  * Returns the view info for all views in the given group in the given context.
  *
  * @param context
  * @param dbName
  * @return
  */
 private DataViewInfoData[] getDataViewInfoData(ResourceId context, String dbName) {
   DashboardInfo dinfo = model.getDashboardInfo(new DashboardId(context, dbName), true);
   if (dinfo == null) {
     return null;
   }
   List<DataViewInfoData> ret = new LinkedList<DataViewInfoData>();
   Dashboard db = dinfo.getDashboard();
   DataViewId[] views = db.getViews();
   if (views == null) {
     return null;
   }
   // find now query info on every member query
   DataViewId m;
   DataViewInfo dvinfo;
   for (int i = 0; i < views.length; i++) {
     m = views[i];
     if (context != null) {
       m = m.complete(context);
     }
     dvinfo = model.getDataViewInfo(m, true);
     if (dvinfo == null) {
       if (logger.isTraceEnabled()) {
         logger.error("Dataview " + m + " not found for dashboard " + db.getName());
       }
       continue;
     }
     ret.add(new DataViewInfoData(m.getContext(), dvinfo));
   }
   return (DataViewInfoData[]) ret.toArray(new DataViewInfoData[ret.size()]);
 }
 /**
  * Returns the view info for all counters that are part of the given dashboard
  *
  * @param context
  * @param dbName
  * @return
  */
 private CounterInfoData[] getCounterInfoData(ResourceId context, String dbName) {
   DashboardInfo dinfo = model.getDashboardInfo(new DashboardId(context, dbName), true);
   if (dinfo == null) {
     return null;
   }
   List<CounterInfoData> ret = new LinkedList<CounterInfoData>();
   Dashboard db = dinfo.getDashboard();
   ResourceId[] counters = db.getCounters();
   if (counters == null) {
     return null;
   }
   // find now query info on every member query
   ResourceId m;
   CounterInfo cinfo;
   for (int i = 0; i < counters.length; i++) {
     m = counters[i];
     if (context != null) {
       m = m.complete(context);
     }
     cinfo = model.getCounterInfo(m, true);
     if (cinfo == null) {
       if (logger.isTraceEnabled()) {
         logger.error("Counter " + m + " not found for dashboard " + db.getName());
       }
       continue;
     }
     ret.add(new CounterInfoData(m, cinfo));
   }
   return ret.toArray(new CounterInfoData[ret.size()]);
 }
 /**
  * @param context
  * @param dashboard
  */
 private void handleSavedDashboard(ResourceId context, Dashboard dashboard) {
   // add to model only if the data view applies for the given context
   String version = fSessionData.getAgentVersionInContext(context);
   // add to model only if:
   // 1. outside of an agent context
   // 2. dashboard applies to version in context
   if (version == null || dashboard.appliesToAgentVersion(version)) {
     fSessionData.getDashboardHelper().addDashboard(context, dashboard);
   } else {
     // otherwise remove
     fSessionData.getDashboardHelper().removeDashboard(context, dashboard.getName());
   }
   // refresh table model
   refreshTableModel();
 }
  /**
   * @see
   *     com.ixora.rms.client.model.DashboardModelHelper#isDashboardReady(com.ixora.rms.internal.ResourceId,
   *     com.ixora.rms.repository.QueryGroup)
   */
  public boolean isDashboardReady(ResourceId context, Dashboard dashboard) {
    boolean ready = true;
    ResourceId[] counters = dashboard.getCounters();
    if (!Utils.isEmptyArray(counters)) {
      // check that all counters are present
      for (ResourceId c : counters) {
        if (context != null) {
          c = c.complete(context);
        }
        CounterInfo cinfo = model.getCounterInfo(c, true);
        if (cinfo == null) {
          return false;
        }
      }
    }

    // now check that all views are ready
    DataViewId[] views = dashboard.getViews();
    if (ready && !Utils.isEmptyArray(views)) {
      // find now query info on every member query
      for (DataViewId m : views) {
        if (context != null) {
          m = m.complete(context);
        }
        DataViewInfo dvinfo = model.getDataViewInfo(m, true);
        if (dvinfo == null) {
          return false;
        }
        if (!model
            .getQueryHelper()
            .isQueryReady(m.getContext(), dvinfo.getDataView().getQueryDef())) {
          return false;
        }
      }
    }
    return ready;
  }
  // the behaviour is as follows:
  // if the dashboard is not commited, the state will not be changed
  // else the state of the dasboard will be updated only if there is at least
  // one view already in the session model that belongs to this dashboard and
  // it is not enabled
  public void recalculateDashboardsStatus(ResourceId context) {
    ArtefactInfoContainerImpl acimpl = model.getArtefactContainerImplForResource(context, true);
    if (acimpl == null) {
      if (logger.isTraceEnabled()) {
        logger.error("Couldn't find container for: " + context);
      }
      return;
    }
    Collection<DashboardInfoImpl> cs = acimpl.getDashboardInfoImpl();
    if (cs == null) {
      return;
    }

    for (DashboardInfoImpl dinfo : cs) {
      if (dinfo.isCommitted()) {
        Dashboard db = dinfo.getDashboard();
        // flags to update
        boolean enabled = dinfo.getFlag(DashboardInfo.ENABLED);
        boolean plotted = dinfo.getFlag(DashboardInfo.ACTIVATED);
        boolean committed = dinfo.isCommitted();

        // check counters
        ResourceId[] counters = db.getCounters();
        if (!Utils.isEmptyArray(counters)) {
          // disable this dashboard only if one of it's counters
          // exists in the model and it's disabled
          for (ResourceId c : counters) {
            if (context != null) {
              c = c.complete(context);
            }
            CounterInfo cinfo = model.getCounterInfo(c, false);
            if (cinfo == null) {
              if (logger.isTraceEnabled()) {
                logger.error("Couldn't find counter: " + c);
              }
              break;
            }
            if (!cinfo.getFlag(CounterInfo.ENABLED)) {
              enabled = false;
            }
            if (!cinfo.getFlag(CounterInfo.ACTIVATED)) {
              plotted = false;
            }
            if (!cinfo.isCommitted()) {
              committed = false;
            }
          }
        }

        // check data views
        DataViewId[] views = db.getViews();
        if (!Utils.isEmptyArray(views)) {
          // disable this dashboard only if one of it's queries
          // exists in the model and it's disabled

          // find now info on every member
          DataViewId m;
          DataViewInfo dvinfo;
          for (int i = 0; i < views.length; i++) {
            m = views[i];
            if (context != null) {
              m = m.complete(context);
            }
            // refresh data views first
            model.getDataViewHelper().recalculateDataViewsStatus(m.getContext());
            dvinfo = model.getDataViewInfo(m, false);
            if (dvinfo == null) {
              // this query no longer exists...
              // ignore it with a warning in logs
              if (logger.isInfoEnabled()) {
                logger.error(
                    "Couldn't find view info for: "
                        + m
                        + ". Dashboard "
                        + dinfo.getTranslatedName()
                        + " will be incomplete.");
              }
              break;
            }
            // query exists and it will contribute to the state
            // of this dashboard
            if (!dvinfo.getFlag(QueryInfo.ENABLED)) {
              enabled = false;
            }
            if (!dvinfo.getFlag(QueryInfo.ACTIVATED)) {
              plotted = false;
            }
            if (!dvinfo.isCommitted()) {
              committed = false;
            }
          }
        }

        dinfo.setFlag(DashboardInfo.ENABLED, enabled);
        dinfo.setFlag(DashboardInfo.ACTIVATED, plotted);
        if (committed) {
          dinfo.commit();
        }
      } else {
        // just commit it
        dinfo.commit();
      }
    }
  }