@RequestMapping(
      method = RequestMethod.DELETE,
      value = "/delete/{refsetId}/members",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(value = "Removes list of members from refset.")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> removeMembers(
      @PathVariable String refsetId, @RequestBody Set<String> referencedComponentIds)
      throws Exception {

    logger.debug("Removing an existing refsets {}", refsetId);

    Result<Map<String, Object>> result = new Result<Map<String, Object>>();
    Meta m = new Meta();

    result.setMeta(m);

    Map<String, String> outcome =
        aService.removeMembers(refsetId, referencedComponentIds, 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.DELETE,
      value = "/delete/{refsetId}/member/{referenceComponentId}",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(value = "Removes a member from refset")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> removeMember(
      @PathVariable String refsetId, @PathVariable String referenceComponentId) throws Exception {

    logger.debug("Removing an existing refsets {}", refsetId);

    Result<Map<String, Object>> response = new Result<Map<String, Object>>();
    Meta m = new Meta();

    response.setMeta(m);

    Assert.notNull(refsetId, "Required refset id is not available in request");

    aService.removeMemberFromRefset(refsetId, referenceComponentId, getUserDetails().getUsername());

    m.add(linkTo(methodOn(RefsetBrowseController.class).getRefsets(1, 10)).withRel("Refset"));

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

    return new ResponseEntity<Result<Map<String, Object>>>(response, HttpStatus.OK);
  }
  @RequestMapping(
      method = RequestMethod.POST,
      value = "/update",
      produces = "application/json",
      consumes = "application/json")
  @ApiOperation(value = "Update a Refset")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResponseEntity<Result<Map<String, Object>>> updateRefset(@RequestBody Refset r)
      throws Exception {

    logger.debug("Updating an existing 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>> response = new Result<Map<String, Object>>();
    Meta m = new Meta();

    response.setMeta(m);

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

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

      member.setModifiedBy(getUserDetails().getUsername());
    }

    aService.updateRefset(r);

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

    response.setData(data);

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

    return new ResponseEntity<Result<Map<String, Object>>>(response, HttpStatus.OK);
  }
  @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);
  }
  @Override
  public void commence(HttpServletRequest req, HttpServletResponse res, AuthenticationException e)
      throws IOException, ServletException {

    res.setContentType(MediaType.APPLICATION_JSON_VALUE);
    Result<Map<String, Object>> result = new Result<Map<String, Object>>();
    Meta meta = new Meta();
    meta.setMessage(e.getMessage());
    meta.setStatus(HttpStatus.UNAUTHORIZED);
    ErrorInfo errorInfo =
        new ErrorInfo(
            e.getMessage(),
            Integer.toString(org.apache.commons.httpclient.HttpStatus.SC_UNAUTHORIZED));
    meta.setErrorInfo(errorInfo);
    result.setMeta(meta);

    PrintWriter out = res.getWriter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    out.print(mapper.writeValueAsString(result));
    out.close();
  }