@Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    Team newTeam = _persistence.create(pk);

    newTeam.setMvccVersion(RandomTestUtil.nextLong());

    newTeam.setCompanyId(RandomTestUtil.nextLong());

    newTeam.setUserId(RandomTestUtil.nextLong());

    newTeam.setUserName(RandomTestUtil.randomString());

    newTeam.setCreateDate(RandomTestUtil.nextDate());

    newTeam.setModifiedDate(RandomTestUtil.nextDate());

    newTeam.setGroupId(RandomTestUtil.nextLong());

    newTeam.setName(RandomTestUtil.randomString());

    newTeam.setDescription(RandomTestUtil.randomString());

    _teams.add(_persistence.update(newTeam));

    Team existingTeam = _persistence.findByPrimaryKey(newTeam.getPrimaryKey());

    Assert.assertEquals(existingTeam.getMvccVersion(), newTeam.getMvccVersion());
    Assert.assertEquals(existingTeam.getTeamId(), newTeam.getTeamId());
    Assert.assertEquals(existingTeam.getCompanyId(), newTeam.getCompanyId());
    Assert.assertEquals(existingTeam.getUserId(), newTeam.getUserId());
    Assert.assertEquals(existingTeam.getUserName(), newTeam.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingTeam.getCreateDate()),
        Time.getShortTimestamp(newTeam.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingTeam.getModifiedDate()),
        Time.getShortTimestamp(newTeam.getModifiedDate()));
    Assert.assertEquals(existingTeam.getGroupId(), newTeam.getGroupId());
    Assert.assertEquals(existingTeam.getName(), newTeam.getName());
    Assert.assertEquals(existingTeam.getDescription(), newTeam.getDescription());
  }
  public void addPermissions(String resourceName, long resourcePK)
      throws PortalException, SystemException {

    if (!MapUtil.getBoolean(_parameterMap, PortletDataHandlerKeys.PERMISSIONS)) {

      return;
    }

    List<KeyValuePair> permissions = new ArrayList<KeyValuePair>();

    Group group = GroupLocalServiceUtil.getGroup(_groupId);

    List<Role> roles = RoleLocalServiceUtil.getRoles(_companyId);

    PrimitiveLongList roleIds = new PrimitiveLongList(roles.size());
    Map<Long, String> roleIdsToNames = new HashMap<Long, String>();

    for (Role role : roles) {
      int type = role.getType();

      if ((type == RoleConstants.TYPE_REGULAR)
          || ((type == RoleConstants.TYPE_ORGANIZATION) && group.isOrganization())
          || ((type == RoleConstants.TYPE_SITE)
              && (group.isLayoutSetPrototype() || group.isSite()))) {

        String name = role.getName();

        roleIds.add(role.getRoleId());
        roleIdsToNames.put(role.getRoleId(), name);
      } else if ((type == RoleConstants.TYPE_PROVIDER) && role.isTeam()) {
        Team team = TeamLocalServiceUtil.getTeam(role.getClassPK());

        if (team.getGroupId() == _groupId) {
          String name = PermissionExporter.ROLE_TEAM_PREFIX + team.getName();

          roleIds.add(role.getRoleId());
          roleIdsToNames.put(role.getRoleId(), name);
        }
      }
    }

    List<String> actionIds = ResourceActionsUtil.getModelResourceActions(resourceName);

    Map<Long, Set<String>> roleIdsToActionIds =
        getActionIds(_companyId, roleIds.getArray(), resourceName, resourcePK, actionIds);

    for (Map.Entry<Long, String> entry : roleIdsToNames.entrySet()) {
      long roleId = entry.getKey();
      String name = entry.getValue();

      Set<String> availableActionIds = roleIdsToActionIds.get(roleId);

      if ((availableActionIds == null) || availableActionIds.isEmpty()) {
        continue;
      }

      KeyValuePair permission = new KeyValuePair(name, StringUtil.merge(availableActionIds));

      permissions.add(permission);
    }

    _permissionsMap.put(getPrimaryKeyString(resourceName, resourcePK), permissions);
  }