@Override
  public Representation represent(Variant variant) throws ResourceException {
    IntParameterInfo parameterInfo;
    OpenAcdAgent agent;
    OpenAcdAgentRestInfoFull agentRestInfo;

    // if have id then get a single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        return RestUtilities.getResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
      }

      try {
        agent = m_openAcdContext.getAgentById(parameterInfo.getValue());
      } catch (Exception exception) {
        return RestUtilities.getResponseError(
            getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
      }

      try {
        agentRestInfo = createAgentRestInfo(agent);
      } catch (Exception exception) {
        return RestUtilities.getResponseError(
            getResponse(),
            ERROR_READ_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
      }

      return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo);
    }

    // if not single, process request for list
    List<OpenAcdAgent> agents = m_openAcdContext.getAgents();
    List<OpenAcdAgentRestInfoFull> agentsRestInfo = new ArrayList<OpenAcdAgentRestInfoFull>();
    MetadataRestInfo metadataRestInfo;

    // sort if specified
    sortAgents(agents);

    // set requested agents groups and get resulting metadata
    metadataRestInfo = addAgents(agentsRestInfo, agents);

    // create final restinfo
    OpenAcdAgentsBundleRestInfo agentsBundleRestInfo =
        new OpenAcdAgentsBundleRestInfo(agentsRestInfo, metadataRestInfo);

    return new OpenAcdAgentsRepresentation(variant.getMediaType(), agentsBundleRestInfo);
  }
Example #2
0
  @Override
  public Representation represent(Variant variant) throws ResourceException {
    IntParameterInfo parameterInfo;
    Branch branch;
    BranchRestInfoFull branchRestInfo;

    // if have id then get a single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        return RestUtilities.getResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
      }

      try {
        branch = m_branchManager.retrieveBranch(parameterInfo.getValue());
        if (branch == null) {
          return RestUtilities.getResponseError(
              getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
        }

        branchRestInfo = createBranchRestInfo(branch);
      } catch (Exception exception) {
        return RestUtilities.getResponseError(
            getResponse(),
            ERROR_READ_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
      }

      return new BranchRepresentation(variant.getMediaType(), branchRestInfo);
    }

    // if not single, process request for list
    List<Branch> branches = m_branchManager.getBranches();
    List<BranchRestInfoFull> branchesRestInfo = new ArrayList<BranchRestInfoFull>();
    MetadataRestInfo metadataRestInfo;

    // sort if specified
    sortBranches(branches);

    // set requested agents groups and get resulting metadata
    metadataRestInfo = addBranches(branchesRestInfo, branches);

    // create final restinfo
    BranchesBundleRestInfo branchesBundleRestInfo =
        new BranchesBundleRestInfo(branchesRestInfo, metadataRestInfo);

    return new BranchesRepresentation(variant.getMediaType(), branchesBundleRestInfo);
  }
Example #3
0
  @Override
  public void removeRepresentations() throws ResourceException {
    IntParameterInfo parameterInfo;
    Branch branch;

    // get id then delete a single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
        return;
      }

      try {
        // do not need object to delete, but confirm existence for error message
        branch = m_branchManager.retrieveBranch(parameterInfo.getValue());
        if (branch == null) {
          RestUtilities.setResponseError(
              getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
          return;
        }

        List<Integer> branchIds = new ArrayList<Integer>();
        branchIds.add(parameterInfo.getValue());
        m_branchManager.deleteBranches(branchIds);
      } catch (Exception exception) {
        RestUtilities.setResponseError(
            getResponse(),
            ERROR_DELETE_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
        return;
      }

      RestUtilities.setResponse(getResponse(), SUCCESS_DELETED, parameterInfo.getValue());
      return;
    }

    // no id string
    RestUtilities.setResponse(getResponse(), ERROR_MISSING_ID);
  }
  @Override
  public void removeRepresentations() throws ResourceException {
    IntParameterInfo parameterInfo;
    OpenAcdAgent agent;

    // get id then delete a single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
        return;
      }

      try {
        agent = m_openAcdContext.getAgentById(parameterInfo.getValue());
      } catch (Exception ex) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
        return;
      }

      try {
        m_openAcdContext.deleteAgent(agent);
      } catch (Exception exception) {
        RestUtilities.setResponseError(
            getResponse(),
            ERROR_DELETE_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
        return;
      }

      RestUtilities.setResponse(getResponse(), SUCCESS_DELETED, agent.getId());
      return;
    }

    // no id string
    RestUtilities.setResponse(getResponse(), ERROR_MISSING_ID);
  }
  @Override
  public void storeRepresentation(Representation entity) throws ResourceException {
    IntParameterInfo parameterInfo;

    // get item from request body
    OpenAcdAgentRepresentation representation = new OpenAcdAgentRepresentation(entity);
    OpenAcdAgentRestInfoFull agentRestInfo = (OpenAcdAgentRestInfoFull) representation.getObject();
    OpenAcdAgent agent;

    // validate input for update or create
    ValidationInfo validationInfo = validate(agentRestInfo);

    if (!validationInfo.getValid()) {
      RestUtilities.setResponseError(
          getResponse(), validationInfo.getResponseCode(), validationInfo.getMessage());
      return;
    }

    // if have id then update single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
        return;
      }

      try {
        agent = m_openAcdContext.getAgentById(parameterInfo.getValue());
      } catch (Exception exception) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
        return;
      }

      // copy values over to existing item
      try {
        updateAgent(agent, agentRestInfo);
        m_openAcdContext.saveAgent(agent);
      } catch (Exception exception) {
        RestUtilities.setResponseError(
            getResponse(),
            ERROR_UPDATE_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
        return;
      }

      RestUtilities.setResponse(getResponse(), SUCCESS_UPDATED, agent.getId());
      return;
    }

    // if not single, add new item
    try {
      agent = createOpenAcdAgent(agentRestInfo);
      m_openAcdContext.saveAgent(agent);
    } catch (Exception exception) {
      RestUtilities.setResponseError(
          getResponse(), ERROR_UPDATE_FAILED, exception.getLocalizedMessage());
      return;
    }

    RestUtilities.setResponse(getResponse(), SUCCESS_CREATED, agent.getId());
  }
Example #6
0
  @Override
  public void storeRepresentation(Representation entity) throws ResourceException {
    IntParameterInfo parameterInfo;

    // get item from request body
    BranchRepresentation representation = new BranchRepresentation(entity);
    BranchRestInfoFull branchRestInfo = representation.getObject();
    Branch branch;

    // validate input for update or create
    ValidationInfo validationInfo = validate(branchRestInfo);

    if (!validationInfo.getValid()) {
      RestUtilities.setResponseError(
          getResponse(), validationInfo.getResponseCode(), validationInfo.getMessage());
      return;
    }

    // if have id then update single item
    parameterInfo = RestUtilities.getIntFromAttribute(getRequest(), REQUEST_ATTRIBUTE_ID);
    if (parameterInfo.getExists()) {
      if (!parameterInfo.getValid()) {
        RestUtilities.setResponseError(
            getResponse(), ERROR_ID_INVALID, parameterInfo.getValueString());
        return;
      }

      try {
        branch = m_branchManager.retrieveBranch(parameterInfo.getValue());
        if (branch == null) {
          RestUtilities.setResponseError(
              getResponse(), ERROR_OBJECT_NOT_FOUND, parameterInfo.getValue());
          return;
        }

        // copy values over to existing item
        updateBranch(branch, branchRestInfo);
        m_branchManager.saveBranch(branch);
      } catch (Exception exception) {
        RestUtilities.setResponseError(
            getResponse(),
            ERROR_UPDATE_FAILED,
            parameterInfo.getValue(),
            exception.getLocalizedMessage());
        return;
      }

      RestUtilities.setResponse(getResponse(), SUCCESS_UPDATED, branch.getId());
      return;
    }

    // if not single, add new item
    try {
      branch = createBranch(branchRestInfo);
      m_branchManager.saveBranch(branch);
    } catch (Exception exception) {
      RestUtilities.setResponseError(
          getResponse(), ERROR_CREATE_FAILED, exception.getLocalizedMessage());
      return;
    }

    RestUtilities.setResponse(getResponse(), SUCCESS_CREATED, branch.getId());
  }