private void updateBranch(Branch branch, BranchRestInfoFull branchRestInfo) { Address address; branch.setName(branchRestInfo.getName()); branch.setDescription(branchRestInfo.getDescription()); branch.setPhoneNumber(branchRestInfo.getPhoneNumber()); branch.setFaxNumber(branchRestInfo.getFaxNumber()); address = getAddress(branchRestInfo); branch.setAddress(address); }
private Branch createBranch(BranchRestInfoFull branchRestInfo) throws ResourceException { Address address; Branch branch = new Branch(); // copy fields from rest info branch.setName(branchRestInfo.getName()); branch.setDescription(branchRestInfo.getDescription()); address = getAddress(branchRestInfo); branch.setAddress(address); return branch; }
private void updateBranch(Branch branch, BranchRestInfoFull branchRestInfo) { Address address; String tempString; // do not allow empty name tempString = branchRestInfo.getName(); if (!tempString.isEmpty()) { branch.setName(tempString); } branch.setDescription(branchRestInfo.getDescription()); address = getAddress(branchRestInfo); branch.setAddress(address); }
@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()); }
@Override public int compare(Branch item1, Branch item2) { return RestUtilities.compareIgnoreCaseNullSafe( item1.getDescription(), item2.getDescription()); }
@Override public int compare(Branch item1, Branch item2) { return RestUtilities.compareIgnoreCaseNullSafe( item1.getAddress().getOfficeDesignation(), item2.getAddress().getOfficeDesignation()); }
@Override public int compare(Branch branch1, Branch branch2) { return RestUtilities.compareIgnoreCaseNullSafe( branch1.getAddress().getCity(), branch2.getAddress().getCity()); }
@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()); }