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 { 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 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()); }