@RequestMapping(
      method = RequestMethod.POST,
      value = "/new",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(value = "Add a Refset) ")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> addRefset(@RequestBody Refset r)
      throws Exception {

    logger.debug("Adding refsets {}", r);
    validateRefset(r);
    Set<Member> ms = new HashSet<Member>();
    ms.addAll(r.getMembers());
    if (ms != null && !ms.isEmpty()) {

      validateMembers(ms);
    }
    Result<Map<String, Object>> result = getResult();

    addMetaDetails(r);
    r.setCreatedBy(getUserDetails().getUsername());
    r.setModifiedBy(getUserDetails().getUsername());

    List<Member> members = r.getMembers();
    for (Member member : members) {

      member.setCreatedBy(getUserDetails().getUsername());
      member.setModifiedBy(getUserDetails().getUsername());
    }
    aService.addRefset(r);

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("uuid", r.getUuid());
    result
        .getMeta()
        .add(
            linkTo(methodOn(RefsetBrowseController.class).getRefsetDetails(r.getUuid()))
                .withRel("Refset"));

    result.setData(data);

    result.getMeta().setMessage(SUCCESS);
    result.getMeta().setStatus(HttpStatus.CREATED);

    return new ResponseEntity<Result<Map<String, Object>>>(result, HttpStatus.CREATED);
  }
  @RequestMapping(
      method = RequestMethod.POST,
      value = "/{refSetId}/add/members",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(
      value =
          "Add no of members in this call. It is required that member supplied is a valid member")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> addMembers(
      @PathVariable(value = "refSetId") String refsetId,
      @RequestBody(required = true) Set<Member> members)
      throws Exception {

    logger.debug("Adding member {} to refset {}", members, refsetId);

    validateMembers(members);
    Result<Map<String, Object>> result = new Result<Map<String, Object>>();

    Meta m = new Meta();

    for (Member member : members) {

      member.setCreatedBy(getUserDetails().getUsername());
      member.setModifiedBy(getUserDetails().getUsername());
      member.setCreated(new DateTime());
      member.setModifiedDate(new DateTime());
    }
    Map<String, String> outcome =
        aService.addMembers(refsetId, members, getUserDetails().getUsername());

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("outcome", outcome);
    m.add(
        linkTo(methodOn(RefsetBrowseController.class).getRefsetDetails(refsetId))
            .withRel("Refset"));

    result.setData(data);

    m.setMessage(SUCCESS);
    m.setStatus(HttpStatus.OK);

    return new ResponseEntity<Result<Map<String, Object>>>(result, HttpStatus.OK);
  }
  @RequestMapping(
      method = RequestMethod.POST,
      value = "/{refSetId}/add/member",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(value = "Shortcut method to add a member to an existing refset.")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> addMember(
      @PathVariable(value = "refSetId") String refsetId,
      @RequestBody(required = true) Member member)
      throws Exception {

    logger.debug("Adding member {} to refset {}", member, refsetId);

    Set<Member> ms = new HashSet<Member>();
    ms.add(member);
    if (ms != null && !ms.isEmpty()) {

      validateMembers(ms);
    }
    Result<Map<String, Object>> result = new Result<Map<String, Object>>();

    Meta m = new Meta();
    member.setCreatedBy(getUserDetails().getUsername());
    member.setModifiedBy(getUserDetails().getUsername());

    aService.addMember(refsetId, member);

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("uuid", refsetId);
    m.add(
        linkTo(methodOn(RefsetBrowseController.class).getRefsetDetails(refsetId))
            .withRel("Refset"));

    result.setData(data);

    m.setMessage(SUCCESS);
    m.setStatus(HttpStatus.OK);

    return new ResponseEntity<Result<Map<String, Object>>>(result, HttpStatus.OK);
  }