public void doUpgrade(boolean setupMode) throws Exception {
    // find all portalpages that have no version set yet.
    final OfBizListIterator iterator =
        delegator.findListIteratorByCondition(
            OfBizPortalPageStore.Table.NAME,
            new EntityExpr(OfBizPortalPageStore.Column.VERSION, EntityOperator.EQUALS, null));
    final List<Long> portalPageIds = new ArrayList<Long>();
    try {
      GenericValue portalPageGv = iterator.next();
      while (portalPageGv != null) {
        portalPageIds.add(portalPageGv.getLong(OfBizPortalPageStore.Column.ID));
        portalPageGv = iterator.next();
      }
    } finally {
      iterator.close();
    }

    try {
      // set version to 0 for all dashboard pages returned by the previous query.
      final int rowsUpdated =
          delegator.bulkUpdateByPrimaryKey(
              OfBizPortalPageStore.Table.NAME,
              MapBuilder.<String, Object>newBuilder()
                  .add(OfBizPortalPageStore.Column.VERSION, 0L)
                  .toMap(),
              portalPageIds);
      log.info("Initialised " + rowsUpdated + " dashboard versions to 0.");
    } finally {
      portalPageStore.flush();
    }
  }
 private void updatePortalPage(final PortalPage portalPage, final DashboardState dashboardState) {
   // update the portalPageStore's title and layout if they changed.
   if (!portalPage.getLayout().equals(dashboardState.getLayout())
       || !StringUtils.equals(portalPage.getName(), dashboardState.getTitle())) {
     final PortalPage.Builder builder = PortalPage.portalPage(portalPage);
     builder.name(dashboardState.getTitle());
     builder.layout(dashboardState.getLayout());
     portalPageStore.update(builder.build());
   }
 }
  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);
  }