@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); }
private BranchRestInfoFull createBranchRestInfo(int id) throws ResourceException { BranchRestInfoFull branchRestInfo = null; Branch branch = m_branchManager.getBranch(id); branchRestInfo = new BranchRestInfoFull(branch); return branchRestInfo; }
@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 { Branch branch; int idInt; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { idInt = RestUtilities.getIntFromAttribute(idString); branch = m_branchManager.getBranch( idInt); // just obtain to make sure exists, use int id for actual delete } catch (Exception exception) { RestUtilities.setResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } List<Integer> branchIds = new ArrayList<Integer>(); branchIds.add(idInt); m_branchManager.deleteBranches(branchIds); RestUtilities.setResponse( getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Branch", branch.getId()); return; } // no id string RestUtilities.setResponse( getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); }
@Override public Representation represent(Variant variant) throws ResourceException { // process request for single int idInt; BranchRestInfoFull branchRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return RestUtilities.getResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { branchRestInfo = createBranchRestInfo(idInt); } catch (Exception exception) { return RestUtilities.getResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); } return new BranchRepresentation(variant.getMediaType(), branchRestInfo); } // if not single, process request for all 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); }
@Override public void storeRepresentation(Representation entity) throws ResourceException { // get from request body BranchRepresentation representation = new BranchRepresentation(entity); BranchRestInfoFull branchRestInfo = representation.getObject(); Branch branch = null; // validate input for update or create ValidationInfo validationInfo = validate(branchRestInfo); if (!validationInfo.valid) { RestUtilities.setResponseError( getResponse(), validationInfo.responseCode, validationInfo.message); return; } // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { int idInt = RestUtilities.getIntFromAttribute(idString); branch = m_branchManager.getBranch(idInt); } catch (Exception exception) { RestUtilities.setResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } // copy values over to existing try { updateBranch(branch, branchRestInfo); m_branchManager.saveBranch(branch); } catch (Exception exception) { RestUtilities.setResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Branch failed", exception.getLocalizedMessage()); return; } RestUtilities.setResponse( getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Branch", branch.getId()); return; } // otherwise add new try { branch = createBranch(branchRestInfo); m_branchManager.saveBranch(branch); } catch (Exception exception) { RestUtilities.setResponseError( getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Branch failed", exception.getLocalizedMessage()); return; } RestUtilities.setResponse( getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Branch", branch.getId()); }
@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()); }