@Override
  public String doExportData(
      PortletDataContext context, String portletId, PortletPreferences preferences)
      throws PortletDataException {

    long communityId = context.getScopeGroupId();

    LOGGER.debug(
        "Exporting CRUD portlet data for portletId {}, communityId {}", portletId, communityId);

    try {
      preferences.setValue("VERSION", String.valueOf(EXPORT_VERSION));

      if (configurationService.hasConfigData(portletId, communityId)) {

        exportRolesAndPermissions(context, portletId, preferences, communityId);

        exportConfig(context, portletId, communityId);

        preferences.store();

        ConfigurationMetaData metaData =
            configurationService.getConfigurationMetaData(portletId, communityId);
        return metaData.getFileName();
      } else {
        preferences.store();
        return "Keine Konfiguration vorhanden";
      }

    } catch (Exception e) {
      throw new PortletDataException("Error exporting portlet " + portletId + "/" + communityId, e);
    }
  }
  private void exportRolesAndPermissions(
      PortletDataContext context,
      String portletId,
      PortletPreferences preferences,
      long communityId)
      throws ReadOnlyException, PortalException, SystemException {

    PortletConfig portletConfig =
        configurationService.getPortletConfig(portletId, communityId).getPortletConfig();

    if (portletConfig.getRoles() != null) {
      List<RoleConfig> roleList = collectPortletRoles(portletConfig.getRoles().getRole());
      String[] roleNames = new String[roleList.size()];
      int idx = 0;
      for (RoleConfig role : roleList) {
        long primkey =
            configurationService.readRoleResourceIdPrimKey(portletId, communityId, role.getName());

        roleNames[idx++] = role.getName();

        preferences.setValue(ROLE_PREF_PREFIX + String.valueOf(primkey), role.getName());

        context.addPermissions(PortletRole.RESOURCE_KEY, primkey);
      }
      preferences.setValues(ROLE_NAMES_PREF, roleNames);
    }
  }
  @Override
  public PortletPreferences doImportData(
      PortletDataContext context, String portletId, PortletPreferences preferences, String data)
      throws PortletDataException {

    long communityId = context.getScopeGroupId();

    LOGGER.debug(
        "Importing CRUD portlet data for portletId {}, communityId {}", portletId, communityId);

    checkCompatibility(preferences);

    try {
      ZipReader zipReader = context.getZipReader();
      byte[] portletConfigData =
          zipReader.getEntryAsByteArray(portletId + "/portletConfiguration.xml");

      if (portletConfigData != null) {
        configurationService.storeConfigurationFile(
            data, portletConfigData, portletId, communityId, "import");

        Map<String, Long> newRoleIds = createResourceRoles(portletId, communityId, preferences);

        importPermissions(context, preferences, newRoleIds);
      }

      return null;

    } catch (RuntimeException e) {
      throw new PortletDataException("Error importing portlet " + portletId + "/" + communityId, e);
    }
  }
 private void exportConfig(PortletDataContext context, String portletId, long communityId)
     throws IOException {
   String configString = configurationService.readConfigAsString(portletId, communityId);
   if (configString != null) {
     ZipWriter zipWriter = context.getZipWriter();
     String filePath = portletId + "/portletConfiguration.xml";
     zipWriter.addEntry(filePath, configString);
   }
 }
  /**
   * Löscht die Rollen-Ressourcen, so dass beim nächsten Import die Berechtigungen erneut importiert
   * werden.
   *
   * <p>{@inheritDoc}
   */
  @Override
  public PortletPreferences deleteData(
      PortletDataContext context, String portletId, PortletPreferences preferences)
      throws PortletDataException {

    long communityId = context.getScopeGroupId();

    configurationService.removeExistingRoleResourceIds(portletId, communityId);

    return super.deleteData(context, portletId, preferences);
  }
 private Map<String, Long> createResourceRoles(
     String portletId, long communityId, PortletPreferences preferences) {
   String[] roleNames = preferences.getValues(ROLE_NAMES_PREF, new String[0]);
   Map<String, Long> newRoleIds = new HashMap<String, Long>(roleNames.length);
   for (String roleName : roleNames) {
     Long newId = configurationService.storeRoleResourceId(portletId, communityId, roleName);
     newRoleIds.put(roleName, newId);
     LOGGER.debug("Creating resource for role {}, newId: {}", roleName, newId);
   }
   return newRoleIds;
 }