@FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result delete(Context context) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    // TODO FIX ME locking until database modification done
    if (taskManager.isGoogleRunning()) {
      flash.error("admin.google.errorTaskRunning");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    List<GoogleTarget> targets = googleDB.target.list(Arrays.asList(group.getId()));
    for (GoogleTarget target : targets) {
      googleDB.targetSummary.deleteByTarget(target.getId());
      googleDB.rank.deleteByTarget(group.getId(), target.getId());
      googleDB.target.delete(target.getId());
    }

    List<GoogleSearch> searches = googleDB.search.listByGroup(Arrays.asList(group.getId()));
    for (GoogleSearch search : searches) {
      deleteSearch(group, search);
    }

    baseDB.event.delete(group);
    baseDB.user.delPerm(group);
    if (!baseDB.group.delete(group)) {
      flash.error("admin.google.failedDeleteGroup");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    } else {
      flash.success("admin.google.groupDeleted");
      return Results.redirect(router.getReverseRoute(GroupController.class, "groups"));
    }
  }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result renameTarget(
      Context context, @Param("name") String name, @Param("id") Integer targetId) {

    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    GoogleTarget target = getTarget(context, targetId);
    if (target == null) {
      flash.error("error.invalidWebsite");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    if (name != null) {
      name = name.replaceAll("(^\\s+)|(\\s+$)", "");
    }

    if (Validator.isEmpty(name)) {
      flash.error("error.invalidName");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    target.setName(name);
    googleDB.target.rename(target);

    flash.success("google.group.websiteRenamed");
    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
  }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result delTarget(Context context, @Params("id[]") String[] ids) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    // TODO FIX ME locking until database modification done
    if (taskManager.isGoogleRunning()) {
      flash.error("admin.google.errorTaskRunning");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    if (ids == null || ids.length == 0) {
      flash.error("error.noWebsiteSelected");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    for (String id : ids) {
      GoogleTarget target = null;
      try {
        target = getTarget(context, Integer.parseInt(id));
      } catch (Exception ex) {
        target = null;
      }

      if (target == null) {
        flash.error("error.invalidWebsite");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      googleDB.targetSummary.deleteByTarget(target.getId());
      googleDB.rank.deleteByTarget(group.getId(), target.getId());
      googleDB.target.delete(target.getId());
    }

    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
  }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result addTarget(
      Context context,
      @Param("target-radio") String targetType,
      @Params("name[]") String[] names,
      @Params("pattern[]") String[] patterns) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    if (targetType == null
        || names == null
        || names.length == 0
        || patterns == null
        || patterns.length == 0
        || names.length != patterns.length) {
      flash.error("error.invalidParameters");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    Set<GoogleTarget> targets = new HashSet<>();
    for (int i = 0; i < names.length; i++) {
      String name = names[i];
      String pattern = patterns[i];

      if (name != null) {
        name = name.replaceAll("(^\\s+)|(\\s+$)", "");
      }

      if (pattern != null) {
        pattern = pattern.replaceAll("(^\\s+)|(\\s+$)", "");
      }

      if (Validator.isEmpty(name)) {
        flash.error("error.invalidName");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      PatternType type = null;
      try {
        type = PatternType.valueOf(targetType);
      } catch (Exception ex) {
        flash.error("error.invalidTargetType");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      if (PatternType.DOMAIN.equals(type) || PatternType.SUBDOMAIN.equals(type)) {
        try {
          pattern = IDN.toASCII(pattern);
        } catch (Exception ex) {
          pattern = null;
        }
      }

      if (!GoogleTarget.isValidPattern(type, pattern)) {
        flash.error("error.invalidPattern");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }

      targets.add(new GoogleTarget(group.getId(), name, type, pattern));
    }

    if (googleDB.target.insert(targets) < 1) {
      flash.error("error.internalError");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }
    googleDB.serpRescan.rescan(null, targets, getSearches(context), true);

    Run runningGoogleTask = taskManager.getRunningGoogleTask();
    if (runningGoogleTask != null) {
      flash.put(
          "warning",
          msg.get(
                  "google.group.websiteInsertedWhileRun",
                  context,
                  Optional.absent(),
                  runningGoogleTask.getId())
              .or(""));
    } else {
      flash.success("google.group.websiteInserted");
    }

    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
  }