/**
   * Sets the flag for the given dashboard.
   *
   * @param flag
   * @param context
   * @param dbName
   * @param value
   * @param commit
   */
  public void setDashboardFlag(
      int flag, ResourceId context, String dbName, boolean value, boolean commit) {
    // get views first
    DataViewInfoData[] data = getDataViewInfoData(context, dbName);
    if (data != null) {
      ResourceId rid;
      DataViewInfoData d;
      DataViewInfo dvinfo;
      for (int i = 0; i < data.length; i++) {
        d = data[i];
        dvinfo = d.getInfo();
        rid = d.getContext();
        model
            .getDataViewHelper()
            .setDataViewFlag(flag, rid, dvinfo.getDataView().getName(), value, commit);
      }
    }

    // now work with counters
    CounterInfoData[] counters = getCounterInfoData(context, dbName);
    if (!Utils.isEmptyArray(counters)) {
      // check that all counters are present
      for (CounterInfoData cid : counters) {
        ResourceInfo cInfo = model.getCounterHelper().getCounterInfo(cid.counterId);
        if (cInfo != null) {
          switch (flag) {
            case DashboardInfo.ENABLED:
              // if enabled is false then change the flag only for
              // counters which are not committed (this is to avoid a query
              // disabling the commited counters of another one)
              // @see QueryRealizerLiveSession for an excuse
              if (value || !cInfo.getCounterInfo().isCommitted()) {
                model
                    .getCounterHelper()
                    .setCounterFlag(cid.counterId, CounterInfo.ENABLED, value, true);
              }
              break;
            case DashboardInfo.ACTIVATED:
              model
                  .getCounterHelper()
                  .setCounterFlag(cid.counterId, CounterInfo.ACTIVATED, value, true);
          }
        }
      }
    }

    ArtefactInfoContainerImpl ac = model.getArtefactContainerImplForResource(context, true);
    if (ac == null) {
      if (logger.isTraceEnabled()) {
        logger.error("Couldn't find container for dashboard " + context);
      }
      return;
    }

    // enable the dashboard
    ac.setDashboardFlag(flag, dbName, value, commit);
    // refresh context node
    model.refreshNode(context);
  }
 /**
  * @see com.ixora.rms.client.model.DashboardModelHelper#removeDashboard(com.ixora.rms.ResourceId,
  *     java.lang.String)
  */
 public void removeDashboard(ResourceId id, String dbName) {
   ArtefactInfoContainerImpl ac = model.getArtefactContainerImplForResource(id, false);
   if (ac == null) {
     if (logger.isTraceEnabled()) {
       logger.error("Couldn't find container for: " + id);
     }
     return;
   }
   ac.removeDashboard(dbName);
   model.refreshNode(id);
 }
 /**
  * @see com.ixora.rms.client.model.DashboardModelHelper#addDashboard(com.ixora.rms.ResourceId,
  *     com.ixora.rms.repository.QueryGroup)
  */
 public void addDashboard(ResourceId id, Dashboard group) {
   ArtefactInfoContainerImpl ac = model.getArtefactContainerImplForResource(id, false);
   if (ac == null) {
     if (logger.isTraceEnabled()) {
       logger.error("Couldn't find container for: " + id);
     }
     return;
   }
   ac.addDashboard(group);
   recalculateDashboardsStatus(id);
   model.refreshNode(id);
 }
 /**
  * Sets the dashboards associated with the given resource.
  *
  * @param id a valid, non regex resource id
  * @param q
  */
 public void setDashboards(ResourceId id, Dashboard[] groups) {
   ArtefactInfoContainerImpl qc = model.getArtefactContainerImplForResource(id, true);
   if (qc == null) {
     if (logger.isTraceEnabled()) {
       logger.error("Couldn't find query container for: " + id.toString());
     }
     return;
   }
   qc.setDashboards(groups);
   recalculateDashboardsStatus(id);
   model.refreshNode(id);
 }
 /**
  * @see
  *     com.ixora.rms.client.model.DashboardModelHelper#commitDashboard(com.ixora.rms.internal.ResourceId,
  *     java.lang.String)
  */
 public void commitDashboard(ResourceId context, String dashboard) {
   ArtefactInfoContainerImpl qc = model.getArtefactContainerImplForResource(context, true);
   if (qc == null) {
     return;
   }
   DashboardInfoImpl di = qc.getDashboardInfoImpl(dashboard);
   if (di == null) {
     if (logger.isTraceEnabled()) {
       logger.error("Couldn't find dashboard info for: " + di);
     }
     return;
   }
   di.commit();
 }
 /**
  * Returns the counter info for all counters for all dashboards in the given context
  *
  * @param context
  * @param dbName
  * @return
  */
 private CounterInfoData[] getCounterInfoData(ResourceId context) {
   ArtefactInfoContainerImpl ac = model.getArtefactContainerImplForResource(context, true);
   if (ac == null) {
     if (logger.isTraceEnabled()) {
       logger.error("Couldn't find query group container for: " + context.toString());
     }
     return null;
   }
   Collection<DashboardInfo> cs = ac.getDashboardInfo();
   if (cs == null) {
     return null;
   }
   List<CounterInfoData> ret = new LinkedList<CounterInfoData>();
   for (DashboardInfo dinfo : cs) {
     CounterInfoData[] counters = getCounterInfoData(context, dinfo.getDashboard().getName());
     if (counters != null) {
       ret.addAll(Arrays.asList(counters));
     }
   }
   return ret.toArray(new CounterInfoData[ret.size()]);
 }
  /** @param context */
  public void rollbackDashboards(ResourceId context) {
    // rollback data views
    DataViewInfoData[] data = getDataViewInfoData(context);
    if (data != null) {
      ResourceId rid;
      DataViewInfoData d;
      DataViewInfo dvinfo;
      for (int i = 0; i < data.length; i++) {
        d = data[i];
        dvinfo = d.getInfo();
        rid = d.getContext();
        if (!dvinfo.isCommitted()) {
          model.getDataViewHelper().rollbackDataView(rid, dvinfo.getDataView().getName());
        }
      }
    }

    // rollback counters
    CounterInfoData[] counters = getCounterInfoData(context);
    if (!Utils.isEmptyArray(counters)) {
      // check that all counters are present
      for (CounterInfoData cid : counters) {
        model.getCounterHelper().rollbackCounter(cid.counterId, true);
      }
    }

    ArtefactInfoContainerImpl qc = model.getArtefactContainerImplForResource(context, true);
    if (qc == null) {
      if (logger.isTraceEnabled()) {
        logger.error("Couldn't find container for: " + context);
      }
      return;
    }
    Collection<DashboardInfoImpl> dis = qc.getDashboardInfoImpl();
    if (!Utils.isEmptyCollection(dis)) {
      for (DashboardInfoImpl di : dis) {
        di.rollback();
      }
    }
  }
  // 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();
      }
    }
  }