protected List<ResourceTypePermission> getResourceTypePermissions(
      PortletDataContext portletDataContext, Role importedRole) throws SystemException {

    DynamicQuery dynamicQuery = ResourceTypePermissionLocalServiceUtil.dynamicQuery();

    Property companyIdProperty = PropertyFactoryUtil.forName("companyId");

    dynamicQuery.add(companyIdProperty.eq(portletDataContext.getCompanyId()));

    Junction junction = RestrictionsFactoryUtil.disjunction();

    long[] permissibleGroupIds = {
      GroupConstants.DEFAULT_PARENT_GROUP_ID,
      portletDataContext.getCompanyId(),
      portletDataContext.getCompanyGroupId(),
      portletDataContext.getUserPersonalSiteGroupId()
    };

    for (long permissibleGroupId : permissibleGroupIds) {
      Property property = PropertyFactoryUtil.forName("groupId");

      junction.add(property.eq(permissibleGroupId));
    }

    dynamicQuery.add(junction);

    Property roleIdProperty = PropertyFactoryUtil.forName("roleId");

    dynamicQuery.add(roleIdProperty.eq(importedRole.getRoleId()));

    return ResourceTypePermissionLocalServiceUtil.dynamicQuery(dynamicQuery);
  }
  protected void deleteRolePermissions(PortletDataContext portletDataContext, Role importedRole)
      throws SystemException {

    List<ResourcePermission> resourcePermissions =
        ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(
            importedRole.getRoleId(),
            new int[] {ResourceConstants.SCOPE_COMPANY, ResourceConstants.SCOPE_GROUP_TEMPLATE},
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS);

    for (ResourcePermission resourcePermission : resourcePermissions) {
      ResourcePermissionLocalServiceUtil.deleteResourcePermission(resourcePermission);
    }

    List<ResourcePermission> groupResourcePermissions =
        ResourcePermissionLocalServiceUtil.getRoleResourcePermissions(
            importedRole.getRoleId(),
            new int[] {ResourceConstants.SCOPE_GROUP},
            QueryUtil.ALL_POS,
            QueryUtil.ALL_POS);

    for (ResourcePermission groupResourcePermission : groupResourcePermissions) {

      long groupId = GetterUtil.getLong(groupResourcePermission.getPrimKey());

      if ((groupId == portletDataContext.getCompanyGroupId())
          || (groupId == portletDataContext.getUserPersonalSiteGroupId())) {

        ResourcePermissionLocalServiceUtil.deleteResourcePermission(groupResourcePermission);
      }
    }

    List<ResourceTypePermission> resourceTypePermissions =
        getResourceTypePermissions(portletDataContext, importedRole);

    for (ResourceTypePermission resourceTypePermission : resourceTypePermissions) {

      ResourceTypePermissionLocalServiceUtil.deleteResourceTypePermission(resourceTypePermission);
    }
  }