@Override
  public void deletePortletRepository(long groupId, String portletId)
      throws PortalException, SystemException {

    Repository repository = RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);

    if (repository != null) {
      RepositoryLocalServiceUtil.deleteRepository(repository.getRepositoryId());
    }
  }
  @Override
  public Repository addPortletRepository(
      long groupId, String portletId, ServiceContext serviceContext)
      throws PortalException, SystemException {

    Repository repository = RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);

    if (repository != null) {
      return repository;
    }

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());

    long classNameId = PortalUtil.getClassNameId(LiferayRepository.class.getName());

    UnicodeProperties typeSettingsProperties = new UnicodeProperties();

    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();

    try {
      DLAppHelperThreadLocal.setEnabled(false);

      return RepositoryLocalServiceUtil.addRepository(
          user.getUserId(),
          groupId,
          classNameId,
          DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
          portletId,
          StringPool.BLANK,
          portletId,
          typeSettingsProperties,
          true,
          serviceContext);
    } finally {
      DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
    }
  }
  @Override
  public Repository fetchPortletRepository(long groupId, String portletId) throws SystemException {

    return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Repository repository)
      throws Exception {

    long userId = portletDataContext.getUserId(repository.getUserUuid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(repository);

    Repository importedRepository = null;

    Element repositoryElement = portletDataContext.getImportDataStagedModelElement(repository);

    try {
      boolean hidden = GetterUtil.getBoolean(repositoryElement.attributeValue("hidden"));

      if (portletDataContext.isDataStrategyMirror()) {
        Repository existingRepository =
            RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(
                repository.getUuid(), portletDataContext.getScopeGroupId());

        if (existingRepository == null) {
          existingRepository =
              RepositoryLocalServiceUtil.fetchRepository(
                  portletDataContext.getScopeGroupId(), repository.getName());
        }

        long classNameId = 0;

        if (existingRepository != null) {
          classNameId = existingRepository.getClassNameId();
        }

        if ((existingRepository == null)
            || (classNameId != PortalUtil.getClassNameId(LiferayRepository.class))) {

          serviceContext.setUuid(repository.getUuid());

          importedRepository =
              RepositoryLocalServiceUtil.addRepository(
                  userId,
                  portletDataContext.getScopeGroupId(),
                  repository.getClassNameId(),
                  DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
                  repository.getName(),
                  repository.getDescription(),
                  repository.getPortletId(),
                  repository.getTypeSettingsProperties(),
                  hidden,
                  serviceContext);
        } else {
          RepositoryLocalServiceUtil.updateRepository(
              existingRepository.getRepositoryId(),
              repository.getName(),
              repository.getDescription());

          importedRepository = existingRepository;
        }
      } else {
        importedRepository =
            RepositoryLocalServiceUtil.addRepository(
                userId,
                portletDataContext.getScopeGroupId(),
                repository.getClassNameId(),
                DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
                repository.getName(),
                repository.getDescription(),
                repository.getPortletId(),
                repository.getTypeSettingsProperties(),
                hidden,
                serviceContext);
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "Unable to connect to repository {name="
                + repository.getName()
                + ", typeSettings="
                + repository.getTypeSettingsProperties()
                + "}",
            e);
      }
    }

    portletDataContext.importClassedModel(repository, importedRepository);

    StagedModelDataHandlerUtil.importReferenceStagedModels(
        portletDataContext, repository, RepositoryEntry.class);
  }