private DashboardState storeDashboardState(final DashboardState dashboardState) {
    Assertions.notNull("dashboardState", dashboardState);

    final DashboardId dashboardId = dashboardState.getId();
    final long portalPageId = toLong(dashboardId);

    // check first if the portal page exists!
    final PortalPage portalPage = portalPageStore.getPortalPage(portalPageId);
    if (portalPage == null) {
      throw new DashboardStateStoreException("No portal page found with id '" + portalPageId + "'");
    }
    updatePortalPage(portalPage, dashboardState);

    final Map<Long, PortletConfiguration> oldPortlets =
        getCurrentPortletConfigurationsMap(portalPageId);
    for (DashboardState.ColumnIndex columnIndex : dashboardState.getLayout().getColumnRange()) {
      int row = 0;
      for (final GadgetState gadgetState : dashboardState.getGadgetsInColumn(columnIndex)) {
        final long gadgetId = toLong(gadgetState.getId());
        // update existing portlets
        if (oldPortlets.containsKey(gadgetId)) {
          final PortletConfiguration oldPortletConfiguration = oldPortlets.get(gadgetId);
          oldPortletConfiguration.setColumn(columnIndex.index());
          oldPortletConfiguration.setRow(row++);
          oldPortletConfiguration.setColor(gadgetState.getColor());
          oldPortletConfiguration.setUserPrefs(gadgetState.getUserPrefs());
          portletConfigurationStore.store(oldPortletConfiguration);
          oldPortlets.remove(gadgetId);
        } else {
          portletConfigurationStore.addGadget(
              portalPageId,
              gadgetId,
              columnIndex.index(),
              row++,
              gadgetState.getGadgetSpecUri(),
              gadgetState.getColor(),
              gadgetState.getUserPrefs());
        }
      }
    }

    // delete any portlets left over in the oldPortlets map
    for (PortletConfiguration existingPortlet : oldPortlets.values()) {
      portletConfigurationStore.delete(existingPortlet);
    }

    return retrieve(dashboardId);
  }
 private Map<Long, PortletConfiguration> getCurrentPortletConfigurationsMap(
     final Long portalPageId) {
   final Map<Long, PortletConfiguration> ret = new HashMap<Long, PortletConfiguration>();
   final List<PortletConfiguration> list = portletConfigurationStore.getByPortalPage(portalPageId);
   for (PortletConfiguration portletConfiguration : list) {
     ret.put(portletConfiguration.getId(), portletConfiguration);
   }
   return ret;
 }
  public DashboardState findDashboardWithGadget(final GadgetId gadgetId)
      throws DashboardNotFoundException {
    Assertions.notNull("gagdetId", gadgetId);

    try {
      final PortletConfiguration portletConfiguration =
          portletConfigurationStore.getByPortletId(toLong(gadgetId));
      if (portletConfiguration != null) {
        return retrieve(
            DashboardId.valueOf(Long.toString(portletConfiguration.getDashboardPageId())));
      } else {
        throw new DashboardStateStoreException("Gadget with id '" + gadgetId + "' not found!");
      }
    } catch (DataAccessException e) {
      throw new DashboardStateStoreException(
          "Error looking up gadget with id '" + gadgetId + "'.", e);
    }
  }