From fb3de1fae5aa34736037ce6209b6c1b108ace538 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 13 Mar 2012 14:21:25 -0400 Subject: [PATCH 01/99] Added OpenACD Groups REST service that allows GET, includes sorting and pagination --- .../rest/OpenAcdAgentGroupsResource.java | 409 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 7 + 2 files changed, 416 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java new file mode 100644 index 0000000000..5d21608f28 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -0,0 +1,409 @@ +/* + * + * OpenAcdAgentGroupsResource.java - A Restlet to read group data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; + + +public class OpenAcdAgentGroupsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + + private String m_sortDirectionString; + private String m_sortFieldString; + private SortDirection m_sortDirection; + private SortField m_sortField; + private String m_pageNumberString; + private String m_resultsPerPageString; + + // default 0 indicates unused + private int m_pageNumber = 0; + private int m_resultsPerPage = 0; + private Boolean m_paginate = false; + + private enum SortDirection { + FORWARD, REVERSE, NONE + } + + private enum SortField { + NAME, DESCRIPTION + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url (get) + Form form = getRequest().getResourceRef().getQueryAsForm(); + m_sortDirectionString = form.getFirstValue("sortdir"); + m_sortFieldString = form.getFirstValue("sortby"); + m_pageNumberString = form.getFirstValue("page"); + m_resultsPerPageString = form.getFirstValue("pagesize"); + + try { + m_sortDirection = SortDirection.valueOf(m_sortDirectionString.toUpperCase()); + } + catch (Exception exception) { + // default to no sort + m_sortDirection = SortDirection.NONE; + } + + try { + m_sortField = SortField.valueOf(m_sortFieldString.toUpperCase()); + } + catch (Exception exception) { + // default + m_sortField = SortField.NAME; + } + + // must specify both PageNumber and ResultsPerPage together + try { + m_pageNumber = Integer.parseInt(m_pageNumberString); + m_resultsPerPage = Integer.parseInt(m_resultsPerPageString); + } + catch (Exception exception) { + // default 0 for nothing + m_pageNumber = 0; + m_resultsPerPage = 0; + } + + // check for outrageous values or lack of parameters + if ((m_pageNumber < 1) || (m_resultsPerPage < 1)) { + m_pageNumber = 0; + m_resultsPerPage = 0; + m_paginate = false; + } + else { + m_paginate = true; + } + } + + @Override + public boolean allowGet() { + return true; + } + + @Override + public Representation represent(Variant variant) throws ResourceException { + List agentGroups = m_openAcdContext.getAgentGroups(); + List agentGroupsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + SortGroups(agentGroups); + + // set requested agents groups and get resulting metadata + metadataRestInfo = AddAgentGroups(agentGroupsRestInfo, agentGroups); + + // create final restinfo + OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); + + return new OpenAcdAgentGroupsRepresentation(variant.getMediaType(), agentGroupsBundleRestInfo); + } + + private MetadataRestInfo AddAgentGroups(List agentGroupsRestInfo, List agentGroups) { + int totalResults = agentGroups.size(); + List skillsRestInfo; + + int startIndex, endIndex; + int currentPage, totalPages, resultsPerPage; + + // determine pagination + if (m_paginate) { + currentPage = m_pageNumber; + resultsPerPage = m_resultsPerPage; + totalPages = ((totalResults - 1) / resultsPerPage) + 1; + + // check if only one page + //if (resultsPerPage >= totalResults) { + if (totalPages == 1) { + startIndex = 0; + endIndex = totalResults - 1; + currentPage = 1; + // design decision: should the resultsPerPage actually be set to totalResults? + // since totalResults are already available preserve call value + } + else { + // check if specified page number is on or beyoned last page (then use last page) + if (currentPage >= totalPages) { + currentPage = totalPages; + startIndex = (totalPages - 1) * resultsPerPage; + endIndex = totalResults - 1; + } + else { + startIndex = (currentPage - 1) * resultsPerPage; + endIndex = startIndex + resultsPerPage - 1; + } + } + } + else { + // default values assuming no pagination + startIndex = 0; + endIndex = totalResults - 1; + currentPage = 1; + totalPages = 1; + resultsPerPage = totalResults; + } + + // create list of group restinfos + for (int index = startIndex; index <= endIndex; index++) { + OpenAcdAgentGroup agentGroup = agentGroups.get(index); + + // create list of skill restinfos for single group + Set groupSkills = agentGroup.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + for (OpenAcdSkill groupSkill : groupSkills) { + OpenAcdSkillRestInfo skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } + + OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); + agentGroupsRestInfo.add(agentGroupRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(totalResults, currentPage, totalPages, resultsPerPage); + return metadata; + } + + private void SortGroups(List agentGroups) { + + // sort groups if requested (will simply leave as creation order if unrecognized parameters) + switch (m_sortDirection) { + case FORWARD: + + switch (m_sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); + } + + }); + break; + } + + break; + + case REVERSE: + switch (m_sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); + } + + }); + break; + } + + break; + } + } + + static class OpenAcdAgentGroupsRepresentation extends XStreamRepresentation { + + public OpenAcdAgentGroupsRepresentation(MediaType mediaType, OpenAcdAgentGroupsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdAgentGroupsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + //xstream.alias("openAcdGroups", List.class); + xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + static class OpenAcdAgentGroupRestInfo { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills) { + m_name = agentGroup.getName(); + m_id = agentGroup.getId(); + m_description = agentGroup.getDescription(); + m_skills = skills; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(int totalResults, int currentPage, int totalPages, int resultsPerPage) { + m_totalResults = totalResults; + m_currentPage = currentPage; + m_totalPages = totalPages; + m_resultsPerPage = resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdAgentGroupsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_groups; + + public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { + m_metadata = metadata; + m_groups = agentGroups; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getGroups() { + return m_groups; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 219b631e4e..da902edf1d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -224,4 +224,11 @@ + + + + + + + From a2d9c9accbbaca10735c5226dcb6d6e56d73cc8a Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 15 Mar 2012 02:45:38 -0400 Subject: [PATCH 02/99] Add GET, PUT, DELETE to retrieve, update and delete single OpenACD groups. Alsoo refactor to put common functionality in separate utilities class. --- .../rest/OpenAcdAgentGroupsResource.java | 370 ++++++++++++------ .../sipxconfig/rest/OpenAcdUtilities.java | 171 ++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 9 + 3 files changed, 425 insertions(+), 125 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 5d21608f28..a561ddbb37 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.LinkedHashSet; import java.util.Collections; import java.util.Comparator; @@ -34,6 +35,7 @@ import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Form; +import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; @@ -42,30 +44,32 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; - +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; public class OpenAcdAgentGroupsResource extends UserResource { private OpenAcdContext m_openAcdContext; + private Form m_form; - private String m_sortDirectionString; - private String m_sortFieldString; - private SortDirection m_sortDirection; - private SortField m_sortField; - private String m_pageNumberString; - private String m_resultsPerPageString; - - // default 0 indicates unused - private int m_pageNumber = 0; - private int m_resultsPerPage = 0; - private Boolean m_paginate = false; + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; - private enum SortDirection { - FORWARD, REVERSE, NONE - } + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } - private enum SortField { - NAME, DESCRIPTION + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } } @@ -75,67 +79,58 @@ public void init(Context context, Request request, Response response) { getVariants().add(new Variant(TEXT_XML)); getVariants().add(new Variant(APPLICATION_JSON)); - // pull parameters from url (get) - Form form = getRequest().getResourceRef().getQueryAsForm(); - m_sortDirectionString = form.getFirstValue("sortdir"); - m_sortFieldString = form.getFirstValue("sortby"); - m_pageNumberString = form.getFirstValue("page"); - m_resultsPerPageString = form.getFirstValue("pagesize"); + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } - try { - m_sortDirection = SortDirection.valueOf(m_sortDirectionString.toUpperCase()); - } - catch (Exception exception) { - // default to no sort - m_sortDirection = SortDirection.NONE; - } - try { - m_sortField = SortField.valueOf(m_sortFieldString.toUpperCase()); - } - catch (Exception exception) { - // default - m_sortField = SortField.NAME; - } + // Allowed REST operations + // ----------------------- - // must specify both PageNumber and ResultsPerPage together - try { - m_pageNumber = Integer.parseInt(m_pageNumberString); - m_resultsPerPage = Integer.parseInt(m_resultsPerPageString); - } - catch (Exception exception) { - // default 0 for nothing - m_pageNumber = 0; - m_resultsPerPage = 0; - } + @Override + public boolean allowGet() { + return true; + } - // check for outrageous values or lack of parameters - if ((m_pageNumber < 1) || (m_resultsPerPage < 1)) { - m_pageNumber = 0; - m_resultsPerPage = 0; - m_paginate = false; - } - else { - m_paginate = true; - } + @Override + public boolean allowPut() { + return true; } @Override - public boolean allowGet() { + public boolean allowDelete() { return true; } + // GET - Retrieve Groups and single Group + // -------------------------------------- + @Override public Representation represent(Variant variant) throws ResourceException { + // process request for a single group + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroupRestInfo = getAgentGroupRestInfoById(groupId); + + // finally return group representation + return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); + } + + + // if not single group, process request for all groups List agentGroups = m_openAcdContext.getAgentGroups(); List agentGroupsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified - SortGroups(agentGroups); + sortGroups(agentGroups); // set requested agents groups and get resulting metadata - metadataRestInfo = AddAgentGroups(agentGroupsRestInfo, agentGroups); + metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); // create final restinfo OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); @@ -143,78 +138,159 @@ public Representation represent(Variant variant) throws ResourceException { return new OpenAcdAgentGroupsRepresentation(variant.getMediaType(), agentGroupsBundleRestInfo); } - private MetadataRestInfo AddAgentGroups(List agentGroupsRestInfo, List agentGroups) { - int totalResults = agentGroups.size(); + + // PUT - Update or Add single Group + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get group from body + OpenAcdAgentGroupRepresentation representation = new OpenAcdAgentGroupRepresentation(entity); + OpenAcdAgentGroupRestInfo agentGroupRestInfo = representation.getObject(); + OpenAcdAgentGroup agentGroup; + + // if have id then update a single group + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + // copy values over to existing group + updateAgentGroup(agentGroup, agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + + return; + } + + + // otherwise add new agent group + agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Group + // -------------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdAgentGroup agentGroup; + + // get id then delete a single group + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + m_openAcdContext.deleteAgentGroup(agentGroup); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + + // Helper functions + // ---------------- + + private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int groupId) throws ResourceException { + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + try { + agentGroupRestInfo = createAgentGroupRestInfo(groupId); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); + } + + return agentGroupRestInfo; + } + + private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int groupId) { List skillsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); - int startIndex, endIndex; - int currentPage, totalPages, resultsPerPage; + return agentGroupRestInfo; + } - // determine pagination - if (m_paginate) { - currentPage = m_pageNumber; - resultsPerPage = m_resultsPerPage; - totalPages = ((totalResults - 1) / resultsPerPage) + 1; - - // check if only one page - //if (resultsPerPage >= totalResults) { - if (totalPages == 1) { - startIndex = 0; - endIndex = totalResults - 1; - currentPage = 1; - // design decision: should the resultsPerPage actually be set to totalResults? - // since totalResults are already available preserve call value - } - else { - // check if specified page number is on or beyoned last page (then use last page) - if (currentPage >= totalPages) { - currentPage = totalPages; - startIndex = (totalPages - 1) * resultsPerPage; - endIndex = totalResults - 1; - } - else { - startIndex = (currentPage - 1) * resultsPerPage; - endIndex = startIndex + resultsPerPage - 1; - } - } + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + String tempString; + + // do not allow empty name + tempString = agentGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + agentGroup.setName(tempString); } - else { - // default values assuming no pagination - startIndex = 0; - endIndex = totalResults - 1; - currentPage = 1; - totalPages = 1; - resultsPerPage = totalResults; + + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + // set skills (should this be a separate "setskills" api call?) + OpenAcdSkill skill; + List skillsRestInfo = agentGroupRestInfo.getSkills(); + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + agentGroup.addSkill(skill); } + } + + private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { + List skillsRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); // create list of group restinfos - for (int index = startIndex; index <= endIndex; index++) { + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdAgentGroup agentGroup = agentGroups.get(index); - - // create list of skill restinfos for single group - Set groupSkills = agentGroup.getSkills(); - skillsRestInfo = new ArrayList(groupSkills.size()); - for (OpenAcdSkill groupSkill : groupSkills) { - OpenAcdSkillRestInfo skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); - skillsRestInfo.add(skillRestInfo); - } + skillsRestInfo = createSkillsRestInfo(agentGroup); OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } // create metadata about agent groups - MetadataRestInfo metadata = new MetadataRestInfo(totalResults, currentPage, totalPages, resultsPerPage); + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); return metadata; } - private void SortGroups(List agentGroups) { + private List createSkillsRestInfo(OpenAcdAgentGroup agentGroup) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + Set groupSkills = agentGroup.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } - // sort groups if requested (will simply leave as creation order if unrecognized parameters) - switch (m_sortDirection) { - case FORWARD: + return skillsRestInfo; + } + + private void sortGroups(List agentGroups) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { - switch (m_sortField) { + switch (sortField) { case NAME: Collections.sort(agentGroups, new Comparator(){ @@ -239,11 +315,10 @@ public int compare(Object object1, Object object2) { }); break; } - - break; - - case REVERSE: - switch (m_sortField) { + } + else { + // must be reverse + switch (sortField) { case NAME: Collections.sort(agentGroups, new Comparator(){ @@ -268,11 +343,33 @@ public int compare(Object object1, Object object2) { }); break; } - - break; } } + private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); + + // copy fields from rest info + agentGroup.setName(agentGroupRestInfo.getName()); + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + // add skills + Set skills = new LinkedHashSet(); + List skillsRestInfo = agentGroupRestInfo.getSkills(); + + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + } + + agentGroup.setSkills(skills); + + return agentGroup; + } + + + // REST Representations + // -------------------- + static class OpenAcdAgentGroupsRepresentation extends XStreamRepresentation { public OpenAcdAgentGroupsRepresentation(MediaType mediaType, OpenAcdAgentGroupsBundleRestInfo object) { @@ -285,13 +382,33 @@ public OpenAcdAgentGroupsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - //xstream.alias("openAcdGroups", List.class); xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); xstream.alias("group", OpenAcdAgentGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); } } + static class OpenAcdAgentGroupRepresentation extends XStreamRepresentation { + + public OpenAcdAgentGroupRepresentation(MediaType mediaType, OpenAcdAgentGroupRestInfo object) { + super(mediaType, object); + } + + public OpenAcdAgentGroupRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + + // REST info objects + // ----------------- + static class OpenAcdAgentGroupRestInfo { private final String m_name; private final int m_id; @@ -328,11 +445,11 @@ static class MetadataRestInfo { private final int m_totalPages; private final int m_resultsPerPage; - public MetadataRestInfo(int totalResults, int currentPage, int totalPages, int resultsPerPage) { - m_totalResults = totalResults; - m_currentPage = currentPage; - m_totalPages = totalPages; - m_resultsPerPage = resultsPerPage; + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; } public int getTotalResults() { @@ -401,6 +518,9 @@ public String getGroupName() { } + // Injected objects + // ---------------- + @Required public void setOpenAcdContext(OpenAcdContext openAcdContext) { m_openAcdContext = openAcdContext; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java new file mode 100644 index 0000000000..b4ff03cb86 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -0,0 +1,171 @@ +/* + * + * OpenAcdUtilities.java - Support functionality for OpenAcd Restlets + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.data.Status; +import org.restlet.data.Form; +import org.restlet.resource.ResourceException; + +public class OpenAcdUtilities { + + public static int getIntFromAttribute(String attributeString) throws ResourceException { + int intFromAttribute; + + // attempt to parse attribute provided as an id + try { + intFromAttribute = Integer.parseInt(attributeString); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attribute " + attributeString + " invalid."); + } + + return intFromAttribute; + } + + public static PaginationInfo calculatePagination(Form form, int totalResults) { + PaginationInfo paginationInfo = new PaginationInfo(); + paginationInfo.totalResults = totalResults; + + // must specify both PageNumber and ResultsPerPage together + String pageNumberString = form.getFirstValue("page"); + String resultsPerPageString = form.getFirstValue("pagesize"); + + // attempt to parse pagination values from request + try { + paginationInfo.pageNumber = Integer.parseInt(pageNumberString); + paginationInfo.resultsPerPage = Integer.parseInt(resultsPerPageString); + } + catch (Exception exception) { + // default 0 for nothing + paginationInfo.pageNumber = 0; + paginationInfo.resultsPerPage = 0; + } + + // check for outrageous values or lack of parameters + if ((paginationInfo.pageNumber < 1) || (paginationInfo.resultsPerPage < 1)) { + paginationInfo.pageNumber = 0; + paginationInfo.resultsPerPage = 0; + paginationInfo.paginate = false; + } + else { + paginationInfo.paginate = true; + } + + + // do we have to paginate? + if (paginationInfo.paginate) { + paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; + + // check if only one page + //if (resultsPerPage >= totalResults) { + if (paginationInfo.totalPages == 1) { + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + // design decision: should the resultsPerPage actually be set to totalResults? + // since totalResults are already available preserve call value + } + else { + // check if specified page number is on or beyoned last page (then use last page) + if (paginationInfo.pageNumber >= paginationInfo.totalPages) { + paginationInfo.pageNumber = paginationInfo.totalPages; + paginationInfo.startIndex = (paginationInfo.totalPages - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + } + else { + paginationInfo.startIndex = (paginationInfo.pageNumber - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.startIndex + paginationInfo.resultsPerPage - 1; + } + } + } + else { + // default values assuming no pagination + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + paginationInfo.totalPages = 1; + paginationInfo.resultsPerPage = paginationInfo.totalResults; + } + + return paginationInfo; + } + + public static SortInfo calculateSorting(Form form) { + SortInfo sortInfo = new SortInfo(); + + String sortDirectionString = form.getFirstValue("sortdir"); + String sortFieldString = form.getFirstValue("sortby"); + + // check for invalid input + if ((sortDirectionString == null) || (sortFieldString == null)) { + sortInfo.sort = false; + return sortInfo; + } + + if ((sortDirectionString.isEmpty()) || (sortFieldString.isEmpty())) { + sortInfo.sort = false; + return sortInfo; + } + + sortInfo.sort = true; + + // assume forward if get anything else but "reverse" + if (sortDirectionString.toLowerCase().equals("reverse")) { + sortInfo.directionForward = false; + } + else { + sortInfo.directionForward = true; + } + + // tough to type-check this one + sortInfo.sortField = sortFieldString; + + return sortInfo; + } + + + // Data objects + // ------------ + + public static class PaginationInfo { + Boolean paginate = false; + int pageNumber = 0; + int resultsPerPage = 0; + int totalPages = 0; + int totalResults = 0; + int startIndex = 0; + int endIndex = 0; + } + + public static class SortInfo { + Boolean sort = false; + Boolean directionForward = true; + String sortField = ""; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index da902edf1d..a12056ca41 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -228,7 +228,16 @@ + + + + + + + + + From 294845ebecf3a1a6858c770127456ccd21244805 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 16 Mar 2012 12:59:29 -0400 Subject: [PATCH 03/99] Added OpenACD Skills REST API, cleaned up formatting in other OpenACD APIs --- .../rest/OpenAcdAgentGroupsResource.java | 490 ++++++++-------- .../rest/OpenAcdSkillGroupsResource.java | 529 ++++++++++++++++++ .../rest/OpenAcdSkillsResource.java | 495 ++++++++++++++++ .../sipxconfig/rest/OpenAcdUtilities.java | 151 +++-- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 5 files changed, 1357 insertions(+), 324 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index a561ddbb37..ba89b117a1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -55,21 +55,21 @@ public class OpenAcdAgentGroupsResource extends UserResource { // use to define all possible sort fields enum SortField { - NAME, DESCRIPTION, NONE; - - public static SortField toSortField(String fieldString) - { - if (fieldString == null) { - return NONE; - } - - try { - return valueOf(fieldString.toUpperCase()); - } - catch (Exception ex) { - return NONE; - } - } + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } } @@ -79,8 +79,8 @@ public void init(Context context, Request request, Response response) { getVariants().add(new Variant(TEXT_XML)); getVariants().add(new Variant(APPLICATION_JSON)); - // pull parameters from url - m_form = getRequest().getResourceRef().getQueryAsForm(); + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); } @@ -107,33 +107,33 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { - // process request for a single group - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - String groupIdString = (String) getRequest().getAttributes().get("id"); + // process request for a single group + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroupRestInfo = getAgentGroupRestInfoById(groupId); + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agentGroupRestInfo = getAgentGroupRestInfoById(idInt); - // finally return group representation - return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); - } + // finally return group representation + return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); + } - // if not single group, process request for all groups - List agentGroups = m_openAcdContext.getAgentGroups(); + // if not single group, process request for all groups + List agentGroups = m_openAcdContext.getAgentGroups(); List agentGroupsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); - MetadataRestInfo metadataRestInfo; + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; - // sort groups if specified - sortGroups(agentGroups); + // sort groups if specified + sortGroups(agentGroups); - // set requested agents groups and get resulting metadata - metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); + // set requested agents groups and get resulting metadata + metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); - // create final restinfo - OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); + // create final restinfo + OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); return new OpenAcdAgentGroupsRepresentation(variant.getMediaType(), agentGroupsBundleRestInfo); } @@ -144,29 +144,29 @@ public Representation represent(Variant variant) throws ResourceException { @Override public void storeRepresentation(Representation entity) throws ResourceException { - // get group from body + // get group from body OpenAcdAgentGroupRepresentation representation = new OpenAcdAgentGroupRepresentation(entity); OpenAcdAgentGroupRestInfo agentGroupRestInfo = representation.getObject(); - OpenAcdAgentGroup agentGroup; + OpenAcdAgentGroup agentGroup; - // if have id then update a single group - String groupIdString = (String) getRequest().getAttributes().get("id"); + // if have id then update a single group + String idString = (String) getRequest().getAttributes().get("id"); - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroup = m_openAcdContext.getAgentGroupById(groupId); + if (idString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(idString); + agentGroup = m_openAcdContext.getAgentGroupById(id); - // copy values over to existing group - updateAgentGroup(agentGroup, agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); + // copy values over to existing group + updateAgentGroup(agentGroup, agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); - return; - } + return; + } - // otherwise add new agent group - agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); + // otherwise add new agent group + agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); getResponse().setStatus(Status.SUCCESS_CREATED); } @@ -176,21 +176,21 @@ public void storeRepresentation(Representation entity) throws ResourceException @Override public void removeRepresentations() throws ResourceException { - OpenAcdAgentGroup agentGroup; + OpenAcdAgentGroup agentGroup; + + // get id then delete a single group + String idString = (String) getRequest().getAttributes().get("id"); - // get id then delete a single group - String groupIdString = (String) getRequest().getAttributes().get("id"); + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agentGroup = m_openAcdContext.getAgentGroupById(idInt); - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroup = m_openAcdContext.getAgentGroupById(groupId); + m_openAcdContext.deleteAgentGroup(agentGroup); - m_openAcdContext.deleteAgentGroup(agentGroup); + return; + } - return; - } - - // no id string + // no id string getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } @@ -199,171 +199,171 @@ public void removeRepresentations() throws ResourceException { // ---------------- private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int groupId) throws ResourceException { - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - - try { - agentGroupRestInfo = createAgentGroupRestInfo(groupId); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); - } - - return agentGroupRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + try { + agentGroupRestInfo = createAgentGroupRestInfo(groupId); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); + } + + return agentGroupRestInfo; } private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int groupId) { - List skillsRestInfo; - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); - - skillsRestInfo = createSkillsRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); - - return agentGroupRestInfo; + List skillsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); + + return agentGroupRestInfo; } private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { - String tempString; - - // do not allow empty name - tempString = agentGroupRestInfo.getName(); - if (!tempString.isEmpty()) { - agentGroup.setName(tempString); - } - - agentGroup.setDescription(agentGroupRestInfo.getDescription()); - - // set skills (should this be a separate "setskills" api call?) - OpenAcdSkill skill; - List skillsRestInfo = agentGroupRestInfo.getSkills(); - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); - agentGroup.addSkill(skill); - } + String tempString; + + // do not allow empty name + tempString = agentGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + agentGroup.setName(tempString); + } + + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + // set skills (should this be a separate "setskills" api call?) + OpenAcdSkill skill; + List skillsRestInfo = agentGroupRestInfo.getSkills(); + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + agentGroup.addSkill(skill); + } } private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { - List skillsRestInfo; + List skillsRestInfo; - // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); - // create list of group restinfos + // create list of group restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { - OpenAcdAgentGroup agentGroup = agentGroups.get(index); - skillsRestInfo = createSkillsRestInfo(agentGroup); + OpenAcdAgentGroup agentGroup = agentGroups.get(index); + skillsRestInfo = createSkillsRestInfo(agentGroup); OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } - // create metadata about agent groups - MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); - return metadata; + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; } private List createSkillsRestInfo(OpenAcdAgentGroup agentGroup) { - List skillsRestInfo; - OpenAcdSkillRestInfo skillRestInfo; + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; - // create list of skill restinfos for single group - Set groupSkills = agentGroup.getSkills(); - skillsRestInfo = new ArrayList(groupSkills.size()); + // create list of skill restinfos for single group + Set groupSkills = agentGroup.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); - for (OpenAcdSkill groupSkill : groupSkills) { - skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); - skillsRestInfo.add(skillRestInfo); - } + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } - return skillsRestInfo; + return skillsRestInfo; } private void sortGroups(List agentGroups) { - // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); - - if (!sortInfo.sort) { - return; - } - - SortField sortField = SortField.toSortField(sortInfo.sortField); - - if (sortInfo.directionForward) { - - switch (sortField) { - case NAME: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); - } - - }); - break; - - case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); - } - - }); - break; - } - } - else { - // must be reverse - switch (sortField) { - case NAME: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); - } - - }); - break; - - case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); - } - - }); - break; - } - } + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); + } + + }); + break; + } + } } private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfo agentGroupRestInfo) { - OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); - - // copy fields from rest info - agentGroup.setName(agentGroupRestInfo.getName()); - agentGroup.setDescription(agentGroupRestInfo.getDescription()); + OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); + + // copy fields from rest info + agentGroup.setName(agentGroupRestInfo.getName()); + agentGroup.setDescription(agentGroupRestInfo.getDescription()); - // add skills - Set skills = new LinkedHashSet(); - List skillsRestInfo = agentGroupRestInfo.getSkills(); + // add skills + Set skills = new LinkedHashSet(); + List skillsRestInfo = agentGroupRestInfo.getSkills(); - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); - } + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + } - agentGroup.setSkills(skills); + agentGroup.setSkills(skills); - return agentGroup; + return agentGroup; } @@ -413,13 +413,13 @@ static class OpenAcdAgentGroupRestInfo { private final String m_name; private final int m_id; private final String m_description; - private final List m_skills; + private final List m_skills; public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills) { m_name = agentGroup.getName(); m_id = agentGroup.getId(); m_description = agentGroup.getDescription(); - m_skills = skills; + m_skills = skills; } public String getName() { @@ -440,81 +440,81 @@ public List getSkills() { } static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } public int getTotalResults() { return m_totalResults; } - public int getCurrentPage() { - return m_currentPage; - } + public int getCurrentPage() { + return m_currentPage; + } - public int getTotalPages() { - return m_totalPages; - } + public int getTotalPages() { + return m_totalPages; + } - public int getResultsPerPage() { - return m_resultsPerPage; - } + public int getResultsPerPage() { + return m_resultsPerPage; + } } static class OpenAcdAgentGroupsBundleRestInfo { - private final MetadataRestInfo m_metadata; - private final List m_groups; + private final MetadataRestInfo m_metadata; + private final List m_groups; - public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { - m_metadata = metadata; - m_groups = agentGroups; - } + public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { + m_metadata = metadata; + m_groups = agentGroups; + } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getGroups() { - return m_groups; - } + public List getGroups() { + return m_groups; + } } static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } public int getId() { return m_id; } - public String getName() { - return m_name; - } + public String getName() { + return m_name; + } - public String getDescription() { - return m_description; - } + public String getDescription() { + return m_description; + } - public String getGroupName() { - return m_groupName; - } + public String getGroupName() { + return m_groupName; + } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java new file mode 100644 index 0000000000..5ea43bfd60 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -0,0 +1,529 @@ +/* + * + * OpenAcdAgentGroupsResource.java - A Restlet to read group data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdSkillGroupsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve Groups and single Group + // -------------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for a single group + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroupRestInfo = getAgentGroupRestInfoById(groupId); + + // finally return group representation + return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); + } + + + // if not single group, process request for all groups + List agentGroups = m_openAcdContext.getAgentGroups(); + List agentGroupsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortGroups(agentGroups); + + // set requested agents groups and get resulting metadata + metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); + + // create final restinfo + OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); + + return new OpenAcdAgentGroupsRepresentation(variant.getMediaType(), agentGroupsBundleRestInfo); + } + + + // PUT - Update or Add single Group + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get group from body + OpenAcdAgentGroupRepresentation representation = new OpenAcdAgentGroupRepresentation(entity); + OpenAcdAgentGroupRestInfo agentGroupRestInfo = representation.getObject(); + OpenAcdAgentGroup agentGroup; + + // if have id then update a single group + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + // copy values over to existing group + updateAgentGroup(agentGroup, agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + + return; + } + + + // otherwise add new agent group + agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Group + // -------------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdAgentGroup agentGroup; + + // get id then delete a single group + String groupIdString = (String) getRequest().getAttributes().get("id"); + + if (groupIdString != null) { + int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); + agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + m_openAcdContext.deleteAgentGroup(agentGroup); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + + // Helper functions + // ---------------- + + private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int groupId) throws ResourceException { + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + try { + agentGroupRestInfo = createAgentGroupRestInfo(groupId); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); + } + + return agentGroupRestInfo; + } + + private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int groupId) { + List skillsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); + + return agentGroupRestInfo; + } + + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + String tempString; + + // do not allow empty name + tempString = agentGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + agentGroup.setName(tempString); + } + + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + // set skills (should this be a separate "setskills" api call?) + OpenAcdSkill skill; + List skillsRestInfo = agentGroupRestInfo.getSkills(); + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + agentGroup.addSkill(skill); + } + } + + private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { + List skillsRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); + + // create list of group restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdAgentGroup agentGroup = agentGroups.get(index); + skillsRestInfo = createSkillsRestInfo(agentGroup); + + OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); + agentGroupsRestInfo.add(agentGroupRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private List createSkillsRestInfo(OpenAcdAgentGroup agentGroup) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + Set groupSkills = agentGroup.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } + + return skillsRestInfo; + } + + private void sortGroups(List agentGroups) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(agentGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; + OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; + return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); + } + + }); + break; + } + } + } + + private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); + + // copy fields from rest info + agentGroup.setName(agentGroupRestInfo.getName()); + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + // add skills + Set skills = new LinkedHashSet(); + List skillsRestInfo = agentGroupRestInfo.getSkills(); + + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + } + + agentGroup.setSkills(skills); + + return agentGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdAgentGroupsRepresentation extends XStreamRepresentation { + + public OpenAcdAgentGroupsRepresentation(MediaType mediaType, OpenAcdAgentGroupsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdAgentGroupsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + static class OpenAcdAgentGroupRepresentation extends XStreamRepresentation { + + public OpenAcdAgentGroupRepresentation(MediaType mediaType, OpenAcdAgentGroupRestInfo object) { + super(mediaType, object); + } + + public OpenAcdAgentGroupRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdAgentGroupRestInfo { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills) { + m_name = agentGroup.getName(); + m_id = agentGroup.getId(); + m_description = agentGroup.getDescription(); + m_skills = skills; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdAgentGroupsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_groups; + + public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { + m_metadata = metadata; + m_groups = agentGroups; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getGroups() { + return m_groups; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java new file mode 100644 index 0000000000..ecd9bf0e7a --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -0,0 +1,495 @@ +/* + * + * OpenAcdAgentGroupsResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdSkillsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for a single skill + OpenAcdSkillRestInfo skillRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillRestInfo = getSkillRestInfoById(idInt); + + // finally return group representation + return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); + } + + + // if not single, process request for all + List skills = m_openAcdContext.getSkills(); + List skillsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortSkills(skills); + + // set requested agents groups and get resulting metadata + metadataRestInfo = addSkills(skillsRestInfo, skills); + + // create final restinfo + OpenAcdSkillsBundleRestInfo skillsBundleRestInfo = new OpenAcdSkillsBundleRestInfo(skillsRestInfo, metadataRestInfo); + + return new OpenAcdSkillsRepresentation(variant.getMediaType(), skillsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); + OpenAcdSkillRestInfo skillRestInfo = representation.getObject(); + OpenAcdSkill skill; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + + // copy values over to existing + updateSkill(skill, skillRestInfo); + m_openAcdContext.saveSkill(skill); + + return; + } + + + // otherwise add new + skill = createSkill(skillRestInfo); + m_openAcdContext.saveSkill(skill); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSkill skill; + + // get id then delete a single group + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + + m_openAcdContext.deleteSkill(skill); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + + // Helper functions + // ---------------- + + private OpenAcdSkillRestInfo getSkillRestInfoById(int id) throws ResourceException { + OpenAcdSkillRestInfo skillRestInfo; + + try { + skillRestInfo = createSkillRestInfo(id); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + id + " not found."); + } + + return skillRestInfo; + } + + private OpenAcdSkillRestInfo createSkillRestInfo(int id) { + OpenAcdSkillRestInfo skillRestInfo; + OpenAcdSkill skill = m_openAcdContext.getSkillById(id); + + skillRestInfo = new OpenAcdSkillRestInfo(skill); + + return skillRestInfo; + } + + private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { + OpenAcdSkillRestInfo skillRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skills.size()); + + // create list of skill restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdSkill skill = skills.get(index); + + skillRestInfo = new OpenAcdSkillRestInfo(skill); + skillsRestInfo.add(skillRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortSkills(List skills) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getName().compareToIgnoreCase(skill2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getName().compareToIgnoreCase(skill1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); + } + + }); + break; + } + } + } + + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + String tempString; + int groupId = 0; + + // do not allow empty name + tempString = skillRestInfo.getName(); + if (!tempString.isEmpty()) { + skill.setName(tempString); + } + + skill.setDescription(skillRestInfo.getDescription()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + } + + private OpenAcdSkill createSkill(OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + OpenAcdSkill skill = new OpenAcdSkill(); + + // copy fields from rest info + skill.setName(skillRestInfo.getName()); + skill.setDescription(skillRestInfo.getDescription()); + skill.setAtom(skillRestInfo.getAtom()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + + return skill; + } + + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + int groupId = 0; + + try { + groupId = skillRestInfo.getId(); + skillGroup = m_openAcdContext.getSkillGroupById(groupId); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Skill Group ID " + groupId + " not found."); + } + + return skillGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdSkillsRepresentation extends XStreamRepresentation { + + public OpenAcdSkillsRepresentation(MediaType mediaType, OpenAcdSkillsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + static class OpenAcdSkillRepresentation extends XStreamRepresentation { + + public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdSkillsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_skills; + + public OpenAcdSkillsBundleRestInfo(List skills, MetadataRestInfo metadata) { + m_metadata = metadata; + m_skills = skills; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getSkills() { + return m_skills; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_atom; + private final String m_groupName; + private final int m_groupId; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_atom = skill.getAtom(); + m_groupName = skill.getGroupName(); + m_groupId = skill.getGroup().getId(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getAtom() { + return m_atom; + } + + public String getGroupName() { + return m_groupName; + } + + public int getGroupId() { + return m_groupId; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index b4ff03cb86..47530a8e81 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -20,13 +20,6 @@ package org.sipfoundry.sipxconfig.rest; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.LinkedHashSet; -import java.util.Collections; -import java.util.Comparator; - import org.restlet.data.Status; import org.restlet.data.Form; import org.restlet.resource.ResourceException; @@ -34,28 +27,28 @@ public class OpenAcdUtilities { public static int getIntFromAttribute(String attributeString) throws ResourceException { - int intFromAttribute; + int intFromAttribute; - // attempt to parse attribute provided as an id - try { - intFromAttribute = Integer.parseInt(attributeString); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attribute " + attributeString + " invalid."); - } + // attempt to parse attribute provided as an id + try { + intFromAttribute = Integer.parseInt(attributeString); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attribute " + attributeString + " invalid."); + } - return intFromAttribute; + return intFromAttribute; } public static PaginationInfo calculatePagination(Form form, int totalResults) { - PaginationInfo paginationInfo = new PaginationInfo(); - paginationInfo.totalResults = totalResults; + PaginationInfo paginationInfo = new PaginationInfo(); + paginationInfo.totalResults = totalResults; // must specify both PageNumber and ResultsPerPage together String pageNumberString = form.getFirstValue("page"); String resultsPerPageString = form.getFirstValue("pagesize"); - // attempt to parse pagination values from request + // attempt to parse pagination values from request try { paginationInfo.pageNumber = Integer.parseInt(pageNumberString); paginationInfo.resultsPerPage = Integer.parseInt(resultsPerPageString); @@ -77,75 +70,75 @@ public static PaginationInfo calculatePagination(Form form, int totalResults) { } - // do we have to paginate? - if (paginationInfo.paginate) { - paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; - - // check if only one page - //if (resultsPerPage >= totalResults) { - if (paginationInfo.totalPages == 1) { - paginationInfo.startIndex = 0; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - paginationInfo.pageNumber = 1; - // design decision: should the resultsPerPage actually be set to totalResults? - // since totalResults are already available preserve call value - } - else { - // check if specified page number is on or beyoned last page (then use last page) - if (paginationInfo.pageNumber >= paginationInfo.totalPages) { - paginationInfo.pageNumber = paginationInfo.totalPages; - paginationInfo.startIndex = (paginationInfo.totalPages - 1) * paginationInfo.resultsPerPage; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - } - else { - paginationInfo.startIndex = (paginationInfo.pageNumber - 1) * paginationInfo.resultsPerPage; - paginationInfo.endIndex = paginationInfo.startIndex + paginationInfo.resultsPerPage - 1; - } - } - } - else { - // default values assuming no pagination - paginationInfo.startIndex = 0; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - paginationInfo.pageNumber = 1; - paginationInfo.totalPages = 1; - paginationInfo.resultsPerPage = paginationInfo.totalResults; - } - - return paginationInfo; + // do we have to paginate? + if (paginationInfo.paginate) { + paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; + + // check if only one page + //if (resultsPerPage >= totalResults) { + if (paginationInfo.totalPages == 1) { + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + // design decision: should the resultsPerPage actually be set to totalResults? + // since totalResults are already available preserve call value + } + else { + // check if specified page number is on or beyoned last page (then use last page) + if (paginationInfo.pageNumber >= paginationInfo.totalPages) { + paginationInfo.pageNumber = paginationInfo.totalPages; + paginationInfo.startIndex = (paginationInfo.totalPages - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + } + else { + paginationInfo.startIndex = (paginationInfo.pageNumber - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.startIndex + paginationInfo.resultsPerPage - 1; + } + } + } + else { + // default values assuming no pagination + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + paginationInfo.totalPages = 1; + paginationInfo.resultsPerPage = paginationInfo.totalResults; + } + + return paginationInfo; } public static SortInfo calculateSorting(Form form) { - SortInfo sortInfo = new SortInfo(); + SortInfo sortInfo = new SortInfo(); String sortDirectionString = form.getFirstValue("sortdir"); String sortFieldString = form.getFirstValue("sortby"); - // check for invalid input - if ((sortDirectionString == null) || (sortFieldString == null)) { - sortInfo.sort = false; - return sortInfo; - } + // check for invalid input + if ((sortDirectionString == null) || (sortFieldString == null)) { + sortInfo.sort = false; + return sortInfo; + } - if ((sortDirectionString.isEmpty()) || (sortFieldString.isEmpty())) { - sortInfo.sort = false; - return sortInfo; - } + if ((sortDirectionString.isEmpty()) || (sortFieldString.isEmpty())) { + sortInfo.sort = false; + return sortInfo; + } - sortInfo.sort = true; + sortInfo.sort = true; - // assume forward if get anything else but "reverse" - if (sortDirectionString.toLowerCase().equals("reverse")) { - sortInfo.directionForward = false; - } - else { - sortInfo.directionForward = true; - } + // assume forward if get anything else but "reverse" + if (sortDirectionString.toLowerCase().equals("reverse")) { + sortInfo.directionForward = false; + } + else { + sortInfo.directionForward = true; + } - // tough to type-check this one - sortInfo.sortField = sortFieldString; + // tough to type-check this one + sortInfo.sortField = sortFieldString; - return sortInfo; + return sortInfo; } @@ -163,9 +156,9 @@ public static class PaginationInfo { } public static class SortInfo { - Boolean sort = false; - Boolean directionForward = true; - String sortField = ""; + Boolean sort = false; + Boolean directionForward = true; + String sortField = ""; } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index a12056ca41..49be0cd3af 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -240,4 +240,20 @@ + + + + + + + + + + + + + + + + From 58ae0c7be2e2b33db31b5198a567030d4ad4f846 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 16 Mar 2012 13:06:05 -0400 Subject: [PATCH 04/99] Removed incomplete and accidentally pushed REST api file (was supposed to be deleted) --- .../rest/OpenAcdSkillGroupsResource.java | 529 ------------------ 1 file changed, 529 deletions(-) delete mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java deleted file mode 100644 index 5ea43bfd60..0000000000 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ /dev/null @@ -1,529 +0,0 @@ -/* - * - * OpenAcdAgentGroupsResource.java - A Restlet to read group data from OpenACD within SipXecs - * Copyright (C) 2012 PATLive, D. Chang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -package org.sipfoundry.sipxconfig.rest; - -import static org.restlet.data.MediaType.APPLICATION_JSON; -import static org.restlet.data.MediaType.TEXT_XML; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.LinkedHashSet; -import java.util.Collections; -import java.util.Comparator; - -import org.restlet.Context; -import org.restlet.data.MediaType; -import org.restlet.data.Request; -import org.restlet.data.Response; -import org.restlet.data.Form; -import org.restlet.data.Status; -import org.restlet.resource.Representation; -import org.restlet.resource.ResourceException; -import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; -import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; - -public class OpenAcdSkillGroupsResource extends UserResource { - - private OpenAcdContext m_openAcdContext; - private Form m_form; - - // use to define all possible sort fields - enum SortField - { - NAME, DESCRIPTION, NONE; - - public static SortField toSortField(String fieldString) - { - if (fieldString == null) { - return NONE; - } - - try { - return valueOf(fieldString.toUpperCase()); - } - catch (Exception ex) { - return NONE; - } - } - } - - - @Override - public void init(Context context, Request request, Response response) { - super.init(context, request, response); - getVariants().add(new Variant(TEXT_XML)); - getVariants().add(new Variant(APPLICATION_JSON)); - - // pull parameters from url - m_form = getRequest().getResourceRef().getQueryAsForm(); - } - - - // Allowed REST operations - // ----------------------- - - @Override - public boolean allowGet() { - return true; - } - - @Override - public boolean allowPut() { - return true; - } - - @Override - public boolean allowDelete() { - return true; - } - - // GET - Retrieve Groups and single Group - // -------------------------------------- - - @Override - public Representation represent(Variant variant) throws ResourceException { - // process request for a single group - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - String groupIdString = (String) getRequest().getAttributes().get("id"); - - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroupRestInfo = getAgentGroupRestInfoById(groupId); - - // finally return group representation - return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); - } - - - // if not single group, process request for all groups - List agentGroups = m_openAcdContext.getAgentGroups(); - List agentGroupsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); - MetadataRestInfo metadataRestInfo; - - // sort groups if specified - sortGroups(agentGroups); - - // set requested agents groups and get resulting metadata - metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); - - // create final restinfo - OpenAcdAgentGroupsBundleRestInfo agentGroupsBundleRestInfo = new OpenAcdAgentGroupsBundleRestInfo(agentGroupsRestInfo, metadataRestInfo); - - return new OpenAcdAgentGroupsRepresentation(variant.getMediaType(), agentGroupsBundleRestInfo); - } - - - // PUT - Update or Add single Group - // -------------------------------- - - @Override - public void storeRepresentation(Representation entity) throws ResourceException { - // get group from body - OpenAcdAgentGroupRepresentation representation = new OpenAcdAgentGroupRepresentation(entity); - OpenAcdAgentGroupRestInfo agentGroupRestInfo = representation.getObject(); - OpenAcdAgentGroup agentGroup; - - // if have id then update a single group - String groupIdString = (String) getRequest().getAttributes().get("id"); - - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroup = m_openAcdContext.getAgentGroupById(groupId); - - // copy values over to existing group - updateAgentGroup(agentGroup, agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); - - return; - } - - - // otherwise add new agent group - agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); - getResponse().setStatus(Status.SUCCESS_CREATED); - } - - - // DELETE - Delete single Group - // -------------------------------- - - @Override - public void removeRepresentations() throws ResourceException { - OpenAcdAgentGroup agentGroup; - - // get id then delete a single group - String groupIdString = (String) getRequest().getAttributes().get("id"); - - if (groupIdString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(groupIdString); - agentGroup = m_openAcdContext.getAgentGroupById(groupId); - - m_openAcdContext.deleteAgentGroup(agentGroup); - - return; - } - - // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); - } - - - // Helper functions - // ---------------- - - private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int groupId) throws ResourceException { - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - - try { - agentGroupRestInfo = createAgentGroupRestInfo(groupId); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); - } - - return agentGroupRestInfo; - } - - private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int groupId) { - List skillsRestInfo; - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); - - skillsRestInfo = createSkillsRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); - - return agentGroupRestInfo; - } - - private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { - String tempString; - - // do not allow empty name - tempString = agentGroupRestInfo.getName(); - if (!tempString.isEmpty()) { - agentGroup.setName(tempString); - } - - agentGroup.setDescription(agentGroupRestInfo.getDescription()); - - // set skills (should this be a separate "setskills" api call?) - OpenAcdSkill skill; - List skillsRestInfo = agentGroupRestInfo.getSkills(); - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); - agentGroup.addSkill(skill); - } - } - - private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { - List skillsRestInfo; - - // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); - - // create list of group restinfos - for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { - OpenAcdAgentGroup agentGroup = agentGroups.get(index); - skillsRestInfo = createSkillsRestInfo(agentGroup); - - OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); - agentGroupsRestInfo.add(agentGroupRestInfo); - } - - // create metadata about agent groups - MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); - return metadata; - } - - private List createSkillsRestInfo(OpenAcdAgentGroup agentGroup) { - List skillsRestInfo; - OpenAcdSkillRestInfo skillRestInfo; - - // create list of skill restinfos for single group - Set groupSkills = agentGroup.getSkills(); - skillsRestInfo = new ArrayList(groupSkills.size()); - - for (OpenAcdSkill groupSkill : groupSkills) { - skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); - skillsRestInfo.add(skillRestInfo); - } - - return skillsRestInfo; - } - - private void sortGroups(List agentGroups) { - // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); - - if (!sortInfo.sort) { - return; - } - - SortField sortField = SortField.toSortField(sortInfo.sortField); - - if (sortInfo.directionForward) { - - switch (sortField) { - case NAME: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); - } - - }); - break; - - case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); - } - - }); - break; - } - } - else { - // must be reverse - switch (sortField) { - case NAME: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); - } - - }); - break; - - case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ - - public int compare(Object object1, Object object2) { - OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; - OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); - } - - }); - break; - } - } - } - - private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfo agentGroupRestInfo) { - OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); - - // copy fields from rest info - agentGroup.setName(agentGroupRestInfo.getName()); - agentGroup.setDescription(agentGroupRestInfo.getDescription()); - - // add skills - Set skills = new LinkedHashSet(); - List skillsRestInfo = agentGroupRestInfo.getSkills(); - - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); - } - - agentGroup.setSkills(skills); - - return agentGroup; - } - - - // REST Representations - // -------------------- - - static class OpenAcdAgentGroupsRepresentation extends XStreamRepresentation { - - public OpenAcdAgentGroupsRepresentation(MediaType mediaType, OpenAcdAgentGroupsBundleRestInfo object) { - super(mediaType, object); - } - - public OpenAcdAgentGroupsRepresentation(Representation representation) { - super(representation); - } - - @Override - protected void configureXStream(XStream xstream) { - xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); - xstream.alias("group", OpenAcdAgentGroupRestInfo.class); - xstream.alias("skill", OpenAcdSkillRestInfo.class); - } - } - - static class OpenAcdAgentGroupRepresentation extends XStreamRepresentation { - - public OpenAcdAgentGroupRepresentation(MediaType mediaType, OpenAcdAgentGroupRestInfo object) { - super(mediaType, object); - } - - public OpenAcdAgentGroupRepresentation(Representation representation) { - super(representation); - } - - @Override - protected void configureXStream(XStream xstream) { - xstream.alias("group", OpenAcdAgentGroupRestInfo.class); - xstream.alias("skill", OpenAcdSkillRestInfo.class); - } - } - - - // REST info objects - // ----------------- - - static class OpenAcdAgentGroupRestInfo { - private final String m_name; - private final int m_id; - private final String m_description; - private final List m_skills; - - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills) { - m_name = agentGroup.getName(); - m_id = agentGroup.getId(); - m_description = agentGroup.getDescription(); - m_skills = skills; - } - - public String getName() { - return m_name; - } - - public int getId() { - return m_id; - } - - public String getDescription() { - return m_description; - } - - public List getSkills() { - return m_skills; - } - } - - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - - static class OpenAcdAgentGroupsBundleRestInfo { - private final MetadataRestInfo m_metadata; - private final List m_groups; - - public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { - m_metadata = metadata; - m_groups = agentGroups; - } - - public MetadataRestInfo getMetadata() { - return m_metadata; - } - - public List getGroups() { - return m_groups; - } - } - - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - - - // Injected objects - // ---------------- - - @Required - public void setOpenAcdContext(OpenAcdContext openAcdContext) { - m_openAcdContext = openAcdContext; - } - -} From 12fc6be02fdfc0384d5e64ae3bbee9d7b3dce9a5 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 16 Mar 2012 15:06:12 -0400 Subject: [PATCH 05/99] Correct syntax error --- .../rest/OpenAcdAgentGroupsResource.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index ba89b117a1..d88b5d02f8 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -107,7 +107,7 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { - // process request for a single group + // process request for single OpenAcdAgentGroupRestInfo agentGroupRestInfo; String idString = (String) getRequest().getAttributes().get("id"); @@ -120,16 +120,16 @@ public Representation represent(Variant variant) throws ResourceException { } - // if not single group, process request for all groups + // if not single, process request for all List agentGroups = m_openAcdContext.getAgentGroups(); List agentGroupsRestInfo = new ArrayList(); Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; - // sort groups if specified + // sort if specified sortGroups(agentGroups); - // set requested agents groups and get resulting metadata + // set requested based on pagination and get resulting metadata metadataRestInfo = addAgentGroups(agentGroupsRestInfo, agentGroups); // create final restinfo @@ -153,8 +153,8 @@ public void storeRepresentation(Representation entity) throws ResourceException String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int groupId = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroup = m_openAcdContext.getAgentGroupById(id); + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agentGroup = m_openAcdContext.getAgentGroupById(idInt); // copy values over to existing group updateAgentGroup(agentGroup, agentGroupRestInfo); @@ -198,14 +198,14 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int groupId) throws ResourceException { + private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int id) throws ResourceException { OpenAcdAgentGroupRestInfo agentGroupRestInfo; try { - agentGroupRestInfo = createAgentGroupRestInfo(groupId); + agentGroupRestInfo = createAgentGroupRestInfo(id); } catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + groupId + " not found."); + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); } return agentGroupRestInfo; From b091c2672e72bc3ecca6de13b7ef542af42a64bc Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 16 Mar 2012 18:34:55 -0400 Subject: [PATCH 06/99] Added OpenACD Skill Groups REST API, some other cleanup --- .../rest/OpenAcdSkillGroupsResource.java | 454 ++++++++++++++++++ .../rest/OpenAcdSkillsResource.java | 6 +- 2 files changed, 457 insertions(+), 3 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java new file mode 100644 index 0000000000..8d91d6f472 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -0,0 +1,454 @@ +/* + * + * OpenAcdAgentGroupsResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdSkillGroupsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdSkillGroupRestInfo skillGroupRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroupRestInfo = getSkillGroupRestInfoById(idInt); + + // return representation + return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); + } + + + // if not single, process request for list + List skillGroups = m_openAcdContext.getSkillGroups(); + List skillGroupsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortSkillGroups(skillGroups); + + // set requested records and get resulting metadata + metadataRestInfo = addSkillGroups(skillGroupsRestInfo, skillGroups); + + // create final restinfo + OpenAcdSkillGroupsBundleRestInfo skillGroupsBundleRestInfo = new OpenAcdSkillGroupsBundleRestInfo(skillGroupsRestInfo, metadataRestInfo); + + return new OpenAcdSkillGroupsRepresentation(variant.getMediaType(), skillGroupsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSkillGroupRepresentation representation = new OpenAcdSkillGroupRepresentation(entity); + OpenAcdSkillGroupRestInfo skillGroupRestInfo = representation.getObject(); + OpenAcdSkillGroup skillGroup; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroup = m_openAcdContext.getSkillGroupById(idInt); + + // copy values over to existing + updateSkillGroup(skillGroup, skillGroupRestInfo); + m_openAcdContext.saveSkillGroup(skillGroup); + + return; + } + + + // otherwise add new + skillGroup = createSkillGroup(skillGroupRestInfo); + m_openAcdContext.saveSkillGroup(skillGroup); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + // deleteSkillGroup() not available from openAcdContext + /* + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSkillGroup skillGroup; + + // get id then delete a single group + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroup = m_openAcdContext.getSkillGroupById(idInt); + + m_openAcdContext.deleteSkillGroup(skillGroup); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } +*/ + + // Helper functions + // ---------------- + + private OpenAcdSkillGroupRestInfo getSkillGroupRestInfoById(int id) throws ResourceException { + OpenAcdSkillGroupRestInfo skillGroupRestInfo; + + try { + skillGroupRestInfo = createSkillGroupRestInfo(id); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return skillGroupRestInfo; + } + + private OpenAcdSkillGroupRestInfo createSkillGroupRestInfo(int id) { + OpenAcdSkillGroupRestInfo skillRestInfo; + OpenAcdSkillGroup skillGroup = m_openAcdContext.getSkillGroupById(id); + + skillRestInfo = new OpenAcdSkillGroupRestInfo(skillGroup); + + return skillRestInfo; + } + + private MetadataRestInfo addSkillGroups(List skillsRestInfo, List skillGroups) { + OpenAcdSkillGroupRestInfo skillRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skillGroups.size()); + + // create list of skill restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdSkillGroup skillGroup = skillGroups.get(index); + + skillRestInfo = new OpenAcdSkillGroupRestInfo(skillGroup); + skillsRestInfo.add(skillRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortSkillGroups(List skillGroups) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(skillGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return skillGroup1.getName().compareToIgnoreCase(skillGroup2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skillGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return skillGroup1.getDescription().compareToIgnoreCase(skillGroup2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getName().compareToIgnoreCase(skill1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skillGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return skillGroup2.getDescription().compareToIgnoreCase(skillGroup1.getDescription()); + } + + }); + break; + } + } + } + + private void updateSkillGroup(OpenAcdSkillGroup skillGroup, OpenAcdSkillGroupRestInfo skillGroupRestInfo) throws ResourceException { + String tempString; + + // do not allow empty name + tempString = skillGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + skillGroup.setName(tempString); + } + + skillGroup.setDescription(skillGroupRestInfo.getDescription()); + } + + private OpenAcdSkillGroup createSkillGroup(OpenAcdSkillGroupRestInfo skillGroupRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup = new OpenAcdSkillGroup(); + + // copy fields from rest info + skillGroup.setName(skillGroupRestInfo.getName()); + skillGroup.setDescription(skillGroupRestInfo.getDescription()); + + return skillGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdSkillGroupsRepresentation extends XStreamRepresentation { + + public OpenAcdSkillGroupsRepresentation(MediaType mediaType, OpenAcdSkillGroupsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillGroupsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-skill-group", OpenAcdSkillGroupsBundleRestInfo.class); + xstream.alias("group", OpenAcdSkillGroupRestInfo.class); + } + } + + static class OpenAcdSkillGroupRepresentation extends XStreamRepresentation { + + public OpenAcdSkillGroupRepresentation(MediaType mediaType, OpenAcdSkillGroupRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillGroupRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("group", OpenAcdSkillGroupRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdSkillGroupsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_skills; + + public OpenAcdSkillGroupsBundleRestInfo(List skills, MetadataRestInfo metadata) { + m_metadata = metadata; + m_skills = skills; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getSkills() { + return m_skills; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdSkillGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdSkillGroupRestInfo(OpenAcdSkillGroup skillGroup) { + m_id = skillGroup.getId(); + m_name = skillGroup.getName(); + m_description = skillGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index ecd9bf0e7a..e82a185bec 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -105,7 +105,7 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { - // process request for a single skill + // process request for single OpenAcdSkillRestInfo skillRestInfo; String idString = (String) getRequest().getAttributes().get("id"); @@ -176,7 +176,7 @@ public void storeRepresentation(Representation entity) throws ResourceException public void removeRepresentations() throws ResourceException { OpenAcdSkill skill; - // get id then delete a single group + // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { @@ -203,7 +203,7 @@ private OpenAcdSkillRestInfo getSkillRestInfoById(int id) throws ResourceExcepti skillRestInfo = createSkillRestInfo(id); } catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Group ID " + id + " not found."); + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); } return skillRestInfo; From 38c2d8bba3eed07931741494c1ca32a8ce60dc67 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 01:49:55 -0400 Subject: [PATCH 07/99] Add Skills Group REST API, also do some cleanup and bug fix --- .../rest/OpenAcdSkillGroupsResource.java | 18 +++++++++--------- .../sipxconfig/rest/OpenAcdSkillsResource.java | 2 +- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 8d91d6f472..64248ee4cd 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -25,6 +25,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Collection; +import java.util.HashSet; import java.util.Collections; import java.util.Comparator; @@ -173,19 +175,18 @@ public void storeRepresentation(Representation entity) throws ResourceException // ---------------------------- // deleteSkillGroup() not available from openAcdContext - /* @Override public void removeRepresentations() throws ResourceException { - OpenAcdSkillGroup skillGroup; - - // get id then delete a single group + Collection skillGroupIds = new HashSet(); + + // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillGroup = m_openAcdContext.getSkillGroupById(idInt); - - m_openAcdContext.deleteSkillGroup(skillGroup); + + skillGroupIds.add(idInt); + m_openAcdContext.removeSkillGroups(skillGroupIds); return; } @@ -193,7 +194,6 @@ public void removeRepresentations() throws ResourceException { // no id string getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } -*/ // Helper functions // ---------------- @@ -281,7 +281,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(skills, new Comparator(){ + Collections.sort(skillGroups, new Comparator(){ public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index e82a185bec..b7f4c17b6d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -342,7 +342,7 @@ private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfo skillRestInfo) thro int groupId = 0; try { - groupId = skillRestInfo.getId(); + groupId = skillRestInfo.getGroupId(); skillGroup = m_openAcdContext.getSkillGroupById(groupId); } catch (Exception exception) { diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 49be0cd3af..3d4f0c7a80 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -256,4 +256,20 @@ + + + + + + + + + + + + + + + + From 84a5c867d658efb018674f3f141c054b08d5a731 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 13:09:38 -0400 Subject: [PATCH 08/99] Minor code format corrections --- .../sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 64248ee4cd..89c0dfb5b1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -53,7 +53,7 @@ public class OpenAcdSkillGroupsResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField + private enum SortField { NAME, DESCRIPTION, NONE; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index b7f4c17b6d..a6893ba608 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -51,7 +51,7 @@ public class OpenAcdSkillsResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField + private enum SortField { NAME, DESCRIPTION, NONE; From 372cfa3e9453be3f0137395cb9f5494181341500 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 13:10:31 -0400 Subject: [PATCH 09/99] Correct update of existing Agent Group to replace skills, not just add to them --- .../sipxconfig/rest/OpenAcdAgentGroupsResource.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index d88b5d02f8..c42fbd58f4 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -53,7 +53,7 @@ public class OpenAcdAgentGroupsResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField + private enum SortField { NAME, DESCRIPTION, NONE; @@ -233,7 +233,10 @@ private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRes agentGroup.setDescription(agentGroupRestInfo.getDescription()); - // set skills (should this be a separate "setskills" api call?) + // remove all current skills + agentGroup.getSkills().clear(); + + // set skills OpenAcdSkill skill; List skillsRestInfo = agentGroupRestInfo.getSkills(); for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { From 61596e7a2225c5bb097a1ec0f037707c135ea1cd Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 13:18:21 -0400 Subject: [PATCH 10/99] Move code around for better organization and consistency with other REST APIs --- .../rest/OpenAcdAgentGroupsResource.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index c42fbd58f4..0ce6681d9a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -412,6 +412,24 @@ protected void configureXStream(XStream xstream) { // REST info objects // ----------------- + static class OpenAcdAgentGroupsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_groups; + + public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { + m_metadata = metadata; + m_groups = agentGroups; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getGroups() { + return m_groups; + } + } + static class OpenAcdAgentGroupRestInfo { private final String m_name; private final int m_id; @@ -472,24 +490,6 @@ public int getResultsPerPage() { } } - static class OpenAcdAgentGroupsBundleRestInfo { - private final MetadataRestInfo m_metadata; - private final List m_groups; - - public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { - m_metadata = metadata; - m_groups = agentGroups; - } - - public MetadataRestInfo getMetadata() { - return m_metadata; - } - - public List getGroups() { - return m_groups; - } - } - static class OpenAcdSkillRestInfo { private final int m_id; private final String m_name; From 2031327b52915290a7e2e7543ba38f1b132765e9 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 15:58:56 -0400 Subject: [PATCH 11/99] Add OpenACD Clients REST API --- .../rest/OpenAcdClientsResource.java | 461 ++++++++++++++++++ 1 file changed, 461 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java new file mode 100644 index 0000000000..3f68abf164 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -0,0 +1,461 @@ +/* + * + * OpenAcdClientsResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, I. Wesson, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import java.util.HashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; + +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdClientsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Client + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdClientRestInfo clientRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + clientRestInfo = getClientRestInfoById(idInt); + + // return representation + return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); + } + + + // if not single, process request for list + List clients = m_openAcdContext.getClients(); + List clientsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortClients(clients); + + // set requested records and get resulting metadata + metadataRestInfo = addClients(clientsRestInfo, clients); + + // create final restinfo + OpenAcdClientsBundleRestInfo clientsBundleRestInfo = new OpenAcdClientsBundleRestInfo(clientsRestInfo, metadataRestInfo); + + return new OpenAcdClientsRepresentation(variant.getMediaType(), clientsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdClientRepresentation representation = new OpenAcdClientRepresentation(entity); + OpenAcdClientRestInfo clientRestInfo = representation.getObject(); + OpenAcdClient client; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + client = m_openAcdContext.getClientById(idInt); + + // copy values over to existing + updateClient(client, clientRestInfo); + m_openAcdContext.saveClient(client); + + return; + } + + + // otherwise add new + client = createClient(clientRestInfo); + m_openAcdContext.saveClient(client); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + // deleteClient() not available from openAcdContext + @Override + public void removeRepresentations() throws ResourceException { + Collection clientIds = new HashSet(); + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + + clientIds.add(idInt); + m_openAcdContext.deleteClient(m_openAcdContext.getClientById(idInt)); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + // Helper functions + // ---------------- + + private OpenAcdClientRestInfo getClientRestInfoById(int id) throws ResourceException { + OpenAcdClientRestInfo clientRestInfo; + + try { + clientRestInfo = createClientRestInfo(id); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return clientRestInfo; + } + + private OpenAcdClientRestInfo createClientRestInfo(int id) { + OpenAcdClientRestInfo clientRestInfo; + OpenAcdClient client = m_openAcdContext.getClientById(id); + + clientRestInfo = new OpenAcdClientRestInfo(client); + + return clientRestInfo; + } + + private MetadataRestInfo addClients(List clientsRestInfo, List clients) { + OpenAcdClientRestInfo clientRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, clients.size()); + + // create list of client restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdClient client = clients.get(index); + + clientRestInfo = new OpenAcdClientRestInfo(client); + clientsRestInfo.add(clientRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortClients(List clients) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(clients, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdClient client1 = (OpenAcdClient) object1; + OpenAcdClient client2 = (OpenAcdClient) object2; + return client1.getName().compareToIgnoreCase(client2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(clients, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdClient client1 = (OpenAcdClient) object1; + OpenAcdClient client2 = (OpenAcdClient) object2; + return client1.getDescription().compareToIgnoreCase(client2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(clients, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdClient client1 = (OpenAcdClient) object1; + OpenAcdClient client2 = (OpenAcdClient) object2; + return client2.getName().compareToIgnoreCase(client1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(clients, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdClient client1 = (OpenAcdClient) object1; + OpenAcdClient client2 = (OpenAcdClient) object2; + return client2.getDescription().compareToIgnoreCase(client1.getDescription()); + } + + }); + break; + } + } + } + + private void updateClient(OpenAcdClient client, OpenAcdClientRestInfo clientRestInfo) throws ResourceException { + String tempString; + + // do not allow empty name + tempString = clientRestInfo.getName(); + if (!tempString.isEmpty()) { + client.setName(tempString); + } + + client.setDescription(clientRestInfo.getDescription()); + } + + private OpenAcdClient createClient(OpenAcdClientRestInfo clientRestInfo) throws ResourceException { + OpenAcdClient client = new OpenAcdClient(); + + // copy fields from rest info + client.setName(clientRestInfo.getName()); + client.setDescription(clientRestInfo.getDescription()); + client.setIdentity(clientRestInfo.getIdentity()); + + return client; + } + + + // REST Representations + // -------------------- + + static class OpenAcdClientsRepresentation extends XStreamRepresentation { + + public OpenAcdClientsRepresentation(MediaType mediaType, OpenAcdClientsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdClientsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-client", OpenAcdClientsBundleRestInfo.class); + xstream.alias("client", OpenAcdClientRestInfo.class); + } + } + + static class OpenAcdClientRepresentation extends XStreamRepresentation { + + public OpenAcdClientRepresentation(MediaType mediaType, OpenAcdClientRestInfo object) { + super(mediaType, object); + } + + public OpenAcdClientRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("client", OpenAcdClientRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdClientsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_clients; + + public OpenAcdClientsBundleRestInfo(List clients, MetadataRestInfo metadata) { + m_metadata = metadata; + m_clients = clients; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getClients() { + return m_clients; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdClientRestInfo { + private final int m_id; + private final String m_identity; + private final String m_name; + private final String m_description; + + public OpenAcdClientRestInfo(OpenAcdClient client) { + m_id = client.getId(); + m_identity = client.getIdentity(); + m_name = client.getName(); + m_description = client.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getIdentity() { + return m_identity; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} From da816d63f08b06864d3a999b96949d6c42160065 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 15:59:25 -0400 Subject: [PATCH 12/99] Add OpenACD Client REST API --- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 3d4f0c7a80..c4908313fd 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -272,4 +272,20 @@ + + + + + + + + + + + + + + + + From 126a55a90dab87baf139c92e57b2525b12da4756 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 16:05:16 -0400 Subject: [PATCH 13/99] Change order of fields in Client RestInfo --- .../sipxconfig/rest/OpenAcdClientsResource.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 3f68abf164..fbae211af6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -421,14 +421,14 @@ public int getResultsPerPage() { static class OpenAcdClientRestInfo { private final int m_id; - private final String m_identity; private final String m_name; + private final String m_identity; private final String m_description; public OpenAcdClientRestInfo(OpenAcdClient client) { m_id = client.getId(); - m_identity = client.getIdentity(); m_name = client.getName(); + m_identity = client.getIdentity(); m_description = client.getDescription(); } @@ -436,14 +436,14 @@ public int getId() { return m_id; } - public String getIdentity() { - return m_identity; - } - public String getName() { return m_name; } + public String getIdentity() { + return m_identity; + } + public String getDescription() { return m_description; } From 102803e8902ff30d37066eb2f7be974a807e4418 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 18:37:42 -0400 Subject: [PATCH 14/99] Add OpenACD Queue Groups REST API --- .../rest/OpenAcdQueueGroupsResource.java | 532 ++++++++++++++++++ 1 file changed, 532 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java new file mode 100644 index 0000000000..d0bda7eaad --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -0,0 +1,532 @@ +/* + * + * OpenAcdQueueGroupsResource.java - A Restlet to read group data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdQueueGroupsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + private enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve Groups and single Group + // -------------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdQueueGroupRestInfo queueGroupRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroupRestInfo = getQueueGroupRestInfoById(idInt); + + // finally return group representation + return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); + } + + + // if not single, process request for all + List queueGroups = m_openAcdContext.getQueueGroups(); + List queueGroupsRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort if specified + sortGroups(queueGroups); + + // set requested based on pagination and get resulting metadata + metadataRestInfo = addQueueGroups(queueGroupsRestInfo, queueGroups); + + // create final restinfo + OpenAcdQueueGroupsBundleRestInfo queueGroupsBundleRestInfo = new OpenAcdQueueGroupsBundleRestInfo(queueGroupsRestInfo, metadataRestInfo); + + return new OpenAcdQueueGroupsRepresentation(variant.getMediaType(), queueGroupsBundleRestInfo); + } + + + // PUT - Update or Add single Group + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get group from body + OpenAcdQueueGroupRepresentation representation = new OpenAcdQueueGroupRepresentation(entity); + OpenAcdQueueGroupRestInfo queueGroupRestInfo = representation.getObject(); + OpenAcdQueueGroup queueGroup; + + // if have id then update a single group + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroup = m_openAcdContext.getQueueGroupById(idInt); + + // copy values over to existing group + updateQueueGroup(queueGroup, queueGroupRestInfo); + m_openAcdContext.saveQueueGroup(queueGroup); + + return; + } + + + // otherwise add new agent group + queueGroup = createOpenAcdQueueGroup(queueGroupRestInfo); + m_openAcdContext.saveQueueGroup(queueGroup); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Group + // -------------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdQueueGroup queueGroup; + + // get id then delete a single group + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroup = m_openAcdContext.getQueueGroupById(idInt); + + m_openAcdContext.deleteQueueGroup(queueGroup); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + + // Helper functions + // ---------------- + + private OpenAcdQueueGroupRestInfo getQueueGroupRestInfoById(int id) throws ResourceException { + OpenAcdQueueGroupRestInfo queueGroupRestInfo; + + try { + queueGroupRestInfo = createQueueGroupRestInfo(id); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return queueGroupRestInfo; + } + + private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int groupId) { + List skillsRestInfo; + OpenAcdQueueGroupRestInfo queueGroupRestInfo; + OpenAcdQueueGroup agentGroup = m_openAcdContext.getQueueGroupById(groupId); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(agentGroup, skillsRestInfo); + + return queueGroupRestInfo; + } + + private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfo queueGroupRestInfo) { + String tempString; + + // do not allow empty name + tempString = queueGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + queueGroup.setName(tempString); + } + + queueGroup.setDescription(queueGroupRestInfo.getDescription()); + + // remove all current skills + queueGroup.getSkills().clear(); + + // set skills + OpenAcdSkill skill; + List skillsRestInfo = queueGroupRestInfo.getSkills(); + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + queueGroup.addSkill(skill); + } + } + + private MetadataRestInfo addQueueGroups(List queueGroupsRestInfo, List queueGroups) { + List skillsRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queueGroups.size()); + + // create list of group restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdQueueGroup queueGroup = queueGroups.get(index); + skillsRestInfo = createSkillsRestInfo(queueGroup); + + OpenAcdQueueGroupRestInfo queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo); + queueGroupsRestInfo.add(queueGroupRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private List createSkillsRestInfo(OpenAcdQueueGroup queueGroup) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + Set groupSkills = queueGroup.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } + + return skillsRestInfo; + } + + private void sortGroups(List queueGroups) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(queueGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; + OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; + return queueGroup1.getName().compareToIgnoreCase(queueGroup2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(queueGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; + OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; + return queueGroup1.getDescription().compareToIgnoreCase(queueGroup2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(queueGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; + OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; + return queueGroup2.getName().compareToIgnoreCase(queueGroup1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(queueGroups, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; + OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; + return queueGroup2.getDescription().compareToIgnoreCase(queueGroup1.getDescription()); + } + + }); + break; + } + } + } + + private OpenAcdQueueGroup createOpenAcdQueueGroup(OpenAcdQueueGroupRestInfo queueGroupRestInfo) { + OpenAcdQueueGroup queueGroup = new OpenAcdQueueGroup(); + + // copy fields from rest info + queueGroup.setName(queueGroupRestInfo.getName()); + queueGroup.setDescription(queueGroupRestInfo.getDescription()); + + // add skills + Set skills = new LinkedHashSet(); + List skillsRestInfo = queueGroupRestInfo.getSkills(); + + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + } + + queueGroup.setSkills(skills); + + return queueGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdQueueGroupsRepresentation extends XStreamRepresentation { + + public OpenAcdQueueGroupsRepresentation(MediaType mediaType, OpenAcdQueueGroupsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdQueueGroupsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-queue-group", OpenAcdQueueGroupsBundleRestInfo.class); + xstream.alias("group", OpenAcdQueueGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + static class OpenAcdQueueGroupRepresentation extends XStreamRepresentation { + + public OpenAcdQueueGroupRepresentation(MediaType mediaType, OpenAcdQueueGroupRestInfo object) { + super(mediaType, object); + } + + public OpenAcdQueueGroupRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("group", OpenAcdQueueGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdQueueGroupsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_groups; + + public OpenAcdQueueGroupsBundleRestInfo(List queueGroups, MetadataRestInfo metadata) { + m_metadata = metadata; + m_groups = queueGroups; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getGroups() { + return m_groups; + } + } + + static class OpenAcdQueueGroupRestInfo { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + + public OpenAcdQueueGroupRestInfo(OpenAcdQueueGroup queueGroup, List skills) { + m_name = queueGroup.getName(); + m_id = queueGroup.getId(); + m_description = queueGroup.getDescription(); + m_skills = skills; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} From 7b838b24764dbd6bdabd23b88945ac6335da84c7 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 18:38:38 -0400 Subject: [PATCH 15/99] Add OpenACD Queue Group REST API --- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index c4908313fd..d88234fdb9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -288,4 +288,20 @@ + + + + + + + + + + + + + + + + From bec0b687369c851114858ff786d767ed6bbf9b40 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 20 Mar 2012 18:38:58 -0400 Subject: [PATCH 16/99] Change order of fields in Client REST Info --- .../sipxconfig/rest/OpenAcdClientsResource.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index fbae211af6..94e9580078 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -422,14 +422,14 @@ public int getResultsPerPage() { static class OpenAcdClientRestInfo { private final int m_id; private final String m_name; - private final String m_identity; private final String m_description; + private final String m_identity; public OpenAcdClientRestInfo(OpenAcdClient client) { m_id = client.getId(); m_name = client.getName(); - m_identity = client.getIdentity(); m_description = client.getDescription(); + m_identity = client.getIdentity(); } public int getId() { @@ -440,13 +440,13 @@ public String getName() { return m_name; } - public String getIdentity() { - return m_identity; - } - public String getDescription() { return m_description; } + + public String getIdentity() { + return m_identity; + } } From b6ab4a8818f10f87f3a9cbc4b53fe63cfb7604f1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 21 Mar 2012 03:11:14 -0400 Subject: [PATCH 17/99] Include list of Queues with "Skills" list --- .../rest/OpenAcdAgentGroupsResource.java | 89 +++++++++++++++---- 1 file changed, 70 insertions(+), 19 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 0ce6681d9a..59a5610706 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -43,6 +43,7 @@ import com.thoughtworks.xstream.XStream; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; @@ -113,7 +114,7 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroupRestInfo = getAgentGroupRestInfoById(idInt); + agentGroupRestInfo = createAgentGroupRestInfo(idInt); // finally return group representation return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); @@ -198,11 +199,17 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int id) throws ResourceException { + private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws ResourceException { OpenAcdAgentGroupRestInfo agentGroupRestInfo; try { - agentGroupRestInfo = createAgentGroupRestInfo(id); + List skillsRestInfo; + List queuesRestInfo; + OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(id); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + queuesRestInfo = createQueuesRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -210,18 +217,7 @@ private OpenAcdAgentGroupRestInfo getAgentGroupRestInfoById(int id) throws Resou return agentGroupRestInfo; } - - private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int groupId) { - List skillsRestInfo; - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(groupId); - - skillsRestInfo = createSkillsRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); - - return agentGroupRestInfo; - } - + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { String tempString; @@ -247,6 +243,7 @@ private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRes private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { List skillsRestInfo; + List queuesRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); @@ -255,8 +252,9 @@ private MetadataRestInfo addAgentGroups(List agentGro for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdAgentGroup agentGroup = agentGroups.get(index); skillsRestInfo = createSkillsRestInfo(agentGroup); - - OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo); + queuesRestInfo = createQueuesRestInfo(agentGroup); + + OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } @@ -281,6 +279,22 @@ private List createSkillsRestInfo(OpenAcdAgentGroup agentG return skillsRestInfo; } + private List createQueuesRestInfo(OpenAcdAgentGroup agentGroup) { + List queuesRestInfo; + OpenAcdQueueRestInfo queueRestInfo; + + // create list of queue restinfos for single group + Set groupQueues = agentGroup.getQueues(); + queuesRestInfo = new ArrayList(groupQueues.size()); + + for (OpenAcdQueue groupQueue : groupQueues) { + queueRestInfo = new OpenAcdQueueRestInfo(groupQueue); + queuesRestInfo.add(queueRestInfo); + } + + return queuesRestInfo; + } + private void sortGroups(List agentGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); @@ -323,7 +337,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(agentGroups, new Comparator(){ + Collections.sort(agentGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; @@ -388,6 +402,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); xstream.alias("group", OpenAcdAgentGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfo.class); } } @@ -405,6 +420,7 @@ public OpenAcdAgentGroupRepresentation(Representation representation) { protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdAgentGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfo.class); } } @@ -435,12 +451,14 @@ static class OpenAcdAgentGroupRestInfo { private final int m_id; private final String m_description; private final List m_skills; + private final List m_queues; - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills) { + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills, List queues) { m_name = agentGroup.getName(); m_id = agentGroup.getId(); m_description = agentGroup.getDescription(); m_skills = skills; + m_queues = queues; } public String getName() { @@ -458,6 +476,10 @@ public String getDescription() { public List getSkills() { return m_skills; } + + public List getQueues() { + return m_queues; + } } static class MetadataRestInfo { @@ -520,6 +542,35 @@ public String getGroupName() { } } + static class OpenAcdQueueRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdQueueRestInfo(OpenAcdQueue queue) { + m_id = queue.getId(); + m_name = queue.getName(); + m_description = queue.getDescription(); + m_groupName = queue.getQueueGroup(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } // Injected objects // ---------------- From 04e53071283c106265f1281b0b91fae6ac3895db Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 21 Mar 2012 03:11:50 -0400 Subject: [PATCH 18/99] Merge two functions to improve code structure --- .../rest/OpenAcdClientsResource.java | 16 ++++--------- .../rest/OpenAcdQueueGroupsResource.java | 23 +++++++------------ .../rest/OpenAcdSkillGroupsResource.java | 16 ++++--------- .../rest/OpenAcdSkillsResource.java | 16 ++++--------- 4 files changed, 20 insertions(+), 51 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 94e9580078..603fdaecfa 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -113,7 +113,7 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - clientRestInfo = getClientRestInfoById(idInt); + clientRestInfo = createClientRestInfo(idInt); // return representation return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); @@ -198,11 +198,12 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdClientRestInfo getClientRestInfoById(int id) throws ResourceException { + private OpenAcdClientRestInfo createClientRestInfo(int id) throws ResourceException { OpenAcdClientRestInfo clientRestInfo; try { - clientRestInfo = createClientRestInfo(id); + OpenAcdClient client = m_openAcdContext.getClientById(id); + clientRestInfo = new OpenAcdClientRestInfo(client); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -211,15 +212,6 @@ private OpenAcdClientRestInfo getClientRestInfoById(int id) throws ResourceExcep return clientRestInfo; } - private OpenAcdClientRestInfo createClientRestInfo(int id) { - OpenAcdClientRestInfo clientRestInfo; - OpenAcdClient client = m_openAcdContext.getClientById(id); - - clientRestInfo = new OpenAcdClientRestInfo(client); - - return clientRestInfo; - } - private MetadataRestInfo addClients(List clientsRestInfo, List clients) { OpenAcdClientRestInfo clientRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index d0bda7eaad..0176d525f0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -113,7 +113,7 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueGroupRestInfo = getQueueGroupRestInfoById(idInt); + queueGroupRestInfo = createQueueGroupRestInfo(idInt); // finally return group representation return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); @@ -198,11 +198,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdQueueGroupRestInfo getQueueGroupRestInfoById(int id) throws ResourceException { + private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int id) throws ResourceException { OpenAcdQueueGroupRestInfo queueGroupRestInfo; try { - queueGroupRestInfo = createQueueGroupRestInfo(id); + List skillsRestInfo; + OpenAcdQueueGroup agentGroup = m_openAcdContext.getQueueGroupById(id); + + skillsRestInfo = createSkillsRestInfo(agentGroup); + queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(agentGroup, skillsRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -210,18 +214,7 @@ private OpenAcdQueueGroupRestInfo getQueueGroupRestInfoById(int id) throws Resou return queueGroupRestInfo; } - - private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int groupId) { - List skillsRestInfo; - OpenAcdQueueGroupRestInfo queueGroupRestInfo; - OpenAcdQueueGroup agentGroup = m_openAcdContext.getQueueGroupById(groupId); - - skillsRestInfo = createSkillsRestInfo(agentGroup); - queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(agentGroup, skillsRestInfo); - - return queueGroupRestInfo; - } - + private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfo queueGroupRestInfo) { String tempString; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 89c0dfb5b1..ce39bda3de 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -113,7 +113,7 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillGroupRestInfo = getSkillGroupRestInfoById(idInt); + skillGroupRestInfo = createSkillGroupRestInfo(idInt); // return representation return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); @@ -198,11 +198,12 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdSkillGroupRestInfo getSkillGroupRestInfoById(int id) throws ResourceException { + private OpenAcdSkillGroupRestInfo createSkillGroupRestInfo(int id) throws ResourceException { OpenAcdSkillGroupRestInfo skillGroupRestInfo; try { - skillGroupRestInfo = createSkillGroupRestInfo(id); + OpenAcdSkillGroup skillGroup = m_openAcdContext.getSkillGroupById(id); + skillGroupRestInfo = new OpenAcdSkillGroupRestInfo(skillGroup); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -211,15 +212,6 @@ private OpenAcdSkillGroupRestInfo getSkillGroupRestInfoById(int id) throws Resou return skillGroupRestInfo; } - private OpenAcdSkillGroupRestInfo createSkillGroupRestInfo(int id) { - OpenAcdSkillGroupRestInfo skillRestInfo; - OpenAcdSkillGroup skillGroup = m_openAcdContext.getSkillGroupById(id); - - skillRestInfo = new OpenAcdSkillGroupRestInfo(skillGroup); - - return skillRestInfo; - } - private MetadataRestInfo addSkillGroups(List skillsRestInfo, List skillGroups) { OpenAcdSkillGroupRestInfo skillRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index a6893ba608..637657a068 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -111,7 +111,7 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillRestInfo = getSkillRestInfoById(idInt); + skillRestInfo = createSkillRestInfo(idInt); // finally return group representation return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); @@ -196,11 +196,12 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdSkillRestInfo getSkillRestInfoById(int id) throws ResourceException { + private OpenAcdSkillRestInfo createSkillRestInfo(int id) throws ResourceException { OpenAcdSkillRestInfo skillRestInfo; try { - skillRestInfo = createSkillRestInfo(id); + OpenAcdSkill skill = m_openAcdContext.getSkillById(id); + skillRestInfo = new OpenAcdSkillRestInfo(skill); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -209,15 +210,6 @@ private OpenAcdSkillRestInfo getSkillRestInfoById(int id) throws ResourceExcepti return skillRestInfo; } - private OpenAcdSkillRestInfo createSkillRestInfo(int id) { - OpenAcdSkillRestInfo skillRestInfo; - OpenAcdSkill skill = m_openAcdContext.getSkillById(id); - - skillRestInfo = new OpenAcdSkillRestInfo(skill); - - return skillRestInfo; - } - private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { OpenAcdSkillRestInfo skillRestInfo; From ee8a5369677da188940f66fa9622946b842b9778 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 21 Mar 2012 22:56:18 -0400 Subject: [PATCH 19/99] Add Agent Groups to list of "Skills" --- .../rest/OpenAcdQueueGroupsResource.java | 68 +++++++++++++++++-- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 0176d525f0..0b71449c64 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -43,6 +43,7 @@ import com.thoughtworks.xstream.XStream; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; @@ -200,13 +201,16 @@ public void removeRepresentations() throws ResourceException { private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int id) throws ResourceException { OpenAcdQueueGroupRestInfo queueGroupRestInfo; + List skillsRestInfo; + List agentGroupRestInfo; try { - List skillsRestInfo; - OpenAcdQueueGroup agentGroup = m_openAcdContext.getQueueGroupById(id); + OpenAcdQueueGroup queueGroup = m_openAcdContext.getQueueGroupById(id); - skillsRestInfo = createSkillsRestInfo(agentGroup); - queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(agentGroup, skillsRestInfo); + skillsRestInfo = createSkillsRestInfo(queueGroup); + agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + + queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo, agentGroupRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -240,6 +244,7 @@ private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRes private MetadataRestInfo addQueueGroups(List queueGroupsRestInfo, List queueGroups) { List skillsRestInfo; + List agentGroupRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queueGroups.size()); @@ -247,9 +252,11 @@ private MetadataRestInfo addQueueGroups(List queueGro // create list of group restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdQueueGroup queueGroup = queueGroups.get(index); - skillsRestInfo = createSkillsRestInfo(queueGroup); - OpenAcdQueueGroupRestInfo queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo); + skillsRestInfo = createSkillsRestInfo(queueGroup); + agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + + OpenAcdQueueGroupRestInfo queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo, agentGroupRestInfo); queueGroupsRestInfo.add(queueGroupRestInfo); } @@ -274,6 +281,22 @@ private List createSkillsRestInfo(OpenAcdQueueGroup queueG return skillsRestInfo; } + private List createAgentGroupsRestInfo(OpenAcdQueueGroup queueGroup) { + List agentGroupsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + // create list of agent group restinfos for single group + Set groupAgentGroups = queueGroup.getAgentGroups(); + agentGroupsRestInfo = new ArrayList(groupAgentGroups.size()); + + for (OpenAcdAgentGroup groupAgentGroup : groupAgentGroups) { + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(groupAgentGroup); + agentGroupsRestInfo.add(agentGroupRestInfo); + } + + return agentGroupsRestInfo; + } + private void sortGroups(List queueGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); @@ -381,6 +404,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("openacd-queue-group", OpenAcdQueueGroupsBundleRestInfo.class); xstream.alias("group", OpenAcdQueueGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } } @@ -398,6 +422,7 @@ public OpenAcdQueueGroupRepresentation(Representation representation) { protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdQueueGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } } @@ -428,12 +453,14 @@ static class OpenAcdQueueGroupRestInfo { private final int m_id; private final String m_description; private final List m_skills; + private final List m_agentGroups; - public OpenAcdQueueGroupRestInfo(OpenAcdQueueGroup queueGroup, List skills) { + public OpenAcdQueueGroupRestInfo(OpenAcdQueueGroup queueGroup, List skills, List agentGroups) { m_name = queueGroup.getName(); m_id = queueGroup.getId(); m_description = queueGroup.getDescription(); m_skills = skills; + m_agentGroups = agentGroups; } public String getName() { @@ -451,6 +478,10 @@ public String getDescription() { public List getSkills() { return m_skills; } + + public List getAgentGroups() { + return m_agentGroups; + } } static class MetadataRestInfo { @@ -513,6 +544,29 @@ public String getGroupName() { } } + static class OpenAcdAgentGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup) { + m_id = agentGroup.getId(); + m_name = agentGroup.getName(); + m_description = agentGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } // Injected objects // ---------------- From 04752b74d4fbf20811a388cb4bff3ac2797c1fcf Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 21 Mar 2012 22:58:59 -0400 Subject: [PATCH 20/99] Code cleanup --- .../sipxconfig/rest/OpenAcdAgentGroupsResource.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 59a5610706..ce0fb17ea3 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -201,10 +201,10 @@ public void removeRepresentations() throws ResourceException { private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws ResourceException { OpenAcdAgentGroupRestInfo agentGroupRestInfo; + List skillsRestInfo; + List queuesRestInfo; try { - List skillsRestInfo; - List queuesRestInfo; OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(id); skillsRestInfo = createSkillsRestInfo(agentGroup); From ec048eff94d75973e47d5f442d5f087b126d36f5 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 23 Mar 2012 16:47:09 -0400 Subject: [PATCH 21/99] Add OpenACD Release Codes API --- .../rest/OpenAcdReleaseCodesResource.java | 457 ++++++++++++++++++ 1 file changed, 457 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java new file mode 100644 index 0000000000..d396b5d161 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -0,0 +1,457 @@ +/* + * + * OpenAcdReleaseCodesResource.java - A Restlet to read Release Code data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Waseem + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import java.util.HashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; + +import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdReleaseCodesResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + LABEL, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Release Code + // ------------------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); + + // return representation + return new OpenAcdReleaseCodeRepresentation(variant.getMediaType(), releaseCodeRestInfo); + } + + + // if not single, process request for list + List releaseCodes = m_openAcdContext.getReleaseCodes(); + List releaseCodesRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortReleaseCodes(releaseCodes); + + // set requested records and get resulting metadata + metadataRestInfo = addReleaseCodes(releaseCodesRestInfo, releaseCodes); + + // create final restinfo + OpenAcdReleaseCodesBundleRestInfo releaseCodesBundleRestInfo = new OpenAcdReleaseCodesBundleRestInfo(releaseCodesRestInfo, metadataRestInfo); + + return new OpenAcdReleaseCodesRepresentation(variant.getMediaType(), releaseCodesBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdReleaseCodeRepresentation representation = new OpenAcdReleaseCodeRepresentation(entity); + OpenAcdReleaseCodeRestInfo releaseCodeRestInfo = representation.getObject(); + OpenAcdReleaseCode releaseCode; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCode = m_openAcdContext.getReleaseCodeById(idInt); + + // copy values over to existing + updateReleaseCode(releaseCode, releaseCodeRestInfo); + m_openAcdContext.saveReleaseCode(releaseCode); + + return; + } + + + // otherwise add new + releaseCode = createReleaseCode(releaseCodeRestInfo); + m_openAcdContext.saveReleaseCode(releaseCode); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + // deleteReleaseCode() not available from openAcdContext + @Override + public void removeRepresentations() throws ResourceException { + Collection releaseCodeIds = new HashSet(); + OpenAcdReleaseCode releaseCode; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCode = m_openAcdContext.getReleaseCodeById(idInt); + + releaseCodeIds.add(idInt); + m_openAcdContext.removeReleaseCodes(releaseCodeIds); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + // Helper functions + // ---------------- + + private OpenAcdReleaseCodeRestInfo createReleaseCodeRestInfo(int id) throws ResourceException { + OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; + + try { + OpenAcdReleaseCode releaseCode = m_openAcdContext.getReleaseCodeById(id); + releaseCodeRestInfo = new OpenAcdReleaseCodeRestInfo(releaseCode); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return releaseCodeRestInfo; + } + + private MetadataRestInfo addReleaseCodes(List releaseCodesRestInfo, List releaseCodes) { + OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, releaseCodes.size()); + + // create list of releaseCode restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdReleaseCode releaseCode = releaseCodes.get(index); + + releaseCodeRestInfo = new OpenAcdReleaseCodeRestInfo(releaseCode); + releaseCodesRestInfo.add(releaseCodeRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortReleaseCodes(List releaseCodes) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case LABEL: + Collections.sort(releaseCodes, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; + OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; + return releaseCode1.getLabel().compareToIgnoreCase(releaseCode2.getLabel()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(releaseCodes, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; + OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; + return releaseCode1.getDescription().compareToIgnoreCase(releaseCode2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case LABEL: + Collections.sort(releaseCodes, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; + OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; + return releaseCode2.getLabel().compareToIgnoreCase(releaseCode1.getLabel()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(releaseCodes, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; + OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; + return releaseCode2.getDescription().compareToIgnoreCase(releaseCode1.getDescription()); + } + + }); + break; + } + } + } + + private void updateReleaseCode(OpenAcdReleaseCode releaseCode, OpenAcdReleaseCodeRestInfo releaseCodeRestInfo) throws ResourceException { + String tempString; + + // do not allow empty name + tempString = releaseCodeRestInfo.getLabel(); + if (!tempString.isEmpty()) { + releaseCode.setName(tempString); + } + + releaseCode.setLabel(releaseCodeRestInfo.getLabel()); + releaseCode.setDescription(releaseCodeRestInfo.getDescription()); + releaseCode.setBias(releaseCodeRestInfo.getBias()); + } + + private OpenAcdReleaseCode createReleaseCode(OpenAcdReleaseCodeRestInfo releaseCodeRestInfo) throws ResourceException { + OpenAcdReleaseCode releaseCode = new OpenAcdReleaseCode(); + + // copy fields from rest info + releaseCode.setLabel(releaseCodeRestInfo.getLabel()); + releaseCode.setDescription(releaseCodeRestInfo.getDescription()); + releaseCode.setBias(releaseCodeRestInfo.getBias()); + + return releaseCode; + } + + + // REST Representations + // -------------------- + + static class OpenAcdReleaseCodesRepresentation extends XStreamRepresentation { + + public OpenAcdReleaseCodesRepresentation(MediaType mediaType, OpenAcdReleaseCodesBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdReleaseCodesRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-release-code", OpenAcdReleaseCodesBundleRestInfo.class); + xstream.alias("release-code", OpenAcdReleaseCodeRestInfo.class); + } + } + + static class OpenAcdReleaseCodeRepresentation extends XStreamRepresentation { + + public OpenAcdReleaseCodeRepresentation(MediaType mediaType, OpenAcdReleaseCodeRestInfo object) { + super(mediaType, object); + } + + public OpenAcdReleaseCodeRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("release-code", OpenAcdReleaseCodeRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdReleaseCodesBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_releaseCodes; + + public OpenAcdReleaseCodesBundleRestInfo(List releaseCodes, MetadataRestInfo metadata) { + m_metadata = metadata; + m_releaseCodes = releaseCodes; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getReleaseCodes() { + return m_releaseCodes; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdReleaseCodeRestInfo { + private final int m_id; + private final String m_label; + private final String m_description; + private final int m_bias; + + public OpenAcdReleaseCodeRestInfo(OpenAcdReleaseCode releaseCode) { + m_id = releaseCode.getId(); + m_label = releaseCode.getLabel(); + m_bias = releaseCode.getBias(); + m_description = releaseCode.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getLabel() { + return m_label; + } + + public String getDescription() { + return m_description; + } + + public int getBias() { + return m_bias; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} \ No newline at end of file From cb64530d90a7f0749b3242e9b2b38ea41887ba81 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 23 Mar 2012 17:12:34 -0400 Subject: [PATCH 22/99] Add OpenACD Release Codes REST API --- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index d88234fdb9..a036a9446e 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -304,4 +304,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 4a3049c5fd7dd54f9858808f8040ab385dd610f3 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 23 Mar 2012 17:17:00 -0400 Subject: [PATCH 23/99] Remove extra resource definitions --- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index a036a9446e..76b67162cb 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -320,20 +320,4 @@ - - - - - - - - - - - - - - - - From a92f0f49e63f6fd97d38aed4823797e25c16ba9c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sat, 24 Mar 2012 22:07:21 -0400 Subject: [PATCH 24/99] Add OpenACD Queues REST API --- .../rest/OpenAcdQueuesResource.java | 562 ++++++++++++++++++ 1 file changed, 562 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java new file mode 100644 index 0000000000..c672baf6c5 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -0,0 +1,562 @@ +/* + * + * OpenAcdQueuesResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, I. Wesson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import java.util.HashSet; +import java.util.Collections; +import java.util.Comparator; +import java.util.Set; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; + +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.rest.OpenAcdAgentGroupsResource.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdQueueGroupsResource.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; + +public class OpenAcdQueuesResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Queue + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdQueueRestInfo queueRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueRestInfo = createQueueRestInfo(idInt); + + // return representation + return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); + } + + + // if not single, process request for list + List queues = m_openAcdContext.getQueues(); + List queuesRestInfo = new ArrayList(); + Form form = getRequest().getResourceRef().getQueryAsForm(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortQueues(queues); + + // set requested records and get resulting metadata + metadataRestInfo = addQueues(queuesRestInfo, queues); + + // create final restinfo + OpenAcdQueuesBundleRestInfo queuesBundleRestInfo = new OpenAcdQueuesBundleRestInfo(queuesRestInfo, metadataRestInfo); + + return new OpenAcdQueuesRepresentation(variant.getMediaType(), queuesBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdQueueRepresentation representation = new OpenAcdQueueRepresentation(entity); + OpenAcdQueueRestInfo queueRestInfo = representation.getObject(); + OpenAcdQueue queue; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queue = m_openAcdContext.getQueueById(idInt); + + // copy values over to existing + updateQueue(queue, queueRestInfo); + m_openAcdContext.saveQueue(queue); + + return; + } + + + // otherwise add new + queue = createQueue(queueRestInfo); + m_openAcdContext.saveQueue(queue); + getResponse().setStatus(Status.SUCCESS_CREATED); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + // deleteQueue() not available from openAcdContext + @Override + public void removeRepresentations() throws ResourceException { + Collection queueIds = new HashSet(); + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + + queueIds.add(idInt); + m_openAcdContext.deleteQueue(m_openAcdContext.getQueueById(idInt)); + + return; + } + + // no id string + getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + } + + // Helper functions + // ---------------- + + private OpenAcdQueueRestInfo createQueueRestInfo(int id) throws ResourceException { + OpenAcdQueueRestInfo queueRestInfo; + List skillsRestInfo; + List agentGroupRestInfo; + + try { + OpenAcdQueue queue = m_openAcdContext.getQueueById(id); + skillsRestInfo = createSkillsRestInfo(queue); + agentGroupRestInfo = createAgentGroupsRestInfo(queue); + queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupRestInfo); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return queueRestInfo; + } + + private List createSkillsRestInfo(OpenAcdQueue queue) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + Set groupSkills = queue.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } + + return skillsRestInfo; + } + + private List createAgentGroupsRestInfo(OpenAcdQueue queue) { + List agentGroupsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + // create list of agent group restinfos for single group + Set groupAgentGroups = queue.getAgentGroups(); + agentGroupsRestInfo = new ArrayList(groupAgentGroups.size()); + + for (OpenAcdAgentGroup groupAgentGroup : groupAgentGroups) { + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(groupAgentGroup); + agentGroupsRestInfo.add(agentGroupRestInfo); + } + + return agentGroupsRestInfo; + } + + private MetadataRestInfo addQueues(List queuesRestInfo, List queues) { + OpenAcdQueueRestInfo queueRestInfo; + List skillsRestInfo; + List agentGroupRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queues.size()); + + // create list of queue restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdQueue queue = queues.get(index); + + skillsRestInfo = createSkillsRestInfo(queue); + agentGroupRestInfo = createAgentGroupsRestInfo(queue); + queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupRestInfo); + queuesRestInfo.add(queueRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortQueues(List queues) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(queues, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return queue1.getName().compareToIgnoreCase(queue2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(queues, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return queue1.getDescription().compareToIgnoreCase(queue2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(queues, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return queue2.getName().compareToIgnoreCase(queue1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(queues, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return queue2.getDescription().compareToIgnoreCase(queue1.getDescription()); + } + + }); + break; + } + } + } + + private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + String tempString; + OpenAcdQueueGroup queueGroup; + + // do not allow empty name + tempString = queueRestInfo.getName(); + if (!tempString.isEmpty()) { + queue.setName(tempString); + } + queueGroup = getQueueGroup(queueRestInfo); + + queue.setGroup(queueGroup); + queue.setDescription(queueRestInfo.getDescription()); + } + + private OpenAcdQueue createQueue(OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + OpenAcdQueue queue = new OpenAcdQueue(); + OpenAcdQueueGroup queueGroup; + + // copy fields from rest info + queue.setName(queueRestInfo.getName()); + queueGroup = getQueueGroup(queueRestInfo); + + queue.setGroup(queueGroup); + queue.setDescription(queueRestInfo.getDescription()); + + return queue; + } + + private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + OpenAcdQueueGroup queueGroup; + int groupId = 0; + + try { + groupId = queueRestInfo.getId(); + queueGroup = m_openAcdContext.getQueueGroupById(groupId); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Queue Group ID " + groupId + " not found."); + } + + return queueGroup; + } + + // REST Representations + // -------------------- + + static class OpenAcdQueuesRepresentation extends XStreamRepresentation { + + public OpenAcdQueuesRepresentation(MediaType mediaType, OpenAcdQueuesBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdQueuesRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-queue", OpenAcdQueuesBundleRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + } + } + + static class OpenAcdQueueRepresentation extends XStreamRepresentation { + + public OpenAcdQueueRepresentation(MediaType mediaType, OpenAcdQueueRestInfo object) { + super(mediaType, object); + } + + public OpenAcdQueueRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdQueuesBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_queues; + + public OpenAcdQueuesBundleRestInfo(List queues, MetadataRestInfo metadata) { + m_metadata = metadata; + m_queues = queues; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getQueues() { + return m_queues; + } + } + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdQueueRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final List m_skills; + private final List m_agentGroups; + private final String m_queueGroup; + + public OpenAcdQueueRestInfo(OpenAcdQueue queue, List skills, List agentGroups) { + m_id = queue.getId(); + m_name = queue.getName(); + m_queueGroup = queue.getQueueGroup(); + m_description = queue.getDescription(); + m_skills = skills; + m_agentGroups = agentGroups; + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + + public String getQueueGroup() + { + return m_queueGroup; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} From e9ee71ea4b671b90c02f1146894c25065332b4fe Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sat, 24 Mar 2012 22:07:46 -0400 Subject: [PATCH 25/99] Add OpenACD Queues REST API --- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 76b67162cb..a036a9446e 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -320,4 +320,20 @@ + + + + + + + + + + + + + + + + From 43f23b32638e0834134fb33d23c8c8968b57afcd Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sat, 24 Mar 2012 22:08:21 -0400 Subject: [PATCH 26/99] Add response data for create, update, delete, errors --- .../rest/OpenAcdSkillsResource.java | 48 +++-- .../sipxconfig/rest/OpenAcdUtilities.java | 168 ++++++++++++++++++ 2 files changed, 204 insertions(+), 12 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 637657a068..f430130d51 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -113,6 +113,10 @@ public Representation represent(Variant variant) throws ResourceException { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); skillRestInfo = createSkillRestInfo(idInt); + if (skillRestInfo == null) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + } + // finally return group representation return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); } @@ -145,19 +149,28 @@ public void storeRepresentation(Representation entity) throws ResourceException // get from request body OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); OpenAcdSkillRestInfo skillRestInfo = representation.getObject(); - OpenAcdSkill skill; + OpenAcdSkill skill = null; // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + try { + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return; + } + // copy values over to existing updateSkill(skill, skillRestInfo); m_openAcdContext.saveSkill(skill); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.UPDATED, skill.getId(), "Updated Skill"); + return; } @@ -165,7 +178,8 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new skill = createSkill(skillRestInfo); m_openAcdContext.saveSkill(skill); - getResponse().setStatus(Status.SUCCESS_CREATED); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.CREATED, skill.getId(), "Created Skill"); } @@ -181,15 +195,24 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + try { + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return; + } + m_openAcdContext.deleteSkill(skill); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, skill.getId(), "Deleted Skill"); + return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.MISSING_INPUT, "ID value missing"); } @@ -197,14 +220,15 @@ public void removeRepresentations() throws ResourceException { // ---------------- private OpenAcdSkillRestInfo createSkillRestInfo(int id) throws ResourceException { - OpenAcdSkillRestInfo skillRestInfo; + OpenAcdSkillRestInfo skillRestInfo = null; try { OpenAcdSkill skill = m_openAcdContext.getSkillById(id); skillRestInfo = new OpenAcdSkillRestInfo(skill); } catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + + //throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); } return skillRestInfo; @@ -212,7 +236,7 @@ private OpenAcdSkillRestInfo createSkillRestInfo(int id) throws ResourceExceptio private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { OpenAcdSkillRestInfo skillRestInfo; - + // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skills.size()); @@ -301,7 +325,7 @@ private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfo skillRestInfo) OpenAcdSkillGroup skillGroup; String tempString; int groupId = 0; - + // do not allow empty name tempString = skillRestInfo.getName(); if (!tempString.isEmpty()) { @@ -332,7 +356,7 @@ private OpenAcdSkill createSkill(OpenAcdSkillRestInfo skillRestInfo) throws Reso private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { OpenAcdSkillGroup skillGroup; int groupId = 0; - + try { groupId = skillRestInfo.getGroupId(); skillGroup = m_openAcdContext.getSkillGroupById(groupId); @@ -340,11 +364,11 @@ private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfo skillRestInfo) thro catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Skill Group ID " + groupId + " not found."); } - + return skillGroup; } - + // REST Representations // -------------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 47530a8e81..9d16692d15 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -20,9 +20,19 @@ package org.sipfoundry.sipxconfig.rest; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.restlet.data.MediaType; import org.restlet.data.Status; import org.restlet.data.Form; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.DomRepresentation; import org.restlet.resource.ResourceException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; public class OpenAcdUtilities { @@ -141,7 +151,162 @@ public static SortInfo calculateSorting(Form form) { return sortInfo; } + public static void setResponse(Response response, ResponseCode code, String message) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // no data related to result (create function overloads to modify) + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void setResponse(Response response, ResponseCode code, int id, String message) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add data related to result + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("id"); + elementId.appendChild(doc.createTextNode(String.valueOf(id))); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void setResponseError(Response response, ResponseCode code, String message) { + Representation representation = getResponseError(response, code, message); + + response.setEntity(representation); + } + + public static Representation getResponseError(Response response, ResponseCode code, String message) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + return representation; //new DomRepresentation(MediaType.TEXT_XML, doc); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + public static Representation getResponseException(Response response, Exception exception) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, ResponseCode.EXCEPTION); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, ResponseCode.EXCEPTION, "An Exception occurred"); + + // add data related to result + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("exception"); + + // convert exception stack trace output to string + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream printStream = new PrintStream(outputStream); + exception.printStackTrace(printStream); + + elementId.appendChild(doc.createTextNode(outputStream.toString())); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + return representation; + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + private static void setResponseStatus(Response response, ResponseCode code) { + // set response status based on code + switch (code) { + case CREATED: + response.setStatus(Status.SUCCESS_CREATED); + break; + + case MISSING_INPUT: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + + case BAD_INPUT: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + + case EXCEPTION: + response.setStatus(Status.SERVER_ERROR_INTERNAL); + break; + + default: + response.setStatus(Status.SUCCESS_OK); + } + } + + private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { + + // add standard elements + Element elementCode = doc.createElement("code"); + elementCode.appendChild(doc.createTextNode(code.toString())); + elementResponse.appendChild(elementCode); + + Element elementMessage = doc.createElement("message"); + elementMessage.appendChild(doc.createTextNode(message)); + elementResponse.appendChild(elementMessage); + } + // Data objects // ------------ @@ -161,4 +326,7 @@ public static class SortInfo { String sortField = ""; } + public static enum ResponseCode { + CREATED, UPDATED, DELETED, MISSING_INPUT, BAD_INPUT, EXCEPTION + } } From 72345f92b18313b88efcc121144aeac212336c20 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sat, 24 Mar 2012 22:58:43 -0400 Subject: [PATCH 27/99] Add response values for create, update, delete --- .../rest/OpenAcdAgentGroupsResource.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index ce0fb17ea3..94116f9508 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -116,6 +116,10 @@ public Representation represent(Variant variant) throws ResourceException { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agentGroupRestInfo = createAgentGroupRestInfo(idInt); + if (agentGroupRestInfo == null) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + } + // finally return group representation return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); } @@ -155,12 +159,21 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroup = m_openAcdContext.getAgentGroupById(idInt); + try { + agentGroup = m_openAcdContext.getAgentGroupById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return; + } + // copy values over to existing group updateAgentGroup(agentGroup, agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.UPDATED, agentGroup.getId(), "Updated Agent Group"); + return; } @@ -168,7 +181,8 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new agent group agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); - getResponse().setStatus(Status.SUCCESS_CREATED); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.CREATED, agentGroup.getId(), "Created Agent Group"); } @@ -184,15 +198,24 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroup = m_openAcdContext.getAgentGroupById(idInt); + try { + agentGroup = m_openAcdContext.getAgentGroupById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return; + } + m_openAcdContext.deleteAgentGroup(agentGroup); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, agentGroup.getId(), "Deleted Agent Group"); + return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.MISSING_INPUT, "ID value missing"); } From ac2febac2ae0bf1d2cc6dba32796c2950a377772 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 25 Mar 2012 01:54:20 -0400 Subject: [PATCH 28/99] Refactor response values for create, update, delete --- .../rest/OpenAcdAgentGroupsResource.java | 38 +++++++++---------- .../rest/OpenAcdSkillsResource.java | 23 +++++------ .../sipxconfig/rest/OpenAcdUtilities.java | 37 ------------------ 3 files changed, 26 insertions(+), 72 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 94116f9508..e5b9954a48 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -113,13 +113,14 @@ public Representation represent(Variant variant) throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroupRestInfo = createAgentGroupRestInfo(idInt); - - if (agentGroupRestInfo == null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agentGroupRestInfo = createAgentGroupRestInfo(idInt); + } + catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); } - + // finally return group representation return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); } @@ -167,13 +168,13 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); return; } - + // copy values over to existing group updateAgentGroup(agentGroup, agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.UPDATED, agentGroup.getId(), "Updated Agent Group"); - + return; } @@ -206,11 +207,11 @@ public void removeRepresentations() throws ResourceException { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); return; } - + m_openAcdContext.deleteAgentGroup(agentGroup); OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, agentGroup.getId(), "Deleted Agent Group"); - + return; } @@ -227,20 +228,15 @@ private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws Resour List skillsRestInfo; List queuesRestInfo; - try { - OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(id); + OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(id); - skillsRestInfo = createSkillsRestInfo(agentGroup); - queuesRestInfo = createQueuesRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); - } + skillsRestInfo = createSkillsRestInfo(agentGroup); + queuesRestInfo = createQueuesRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); return agentGroupRestInfo; } - + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { String tempString; @@ -276,7 +272,7 @@ private MetadataRestInfo addAgentGroups(List agentGro OpenAcdAgentGroup agentGroup = agentGroups.get(index); skillsRestInfo = createSkillsRestInfo(agentGroup); queuesRestInfo = createQueuesRestInfo(agentGroup); - + OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } @@ -317,7 +313,7 @@ private List createQueuesRestInfo(OpenAcdAgentGroup agentG return queuesRestInfo; } - + private void sortGroups(List agentGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index f430130d51..338257ba09 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -110,10 +110,11 @@ public Representation represent(Variant variant) throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillRestInfo = createSkillRestInfo(idInt); - - if (skillRestInfo == null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillRestInfo = createSkillRestInfo(idInt); + } + catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); } @@ -164,7 +165,7 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); return; } - + // copy values over to existing updateSkill(skill, skillRestInfo); m_openAcdContext.saveSkill(skill); @@ -203,7 +204,7 @@ public void removeRepresentations() throws ResourceException { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); return; } - + m_openAcdContext.deleteSkill(skill); OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, skill.getId(), "Deleted Skill"); @@ -222,14 +223,8 @@ public void removeRepresentations() throws ResourceException { private OpenAcdSkillRestInfo createSkillRestInfo(int id) throws ResourceException { OpenAcdSkillRestInfo skillRestInfo = null; - try { - OpenAcdSkill skill = m_openAcdContext.getSkillById(id); - skillRestInfo = new OpenAcdSkillRestInfo(skill); - } - catch (Exception exception) { - - //throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); - } + OpenAcdSkill skill = m_openAcdContext.getSkillById(id); + skillRestInfo = new OpenAcdSkillRestInfo(skill); return skillRestInfo; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 9d16692d15..bbaa9d3074 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -233,43 +233,6 @@ public static Representation getResponseError(Response response, ResponseCode co return null; } - - public static Representation getResponseException(Response response, Exception exception) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, ResponseCode.EXCEPTION); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, ResponseCode.EXCEPTION, "An Exception occurred"); - - // add data related to result - Element elementData = doc.createElement("data"); - Element elementId = doc.createElement("exception"); - - // convert exception stack trace output to string - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - PrintStream printStream = new PrintStream(outputStream); - exception.printStackTrace(printStream); - - elementId.appendChild(doc.createTextNode(outputStream.toString())); - elementData.appendChild(elementId); - elementResponse.appendChild(elementData); - - return representation; - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return null; - } private static void setResponseStatus(Response response, ResponseCode code) { // set response status based on code From b263e82ebfa045227ac00307f11e96e1a3d030b1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 25 Mar 2012 02:12:47 -0400 Subject: [PATCH 29/99] Add success or error status to response codes --- .../rest/OpenAcdAgentGroupsResource.java | 14 +++++++------- .../sipxconfig/rest/OpenAcdSkillsResource.java | 14 +++++++------- .../sipxconfig/rest/OpenAcdUtilities.java | 12 ++++-------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index e5b9954a48..5280d9e003 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -118,7 +118,7 @@ public Representation represent(Variant variant) throws ResourceException { agentGroupRestInfo = createAgentGroupRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } // finally return group representation @@ -165,7 +165,7 @@ public void storeRepresentation(Representation entity) throws ResourceException agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -173,7 +173,7 @@ public void storeRepresentation(Representation entity) throws ResourceException updateAgentGroup(agentGroup, agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.UPDATED, agentGroup.getId(), "Updated Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, agentGroup.getId(), "Updated Agent Group"); return; } @@ -183,7 +183,7 @@ public void storeRepresentation(Representation entity) throws ResourceException agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.CREATED, agentGroup.getId(), "Created Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); } @@ -204,19 +204,19 @@ public void removeRepresentations() throws ResourceException { agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteAgentGroup(agentGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, agentGroup.getId(), "Deleted Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, agentGroup.getId(), "Deleted Agent Group"); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.MISSING_INPUT, "ID value missing"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 338257ba09..dc04b9e0e3 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -115,7 +115,7 @@ public Representation represent(Variant variant) throws ResourceException { skillRestInfo = createSkillRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } // finally return group representation @@ -162,7 +162,7 @@ public void storeRepresentation(Representation entity) throws ResourceException skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -170,7 +170,7 @@ public void storeRepresentation(Representation entity) throws ResourceException updateSkill(skill, skillRestInfo); m_openAcdContext.saveSkill(skill); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.UPDATED, skill.getId(), "Updated Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skill.getId(), "Updated Skill"); return; } @@ -180,7 +180,7 @@ public void storeRepresentation(Representation entity) throws ResourceException skill = createSkill(skillRestInfo); m_openAcdContext.saveSkill(skill); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.CREATED, skill.getId(), "Created Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skill.getId(), "Created Skill"); } @@ -201,19 +201,19 @@ public void removeRepresentations() throws ResourceException { skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteSkill(skill); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.DELETED, skill.getId(), "Deleted Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, skill.getId(), "Deleted Skill"); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.MISSING_INPUT, "ID value missing"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index bbaa9d3074..e3a85af6d3 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -237,22 +237,18 @@ public static Representation getResponseError(Response response, ResponseCode co private static void setResponseStatus(Response response, ResponseCode code) { // set response status based on code switch (code) { - case CREATED: + case SUCCESS_CREATED: response.setStatus(Status.SUCCESS_CREATED); break; - case MISSING_INPUT: + case ERROR_MISSING_INPUT: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; - case BAD_INPUT: + case ERROR_BAD_INPUT: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; - case EXCEPTION: - response.setStatus(Status.SERVER_ERROR_INTERNAL); - break; - default: response.setStatus(Status.SUCCESS_OK); } @@ -290,6 +286,6 @@ public static class SortInfo { } public static enum ResponseCode { - CREATED, UPDATED, DELETED, MISSING_INPUT, BAD_INPUT, EXCEPTION + SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT } } From c1c72120d6896e22fedc7f1612633d332556b001 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 25 Mar 2012 04:40:53 -0400 Subject: [PATCH 30/99] Extract RestInfo objects into OpenAcdUtilities for reuse among APIs --- .../rest/OpenAcdAgentGroupsResource.java | 63 +----- .../rest/OpenAcdClientsResource.java | 31 +-- .../rest/OpenAcdQueueGroupsResource.java | 55 +---- .../rest/OpenAcdQueuesResource.java | 52 ++--- .../rest/OpenAcdReleaseCodesResource.java | 31 +-- .../rest/OpenAcdSkillGroupsResource.java | 25 +-- .../rest/OpenAcdSkillsResource.java | 81 ++----- .../sipxconfig/rest/OpenAcdUtilities.java | 200 +++++++++++++++++- 8 files changed, 239 insertions(+), 299 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 5280d9e003..846039e2c2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -47,6 +47,8 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; public class OpenAcdAgentGroupsResource extends UserResource { @@ -531,66 +533,7 @@ public int getResultsPerPage() { } } - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - - static class OpenAcdQueueRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdQueueRestInfo(OpenAcdQueue queue) { - m_id = queue.getId(); - m_name = queue.getName(); - m_description = queue.getDescription(); - m_groupName = queue.getQueueGroup(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - + // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 603fdaecfa..652feff7cf 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; public class OpenAcdClientsResource extends UserResource { @@ -411,36 +412,6 @@ public int getResultsPerPage() { } } - static class OpenAcdClientRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_identity; - - public OpenAcdClientRestInfo(OpenAcdClient client) { - m_id = client.getId(); - m_name = client.getName(); - m_description = client.getDescription(); - m_identity = client.getIdentity(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getIdentity() { - return m_identity; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 0b71449c64..257d8d1628 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -47,6 +47,8 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; public class OpenAcdQueueGroupsResource extends UserResource { @@ -514,59 +516,6 @@ public int getResultsPerPage() { } } - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - - static class OpenAcdAgentGroupRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup) { - m_id = agentGroup.getId(); - m_name = agentGroup.getName(); - m_description = agentGroup.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - } // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index c672baf6c5..de98618649 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -48,11 +48,10 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.rest.OpenAcdAgentGroupsResource.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdQueueGroupsResource.OpenAcdAgentGroupRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; public class OpenAcdQueuesResource extends UserResource { @@ -208,13 +207,13 @@ public void removeRepresentations() throws ResourceException { private OpenAcdQueueRestInfo createQueueRestInfo(int id) throws ResourceException { OpenAcdQueueRestInfo queueRestInfo; List skillsRestInfo; - List agentGroupRestInfo; + List agentGroupsRestInfo; try { OpenAcdQueue queue = m_openAcdContext.getQueueById(id); skillsRestInfo = createSkillsRestInfo(queue); - agentGroupRestInfo = createAgentGroupsRestInfo(queue); - queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupRestInfo); + agentGroupsRestInfo = createAgentGroupsRestInfo(queue); + queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupsRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -493,8 +492,8 @@ static class OpenAcdQueueRestInfo { public OpenAcdQueueRestInfo(OpenAcdQueue queue, List skills, List agentGroups) { m_id = queue.getId(); m_name = queue.getName(); - m_queueGroup = queue.getQueueGroup(); m_description = queue.getDescription(); + m_queueGroup = queue.getQueueGroup(); m_skills = skills; m_agentGroups = agentGroups; } @@ -511,46 +510,21 @@ public String getDescription() { return m_description; } - public List getSkills() { - return m_skills; - } - public String getQueueGroup() { - return m_queueGroup; - } - } - - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; + return m_queueGroup; } - - public String getDescription() { - return m_description; + + public List getSkills() { + return m_skills; } - public String getGroupName() { - return m_groupName; + public List getAgentGroups() { + return m_agentGroups; } } + // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index d396b5d161..a46168ac3c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; public class OpenAcdReleaseCodesResource extends UserResource { @@ -415,36 +416,6 @@ public int getResultsPerPage() { } } - static class OpenAcdReleaseCodeRestInfo { - private final int m_id; - private final String m_label; - private final String m_description; - private final int m_bias; - - public OpenAcdReleaseCodeRestInfo(OpenAcdReleaseCode releaseCode) { - m_id = releaseCode.getId(); - m_label = releaseCode.getLabel(); - m_bias = releaseCode.getBias(); - m_description = releaseCode.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getLabel() { - return m_label; - } - - public String getDescription() { - return m_description; - } - - public int getBias() { - return m_bias; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index ce39bda3de..aa18f7a916 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillGroupRestInfo; public class OpenAcdSkillGroupsResource extends UserResource { @@ -410,30 +411,6 @@ public int getResultsPerPage() { } } - static class OpenAcdSkillGroupRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - - public OpenAcdSkillGroupRestInfo(OpenAcdSkillGroup skillGroup) { - m_id = skillGroup.getId(); - m_name = skillGroup.getName(); - m_description = skillGroup.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index dc04b9e0e3..17b8404c93 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -44,6 +44,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; public class OpenAcdSkillsResource extends UserResource { @@ -106,7 +107,7 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdSkillRestInfo skillRestInfo; + OpenAcdSkillRestInfoFull skillRestInfo; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { @@ -125,7 +126,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List skills = m_openAcdContext.getSkills(); - List skillsRestInfo = new ArrayList(); + List skillsRestInfo = new ArrayList(); Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; @@ -149,7 +150,7 @@ public Representation represent(Variant variant) throws ResourceException { public void storeRepresentation(Representation entity) throws ResourceException { // get from request body OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); - OpenAcdSkillRestInfo skillRestInfo = representation.getObject(); + OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); OpenAcdSkill skill = null; // if have id then update single @@ -220,17 +221,17 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdSkillRestInfo createSkillRestInfo(int id) throws ResourceException { - OpenAcdSkillRestInfo skillRestInfo = null; + private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { + OpenAcdSkillRestInfoFull skillRestInfo = null; OpenAcdSkill skill = m_openAcdContext.getSkillById(id); - skillRestInfo = new OpenAcdSkillRestInfo(skill); + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); return skillRestInfo; } - private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { - OpenAcdSkillRestInfo skillRestInfo; + private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { + OpenAcdSkillRestInfoFull skillRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skills.size()); @@ -239,7 +240,7 @@ private MetadataRestInfo addSkills(List skillsRestInfo, Li for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdSkill skill = skills.get(index); - skillRestInfo = new OpenAcdSkillRestInfo(skill); + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); skillsRestInfo.add(skillRestInfo); } @@ -316,7 +317,7 @@ public int compare(Object object1, Object object2) { } } - private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { OpenAcdSkillGroup skillGroup; String tempString; int groupId = 0; @@ -333,7 +334,7 @@ private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfo skillRestInfo) skill.setGroup(skillGroup); } - private OpenAcdSkill createSkill(OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { OpenAcdSkillGroup skillGroup; OpenAcdSkill skill = new OpenAcdSkill(); @@ -348,7 +349,7 @@ private OpenAcdSkill createSkill(OpenAcdSkillRestInfo skillRestInfo) throws Reso return skill; } - private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfo skillRestInfo) throws ResourceException { + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { OpenAcdSkillGroup skillGroup; int groupId = 0; @@ -380,13 +381,13 @@ public OpenAcdSkillsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); - xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); } } - static class OpenAcdSkillRepresentation extends XStreamRepresentation { + static class OpenAcdSkillRepresentation extends XStreamRepresentation { - public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfo object) { + public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfoFull object) { super(mediaType, object); } @@ -396,7 +397,7 @@ public OpenAcdSkillRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); } } @@ -406,9 +407,9 @@ protected void configureXStream(XStream xstream) { static class OpenAcdSkillsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_skills; + private final List m_skills; - public OpenAcdSkillsBundleRestInfo(List skills, MetadataRestInfo metadata) { + public OpenAcdSkillsBundleRestInfo(List skills, MetadataRestInfo metadata) { m_metadata = metadata; m_skills = skills; } @@ -417,7 +418,7 @@ public MetadataRestInfo getMetadata() { return m_metadata; } - public List getSkills() { + public List getSkills() { return m_skills; } } @@ -452,48 +453,6 @@ public int getResultsPerPage() { } } - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_atom; - private final String m_groupName; - private final int m_groupId; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_atom = skill.getAtom(); - m_groupName = skill.getGroupName(); - m_groupId = skill.getGroup().getId(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getAtom() { - return m_atom; - } - - public String getGroupName() { - return m_groupName; - } - - public int getGroupId() { - return m_groupId; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index e3a85af6d3..41dd0ac3c6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -20,9 +20,7 @@ package org.sipfoundry.sipxconfig.rest; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.PrintStream; import org.restlet.data.MediaType; import org.restlet.data.Status; @@ -31,6 +29,12 @@ import org.restlet.resource.Representation; import org.restlet.resource.DomRepresentation; import org.restlet.resource.ResourceException; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -288,4 +292,196 @@ public static class SortInfo { public static enum ResponseCode { SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT } + + + // Common Rest Info objects (may be re-defined within Resource to include different fields + // ------------------------ + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + static class OpenAcdSkillRestInfoFull extends OpenAcdSkillRestInfo { + private final String m_atom; + private final int m_groupId; + + public OpenAcdSkillRestInfoFull(OpenAcdSkill skill) { + super(skill); + m_atom = skill.getAtom(); + m_groupId = skill.getGroup().getId(); + } + + public String getAtom() { + return m_atom; + } + + public int getGroupId() { + return m_groupId; + } + } + + static class OpenAcdSkillGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdSkillGroupRestInfo(OpenAcdSkillGroup skillGroup) { + m_id = skillGroup.getId(); + m_name = skillGroup.getName(); + m_description = skillGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + static class OpenAcdQueueRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdQueueRestInfo(OpenAcdQueue queue) { + m_id = queue.getId(); + m_name = queue.getName(); + m_description = queue.getDescription(); + m_groupName = queue.getQueueGroup(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + static class OpenAcdClientRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_identity; + + public OpenAcdClientRestInfo(OpenAcdClient client) { + m_id = client.getId(); + m_name = client.getName(); + m_description = client.getDescription(); + m_identity = client.getIdentity(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getIdentity() { + return m_identity; + } + } + + static class OpenAcdAgentGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup) { + m_id = agentGroup.getId(); + m_name = agentGroup.getName(); + m_description = agentGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + static class OpenAcdReleaseCodeRestInfo { + private final int m_id; + private final String m_label; + private final String m_description; + private final int m_bias; + + public OpenAcdReleaseCodeRestInfo(OpenAcdReleaseCode releaseCode) { + m_id = releaseCode.getId(); + m_label = releaseCode.getLabel(); + m_bias = releaseCode.getBias(); + m_description = releaseCode.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getLabel() { + return m_label; + } + + public String getDescription() { + return m_description; + } + + public int getBias() { + return m_bias; + } + } + } From ab5958619288af95836f9791b41fb17781f44df1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 27 Mar 2012 12:22:21 -0400 Subject: [PATCH 31/99] Add Lines of GET --- .../rest/OpenAcdAgentGroupsResource.java | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 846039e2c2..3ac736f680 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -42,6 +42,7 @@ import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; @@ -229,12 +230,14 @@ private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws Resour OpenAcdAgentGroupRestInfo agentGroupRestInfo; List skillsRestInfo; List queuesRestInfo; + List clientsRestInfo; OpenAcdAgentGroup agentGroup = m_openAcdContext.getAgentGroupById(id); skillsRestInfo = createSkillsRestInfo(agentGroup); queuesRestInfo = createQueuesRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); + clientsRestInfo = createClientsRestInfo(agentGroup); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); return agentGroupRestInfo; } @@ -265,6 +268,7 @@ private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRes private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { List skillsRestInfo; List queuesRestInfo; + List clientsRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); @@ -274,8 +278,8 @@ private MetadataRestInfo addAgentGroups(List agentGro OpenAcdAgentGroup agentGroup = agentGroups.get(index); skillsRestInfo = createSkillsRestInfo(agentGroup); queuesRestInfo = createQueuesRestInfo(agentGroup); - - OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo); + clientsRestInfo = createClientsRestInfo(agentGroup); + OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } @@ -316,6 +320,23 @@ private List createQueuesRestInfo(OpenAcdAgentGroup agentG return queuesRestInfo; } + + private List createClientsRestInfo(OpenAcdAgentGroup agentGroup) { + List clientsRestInfo; + OpenAcdClientRestInfo clientRestInfo; + + // create list of client restinfos for single group + Set groupClients = agentGroup.getClients(); + clientsRestInfo = new ArrayList(groupClients.size()); + + for (OpenAcdClient groupClient : groupClients) { + clientRestInfo = new OpenAcdClientRestInfo(groupClient); + clientsRestInfo.add(clientRestInfo); + } + + return clientsRestInfo; + } + private void sortGroups(List agentGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); @@ -424,6 +445,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdAgentGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("client", OpenAcdClientRestInfo.class); } } @@ -442,6 +464,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdAgentGroupRestInfo.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("client", OpenAcdClientRestInfo.class); } } @@ -473,13 +496,16 @@ static class OpenAcdAgentGroupRestInfo { private final String m_description; private final List m_skills; private final List m_queues; + private final List m_clients; - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills, List queues) { + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills, + List queues, List clients) { m_name = agentGroup.getName(); m_id = agentGroup.getId(); m_description = agentGroup.getDescription(); m_skills = skills; m_queues = queues; + m_clients = clients; } public String getName() { @@ -501,6 +527,10 @@ public List getSkills() { public List getQueues() { return m_queues; } + + public List getClients() { + return m_clients; + } } static class MetadataRestInfo { @@ -533,6 +563,35 @@ public int getResultsPerPage() { } } + static class OpenAcdClientRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_identity; + + public OpenAcdClientRestInfo(OpenAcdClient client) { + m_id = client.getId(); + m_name = client.getName(); + m_description = client.getDescription(); + m_identity = client.getIdentity(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getIdentity() { + return m_identity; + } + } // Injected objects // ---------------- From fe59471dfeb2bbb14753c3c4a0a835729dfdd1a8 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 27 Mar 2012 17:39:29 -0400 Subject: [PATCH 32/99] Finish responses for errors --- .../rest/OpenAcdSkillsResource.java | 40 ++++++++++--------- .../sipxconfig/rest/OpenAcdUtilities.java | 7 +++- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 17b8404c93..cc5835a840 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -168,9 +168,15 @@ public void storeRepresentation(Representation entity) throws ResourceException } // copy values over to existing - updateSkill(skill, skillRestInfo); - m_openAcdContext.saveSkill(skill); - + try { + updateSkill(skill, skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update skill failed"); + return; + } + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skill.getId(), "Updated Skill"); return; @@ -178,9 +184,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new - skill = createSkill(skillRestInfo); - m_openAcdContext.saveSkill(skill); - + try { + skill = createSkill(skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update skill failed"); + return; + } + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skill.getId(), "Created Skill"); } @@ -317,10 +329,9 @@ public int compare(Object object1, Object object2) { } } - private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { OpenAcdSkillGroup skillGroup; String tempString; - int groupId = 0; // do not allow empty name tempString = skillRestInfo.getName(); @@ -349,17 +360,10 @@ private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws return skill; } - private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { OpenAcdSkillGroup skillGroup; - int groupId = 0; - - try { - groupId = skillRestInfo.getGroupId(); - skillGroup = m_openAcdContext.getSkillGroupById(groupId); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Skill Group ID " + groupId + " not found."); - } + int groupId = skillRestInfo.getGroupId(); + skillGroup = m_openAcdContext.getSkillGroupById(groupId); return skillGroup; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 41dd0ac3c6..63b8b215cd 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -253,6 +253,10 @@ private static void setResponseStatus(Response response, ResponseCode code) { response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; + case ERROR_WRITE_FAILED: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + default: response.setStatus(Status.SUCCESS_OK); } @@ -270,6 +274,7 @@ private static void setResponseHeader(Document doc, Element elementResponse, Res elementResponse.appendChild(elementMessage); } + // Data objects // ------------ @@ -290,7 +295,7 @@ public static class SortInfo { } public static enum ResponseCode { - SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT + SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED } From 055eab7676ccf6c09332886eb43f53b7e46f0339 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 27 Mar 2012 22:38:39 -0400 Subject: [PATCH 33/99] Implement Response data for updates, deletes. Refactor some objects to Utilities --- .../rest/OpenAcdAgentGroupsResource.java | 167 ++++----------- .../rest/OpenAcdClientsResource.java | 96 ++++----- .../rest/OpenAcdQueueGroupsResource.java | 162 ++++++--------- .../rest/OpenAcdQueuesResource.java | 192 +++++++----------- .../rest/OpenAcdReleaseCodesResource.java | 91 +++++---- .../rest/OpenAcdSkillGroupsResource.java | 101 ++++----- .../rest/OpenAcdSkillsResource.java | 42 +--- .../sipxconfig/rest/OpenAcdUtilities.java | 171 +++++++++++++--- 8 files changed, 482 insertions(+), 540 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 3ac736f680..431c146d80 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -48,8 +48,11 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; public class OpenAcdAgentGroupsResource extends UserResource { @@ -112,7 +115,7 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdAgentGroupRestInfo agentGroupRestInfo; + OpenAcdAgentGroupRestInfoFull agentGroupRestInfo; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { @@ -131,7 +134,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List agentGroups = m_openAcdContext.getAgentGroups(); - List agentGroupsRestInfo = new ArrayList(); + List agentGroupsRestInfo = new ArrayList(); Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; @@ -155,16 +158,15 @@ public Representation represent(Variant variant) throws ResourceException { public void storeRepresentation(Representation entity) throws ResourceException { // get group from body OpenAcdAgentGroupRepresentation representation = new OpenAcdAgentGroupRepresentation(entity); - OpenAcdAgentGroupRestInfo agentGroupRestInfo = representation.getObject(); + OpenAcdAgentGroupRestInfoFull agentGroupRestInfo = representation.getObject(); OpenAcdAgentGroup agentGroup; // if have id then update a single group String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { @@ -173,8 +175,14 @@ public void storeRepresentation(Representation entity) throws ResourceException } // copy values over to existing group - updateAgentGroup(agentGroup, agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); + try { + updateAgentGroup(agentGroup, agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed"); + return; + } OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, agentGroup.getId(), "Updated Agent Group"); @@ -183,9 +191,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new agent group - agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); - m_openAcdContext.saveAgentGroup(agentGroup); - + try { + agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); + m_openAcdContext.saveAgentGroup(agentGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed"); + return; + } + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); } @@ -201,9 +215,8 @@ public void removeRepresentations() throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { @@ -226,8 +239,8 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws ResourceException { - OpenAcdAgentGroupRestInfo agentGroupRestInfo; + private OpenAcdAgentGroupRestInfoFull createAgentGroupRestInfo(int id) throws ResourceException { + OpenAcdAgentGroupRestInfoFull agentGroupRestInfo; List skillsRestInfo; List queuesRestInfo; List clientsRestInfo; @@ -237,12 +250,12 @@ private OpenAcdAgentGroupRestInfo createAgentGroupRestInfo(int id) throws Resour skillsRestInfo = createSkillsRestInfo(agentGroup); queuesRestInfo = createQueuesRestInfo(agentGroup); clientsRestInfo = createClientsRestInfo(agentGroup); - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); + agentGroupRestInfo = new OpenAcdAgentGroupRestInfoFull(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); return agentGroupRestInfo; } - private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { String tempString; // do not allow empty name @@ -265,7 +278,7 @@ private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRes } } - private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { + private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { List skillsRestInfo; List queuesRestInfo; List clientsRestInfo; @@ -279,7 +292,7 @@ private MetadataRestInfo addAgentGroups(List agentGro skillsRestInfo = createSkillsRestInfo(agentGroup); queuesRestInfo = createQueuesRestInfo(agentGroup); clientsRestInfo = createClientsRestInfo(agentGroup); - OpenAcdAgentGroupRestInfo agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); + OpenAcdAgentGroupRestInfoFull agentGroupRestInfo = new OpenAcdAgentGroupRestInfoFull(agentGroup, skillsRestInfo, queuesRestInfo, clientsRestInfo); agentGroupsRestInfo.add(agentGroupRestInfo); } @@ -405,7 +418,7 @@ public int compare(Object object1, Object object2) { } } - private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfo agentGroupRestInfo) { + private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); // copy fields from rest info @@ -442,16 +455,16 @@ public OpenAcdAgentGroupsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("openacd-agent-group", OpenAcdAgentGroupsBundleRestInfo.class); - xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("group", OpenAcdAgentGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("queue", OpenAcdQueueRestInfo.class); xstream.alias("client", OpenAcdClientRestInfo.class); } } - static class OpenAcdAgentGroupRepresentation extends XStreamRepresentation { + static class OpenAcdAgentGroupRepresentation extends XStreamRepresentation { - public OpenAcdAgentGroupRepresentation(MediaType mediaType, OpenAcdAgentGroupRestInfo object) { + public OpenAcdAgentGroupRepresentation(MediaType mediaType, OpenAcdAgentGroupRestInfoFull object) { super(mediaType, object); } @@ -461,7 +474,7 @@ public OpenAcdAgentGroupRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("group", OpenAcdAgentGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("queue", OpenAcdQueueRestInfo.class); xstream.alias("client", OpenAcdClientRestInfo.class); @@ -474,9 +487,9 @@ protected void configureXStream(XStream xstream) { static class OpenAcdAgentGroupsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_groups; + private final List m_groups; - public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { + public OpenAcdAgentGroupsBundleRestInfo(List agentGroups, MetadataRestInfo metadata) { m_metadata = metadata; m_groups = agentGroups; } @@ -485,113 +498,11 @@ public MetadataRestInfo getMetadata() { return m_metadata; } - public List getGroups() { + public List getGroups() { return m_groups; } } - static class OpenAcdAgentGroupRestInfo { - private final String m_name; - private final int m_id; - private final String m_description; - private final List m_skills; - private final List m_queues; - private final List m_clients; - - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup, List skills, - List queues, List clients) { - m_name = agentGroup.getName(); - m_id = agentGroup.getId(); - m_description = agentGroup.getDescription(); - m_skills = skills; - m_queues = queues; - m_clients = clients; - } - - public String getName() { - return m_name; - } - - public int getId() { - return m_id; - } - - public String getDescription() { - return m_description; - } - - public List getSkills() { - return m_skills; - } - - public List getQueues() { - return m_queues; - } - - public List getClients() { - return m_clients; - } - } - - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - - static class OpenAcdClientRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_identity; - - public OpenAcdClientRestInfo(OpenAcdClient client) { - m_id = client.getId(); - m_name = client.getName(); - m_description = client.getDescription(); - m_identity = client.getIdentity(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getIdentity() { - return m_identity; - } - } // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 652feff7cf..9c281d123e 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -25,8 +25,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Collection; -import java.util.HashSet; import java.util.Collections; import java.util.Comparator; @@ -46,6 +44,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; public class OpenAcdClientsResource extends UserResource { @@ -113,8 +112,13 @@ public Representation represent(Variant variant) throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - clientRestInfo = createClientRestInfo(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + clientRestInfo = createClientRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } // return representation return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); @@ -154,46 +158,74 @@ public void storeRepresentation(Representation entity) throws ResourceException String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - client = m_openAcdContext.getClientById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + client = m_openAcdContext.getClientById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } // copy values over to existing - updateClient(client, clientRestInfo); - m_openAcdContext.saveClient(client); + try { + updateClient(client, clientRestInfo); + m_openAcdContext.saveClient(client); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed"); + return; + } + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, client.getId(), "Updated Client"); + return; } // otherwise add new - client = createClient(clientRestInfo); - m_openAcdContext.saveClient(client); - getResponse().setStatus(Status.SUCCESS_CREATED); + try { + client = createClient(clientRestInfo); + m_openAcdContext.saveClient(client); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, client.getId(), "Created Client"); } // DELETE - Delete single Skill // ---------------------------- - // deleteClient() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { - Collection clientIds = new HashSet(); + OpenAcdClient client; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + try{ + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + client = m_openAcdContext.getClientById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteClient(client); - clientIds.add(idInt); - m_openAcdContext.deleteClient(m_openAcdContext.getClientById(idInt)); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, client.getId(), "Deleted Client"); return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } // Helper functions @@ -382,36 +414,6 @@ public List getClients() { } } - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 257d8d1628..1c69718ad9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -47,6 +47,8 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; @@ -111,12 +113,17 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdQueueGroupRestInfo queueGroupRestInfo; + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueGroupRestInfo = createQueueGroupRestInfo(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroupRestInfo = createQueueGroupRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } // finally return group representation return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); @@ -125,7 +132,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List queueGroups = m_openAcdContext.getQueueGroups(); - List queueGroupsRestInfo = new ArrayList(); + List queueGroupsRestInfo = new ArrayList(); Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; @@ -149,28 +156,49 @@ public Representation represent(Variant variant) throws ResourceException { public void storeRepresentation(Representation entity) throws ResourceException { // get group from body OpenAcdQueueGroupRepresentation representation = new OpenAcdQueueGroupRepresentation(entity); - OpenAcdQueueGroupRestInfo queueGroupRestInfo = representation.getObject(); + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = representation.getObject(); OpenAcdQueueGroup queueGroup; // if have id then update a single group String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueGroup = m_openAcdContext.getQueueGroupById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroup = m_openAcdContext.getQueueGroupById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } // copy values over to existing group - updateQueueGroup(queueGroup, queueGroupRestInfo); - m_openAcdContext.saveQueueGroup(queueGroup); + try { + updateQueueGroup(queueGroup, queueGroupRestInfo); + m_openAcdContext.saveQueueGroup(queueGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, queueGroup.getId(), "Updated Queue"); return; } // otherwise add new agent group - queueGroup = createOpenAcdQueueGroup(queueGroupRestInfo); - m_openAcdContext.saveQueueGroup(queueGroup); - getResponse().setStatus(Status.SUCCESS_CREATED); + try { + queueGroup = createOpenAcdQueueGroup(queueGroupRestInfo); + m_openAcdContext.saveQueueGroup(queueGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue Group failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, queueGroup.getId(), "Created Queue Group"); } @@ -185,24 +213,32 @@ public void removeRepresentations() throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueGroup = m_openAcdContext.getQueueGroupById(idInt); - + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueGroup = m_openAcdContext.getQueueGroupById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + m_openAcdContext.deleteQueueGroup(queueGroup); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, queueGroup.getId(), "Deleted Queue Group"); + return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } // Helper functions // ---------------- - private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int id) throws ResourceException { - OpenAcdQueueGroupRestInfo queueGroupRestInfo; + private OpenAcdQueueGroupRestInfoFull createQueueGroupRestInfo(int id) throws ResourceException { + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo; List skillsRestInfo; List agentGroupRestInfo; @@ -212,7 +248,7 @@ private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int id) throws Resour skillsRestInfo = createSkillsRestInfo(queueGroup); agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); - queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo, agentGroupRestInfo); + queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -221,7 +257,7 @@ private OpenAcdQueueGroupRestInfo createQueueGroupRestInfo(int id) throws Resour return queueGroupRestInfo; } - private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfo queueGroupRestInfo) { + private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { String tempString; // do not allow empty name @@ -244,7 +280,7 @@ private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRes } } - private MetadataRestInfo addQueueGroups(List queueGroupsRestInfo, List queueGroups) { + private MetadataRestInfo addQueueGroups(List queueGroupsRestInfo, List queueGroups) { List skillsRestInfo; List agentGroupRestInfo; @@ -258,7 +294,7 @@ private MetadataRestInfo addQueueGroups(List queueGro skillsRestInfo = createSkillsRestInfo(queueGroup); agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); - OpenAcdQueueGroupRestInfo queueGroupRestInfo = new OpenAcdQueueGroupRestInfo(queueGroup, skillsRestInfo, agentGroupRestInfo); + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); queueGroupsRestInfo.add(queueGroupRestInfo); } @@ -367,7 +403,7 @@ public int compare(Object object1, Object object2) { } } - private OpenAcdQueueGroup createOpenAcdQueueGroup(OpenAcdQueueGroupRestInfo queueGroupRestInfo) { + private OpenAcdQueueGroup createOpenAcdQueueGroup(OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { OpenAcdQueueGroup queueGroup = new OpenAcdQueueGroup(); // copy fields from rest info @@ -404,15 +440,15 @@ public OpenAcdQueueGroupsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("openacd-queue-group", OpenAcdQueueGroupsBundleRestInfo.class); - xstream.alias("group", OpenAcdQueueGroupRestInfo.class); + xstream.alias("group", OpenAcdQueueGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } } - static class OpenAcdQueueGroupRepresentation extends XStreamRepresentation { + static class OpenAcdQueueGroupRepresentation extends XStreamRepresentation { - public OpenAcdQueueGroupRepresentation(MediaType mediaType, OpenAcdQueueGroupRestInfo object) { + public OpenAcdQueueGroupRepresentation(MediaType mediaType, OpenAcdQueueGroupRestInfoFull object) { super(mediaType, object); } @@ -422,7 +458,7 @@ public OpenAcdQueueGroupRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("group", OpenAcdQueueGroupRestInfo.class); + xstream.alias("group", OpenAcdQueueGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } @@ -434,9 +470,9 @@ protected void configureXStream(XStream xstream) { static class OpenAcdQueueGroupsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_groups; + private final List m_groups; - public OpenAcdQueueGroupsBundleRestInfo(List queueGroups, MetadataRestInfo metadata) { + public OpenAcdQueueGroupsBundleRestInfo(List queueGroups, MetadataRestInfo metadata) { m_metadata = metadata; m_groups = queueGroups; } @@ -445,77 +481,11 @@ public MetadataRestInfo getMetadata() { return m_metadata; } - public List getGroups() { + public List getGroups() { return m_groups; } } - static class OpenAcdQueueGroupRestInfo { - private final String m_name; - private final int m_id; - private final String m_description; - private final List m_skills; - private final List m_agentGroups; - - public OpenAcdQueueGroupRestInfo(OpenAcdQueueGroup queueGroup, List skills, List agentGroups) { - m_name = queueGroup.getName(); - m_id = queueGroup.getId(); - m_description = queueGroup.getDescription(); - m_skills = skills; - m_agentGroups = agentGroups; - } - - public String getName() { - return m_name; - } - - public int getId() { - return m_id; - } - - public String getDescription() { - return m_description; - } - - public List getSkills() { - return m_skills; - } - - public List getAgentGroups() { - return m_agentGroups; - } - } - - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index de98618649..d45bf9e7d6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -31,6 +31,7 @@ import java.util.Comparator; import java.util.Set; +import org.jfree.chart.axis.QuarterDateFormat; import org.restlet.Context; import org.restlet.data.MediaType; import org.restlet.data.Request; @@ -50,6 +51,8 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; @@ -114,12 +117,17 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdQueueRestInfo queueRestInfo; + OpenAcdQueueRestInfoFull queueRestInfo; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueRestInfo = createQueueRestInfo(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queueRestInfo = createQueueRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } // return representation return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); @@ -128,7 +136,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list List queues = m_openAcdContext.getQueues(); - List queuesRestInfo = new ArrayList(); + List queuesRestInfo = new ArrayList(); Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; @@ -152,28 +160,49 @@ public Representation represent(Variant variant) throws ResourceException { public void storeRepresentation(Representation entity) throws ResourceException { // get from request body OpenAcdQueueRepresentation representation = new OpenAcdQueueRepresentation(entity); - OpenAcdQueueRestInfo queueRestInfo = representation.getObject(); + OpenAcdQueueRestInfoFull queueRestInfo = representation.getObject(); OpenAcdQueue queue; // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queue = m_openAcdContext.getQueueById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queue = m_openAcdContext.getQueueById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } // copy values over to existing - updateQueue(queue, queueRestInfo); - m_openAcdContext.saveQueue(queue); + try { + updateQueue(queue, queueRestInfo); + m_openAcdContext.saveQueue(queue); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, queue.getId(), "Updated Queue"); return; } // otherwise add new - queue = createQueue(queueRestInfo); - m_openAcdContext.saveQueue(queue); - getResponse().setStatus(Status.SUCCESS_CREATED); + try { + queue = createQueue(queueRestInfo); + m_openAcdContext.saveQueue(queue); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, queue.getId(), "Created Queue"); } @@ -183,29 +212,38 @@ public void storeRepresentation(Representation entity) throws ResourceException // deleteQueue() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { - Collection queueIds = new HashSet(); + OpenAcdQueue queue; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + queue = m_openAcdContext.getQueueById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } - queueIds.add(idInt); - m_openAcdContext.deleteQueue(m_openAcdContext.getQueueById(idInt)); + m_openAcdContext.deleteQueue(queue); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, queue.getId(), "Deleted Queue"); return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } + // Helper functions // ---------------- - private OpenAcdQueueRestInfo createQueueRestInfo(int id) throws ResourceException { - OpenAcdQueueRestInfo queueRestInfo; + private OpenAcdQueueRestInfoFull createQueueRestInfo(int id) throws ResourceException { + OpenAcdQueueRestInfoFull queueRestInfo; List skillsRestInfo; List agentGroupsRestInfo; @@ -213,7 +251,7 @@ private OpenAcdQueueRestInfo createQueueRestInfo(int id) throws ResourceExceptio OpenAcdQueue queue = m_openAcdContext.getQueueById(id); skillsRestInfo = createSkillsRestInfo(queue); agentGroupsRestInfo = createAgentGroupsRestInfo(queue); - queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupsRestInfo); + queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupsRestInfo); } catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); @@ -253,9 +291,9 @@ private List createAgentGroupsRestInfo(OpenAcdQueue q return agentGroupsRestInfo; } - - private MetadataRestInfo addQueues(List queuesRestInfo, List queues) { - OpenAcdQueueRestInfo queueRestInfo; + + private MetadataRestInfo addQueues(List queuesRestInfo, List queues) { + OpenAcdQueueRestInfoFull queueRestInfo; List skillsRestInfo; List agentGroupRestInfo; @@ -268,7 +306,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo, Li skillsRestInfo = createSkillsRestInfo(queue); agentGroupRestInfo = createAgentGroupsRestInfo(queue); - queueRestInfo = new OpenAcdQueueRestInfo(queue, skillsRestInfo, agentGroupRestInfo); + queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupRestInfo); queuesRestInfo.add(queueRestInfo); } @@ -329,7 +367,7 @@ public int compare(Object object1, Object object2) { }); break; - + case DESCRIPTION: Collections.sort(queues, new Comparator(){ @@ -345,7 +383,7 @@ public int compare(Object object1, Object object2) { } } - private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { String tempString; OpenAcdQueueGroup queueGroup; @@ -355,29 +393,29 @@ private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfo queueRestInfo) queue.setName(tempString); } queueGroup = getQueueGroup(queueRestInfo); - + queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); } - private OpenAcdQueue createQueue(OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { OpenAcdQueue queue = new OpenAcdQueue(); OpenAcdQueueGroup queueGroup; // copy fields from rest info queue.setName(queueRestInfo.getName()); queueGroup = getQueueGroup(queueRestInfo); - + queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); return queue; } - private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfo queueRestInfo) throws ResourceException { + private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { OpenAcdQueueGroup queueGroup; int groupId = 0; - + try { groupId = queueRestInfo.getId(); queueGroup = m_openAcdContext.getQueueGroupById(groupId); @@ -385,10 +423,11 @@ private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfo queueRestInfo) thro catch (Exception exception) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Queue Group ID " + groupId + " not found."); } - + return queueGroup; } - + + // REST Representations // -------------------- @@ -405,15 +444,15 @@ public OpenAcdQueuesRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("openacd-queue", OpenAcdQueuesBundleRestInfo.class); - xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } } - static class OpenAcdQueueRepresentation extends XStreamRepresentation { + static class OpenAcdQueueRepresentation extends XStreamRepresentation { - public OpenAcdQueueRepresentation(MediaType mediaType, OpenAcdQueueRestInfo object) { + public OpenAcdQueueRepresentation(MediaType mediaType, OpenAcdQueueRestInfoFull object) { super(mediaType, object); } @@ -423,7 +462,7 @@ public OpenAcdQueueRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); } @@ -435,9 +474,9 @@ protected void configureXStream(XStream xstream) { static class OpenAcdQueuesBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_queues; + private final List m_queues; - public OpenAcdQueuesBundleRestInfo(List queues, MetadataRestInfo metadata) { + public OpenAcdQueuesBundleRestInfo(List queues, MetadataRestInfo metadata) { m_metadata = metadata; m_queues = queues; } @@ -446,85 +485,12 @@ public MetadataRestInfo getMetadata() { return m_metadata; } - public List getQueues() { + public List getQueues() { return m_queues; } } - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - - static class OpenAcdQueueRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final List m_skills; - private final List m_agentGroups; - private final String m_queueGroup; - - public OpenAcdQueueRestInfo(OpenAcdQueue queue, List skills, List agentGroups) { - m_id = queue.getId(); - m_name = queue.getName(); - m_description = queue.getDescription(); - m_queueGroup = queue.getQueueGroup(); - m_skills = skills; - m_agentGroups = agentGroups; - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getQueueGroup() - { - return m_queueGroup; - } - - public List getSkills() { - return m_skills; - } - - public List getAgentGroups() { - return m_agentGroups; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index a46168ac3c..2a0757fb2f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; public class OpenAcdReleaseCodesResource extends UserResource { @@ -113,8 +114,13 @@ public Representation represent(Variant variant) throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } // return representation return new OpenAcdReleaseCodeRepresentation(variant.getMediaType(), releaseCodeRestInfo); @@ -154,21 +160,42 @@ public void storeRepresentation(Representation entity) throws ResourceException String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - releaseCode = m_openAcdContext.getReleaseCodeById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCode = m_openAcdContext.getReleaseCodeById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } // copy values over to existing - updateReleaseCode(releaseCode, releaseCodeRestInfo); - m_openAcdContext.saveReleaseCode(releaseCode); + try { + updateReleaseCode(releaseCode, releaseCodeRestInfo); + m_openAcdContext.saveReleaseCode(releaseCode); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Release Code failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, releaseCode.getId(), "Updated Release Code"); return; } // otherwise add new - releaseCode = createReleaseCode(releaseCodeRestInfo); - m_openAcdContext.saveReleaseCode(releaseCode); - getResponse().setStatus(Status.SUCCESS_CREATED); + try { + releaseCode = createReleaseCode(releaseCodeRestInfo); + m_openAcdContext.saveReleaseCode(releaseCode); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Release Code failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, releaseCode.getId(), "Created Release Code"); } @@ -178,26 +205,32 @@ public void storeRepresentation(Representation entity) throws ResourceException // deleteReleaseCode() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { + // for some reason release codes are deleted by providing collection of ids, not by providing release code object Collection releaseCodeIds = new HashSet(); - OpenAcdReleaseCode releaseCode; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - releaseCode = m_openAcdContext.getReleaseCodeById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + releaseCodeIds.add(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } - releaseCodeIds.add(idInt); m_openAcdContext.removeReleaseCodes(releaseCodeIds); return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } + // Helper functions // ---------------- @@ -386,36 +419,6 @@ public List getReleaseCodes() { } } - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index aa18f7a916..2e47d05e82 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillGroupRestInfo; public class OpenAcdSkillGroupsResource extends UserResource { @@ -113,8 +114,13 @@ public Representation represent(Variant variant) throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillGroupRestInfo = createSkillGroupRestInfo(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroupRestInfo = createSkillGroupRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } // return representation return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); @@ -149,26 +155,47 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdSkillGroupRepresentation representation = new OpenAcdSkillGroupRepresentation(entity); OpenAcdSkillGroupRestInfo skillGroupRestInfo = representation.getObject(); OpenAcdSkillGroup skillGroup; - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillGroup = m_openAcdContext.getSkillGroupById(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroup = m_openAcdContext.getSkillGroupById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } // copy values over to existing - updateSkillGroup(skillGroup, skillGroupRestInfo); - m_openAcdContext.saveSkillGroup(skillGroup); + try { + updateSkillGroup(skillGroup, skillGroupRestInfo); + m_openAcdContext.saveSkillGroup(skillGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skillGroup.getId(), "Updated Skill Group"); return; } // otherwise add new - skillGroup = createSkillGroup(skillGroupRestInfo); - m_openAcdContext.saveSkillGroup(skillGroup); - getResponse().setStatus(Status.SUCCESS_CREATED); + try { + skillGroup = createSkillGroup(skillGroupRestInfo); + m_openAcdContext.saveSkillGroup(skillGroup); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skillGroup.getId(), "Created Skill Group"); } @@ -178,24 +205,32 @@ public void storeRepresentation(Representation entity) throws ResourceException // deleteSkillGroup() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { + // for some reason skill groups are deleted by providing collection of ids, not by providing skill group object Collection skillGroupIds = new HashSet(); - + // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - - skillGroupIds.add(idInt); + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skillGroupIds.add(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + m_openAcdContext.removeSkillGroups(skillGroupIds); return; } // no id string - getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } + // Helper functions // ---------------- @@ -215,7 +250,7 @@ private OpenAcdSkillGroupRestInfo createSkillGroupRestInfo(int id) throws Resour private MetadataRestInfo addSkillGroups(List skillsRestInfo, List skillGroups) { OpenAcdSkillGroupRestInfo skillRestInfo; - + // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skillGroups.size()); @@ -302,7 +337,7 @@ public int compare(Object object1, Object object2) { private void updateSkillGroup(OpenAcdSkillGroup skillGroup, OpenAcdSkillGroupRestInfo skillGroupRestInfo) throws ResourceException { String tempString; - + // do not allow empty name tempString = skillGroupRestInfo.getName(); if (!tempString.isEmpty()) { @@ -322,7 +357,7 @@ private OpenAcdSkillGroup createSkillGroup(OpenAcdSkillGroupRestInfo skillGroupR return skillGroup; } - + // REST Representations // -------------------- @@ -381,36 +416,6 @@ public List getSkills() { } } - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index cc5835a840..2a3d7dd018 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -33,7 +33,6 @@ import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Form; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; @@ -44,6 +43,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; public class OpenAcdSkillsResource extends UserResource { @@ -157,9 +157,8 @@ public void storeRepresentation(Representation entity) throws ResourceException String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { @@ -173,7 +172,7 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update skill failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed"); return; } @@ -189,7 +188,7 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update skill failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); return; } @@ -208,9 +207,8 @@ public void removeRepresentations() throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { @@ -427,36 +425,6 @@ public List getSkills() { } } - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 63b8b215cd..8704af3f0d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -21,6 +21,7 @@ package org.sipfoundry.sipxconfig.rest; import java.io.IOException; +import java.util.List; import org.restlet.data.MediaType; import org.restlet.data.Status; @@ -32,6 +33,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; @@ -155,6 +157,10 @@ public static SortInfo calculateSorting(Form form) { return sortInfo; } + + // XML Response functions + // ---------------------- + public static void setResponse(Response response, ResponseCode code, String message) { try { DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); @@ -168,7 +174,7 @@ public static void setResponse(Response response, ResponseCode code, String mess doc.appendChild(elementResponse); setResponseHeader(doc, elementResponse, code, message); - + // no data related to result (create function overloads to modify) response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); @@ -192,7 +198,7 @@ public static void setResponse(Response response, ResponseCode code, int id, Str doc.appendChild(elementResponse); setResponseHeader(doc, elementResponse, code, message); - + // add data related to result Element elementData = doc.createElement("data"); Element elementId = doc.createElement("id"); @@ -213,7 +219,7 @@ public static void setResponseError(Response response, ResponseCode code, String response.setEntity(representation); } - + public static Representation getResponseError(Response response, ResponseCode code, String message) { try { DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); @@ -227,17 +233,29 @@ public static Representation getResponseError(Response response, ResponseCode co doc.appendChild(elementResponse); setResponseHeader(doc, elementResponse, code, message); - + return representation; //new DomRepresentation(MediaType.TEXT_XML, doc); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } - + return null; } - + + private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { + + // add standard elements + Element elementCode = doc.createElement("code"); + elementCode.appendChild(doc.createTextNode(code.toString())); + elementResponse.appendChild(elementCode); + + Element elementMessage = doc.createElement("message"); + elementMessage.appendChild(doc.createTextNode(message)); + elementResponse.appendChild(elementMessage); + } + private static void setResponseStatus(Response response, ResponseCode code) { // set response status based on code switch (code) { @@ -248,33 +266,25 @@ private static void setResponseStatus(Response response, ResponseCode code) { case ERROR_MISSING_INPUT: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; - + case ERROR_BAD_INPUT: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; - + case ERROR_WRITE_FAILED: response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; - + default: response.setStatus(Status.SUCCESS_OK); } } - private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { + public static enum ResponseCode { + SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED + } - // add standard elements - Element elementCode = doc.createElement("code"); - elementCode.appendChild(doc.createTextNode(code.toString())); - elementResponse.appendChild(elementCode); - Element elementMessage = doc.createElement("message"); - elementMessage.appendChild(doc.createTextNode(message)); - elementResponse.appendChild(elementMessage); - } - - // Data objects // ------------ @@ -294,13 +304,39 @@ public static class SortInfo { String sortField = ""; } - public static enum ResponseCode { - SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED - } - - - // Common Rest Info objects (may be re-defined within Resource to include different fields + + // Common Rest Info objects // ------------------------ + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } static class OpenAcdSkillRestInfo { private final int m_id; @@ -405,6 +441,25 @@ public String getGroupName() { } } + static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { + private final List m_skills; + private final List m_agentGroups; + + public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups) { + super(queue); + m_skills = skills; + m_agentGroups = agentGroups; + } + + public List getSkills() { + return m_skills; + } + + public List getAgentGroups() { + return m_agentGroups; + } + } + static class OpenAcdClientRestInfo { private final int m_id; private final String m_name; @@ -434,7 +489,7 @@ public String getIdentity() { return m_identity; } } - + static class OpenAcdAgentGroupRestInfo { private final int m_id; private final String m_name; @@ -459,6 +514,68 @@ public String getDescription() { } } + static class OpenAcdAgentGroupRestInfoFull extends OpenAcdAgentGroupRestInfo { + private final List m_skills; + private final List m_queues; + private final List m_clients; + + public OpenAcdAgentGroupRestInfoFull(OpenAcdAgentGroup agentGroup, List skills, + List queues, List clients) { + super(agentGroup); + m_skills = skills; + m_queues = queues; + m_clients = clients; + } + + public List getSkills() { + return m_skills; + } + + public List getQueues() { + return m_queues; + } + + public List getClients() { + return m_clients; + } + } + + static class OpenAcdQueueGroupRestInfoFull { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + private final List m_agentGroups; + + public OpenAcdQueueGroupRestInfoFull(OpenAcdQueueGroup queueGroup, List skills, List agentGroups) { + m_name = queueGroup.getName(); + m_id = queueGroup.getId(); + m_description = queueGroup.getDescription(); + m_skills = skills; + m_agentGroups = agentGroups; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + + public List getAgentGroups() { + return m_agentGroups; + } + } + static class OpenAcdReleaseCodeRestInfo { private final int m_id; private final String m_label; From c3c07fe1e37d9a032708d74d87cb19be1edbac51 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 28 Mar 2012 00:17:05 -0400 Subject: [PATCH 34/99] Cleanup --- .../sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 9c281d123e..a811e561d5 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -178,7 +178,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, client.getId(), "Updated Client"); - + return; } @@ -192,7 +192,7 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); return; } - + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, client.getId(), "Created Client"); } @@ -228,6 +228,7 @@ public void removeRepresentations() throws ResourceException { OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } + // Helper functions // ---------------- From eb5b7c26a19d38248103fcab047ec52a0c92dfe6 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 29 Mar 2012 17:27:15 -0400 Subject: [PATCH 35/99] Add OpenACD Agent REST API --- .../rest/OpenAcdAgentsResource.java | 596 ++++++++++++++++++ .../sipxconfig/rest/OpenAcdUtilities.java | 80 ++- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 3 files changed, 689 insertions(+), 3 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java new file mode 100644 index 0000000000..65575b0325 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -0,0 +1,596 @@ +/* + * + * OpenAcdAgentGroupsResource.java - A Restlet to read Agent data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Waseem + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.LinkedHashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; +import com.thoughtworks.xstream.XStream; + +import org.sipfoundry.sipxconfig.common.CoreContext; +import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; + + +public class OpenAcdAgentsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private CoreContext m_coreContext; + private Form m_form; + + // use to define all possible sort fields + private enum SortField + { + NAME, GROUP, SECURITY, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + m_coreContext = getCoreContext(); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Agent + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdAgentRestInfoFull agentRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agentRestInfo = createAgentRestInfo(idInt); + } catch(Exception ex) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } + + // finally return group representation + return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo); + } + + + // if not single, process request for all + List agents = m_openAcdContext.getAgents(); + List agentsRestInfo = new ArrayList(); + 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); + } + + + // PUT - Update or Add single Agent + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdAgentRepresentation representation = new OpenAcdAgentRepresentation(entity); + OpenAcdAgentRestInfoFull agentRestInfo = (OpenAcdAgentRestInfoFull) representation.getObject(); + OpenAcdAgent agent; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agent = m_openAcdContext.getAgentById(idInt); + } catch(Exception ex) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed"); + return; + } + + // copy values over to existing + try { + updateAgent(agent, agentRestInfo); + m_openAcdContext.saveAgent(agent); + } catch(Exception ex) { + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent"); + return; + } + + + // otherwise add new + try { + agent = createOpenAcdAgent(agentRestInfo); + m_openAcdContext.saveAgent(agent); + } catch(Exception ex) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agent.getId(), "Created Skill"); + } + + + // DELETE - Delete single Agent + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdAgent agent; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + agent = m_openAcdContext.getAgentById(idInt); + } catch (Exception ex) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteAgent(agent); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, agent.getId(), "Deleted Agent."); + + return; + } + + // no id string + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value is missing."); + } + + + // Helper functions + // ---------------- + + private OpenAcdAgentRestInfoFull createAgentRestInfo(int id) throws ResourceException { + OpenAcdAgentRestInfoFull agentRestInfo; + List skillsRestInfo; + List queuesRestInfo; + List clientsRestInfo; + + OpenAcdAgent agent = m_openAcdContext.getAgentById(id); + + skillsRestInfo = createSkillsRestInfo(agent); + queuesRestInfo = createQueuesRestInfo(agent); + clientsRestInfo = createClientRestInfo(agent); + agentRestInfo = new OpenAcdAgentRestInfoFull(agent, skillsRestInfo, queuesRestInfo, clientsRestInfo); + + return agentRestInfo; + } + + private MetadataRestInfo addAgents(List agentsRestInfo, List agents) { + List skillsRestInfo; + List queuesRestInfo; + List clientsRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agents.size()); + + // create list of agent restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdAgent agent = agents.get(index); + skillsRestInfo = createSkillsRestInfo(agent); + queuesRestInfo = createQueuesRestInfo(agent); + clientsRestInfo = createClientRestInfo(agent); + + OpenAcdAgentRestInfoFull agentRestInfo = new OpenAcdAgentRestInfoFull(agent, skillsRestInfo, queuesRestInfo, clientsRestInfo); + agentsRestInfo.add(agentRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private List createSkillsRestInfo(OpenAcdAgent agent) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + Set groupSkills = agent.getSkills(); + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); + } + + return skillsRestInfo; + } + + private List createQueuesRestInfo(OpenAcdAgent agent) { + List queuesRestInfo; + OpenAcdQueueRestInfo queueRestInfo; + + // create list of queue restinfos for single group + Set groupQueues = agent.getQueues(); + queuesRestInfo = new ArrayList(groupQueues.size()); + + for (OpenAcdQueue groupQueue : groupQueues) { + queueRestInfo = new OpenAcdQueueRestInfo(groupQueue); + queuesRestInfo.add(queueRestInfo); + } + + return queuesRestInfo; + } + + private List createClientRestInfo(OpenAcdAgent agent) { + List clientsRestInfo; + OpenAcdClientRestInfo clientRestInfo; + + // create list of queue restinfos for single group + Set groupClients = agent.getClients(); + clientsRestInfo = new ArrayList(groupClients.size()); + + for (OpenAcdClient groupClient : groupClients) { + clientRestInfo = new OpenAcdClientRestInfo(groupClient); + clientsRestInfo.add(clientRestInfo); + } + + return clientsRestInfo; + + } + + private void sortAgents(List agents) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + + return agent1.getName().compareToIgnoreCase(agent2.getName()); + } + + }); + break; + + case GROUP: + Collections.sort(agents, new Comparator() { + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + + return agent1.getAgentGroup().compareToIgnoreCase(agent2.getAgentGroup()); + } + }); + break; + + case SECURITY: + Collections.sort(agents, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return agent1.getSecurity().compareToIgnoreCase(agent2.getSecurity()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return agent2.getName().compareToIgnoreCase(agent1.getName()); + } + }); + break; + + case GROUP: + Collections.sort(agents, new Comparator() { + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + + return agent2.getAgentGroup().compareToIgnoreCase(agent1.getAgentGroup()); + } + }); + break; + + case SECURITY: + Collections.sort(agents, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return agent2.getSecurity().compareToIgnoreCase(agent1.getSecurity()); + } + }); + break; + } + } + } + + private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestInfo) throws ResourceException { + OpenAcdAgentGroup agentGroup; + + agentGroup = getAgentGroup(agentRestInfo); + agent.setGroup(agentGroup); + + agent.setSecurity(agentRestInfo.getSecurity()); + + agent.getSkills().clear(); + + OpenAcdSkill skill; + List skillsRestInfo = agentRestInfo.getSkills(); + for(OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + agent.addSkill(skill); + } + + agent.getQueues().clear(); + + OpenAcdQueue queue; + List queuesRestInfo = agentRestInfo.getQueues(); + for(OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + queue = m_openAcdContext.getQueueById(queueRestInfo.getId()); + agent.addQueue(queue); + } + + agent.getClients().clear(); + + OpenAcdClient client; + List clientsRestInfo = agentRestInfo.getClients(); + for(OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + client = m_openAcdContext.getClientById(clientRestInfo.getId()); + agent.addClient(client); + } + + } + + private OpenAcdAgent createOpenAcdAgent(OpenAcdAgentRestInfoFull agentRestInfo) throws ResourceException { + OpenAcdAgent agent = new OpenAcdAgent(); + OpenAcdAgent duplicateAgent = null; + OpenAcdAgentGroup agentGroup; + User user; + + agentGroup = getAgentGroup(agentRestInfo); + agent.setGroup(agentGroup); + + agent.setSecurity(agentRestInfo.getSecurity()); + + user = m_coreContext.getUser(agentRestInfo.getUserId()); + + // check if user is already assigned as agent + duplicateAgent = m_openAcdContext.getAgentByUser(user); + if (duplicateAgent != null) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "User " + user.getId() + " already assigned as agent."); + } + + agent.setUser(user); + + Set skills = new LinkedHashSet(); + List skillsRestInfo = agentRestInfo.getSkills(); + + for(OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + } + + Set queues = new LinkedHashSet(); + List queuesRestInfo = agentRestInfo.getQueues(); + + for(OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + queues.add(m_openAcdContext.getQueueById(queueRestInfo.getId())); + } + + Set clients = new LinkedHashSet(); + List clientsRestInfo = agentRestInfo.getClients(); + + for(OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + clients.add(m_openAcdContext.getClientById(clientRestInfo.getId())); + } + + agent.setSkills(skills); + agent.setQueues(queues); + agent.setClients(clients); + + return agent; + } + private OpenAcdAgentGroup getAgentGroup(OpenAcdAgentRestInfoFull agentRestInfo) throws ResourceException { + OpenAcdAgentGroup agentGroup; + int groupId = 0; + + try { + groupId = agentRestInfo.getGroupId(); + agentGroup = m_openAcdContext.getAgentGroupById(groupId); + } catch(Exception ex) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Agent Group ID " + groupId + " not found."); + } + + return agentGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdAgentsRepresentation extends XStreamRepresentation { + + public OpenAcdAgentsRepresentation(MediaType mediaType, OpenAcdAgentsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdAgentsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-agent", OpenAcdAgentsBundleRestInfo.class); + xstream.alias("agent", OpenAcdAgentRestInfoFull.class); + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("client", OpenAcdClientRestInfo.class); + } + } + + static class OpenAcdAgentRepresentation extends XStreamRepresentation { + + public OpenAcdAgentRepresentation(MediaType mediaType, OpenAcdAgentRestInfoFull object) { + super(mediaType, object); + } + + public OpenAcdAgentRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("agent", OpenAcdAgentRestInfoFull.class); + xstream.alias("group", OpenAcdAgentGroupRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfo.class); + xstream.alias("queue", OpenAcdQueueRestInfo.class); + xstream.alias("client", OpenAcdClientRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdAgentsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_agents; + + public OpenAcdAgentsBundleRestInfo(List agents, MetadataRestInfo metadata) { + m_metadata = metadata; + m_agents = agents; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getAgents() { + return m_agents; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } +} \ No newline at end of file diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 8704af3f0d..e69c5aaa05 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -30,6 +30,7 @@ import org.restlet.resource.Representation; import org.restlet.resource.DomRepresentation; import org.restlet.resource.ResourceException; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; @@ -337,7 +338,7 @@ public int getResultsPerPage() { return m_resultsPerPage; } } - + static class OpenAcdSkillRestInfo { private final int m_id; private final String m_name; @@ -440,7 +441,7 @@ public String getGroupName() { return m_groupName; } } - + static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { private final List m_skills; private final List m_agentGroups; @@ -534,7 +535,7 @@ public List getSkills() { public List getQueues() { return m_queues; } - + public List getClients() { return m_clients; } @@ -606,4 +607,77 @@ public int getBias() { } } + static class OpenAcdAgentRestInfoFull { + + private final int m_id; + private final int m_userId; + private final String m_userName; + private final String m_firstName; + private final String m_lastName; + private final int m_groupId; + private final String m_groupName; + private final String m_security; + private final List m_skills; + private final List m_queues; + private final List m_clients; + + public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List skills, List queues, List clients) { + m_id = agent.getId(); + m_firstName = agent.getFirstName(); + m_lastName = agent.getLastName(); + m_userId = agent.getUser().getId(); + m_userName = agent.getUser().getName(); + m_groupId = agent.getGroup().getId(); + m_groupName = agent.getGroup().getName(); + m_security = agent.getSecurity(); + m_skills = skills; + m_queues = queues; + m_clients = clients; + } + + public int getId() { + return m_id; + } + + public String getFirstName() { + return m_firstName; + } + + public String getLastName() { + return m_lastName; + } + + public int getUserId() { + return m_userId; + } + + public String getUserName() { + return m_userName; + } + + public int getGroupId() { + return m_groupId; + } + + public String getGroupName() { + return m_groupName; + } + + public String getSecurity() { + return m_security; + } + + public List getSkills() { + return m_skills; + } + + public List getQueues() { + return m_queues; + } + + public List getClients() { + return m_clients; + } + } + } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index a036a9446e..c9f4d2995c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -336,4 +336,20 @@ + + + + + + + + + + + + + + + + From 8020029b80db2c0b86c30302d84d3cf3e3ae655c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 29 Mar 2012 18:26:54 -0400 Subject: [PATCH 36/99] Add saving of OpenACD agent password as pinToken --- .../sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java | 3 ++- .../org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index 65575b0325..da99a34203 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -426,7 +426,8 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI agent.setGroup(agentGroup); agent.setSecurity(agentRestInfo.getSecurity()); - + agent.getUser().setPintoken(agentRestInfo.getPassword()); + agent.getSkills().clear(); OpenAcdSkill skill; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index e69c5aaa05..303ceedd2b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -617,6 +617,7 @@ static class OpenAcdAgentRestInfoFull { private final int m_groupId; private final String m_groupName; private final String m_security; + private final String m_password; private final List m_skills; private final List m_queues; private final List m_clients; @@ -630,6 +631,7 @@ public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List s m_groupId = agent.getGroup().getId(); m_groupName = agent.getGroup().getName(); m_security = agent.getSecurity(); + m_password = ""; // only used on updates, not rest get m_skills = skills; m_queues = queues; m_clients = clients; @@ -667,6 +669,10 @@ public String getSecurity() { return m_security; } + public String getPassword() { + return m_password; + } + public List getSkills() { return m_skills; } From 15790e1627fae32b248b7e2790a2e9a2c8b1e933 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 29 Mar 2012 18:27:22 -0400 Subject: [PATCH 37/99] Add tracking of Client in "skills" --- .../rest/OpenAcdAgentGroupsResource.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 431c146d80..07c3a922f6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -35,7 +35,6 @@ import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Form; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; @@ -199,7 +198,7 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed"); return; } - + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); } @@ -276,6 +275,28 @@ private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRes skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); agentGroup.addSkill(skill); } + + // remove all current queues + agentGroup.getQueues().clear(); + + // set queues + OpenAcdQueue queue; + List queuesRestInfo = agentGroupRestInfo.getQueues(); + for (OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + queue = m_openAcdContext.getQueueById(queueRestInfo.getId()); + agentGroup.addQueue(queue); + } + + // remove all current clients + agentGroup.getClients().clear(); + + // set clients + OpenAcdClient client; + List clientsRestInfo = agentGroupRestInfo.getClients(); + for (OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + client = m_openAcdContext.getClientById(clientRestInfo.getId()); + agentGroup.addClient(client); + } } private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { @@ -349,7 +370,7 @@ private List createClientsRestInfo(OpenAcdAgentGroup agen return clientsRestInfo; } - + private void sortGroups(List agentGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); @@ -503,7 +524,7 @@ public List getGroups() { } } - + // Injected objects // ---------------- From ca93ec43a59c6497124c438bd74f9d9d7c05a5ae Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 29 Mar 2012 18:59:11 -0400 Subject: [PATCH 38/99] Correct password update to ignore if password empty --- .../sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index da99a34203..56444d3ca9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -426,7 +426,11 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI agent.setGroup(agentGroup); agent.setSecurity(agentRestInfo.getSecurity()); - agent.getUser().setPintoken(agentRestInfo.getPassword()); + + // only update password if it is not empty (since caller cannot obtain password to pass back for updating) + if (!agentRestInfo.getPassword().isEmpty()) { + agent.getUser().setPintoken(agentRestInfo.getPassword()); + } agent.getSkills().clear(); From 6673796f45f467709c9e396b64e01b25d69cd321 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 29 Mar 2012 18:59:35 -0400 Subject: [PATCH 39/99] Add sorting on Atom --- .../rest/OpenAcdSkillsResource.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 2a3d7dd018..a2dcdb8e2f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -54,7 +54,7 @@ public class OpenAcdSkillsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, NONE; + NAME, DESCRIPTION, ATOM, NONE; public static SortField toSortField(String fieldString) { @@ -295,6 +295,19 @@ public int compare(Object object1, Object object2) { }); break; + + + case ATOM: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + } + + }); + break; } } else { @@ -323,6 +336,18 @@ public int compare(Object object1, Object object2) { }); break; + + case ATOM: + Collections.sort(skills, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + } + + }); + break; } } } From d7e20bd3a2071f636ff73a3b09159a3c13e757b0 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 30 Mar 2012 18:08:18 -0400 Subject: [PATCH 40/99] Add OpenACD Lines REST API --- .../sipxconfig/rest/OpenAcdLinesResource.java | 503 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 2 files changed, 519 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java new file mode 100644 index 0000000000..4def450466 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -0,0 +1,503 @@ +/* + * + * OpenAcdLinesResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, I. Wesson + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import java.util.HashSet; +import java.util.Collections; +import java.util.Comparator; + +import org.restlet.Context; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Form; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; + +// OpenAcdLines are different +// -------------------------- +// Lines is inconsistent with other OpenACD objects. +// OpenAcdContext does not contain functions for getLineById(), saveLine() or removeLine(). +// A Set of Lines is obtained using getLines() and only set operations are available +// so this API will appear slightly different than other APIs, although attempts have been made to preserve general structure. +public class OpenAcdLinesResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField + { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) + { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + + // GET - Retrieve all and single Line + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdLineRestInfo lineRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + lineRestInfo = createLineRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } + + // return representation + return new OpenAcdLineRepresentation(variant.getMediaType(), lineRestInfo); + } + + + // if not single, process request for list + List lines = new ArrayList (m_openAcdContext.getLines()); + List linesRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortLines(lines); + + // set requested records and get resulting metadata + metadataRestInfo = addLines(linesRestInfo, lines); + + // create final restinfo + OpenAcdLinesBundleRestInfo linesBundleRestInfo = new OpenAcdLinesBundleRestInfo(linesRestInfo, metadataRestInfo); + + return new OpenAcdLinesRepresentation(variant.getMediaType(), linesBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdLineRepresentation representation = new OpenAcdLineRepresentation(entity); + OpenAcdLineRestInfo lineRestInfo = representation.getObject(); + OpenAcdLine line = null; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + line = getLineById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateLine(line, lineRestInfo); + // line in set should already be modified since got pointer, if not need a getLines().remove and add + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, line.getId(), "Updated Line"); + + return; + } + + + // otherwise add new + try { + // lines are created using a function without passing an object to it, so perform creation and assignment within createLine + createLine(lineRestInfo); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, line.getId(), "Created Line"); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + // deleteLine() not available from openAcdContext + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdLine line; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + line = getLineById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // need delete + m_openAcdContext.getLines().remove(line); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, line.getId(), "Deleted Skill"); + + return; + } + + // no id string + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + private OpenAcdLine getLineById(int id) { + OpenAcdLine line = null; + + for (OpenAcdLine currentLine : m_openAcdContext.getLines()) { + if(currentLine.getId() == id) + { + line = currentLine; + break; + } + } + + return line; + } + + private OpenAcdLineRestInfo createLineRestInfo(int id) throws ResourceException { + OpenAcdLineRestInfo lineRestInfo; + + OpenAcdLine line = getLineById(id); + lineRestInfo = new OpenAcdLineRestInfo(line); + + return lineRestInfo; + } + + private MetadataRestInfo addLines(List linesRestInfo, List lines) { + OpenAcdLineRestInfo lineRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, lines.size()); + + // create list of line restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdLine line = lines.get(index); + + lineRestInfo = new OpenAcdLineRestInfo(line); + linesRestInfo.add(lineRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortLines(List lines) { + // sort groups if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(lines, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return line1.getName().compareToIgnoreCase(line2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(lines, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return line1.getDescription().compareToIgnoreCase(line2.getDescription()); + } + + }); + break; + + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(lines, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return line2.getName().compareToIgnoreCase(line1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(lines, new Comparator(){ + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return line2.getDescription().compareToIgnoreCase(line1.getDescription()); + } + + }); + break; + } + } + } + + private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) throws ResourceException { + String tempString; + + // do not allow empty name + tempString = lineRestInfo.getName(); + if (!tempString.isEmpty()) { + line.setName(tempString); + } + + //line.setExtension(lineRestInfo.getExtension()); + //line.setRegex(lineRestInfo.getRegex()); + line.setDid(lineRestInfo.getDIDNumber()); + line.setDescription(lineRestInfo.getDescription()); + line.setAlias(lineRestInfo.getAlias()); + } + + private void createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { + OpenAcdLine line = m_openAcdContext.newOpenAcdLine(); + + // copy fields from rest info + line.setName(lineRestInfo.getName()); + //line.setExtension(lineRestInfo.getExtension()); + //line.setRegex(lineRestInfo.getRegex()); + line.setDid(lineRestInfo.getDIDNumber()); + line.setDescription(lineRestInfo.getDescription()); + line.setAlias(lineRestInfo.getAlias()); + } + + + // REST Representations + // -------------------- + + static class OpenAcdLinesRepresentation extends XStreamRepresentation { + + public OpenAcdLinesRepresentation(MediaType mediaType, OpenAcdLinesBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdLinesRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-line", OpenAcdLinesBundleRestInfo.class); + xstream.alias("line", OpenAcdLineRestInfo.class); + } + } + + static class OpenAcdLineRepresentation extends XStreamRepresentation { + + public OpenAcdLineRepresentation(MediaType mediaType, OpenAcdLineRestInfo object) { + super(mediaType, object); + } + + public OpenAcdLineRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("line", OpenAcdLineRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdLinesBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_lines; + + public OpenAcdLinesBundleRestInfo(List lines, MetadataRestInfo metadata) { + m_metadata = metadata; + m_lines = lines; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getLines() { + return m_lines; + } + } + + static class OpenAcdLineRestInfo { + private final int m_id; + private final String m_name; + private final String m_extension; + private final boolean m_regex; + private final String m_didnumber; + private final String m_description; + private final String m_alias; + + public OpenAcdLineRestInfo(OpenAcdLine line) { + m_id = line.getId(); + m_name = line.getName(); + m_extension = line.getExtension(); + m_regex = line.getRegex(); + m_didnumber = line.getDid(); + m_description = line.getDescription(); + m_alias = line.getAlias(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getExtension(){ + return m_extension; + } + + public boolean getRegex(){ + return m_regex; + } + + public String getDIDNumber(){ + return m_didnumber; + } + + public String getDescription() { + return m_description; + } + + public String getAlias() { + return m_alias; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index c9f4d2995c..eafd584a58 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -352,4 +352,20 @@ + + + + + + + + + + + + + + + + From be647bc8c5abb17c040044e0e7f3479cd370e086 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 30 Mar 2012 18:08:43 -0400 Subject: [PATCH 41/99] Remove legacy code line --- .../sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java | 1 - .../org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java | 1 - .../sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java | 1 - .../org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java | 1 - .../sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java | 1 - .../sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java | 1 - .../org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java | 1 - 7 files changed, 7 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 07c3a922f6..25f1c618dc 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -134,7 +134,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List agentGroups = m_openAcdContext.getAgentGroups(); List agentGroupsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index a811e561d5..3e53a53c61 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -128,7 +128,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list List clients = m_openAcdContext.getClients(); List clientsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 1c69718ad9..560c5a7aee 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -133,7 +133,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List queueGroups = m_openAcdContext.getQueueGroups(); List queueGroupsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index d45bf9e7d6..42b3fd22e1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -137,7 +137,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list List queues = m_openAcdContext.getQueues(); List queuesRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 2a0757fb2f..0835309498 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -130,7 +130,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list List releaseCodes = m_openAcdContext.getReleaseCodes(); List releaseCodesRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 2e47d05e82..9ebb682f79 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -130,7 +130,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list List skillGroups = m_openAcdContext.getSkillGroups(); List skillGroupsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index a2dcdb8e2f..5891052807 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -127,7 +127,6 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all List skills = m_openAcdContext.getSkills(); List skillsRestInfo = new ArrayList(); - Form form = getRequest().getResourceRef().getQueryAsForm(); MetadataRestInfo metadataRestInfo; // sort groups if specified From e03f519ae4b6fda6ab4a8407d93101acc765fab1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 30 Mar 2012 18:25:20 -0400 Subject: [PATCH 42/99] Update Lines to conform with behavior of normal OpenACD objects --- .../sipxconfig/rest/OpenAcdLinesResource.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 4def450466..7612c4c4bb 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -113,8 +113,8 @@ public boolean allowDelete() { } - // GET - Retrieve all and single Line - // ----------------------------------- + // GET - Retrieve all and single item + // ---------------------------------- @Override public Representation represent(Variant variant) throws ResourceException { @@ -155,8 +155,8 @@ public Representation represent(Variant variant) throws ResourceException { } - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update or Add single item + // ------------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { @@ -208,10 +208,9 @@ public void storeRepresentation(Representation entity) throws ResourceException } - // DELETE - Delete single Skill - // ---------------------------- + // DELETE - Delete single item + // --------------------------- - // deleteLine() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { OpenAcdLine line; @@ -229,10 +228,10 @@ public void removeRepresentations() throws ResourceException { return; } - // need delete + // deleteLine() not available from openAcdContext m_openAcdContext.getLines().remove(line); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, line.getId(), "Deleted Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, line.getId(), "Deleted Line"); return; } @@ -245,7 +244,7 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - private OpenAcdLine getLineById(int id) { + private OpenAcdLine getLineById(int id) throws ResourceException { OpenAcdLine line = null; for (OpenAcdLine currentLine : m_openAcdContext.getLines()) { @@ -256,6 +255,11 @@ private OpenAcdLine getLineById(int id) { } } + // duplicate behavior of standard OpenAcdContext.getXById() functions + if (line == null) { + throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "No row with the given identifier exists: " + id); + } + return line; } From d794cbfd5dd832f91d512efc7b9e3caafea83d1a Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 1 Apr 2012 19:21:11 -0400 Subject: [PATCH 43/99] Add validation of submitted REST info for create and update --- .../rest/OpenAcdReleaseCodesResource.java | 31 +++++++++++++++++++ .../sipxconfig/rest/OpenAcdUtilities.java | 8 ++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 0835309498..3cdee9b064 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -45,9 +45,11 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdReleaseCodesResource extends UserResource { @@ -155,6 +157,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdReleaseCodeRestInfo releaseCodeRestInfo = representation.getObject(); OpenAcdReleaseCode releaseCode; + // validate input for update or create + ValidationInfo validationInfo = validate(releaseCodeRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -233,6 +244,26 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdReleaseCodeRestInfo restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + // release code object will allow store of bias other value than -1, 0, or 1, + // but then current SipXconfig administrative UI will display nothing for bias name. + int bias = restInfo.getBias(); + if ((bias < -1) || (bias >1)) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Bias must be be -1, 0 or 1"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + + return validationInfo; + } + + return validationInfo; + } + private OpenAcdReleaseCodeRestInfo createReleaseCodeRestInfo(int id) throws ResourceException { OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 303ceedd2b..8ca9945831 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -282,7 +282,7 @@ private static void setResponseStatus(Response response, ResponseCode code) { } public static enum ResponseCode { - SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED + SUCCESS, SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED } @@ -305,6 +305,12 @@ public static class SortInfo { String sortField = ""; } + public static class ValidationInfo { + Boolean valid = true; + String message = "Valid"; + ResponseCode responseCode = ResponseCode.SUCCESS; + } + // Common Rest Info objects // ------------------------ From be3781ac22faf69254fd7e88ece9a163aa3a7b7b Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 1 Apr 2012 19:22:31 -0400 Subject: [PATCH 44/99] Add extension and regex support --- .../sipxconfig/rest/OpenAcdLinesResource.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 7612c4c4bb..9208455a85 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -43,6 +43,7 @@ import com.thoughtworks.xstream.XStream; +import org.sipfoundry.sipxconfig.freeswitch.FreeswitchCondition; import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; @@ -181,7 +182,7 @@ public void storeRepresentation(Representation entity) throws ResourceException // copy values over to existing try { updateLine(line, lineRestInfo); - // line in set should already be modified since got pointer, if not need a getLines().remove and add + saveLine(line); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed"); @@ -263,6 +264,11 @@ private OpenAcdLine getLineById(int id) throws ResourceException { return line; } + private void saveLine(OpenAcdLine line) throws ResourceException { + m_openAcdContext.getLines().remove(getLineById(line.getId())); + m_openAcdContext.getLines().add(line); + } + private OpenAcdLineRestInfo createLineRestInfo(int id) throws ResourceException { OpenAcdLineRestInfo lineRestInfo; @@ -371,6 +377,9 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro //line.setExtension(lineRestInfo.getExtension()); //line.setRegex(lineRestInfo.getRegex()); + line.getNumberCondition().setExpression(lineRestInfo.getExtension()); + line.getNumberCondition().setRegex(lineRestInfo.getRegex()); + line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); @@ -381,8 +390,12 @@ private void createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceExcepti // copy fields from rest info line.setName(lineRestInfo.getName()); + //line.setExtension(lineRestInfo.getExtension()); //line.setRegex(lineRestInfo.getRegex()); + line.getNumberCondition().setExpression(lineRestInfo.getExtension()); + line.getNumberCondition().setRegex(lineRestInfo.getRegex()); + line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); @@ -450,19 +463,19 @@ public List getLines() { static class OpenAcdLineRestInfo { private final int m_id; private final String m_name; + private final String m_description; private final String m_extension; private final boolean m_regex; private final String m_didnumber; - private final String m_description; private final String m_alias; public OpenAcdLineRestInfo(OpenAcdLine line) { m_id = line.getId(); m_name = line.getName(); + m_description = line.getDescription(); m_extension = line.getExtension(); m_regex = line.getRegex(); m_didnumber = line.getDid(); - m_description = line.getDescription(); m_alias = line.getAlias(); } @@ -474,6 +487,10 @@ public String getName() { return m_name; } + public String getDescription() { + return m_description; + } + public String getExtension(){ return m_extension; } @@ -486,13 +503,11 @@ public String getDIDNumber(){ return m_didnumber; } - public String getDescription() { - return m_description; - } - public String getAlias() { return m_alias; } + + // need queue, client, allow voicemail, answer supervision mode, welcome message, options (list) } From e4ac783156f394ff751ca551077dc5079443f29c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 1 Apr 2012 21:49:10 -0400 Subject: [PATCH 45/99] Support validation of REST information provided --- .../rest/OpenAcdAgentGroupsResource.java | 20 ++++++ .../rest/OpenAcdAgentsResource.java | 53 ++++++++++----- .../rest/OpenAcdClientsResource.java | 19 ++++++ .../sipxconfig/rest/OpenAcdLinesResource.java | 66 +++++++++++-------- .../rest/OpenAcdQueueGroupsResource.java | 19 ++++++ .../rest/OpenAcdQueuesResource.java | 20 ++++++ .../rest/OpenAcdSkillGroupsResource.java | 19 ++++++ .../rest/OpenAcdSkillsResource.java | 19 ++++++ 8 files changed, 189 insertions(+), 46 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 25f1c618dc..fbdc028087 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -45,6 +45,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.OpenAcdLinesResource.OpenAcdLineRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; @@ -52,6 +53,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdAgentGroupsResource extends UserResource { @@ -159,6 +161,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdAgentGroupRestInfoFull agentGroupRestInfo = representation.getObject(); OpenAcdAgentGroup agentGroup; + // validate input for update or create + ValidationInfo validationInfo = validate(agentGroupRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update a single group String idString = (String) getRequest().getAttributes().get("id"); @@ -237,6 +248,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdAgentGroupRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdAgentGroupRestInfoFull createAgentGroupRestInfo(int id) throws ResourceException { OpenAcdAgentGroupRestInfoFull agentGroupRestInfo; List skillsRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index 56444d3ca9..fdc6e18920 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -24,40 +24,41 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.LinkedHashSet; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; - import org.sipfoundry.sipxconfig.common.CoreContext; import org.sipfoundry.sipxconfig.common.User; -import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdAgentsResource extends UserResource { @@ -94,7 +95,7 @@ public void init(Context context, Request request, Response response) { getVariants().add(new Variant(APPLICATION_JSON)); m_coreContext = getCoreContext(); - + // pull parameters from url m_form = getRequest().getResourceRef().getQueryAsForm(); } @@ -168,6 +169,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdAgentRestInfoFull agentRestInfo = (OpenAcdAgentRestInfoFull) representation.getObject(); OpenAcdAgent agent; + // validate input for update or create + ValidationInfo validationInfo = validate(agentRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -241,6 +251,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdAgentRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdAgentRestInfoFull createAgentRestInfo(int id) throws ResourceException { OpenAcdAgentRestInfoFull agentRestInfo; List skillsRestInfo; @@ -426,12 +445,12 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI agent.setGroup(agentGroup); agent.setSecurity(agentRestInfo.getSecurity()); - + // only update password if it is not empty (since caller cannot obtain password to pass back for updating) if (!agentRestInfo.getPassword().isEmpty()) { agent.getUser().setPintoken(agentRestInfo.getPassword()); } - + agent.getSkills().clear(); OpenAcdSkill skill; @@ -481,7 +500,7 @@ private OpenAcdAgent createOpenAcdAgent(OpenAcdAgentRestInfoFull agentRestInfo) } agent.setUser(user); - + Set skills = new LinkedHashSet(); List skillsRestInfo = agentRestInfo.getSkills(); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 3e53a53c61..8f23b210e9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdClientsResource extends UserResource { @@ -153,6 +154,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdClientRestInfo clientRestInfo = representation.getObject(); OpenAcdClient client; + // validate input for update or create + ValidationInfo validationInfo = validate(clientRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -231,6 +241,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdClientRestInfo restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdClientRestInfo createClientRestInfo(int id) throws ResourceException { OpenAcdClientRestInfo clientRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 9208455a85..1fb50496b9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -24,33 +24,28 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; -import java.util.Collection; -import java.util.HashSet; import java.util.Collections; import java.util.Comparator; +import java.util.List; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; - -import com.thoughtworks.xstream.XStream; - -import org.sipfoundry.sipxconfig.freeswitch.FreeswitchCondition; -import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; +import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; // OpenAcdLines are different // -------------------------- @@ -122,7 +117,6 @@ public Representation represent(Variant variant) throws ResourceException { // process request for single OpenAcdLineRestInfo lineRestInfo; String idString = (String) getRequest().getAttributes().get("id"); - if (idString != null) { try { @@ -166,6 +160,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdLineRestInfo lineRestInfo = representation.getObject(); OpenAcdLine line = null; + // validate input for update or create + ValidationInfo validationInfo = validate(lineRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -182,7 +185,8 @@ public void storeRepresentation(Representation entity) throws ResourceException // copy values over to existing try { updateLine(line, lineRestInfo); - saveLine(line); + //saveLine(line); + m_openAcdContext.saveExtension(line); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed"); @@ -197,8 +201,8 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new try { - // lines are created using a function without passing an object to it, so perform creation and assignment within createLine - createLine(lineRestInfo); + line = createLine(lineRestInfo); + m_openAcdContext.saveExtension(line); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed"); @@ -245,6 +249,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdLine getLineById(int id) throws ResourceException { OpenAcdLine line = null; @@ -263,12 +276,7 @@ private OpenAcdLine getLineById(int id) throws ResourceException { return line; } - - private void saveLine(OpenAcdLine line) throws ResourceException { - m_openAcdContext.getLines().remove(getLineById(line.getId())); - m_openAcdContext.getLines().add(line); - } - + private OpenAcdLineRestInfo createLineRestInfo(int id) throws ResourceException { OpenAcdLineRestInfo lineRestInfo; @@ -375,8 +383,7 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro line.setName(tempString); } - //line.setExtension(lineRestInfo.getExtension()); - //line.setRegex(lineRestInfo.getRegex()); + //"Expression" is the extension number, which may be a regular expression if regex is set line.getNumberCondition().setExpression(lineRestInfo.getExtension()); line.getNumberCondition().setRegex(lineRestInfo.getRegex()); @@ -385,20 +392,21 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro line.setAlias(lineRestInfo.getAlias()); } - private void createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { - OpenAcdLine line = m_openAcdContext.newOpenAcdLine(); + private OpenAcdLine createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { + OpenAcdLine line = new OpenAcdLine(); // copy fields from rest info line.setName(lineRestInfo.getName()); - //line.setExtension(lineRestInfo.getExtension()); - //line.setRegex(lineRestInfo.getRegex()); + //"Expression" is the extension number, which may be a regular expression if regex is set line.getNumberCondition().setExpression(lineRestInfo.getExtension()); line.getNumberCondition().setRegex(lineRestInfo.getRegex()); line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); + + return line; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 560c5a7aee..c5bef5392c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -51,6 +51,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdQueueGroupsResource extends UserResource { @@ -157,6 +158,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdQueueGroupRepresentation representation = new OpenAcdQueueGroupRepresentation(entity); OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = representation.getObject(); OpenAcdQueueGroup queueGroup; + + // validate input for update or create + ValidationInfo validationInfo = validate(queueGroupRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + // if have id then update a single group String idString = (String) getRequest().getAttributes().get("id"); @@ -236,6 +246,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdQueueGroupRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdQueueGroupRestInfoFull createQueueGroupRestInfo(int id) throws ResourceException { OpenAcdQueueGroupRestInfoFull queueGroupRestInfo; List skillsRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 42b3fd22e1..b863ac6754 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -49,12 +49,14 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdQueuesResource extends UserResource { @@ -162,6 +164,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdQueueRestInfoFull queueRestInfo = representation.getObject(); OpenAcdQueue queue; + // validate input for update or create + ValidationInfo validationInfo = validate(queueRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -241,6 +252,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdQueueRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdQueueRestInfoFull createQueueRestInfo(int id) throws ResourceException { OpenAcdQueueRestInfoFull queueRestInfo; List skillsRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 9ebb682f79..5b161e1be7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -48,6 +48,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdSkillGroupsResource extends UserResource { @@ -155,6 +156,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdSkillGroupRestInfo skillGroupRestInfo = representation.getObject(); OpenAcdSkillGroup skillGroup; + // validate input for update or create + ValidationInfo validationInfo = validate(skillGroupRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -233,6 +243,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdSkillGroupRestInfo restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdSkillGroupRestInfo createSkillGroupRestInfo(int id) throws ResourceException { OpenAcdSkillGroupRestInfo skillGroupRestInfo; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 5891052807..3145d817b7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -45,6 +45,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; public class OpenAcdSkillsResource extends UserResource { @@ -152,6 +153,15 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); OpenAcdSkill skill = null; + // validate input for update or create + ValidationInfo validationInfo = validate(skillRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + return; + } + + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -230,6 +240,15 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- + // basic interface level validation of data provided through REST interface for creation or update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { OpenAcdSkillRestInfoFull skillRestInfo = null; From 5fbd3ba2c5e12cdfd6e47c2c287e54a95e835a46 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Sun, 1 Apr 2012 22:49:16 -0400 Subject: [PATCH 46/99] Implement creation, still needs queue and actions --- .../sipxconfig/rest/OpenAcdLinesResource.java | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 1fb50496b9..138b25d3a4 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -1,7 +1,7 @@ /* * * OpenAcdLinesResource.java - A Restlet to read Skill data from OpenACD within SipXecs - * Copyright (C) 2012 PATLive, I. Wesson + * Copyright (C) 2012 PATLive, I. Wesson, D. Chang * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -51,7 +51,7 @@ // -------------------------- // Lines is inconsistent with other OpenACD objects. // OpenAcdContext does not contain functions for getLineById(), saveLine() or removeLine(). -// A Set of Lines is obtained using getLines() and only set operations are available +// It appears the OpenAcdExtension object is used for lines, and it has all the above functions // so this API will appear slightly different than other APIs, although attempts have been made to preserve general structure. public class OpenAcdLinesResource extends UserResource { @@ -175,7 +175,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - line = getLineById(idInt); + line = (OpenAcdLine) m_openAcdContext.getExtensionById(idInt); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); @@ -198,7 +198,7 @@ public void storeRepresentation(Representation entity) throws ResourceException return; } - + // otherwise add new try { line = createLine(lineRestInfo); @@ -226,7 +226,7 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - line = getLineById(idInt); + line = (OpenAcdLine) m_openAcdContext.getExtensionById(idInt); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); @@ -258,29 +258,10 @@ private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { return validationInfo; } - private OpenAcdLine getLineById(int id) throws ResourceException { - OpenAcdLine line = null; - - for (OpenAcdLine currentLine : m_openAcdContext.getLines()) { - if(currentLine.getId() == id) - { - line = currentLine; - break; - } - } - - // duplicate behavior of standard OpenAcdContext.getXById() functions - if (line == null) { - throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "No row with the given identifier exists: " + id); - } - - return line; - } - private OpenAcdLineRestInfo createLineRestInfo(int id) throws ResourceException { OpenAcdLineRestInfo lineRestInfo; - OpenAcdLine line = getLineById(id); + OpenAcdLine line = (OpenAcdLine) m_openAcdContext.getExtensionById(id); lineRestInfo = new OpenAcdLineRestInfo(line); return lineRestInfo; @@ -393,8 +374,10 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro } private OpenAcdLine createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { - OpenAcdLine line = new OpenAcdLine(); - + // special steps to obtain new line (cannot just "new") + OpenAcdLine line = m_openAcdContext.newOpenAcdLine(); + line.addCondition(OpenAcdLine.createLineCondition()); + // copy fields from rest info line.setName(lineRestInfo.getName()); From e2bba8ab0558f6c5de71bd7909f9bbbc26fc4697 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 3 Apr 2012 12:54:49 -0400 Subject: [PATCH 47/99] Add Queue, Client, AllowVoicemail and custom actions --- .../sipxconfig/rest/OpenAcdLinesResource.java | 265 +++++++++++++----- 1 file changed, 202 insertions(+), 63 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 138b25d3a4..e089ff918d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -28,18 +28,23 @@ import java.util.Comparator; import java.util.List; +import org.apache.commons.lang.StringUtils; import org.restlet.Context; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.freeswitch.FreeswitchAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; @@ -54,28 +59,26 @@ // It appears the OpenAcdExtension object is used for lines, and it has all the above functions // so this API will appear slightly different than other APIs, although attempts have been made to preserve general structure. public class OpenAcdLinesResource extends UserResource { - + private OpenAcdContext m_openAcdContext; private Form m_form; // use to define all possible sort fields - enum SortField - { + enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -108,7 +111,7 @@ public boolean allowDelete() { return true; } - + // GET - Retrieve all and single item // ---------------------------------- @@ -117,7 +120,7 @@ public Representation represent(Variant variant) throws ResourceException { // process request for single OpenAcdLineRestInfo lineRestInfo; String idString = (String) getRequest().getAttributes().get("id"); - + if (idString != null) { try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); @@ -133,7 +136,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for list - List lines = new ArrayList (m_openAcdContext.getLines()); + List lines = new ArrayList(m_openAcdContext.getLines()); List linesRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; @@ -162,13 +165,13 @@ public void storeRepresentation(Representation entity) throws ResourceException // validate input for update or create ValidationInfo validationInfo = validate(lineRestInfo); - + if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -179,26 +182,25 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing try { updateLine(line, lineRestInfo); - //saveLine(line); m_openAcdContext.saveExtension(line); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed"); - return; + return; } - + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, line.getId(), "Updated Line"); return; } - + // otherwise add new try { line = createLine(lineRestInfo); @@ -206,10 +208,10 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed"); - return; + return; } - - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, line.getId(), "Created Line"); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, line.getId(), "Created Line"); } @@ -219,7 +221,7 @@ public void storeRepresentation(Representation entity) throws ResourceException @Override public void removeRepresentations() throws ResourceException { OpenAcdLine line; - + // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); @@ -230,14 +232,13 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } - // deleteLine() not available from openAcdContext - m_openAcdContext.getLines().remove(line); + m_openAcdContext.deleteExtension(line); OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, line.getId(), "Deleted Line"); - + return; } @@ -245,11 +246,12 @@ public void removeRepresentations() throws ResourceException { OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } - + // Helper functions // ---------------- - // basic interface level validation of data provided through REST interface for creation or update + // basic interface level validation of data provided through REST interface for creation or + // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { @@ -258,11 +260,68 @@ private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { return validationInfo; } - private OpenAcdLineRestInfo createLineRestInfo(int id) throws ResourceException { + // parses OpenAcdLine contents instead of just an id because line is so different + private OpenAcdLineActionsBundleRestInfo createLineActionsBundleRestInfo(OpenAcdLine line) { + OpenAcdLineActionsBundleRestInfo lineActionsBundleRestInfo; + OpenAcdQueueRestInfo queueRestInfo; + OpenAcdClientRestInfo clientRestInfo; + List customActions = new ArrayList(); + OpenAcdLineActionRestInfo customActionRestInfo; + + OpenAcdQueue queue; + String queueName = ""; + OpenAcdClient client; + String clientIdentity = ""; + Boolean allowVoicemail = false; + String allowVoicemailString = "false"; + + + List actions = line.getLineActions(); + for (FreeswitchAction action : actions) { + String data = action.getData(); + if (StringUtils.contains(data, OpenAcdLine.Q)) { + queueName = StringUtils.removeStart(data, OpenAcdLine.Q); + } + else if (StringUtils.contains(data, OpenAcdLine.BRAND)) { + clientIdentity = StringUtils.removeStart(data, OpenAcdLine.BRAND); + } + else if (StringUtils.contains(data, OpenAcdLine.ALLOW_VOICEMAIL)) { + allowVoicemailString = StringUtils.removeStart(data, OpenAcdLine.ALLOW_VOICEMAIL); + } + else { + customActionRestInfo = new OpenAcdLineActionRestInfo(action); + customActions.add(customActionRestInfo); + } + } + + queue = m_openAcdContext.getQueueByName(queueName); + queueRestInfo = new OpenAcdQueueRestInfo(queue); + + client = m_openAcdContext.getClientByIdentity(clientIdentity); + clientRestInfo = new OpenAcdClientRestInfo(client); + + allowVoicemail = Boolean.parseBoolean(allowVoicemailString); + + lineActionsBundleRestInfo = new OpenAcdLineActionsBundleRestInfo(queueRestInfo, clientRestInfo, allowVoicemail, customActions); + + return lineActionsBundleRestInfo; + } + + private OpenAcdLineRestInfo createLineRestInfo(int id) { OpenAcdLineRestInfo lineRestInfo; OpenAcdLine line = (OpenAcdLine) m_openAcdContext.getExtensionById(id); - lineRestInfo = new OpenAcdLineRestInfo(line); + lineRestInfo = createLineRestInfo(line); + + return lineRestInfo; + } + + private OpenAcdLineRestInfo createLineRestInfo(OpenAcdLine line) { + OpenAcdLineRestInfo lineRestInfo; + OpenAcdLineActionsBundleRestInfo lineActionsBundleRestInfo; + + lineActionsBundleRestInfo = createLineActionsBundleRestInfo(line); + lineRestInfo = new OpenAcdLineRestInfo(line, lineActionsBundleRestInfo); return lineRestInfo; } @@ -277,7 +336,7 @@ private MetadataRestInfo addLines(List linesRestInfo, List< for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdLine line = lines.get(index); - lineRestInfo = new OpenAcdLineRestInfo(line); + lineRestInfo = createLineRestInfo(line); linesRestInfo.add(lineRestInfo); } @@ -300,7 +359,7 @@ private void sortLines(List lines) { switch (sortField) { case NAME: - Collections.sort(lines, new Comparator(){ + Collections.sort(lines, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; @@ -312,7 +371,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(lines, new Comparator(){ + Collections.sort(lines, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; @@ -322,14 +381,14 @@ public int compare(Object object1, Object object2) { }); break; - + } } else { // must be reverse switch (sortField) { case NAME: - Collections.sort(lines, new Comparator(){ + Collections.sort(lines, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; @@ -341,7 +400,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(lines, new Comparator(){ + Collections.sort(lines, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; @@ -364,31 +423,53 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro line.setName(tempString); } - //"Expression" is the extension number, which may be a regular expression if regex is set + // "Expression" is the extension number, which may be a regular expression if regex is set line.getNumberCondition().setExpression(lineRestInfo.getExtension()); line.getNumberCondition().setRegex(lineRestInfo.getRegex()); - + line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); + + // set standard actions + line.getNumberCondition().getActions().clear(); + line.getNumberCondition().addAction(OpenAcdLine.createQueueAction(lineRestInfo.getActions().getQueue().getName())); + line.getNumberCondition().addAction(OpenAcdLine.createClientAction(lineRestInfo.getActions().getClient().getIdentity())); + line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); + + // set custom actions + for (OpenAcdLineActionRestInfo actionRestInfo : lineRestInfo.getActions().getCustomActions()) { + line.getNumberCondition().addAction(OpenAcdLine.createAction(actionRestInfo.getApplication(), actionRestInfo.getData())); + } } private OpenAcdLine createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { // special steps to obtain new line (cannot just "new") OpenAcdLine line = m_openAcdContext.newOpenAcdLine(); line.addCondition(OpenAcdLine.createLineCondition()); - + // copy fields from rest info line.setName(lineRestInfo.getName()); - //"Expression" is the extension number, which may be a regular expression if regex is set + // "Expression" is the extension number, which may be a regular expression if regex is set line.getNumberCondition().setExpression(lineRestInfo.getExtension()); - line.getNumberCondition().setRegex(lineRestInfo.getRegex()); - + line.getNumberCondition().setRegex(lineRestInfo.getRegex()); + line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); - + + // set standard actions + line.getNumberCondition().getActions().clear(); + line.getNumberCondition().addAction(OpenAcdLine.createQueueAction(lineRestInfo.getActions().getQueue().getName())); + line.getNumberCondition().addAction(OpenAcdLine.createClientAction(lineRestInfo.getActions().getClient().getIdentity())); + line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); + + // set custom actions + for (OpenAcdLineActionRestInfo actionRestInfo : lineRestInfo.getActions().getCustomActions()) { + line.getNumberCondition().addAction(OpenAcdLine.createAction(actionRestInfo.getApplication(), actionRestInfo.getData())); + } + return line; } @@ -455,12 +536,13 @@ static class OpenAcdLineRestInfo { private final int m_id; private final String m_name; private final String m_description; - private final String m_extension; - private final boolean m_regex; - private final String m_didnumber; - private final String m_alias; + private final String m_extension; + private final boolean m_regex; + private final String m_didnumber; + private final String m_alias; + private final OpenAcdLineActionsBundleRestInfo m_actions; - public OpenAcdLineRestInfo(OpenAcdLine line) { + public OpenAcdLineRestInfo(OpenAcdLine line, OpenAcdLineActionsBundleRestInfo lineActionsRestInfo) { m_id = line.getId(); m_name = line.getName(); m_description = line.getDescription(); @@ -468,6 +550,7 @@ public OpenAcdLineRestInfo(OpenAcdLine line) { m_regex = line.getRegex(); m_didnumber = line.getDid(); m_alias = line.getAlias(); + m_actions = lineActionsRestInfo; } public int getId() { @@ -477,28 +560,84 @@ public int getId() { public String getName() { return m_name; } - + public String getDescription() { return m_description; } - - public String getExtension(){ - return m_extension; + + public String getExtension() { + return m_extension; } - - public boolean getRegex(){ - return m_regex; + + public boolean getRegex() { + return m_regex; } - - public String getDIDNumber(){ - return m_didnumber; + + public String getDIDNumber() { + return m_didnumber; } - + public String getAlias() { - return m_alias; + return m_alias; } - - // need queue, client, allow voicemail, answer supervision mode, welcome message, options (list) + + public OpenAcdLineActionsBundleRestInfo getActions() { + return m_actions; + } + } + + static class OpenAcdLineActionRestInfo { + private final String m_application; + private final String m_data; + + public OpenAcdLineActionRestInfo(FreeswitchAction action) { + m_application = action.getApplication(); + m_data = action.getData(); + } + + public String getApplication() { + return m_application; + } + + public String getData() { + return m_data; + } + } + + static class OpenAcdLineActionsBundleRestInfo { + // standard (required?) actions + private final OpenAcdQueueRestInfo m_queue; + private final OpenAcdClientRestInfo m_client; + private final boolean m_allowVoicemail; + + + // additional (custom) actions + private final List m_customActions; + + public OpenAcdLineActionsBundleRestInfo(OpenAcdQueueRestInfo queueRestInfo, OpenAcdClientRestInfo clientRestInfo, boolean allowVoicemail, List customActions) { + m_queue = queueRestInfo; + m_client = clientRestInfo; + m_allowVoicemail = allowVoicemail; + m_customActions = customActions; + } + + public OpenAcdQueueRestInfo getQueue() { + return m_queue; + } + + public OpenAcdClientRestInfo getClient() { + return m_client; + } + + public boolean getAllowVoicemail() { + return m_allowVoicemail; + } + + public List getCustomActions() { + return m_customActions; + } + + // allow answer supervision mode, welcome message } From af4c761d52f9dd55eed1513c59dcfa08029f5544 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 3 Apr 2012 13:23:00 -0400 Subject: [PATCH 48/99] Added validation of initial REST info to conform with SipXconfig UI restrictions --- .../rest/OpenAcdAgentGroupsResource.java | 68 ++++++++------- .../rest/OpenAcdClientsResource.java | 60 ++++++++------ .../rest/OpenAcdSkillGroupsResource.java | 72 +++++++++------- .../rest/OpenAcdSkillsResource.java | 82 ++++++++++++------- 4 files changed, 167 insertions(+), 115 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index fbdc028087..5c18bde6ac 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -24,36 +24,37 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.LinkedHashSet; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdLinesResource.OpenAcdLineRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdAgentGroupsResource extends UserResource { @@ -61,23 +62,21 @@ public class OpenAcdAgentGroupsResource extends UserResource { private Form m_form; // use to define all possible sort fields - private enum SortField - { + private enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -166,7 +165,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } @@ -180,7 +179,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing group @@ -190,7 +189,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed"); - return; + return; } OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, agentGroup.getId(), "Updated Agent Group"); @@ -206,10 +205,10 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed"); - return; + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); } @@ -230,7 +229,7 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.deleteAgentGroup(agentGroup); @@ -248,12 +247,23 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - // basic interface level validation of data provided through REST interface for creation or update + // basic interface level validation of data provided through REST interface for creation or + // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdAgentGroupRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + return validationInfo; } @@ -404,7 +414,7 @@ private void sortGroups(List agentGroups) { switch (sortField) { case NAME: - Collections.sort(agentGroups, new Comparator(){ + Collections.sort(agentGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; @@ -416,7 +426,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ + Collections.sort(agentGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; @@ -444,7 +454,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(agentGroups, new Comparator(){ + Collections.sort(agentGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 8f23b210e9..3eaf2f8c77 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -24,29 +24,30 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; import java.util.Collections; import java.util.Comparator; +import java.util.List; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; - import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdClientsResource extends UserResource { @@ -54,23 +55,21 @@ public class OpenAcdClientsResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField - { + enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -159,7 +158,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } @@ -173,7 +172,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing @@ -183,7 +182,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed"); - return; + return; } OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, client.getId(), "Updated Client"); @@ -199,10 +198,10 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); - return; + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, client.getId(), "Created Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, client.getId(), "Created Client"); } @@ -217,13 +216,13 @@ public void removeRepresentations() throws ResourceException { String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { - try{ + try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); client = m_openAcdContext.getClientById(idInt); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.deleteClient(client); @@ -241,12 +240,23 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - // basic interface level validation of data provided through REST interface for creation or update + // basic interface level validation of data provided through REST interface for creation or + // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdClientRestInfo restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String identity = restInfo.getIdentity(); + + for (int i = 0; i < identity.length(); i++) { + if ((!Character.isLetterOrDigit(identity.charAt(i)) && !(Character.getType(identity.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && identity.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Identity' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + return validationInfo; } @@ -297,7 +307,7 @@ private void sortClients(List clients) { switch (sortField) { case NAME: - Collections.sort(clients, new Comparator(){ + Collections.sort(clients, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdClient client1 = (OpenAcdClient) object1; @@ -309,7 +319,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(clients, new Comparator(){ + Collections.sort(clients, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdClient client1 = (OpenAcdClient) object1; @@ -325,7 +335,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(clients, new Comparator(){ + Collections.sort(clients, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdClient client1 = (OpenAcdClient) object1; @@ -337,7 +347,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(clients, new Comparator(){ + Collections.sort(clients, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdClient client1 = (OpenAcdClient) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 5b161e1be7..113c0150ea 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -24,31 +24,33 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; import java.util.Collection; -import java.util.HashSet; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; +import java.util.List; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdSkillGroupsResource extends UserResource { @@ -56,23 +58,21 @@ public class OpenAcdSkillGroupsResource extends UserResource { private Form m_form; // use to define all possible sort fields - private enum SortField - { + private enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -158,13 +158,13 @@ public void storeRepresentation(Representation entity) throws ResourceException // validate input for update or create ValidationInfo validationInfo = validate(skillGroupRestInfo); - + if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -175,7 +175,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing @@ -185,7 +185,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed"); - return; + return; } OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skillGroup.getId(), "Updated Skill Group"); @@ -201,10 +201,10 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); - return; + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skillGroup.getId(), "Created Skill Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skillGroup.getId(), "Created Skill Group"); } @@ -214,7 +214,8 @@ public void storeRepresentation(Representation entity) throws ResourceException // deleteSkillGroup() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { - // for some reason skill groups are deleted by providing collection of ids, not by providing skill group object + // for some reason skill groups are deleted by providing collection of ids, not by + // providing skill group object Collection skillGroupIds = new HashSet(); // get id then delete single @@ -222,12 +223,12 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); skillGroupIds.add(idInt); } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.removeSkillGroups(skillGroupIds); @@ -243,12 +244,23 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - // basic interface level validation of data provided through REST interface for creation or update + // basic interface level validation of data provided through REST interface for creation or + // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdSkillGroupRestInfo restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Skill group name must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + return validationInfo; } @@ -299,7 +311,7 @@ private void sortSkillGroups(List skillGroups) { switch (sortField) { case NAME: - Collections.sort(skillGroups, new Comparator(){ + Collections.sort(skillGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; @@ -311,7 +323,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(skillGroups, new Comparator(){ + Collections.sort(skillGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; @@ -327,7 +339,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(skillGroups, new Comparator(){ + Collections.sort(skillGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -339,7 +351,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(skillGroups, new Comparator(){ + Collections.sort(skillGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 3145d817b7..6cf0d2b578 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -24,28 +24,30 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; import java.util.Collections; import java.util.Comparator; +import java.util.List; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdSkillsResource extends UserResource { @@ -53,23 +55,21 @@ public class OpenAcdSkillsResource extends UserResource { private Form m_form; // use to define all possible sort fields - private enum SortField - { + private enum SortField { NAME, DESCRIPTION, ATOM, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -155,13 +155,13 @@ public void storeRepresentation(Representation entity) throws ResourceException // validate input for update or create ValidationInfo validationInfo = validate(skillRestInfo); - + if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -172,7 +172,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing @@ -182,9 +182,9 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed"); - return; + return; } - + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skill.getId(), "Updated Skill"); return; @@ -198,10 +198,10 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); - return; + return; } - - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skill.getId(), "Created Skill"); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skill.getId(), "Created Skill"); } @@ -222,7 +222,7 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.deleteSkill(skill); @@ -240,12 +240,32 @@ public void removeRepresentations() throws ResourceException { // Helper functions // ---------------- - // basic interface level validation of data provided through REST interface for creation or update + // basic interface level validation of data provided through REST interface for creation or + // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + String atom = restInfo.getAtom(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < atom.length(); i++) { + if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + return validationInfo; } @@ -291,7 +311,7 @@ private void sortSkills(List skills) { switch (sortField) { case NAME: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -303,7 +323,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -316,7 +336,7 @@ public int compare(Object object1, Object object2) { case ATOM: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -332,7 +352,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -344,7 +364,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; @@ -356,7 +376,7 @@ public int compare(Object object1, Object object2) { break; case ATOM: - Collections.sort(skills, new Comparator(){ + Collections.sort(skills, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; From 1e9ca6a2787e6edcac71965d52e5f2902b3e5484 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 3 Apr 2012 15:58:51 -0400 Subject: [PATCH 49/99] Add OpenACD Settings REST API --- .../rest/OpenAcdSettingsResource.java | 312 ++++++++++++++++++ .../sipxconfig/rest/OpenAcdUtilities.java | 48 ++- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 3 files changed, 362 insertions(+), 14 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java new file mode 100644 index 0000000000..9d73f05eff --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -0,0 +1,312 @@ +/* + * + * OpenAcdSettingsResource.java - A Restlet to read Skill data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Waseem + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSettingRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class OpenAcdSettingsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Client + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + OpenAcdSettingRestInfo settingRestInfo; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + settingRestInfo = createSettingRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } + + // return representation + return new OpenAcdSettingRepresentation(variant.getMediaType(), settingRestInfo); + } + + + // if not single, process request for list + OpenAcdSettings settings = m_openAcdContext.getSettings(); + OpenAcdSettingRestInfo settingsRestInfo = new OpenAcdSettingRestInfo(settings); + MetadataRestInfo metadataRestInfo; + + // set requested records and get resulting metadata + metadataRestInfo = addSettings(settingsRestInfo, settings); + + // create final restinfo + OpenAcdSettingsBundleRestInfo settingsBundleRestInfo = new OpenAcdSettingsBundleRestInfo(settingsRestInfo, metadataRestInfo); + + return new OpenAcdSettingsRepresentation(variant.getMediaType(), settingsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSettingRepresentation representation = new OpenAcdSettingRepresentation(entity); + OpenAcdSettingRestInfo settingRestInfo = representation.getObject(); + OpenAcdSettings setting; + + // if have id then update single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + setting = m_openAcdContext.getSettings(); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateSetting(setting, settingRestInfo); + m_openAcdContext.saveSettings(setting); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, setting.getId(), "Updated Settings"); + + return; + } + + + // otherwise add new + try { + setting = createSetting(settingRestInfo); + m_openAcdContext.saveSettings(setting); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, setting.getId(), "Created Client"); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSettings setting; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + setting = m_openAcdContext.getSettings(); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.saveSettings(setting); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, setting.getId(), "Deleted Client"); + + return; + } + + // no id string + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + private OpenAcdSettingRestInfo createSettingRestInfo(int id) throws ResourceException { + OpenAcdSettingRestInfo settingRestInfo; + + try { + OpenAcdSettings setting = m_openAcdContext.getSettings(); + settingRestInfo = new OpenAcdSettingRestInfo(setting); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return settingRestInfo; + } + + private MetadataRestInfo addSettings(OpenAcdSettingRestInfo settingsRestInfo, OpenAcdSettings settings) { + OpenAcdSettingRestInfo settingRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, 1); + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void updateSetting(OpenAcdSettings setting, OpenAcdSettingRestInfo settingRestInfo) throws ResourceException { + String tempString; + + // do not allow empty name + setting.setSettingValue("openacd-config/log_level", setting.getLogLevel()); + + } + + private OpenAcdSettings createSetting(OpenAcdSettingRestInfo settingRestInfo) throws ResourceException { + OpenAcdSettings setting = new OpenAcdSettings(); + + // copy fields from rest info + setting.setSettingValue("openacd-config/log_level", setting.getLogLevel()); + + return setting; + } + + + // REST Representations + // -------------------- + + static class OpenAcdSettingsRepresentation extends XStreamRepresentation { + + public OpenAcdSettingsRepresentation(MediaType mediaType, OpenAcdSettingsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSettingsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-setting", OpenAcdSettingsBundleRestInfo.class); + xstream.alias("setting", OpenAcdSettingRestInfo.class); + } + } + + static class OpenAcdSettingRepresentation extends XStreamRepresentation { + + public OpenAcdSettingRepresentation(MediaType mediaType, OpenAcdSettingRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSettingRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("setting", OpenAcdSettingRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdSettingsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final OpenAcdSettingRestInfo m_settings; + + public OpenAcdSettingsBundleRestInfo(OpenAcdSettingRestInfo settings, MetadataRestInfo metadata) { + m_metadata = metadata; + m_settings = settings; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public OpenAcdSettingRestInfo getSettings() { + return m_settings; + } + } + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 8ca9945831..3daa0a4ab1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -23,12 +23,12 @@ import java.io.IOException; import java.util.List; -import org.restlet.data.MediaType; -import org.restlet.data.Status; import org.restlet.data.Form; +import org.restlet.data.MediaType; import org.restlet.data.Response; -import org.restlet.resource.Representation; +import org.restlet.data.Status; import org.restlet.resource.DomRepresentation; +import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; @@ -36,6 +36,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.w3c.dom.Document; @@ -92,7 +93,7 @@ public static PaginationInfo calculatePagination(Form form, int totalResults) { paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; // check if only one page - //if (resultsPerPage >= totalResults) { + // if (resultsPerPage >= totalResults) { if (paginationInfo.totalPages == 1) { paginationInfo.startIndex = 0; paginationInfo.endIndex = paginationInfo.totalResults - 1; @@ -180,7 +181,8 @@ public static void setResponse(Response response, ResponseCode code, String mess response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); - } catch (IOException e) { + } + catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -209,7 +211,8 @@ public static void setResponse(Response response, ResponseCode code, int id, Str response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); - } catch (IOException e) { + } + catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -235,9 +238,10 @@ public static Representation getResponseError(Response response, ResponseCode co setResponseHeader(doc, elementResponse, code, message); - return representation; //new DomRepresentation(MediaType.TEXT_XML, doc); + return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); - } catch (IOException e) { + } + catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } @@ -254,7 +258,7 @@ private static void setResponseHeader(Document doc, Element elementResponse, Res Element elementMessage = doc.createElement("message"); elementMessage.appendChild(doc.createTextNode(message)); - elementResponse.appendChild(elementMessage); + elementResponse.appendChild(elementMessage); } private static void setResponseStatus(Response response, ResponseCode code) { @@ -454,7 +458,7 @@ static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups) { super(queue); - m_skills = skills; + m_skills = skills; m_agentGroups = agentGroups; } @@ -526,8 +530,7 @@ static class OpenAcdAgentGroupRestInfoFull extends OpenAcdAgentGroupRestInfo { private final List m_queues; private final List m_clients; - public OpenAcdAgentGroupRestInfoFull(OpenAcdAgentGroup agentGroup, List skills, - List queues, List clients) { + public OpenAcdAgentGroupRestInfoFull(OpenAcdAgentGroup agentGroup, List skills, List queues, List clients) { super(agentGroup); m_skills = skills; m_queues = queues; @@ -637,10 +640,10 @@ public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List s m_groupId = agent.getGroup().getId(); m_groupName = agent.getGroup().getName(); m_security = agent.getSecurity(); - m_password = ""; // only used on updates, not rest get + m_password = ""; // only used on updates, not rest get m_skills = skills; m_queues = queues; - m_clients = clients; + m_clients = clients; } public int getId() { @@ -692,4 +695,21 @@ public List getClients() { } } + static class OpenAcdSettingRestInfo { + private final int m_id; + private final String m_logLevel; + + public OpenAcdSettingRestInfo(OpenAcdSettings setting) { + m_id = setting.getId(); + m_logLevel = setting.getLogLevel(); + } + + public int getId() { + return m_id; + } + + public String getLogLevel() { + return m_logLevel; + } + } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index eafd584a58..2398dc14ba 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -368,4 +368,20 @@ + + + + + + + + + + + + + + + + From f8037f9b65847c21673d4927c992b3b12a8d74a0 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 4 Apr 2012 00:50:39 -0400 Subject: [PATCH 50/99] Add AnswerSupervisionType, WelcomeMessage --- .../sipxconfig/rest/OpenAcdLinesResource.java | 102 ++++++++++++++---- 1 file changed, 84 insertions(+), 18 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index e089ff918d..ebd4c41235 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -274,12 +274,23 @@ private OpenAcdLineActionsBundleRestInfo createLineActionsBundleRestInfo(OpenAcd String clientIdentity = ""; Boolean allowVoicemail = false; String allowVoicemailString = "false"; - + Boolean isFsSet = false; + Boolean isAgentSet = false; + String answerSupervisionType = ""; + String welcomeMessage = ""; List actions = line.getLineActions(); for (FreeswitchAction action : actions) { + String application = action.getApplication(); String data = action.getData(); - if (StringUtils.contains(data, OpenAcdLine.Q)) { + + if (StringUtils.equals(application, FreeswitchAction.PredefinedAction.answer.toString())) { + isFsSet = true; + } + else if (StringUtils.contains(data, OpenAcdLine.ERLANG_ANSWER)) { + isAgentSet = true; + } + else if (StringUtils.contains(data, OpenAcdLine.Q)) { queueName = StringUtils.removeStart(data, OpenAcdLine.Q); } else if (StringUtils.contains(data, OpenAcdLine.BRAND)) { @@ -288,6 +299,11 @@ else if (StringUtils.contains(data, OpenAcdLine.BRAND)) { else if (StringUtils.contains(data, OpenAcdLine.ALLOW_VOICEMAIL)) { allowVoicemailString = StringUtils.removeStart(data, OpenAcdLine.ALLOW_VOICEMAIL); } + else if (StringUtils.equals(application, FreeswitchAction.PredefinedAction.playback.toString())) { + // web ui only displays filename and appends audio directory + //welcomeMessage = StringUtils.removeStart(data, m_openAcdContext.getSettings().getAudioDirectory() + "/"); + welcomeMessage = data; + } else { customActionRestInfo = new OpenAcdLineActionRestInfo(action); customActions.add(customActionRestInfo); @@ -302,7 +318,17 @@ else if (StringUtils.contains(data, OpenAcdLine.ALLOW_VOICEMAIL)) { allowVoicemail = Boolean.parseBoolean(allowVoicemailString); - lineActionsBundleRestInfo = new OpenAcdLineActionsBundleRestInfo(queueRestInfo, clientRestInfo, allowVoicemail, customActions); + if (isFsSet) { + answerSupervisionType = "FS"; + } + else if (isAgentSet) { + answerSupervisionType = "AGENT"; + } + else { + answerSupervisionType = "ACD"; + } + + lineActionsBundleRestInfo = new OpenAcdLineActionsBundleRestInfo(queueRestInfo, clientRestInfo, allowVoicemail, customActions, answerSupervisionType, welcomeMessage); return lineActionsBundleRestInfo; } @@ -423,24 +449,31 @@ private void updateLine(OpenAcdLine line, OpenAcdLineRestInfo lineRestInfo) thro line.setName(tempString); } - // "Expression" is the extension number, which may be a regular expression if regex is set - line.getNumberCondition().setExpression(lineRestInfo.getExtension()); - line.getNumberCondition().setRegex(lineRestInfo.getRegex()); - line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); // set standard actions line.getNumberCondition().getActions().clear(); + + // answer supervision type is an integer code from OpenAcdLine + line.getNumberCondition().addAction(OpenAcdLine.createAnswerAction(getAnswerSupervisionCode(lineRestInfo.getActions().getAnswerSupervisionType()))); + line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); line.getNumberCondition().addAction(OpenAcdLine.createQueueAction(lineRestInfo.getActions().getQueue().getName())); line.getNumberCondition().addAction(OpenAcdLine.createClientAction(lineRestInfo.getActions().getClient().getIdentity())); - line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); + + // web ui only displays filename and appends audio directory + //line.getNumberCondition().addAction(OpenAcdLine.createPlaybackAction(m_openAcdContext.getSettings().getAudioDirectory() + "/" + lineRestInfo.getActions().getWelcomeMessage())); + line.getNumberCondition().addAction(OpenAcdLine.createPlaybackAction(lineRestInfo.getActions().getWelcomeMessage())); // set custom actions for (OpenAcdLineActionRestInfo actionRestInfo : lineRestInfo.getActions().getCustomActions()) { line.getNumberCondition().addAction(OpenAcdLine.createAction(actionRestInfo.getApplication(), actionRestInfo.getData())); } + + // "Expression" is the extension number, which may be a regular expression if regex is set + line.getNumberCondition().setExpression(lineRestInfo.getExtension()); + line.getNumberCondition().setRegex(lineRestInfo.getRegex()); } private OpenAcdLine createLine(OpenAcdLineRestInfo lineRestInfo) throws ResourceException { @@ -450,29 +483,51 @@ private OpenAcdLine createLine(OpenAcdLineRestInfo lineRestInfo) throws Resource // copy fields from rest info line.setName(lineRestInfo.getName()); - - // "Expression" is the extension number, which may be a regular expression if regex is set - line.getNumberCondition().setExpression(lineRestInfo.getExtension()); - line.getNumberCondition().setRegex(lineRestInfo.getRegex()); - line.setDid(lineRestInfo.getDIDNumber()); line.setDescription(lineRestInfo.getDescription()); line.setAlias(lineRestInfo.getAlias()); // set standard actions line.getNumberCondition().getActions().clear(); + + // answer supervision type is an integer code from OpenAcdLine + line.getNumberCondition().addAction(OpenAcdLine.createAnswerAction(getAnswerSupervisionCode(lineRestInfo.getActions().getAnswerSupervisionType()))); + line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); line.getNumberCondition().addAction(OpenAcdLine.createQueueAction(lineRestInfo.getActions().getQueue().getName())); line.getNumberCondition().addAction(OpenAcdLine.createClientAction(lineRestInfo.getActions().getClient().getIdentity())); - line.getNumberCondition().addAction(OpenAcdLine.createVoicemailAction(lineRestInfo.getActions().getAllowVoicemail())); + + // web ui only displays filename and appends audio directory + //line.getNumberCondition().addAction(OpenAcdLine.createPlaybackAction(m_openAcdContext.getSettings().getAudioDirectory() + "/" + lineRestInfo.getActions().getWelcomeMessage())); + line.getNumberCondition().addAction(OpenAcdLine.createPlaybackAction(lineRestInfo.getActions().getWelcomeMessage())); // set custom actions for (OpenAcdLineActionRestInfo actionRestInfo : lineRestInfo.getActions().getCustomActions()) { line.getNumberCondition().addAction(OpenAcdLine.createAction(actionRestInfo.getApplication(), actionRestInfo.getData())); } + // "Expression" is the extension number, which may be a regular expression if regex is set + line.getNumberCondition().setExpression(lineRestInfo.getExtension()); + line.getNumberCondition().setRegex(lineRestInfo.getRegex()); + return line; } + private Integer getAnswerSupervisionCode(String answerSupervisionType) { + Integer answerSupervisionCode; + + if (StringUtils.equalsIgnoreCase(answerSupervisionType, "FS")) { + answerSupervisionCode = OpenAcdLine.FS; + } + else if (StringUtils.equalsIgnoreCase(answerSupervisionType, "AGENT")) { + answerSupervisionCode = OpenAcdLine.AGENT; + } + else { + answerSupervisionCode = OpenAcdLine.ACD; + } + + return answerSupervisionCode; + } + // REST Representations // -------------------- @@ -491,6 +546,7 @@ public OpenAcdLinesRepresentation(Representation representation) { protected void configureXStream(XStream xstream) { xstream.alias("openacd-line", OpenAcdLinesBundleRestInfo.class); xstream.alias("line", OpenAcdLineRestInfo.class); + xstream.alias("action", OpenAcdLineActionRestInfo.class); } } @@ -507,6 +563,7 @@ public OpenAcdLineRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("line", OpenAcdLineRestInfo.class); + xstream.alias("action", OpenAcdLineActionRestInfo.class); } } @@ -609,16 +666,19 @@ static class OpenAcdLineActionsBundleRestInfo { private final OpenAcdQueueRestInfo m_queue; private final OpenAcdClientRestInfo m_client; private final boolean m_allowVoicemail; - + private final String m_answerSupervisionType; + private final String m_welcomeMessage; // additional (custom) actions private final List m_customActions; - public OpenAcdLineActionsBundleRestInfo(OpenAcdQueueRestInfo queueRestInfo, OpenAcdClientRestInfo clientRestInfo, boolean allowVoicemail, List customActions) { + public OpenAcdLineActionsBundleRestInfo(OpenAcdQueueRestInfo queueRestInfo, OpenAcdClientRestInfo clientRestInfo, boolean allowVoicemail, List customActions, String answerSupervisionType, String welcomeMessage) { m_queue = queueRestInfo; m_client = clientRestInfo; m_allowVoicemail = allowVoicemail; m_customActions = customActions; + m_answerSupervisionType = answerSupervisionType; + m_welcomeMessage = welcomeMessage; } public OpenAcdQueueRestInfo getQueue() { @@ -633,11 +693,17 @@ public boolean getAllowVoicemail() { return m_allowVoicemail; } + public String getAnswerSupervisionType() { + return m_answerSupervisionType; + } + + public String getWelcomeMessage() { + return m_welcomeMessage; + } + public List getCustomActions() { return m_customActions; } - - // allow answer supervision mode, welcome message } From 171fde772ed9162e4a226143ea83fa84e6ee642f Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 5 Apr 2012 12:09:15 -0400 Subject: [PATCH 51/99] Add reporting of exception message in some errors --- .../rest/OpenAcdAgentGroupsResource.java | 10 +-- .../rest/OpenAcdAgentsResource.java | 67 +++++++++-------- .../rest/OpenAcdClientsResource.java | 10 +-- .../sipxconfig/rest/OpenAcdLinesResource.java | 10 +-- .../rest/OpenAcdQueueGroupsResource.java | 73 +++++++++---------- .../rest/OpenAcdQueuesResource.java | 66 ++++++++--------- .../rest/OpenAcdReleaseCodesResource.java | 70 +++++++++--------- .../rest/OpenAcdSettingsResource.java | 10 +-- .../rest/OpenAcdSkillGroupsResource.java | 8 +- .../rest/OpenAcdSkillsResource.java | 10 +-- .../sipxconfig/rest/OpenAcdUtilities.java | 46 +++++++++++- 11 files changed, 207 insertions(+), 173 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 5c18bde6ac..2f02bddcf9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -188,11 +188,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgentGroup(agentGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, agentGroup.getId(), "Updated Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent Group", agentGroup.getId()); return; } @@ -204,11 +204,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgentGroup(agentGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agentGroup.getId(), "Created Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent Group", agentGroup.getId()); } @@ -234,7 +234,7 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.deleteAgentGroup(agentGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, agentGroup.getId(), "Deleted Agent Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent Group", agentGroup.getId()); return; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index fdc6e18920..3728996a09 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -68,23 +68,21 @@ public class OpenAcdAgentsResource extends UserResource { private Form m_form; // use to define all possible sort fields - private enum SortField - { + private enum SortField { NAME, GROUP, SECURITY, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -132,9 +130,10 @@ public Representation represent(Variant variant) throws ResourceException { try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agentRestInfo = createAgentRestInfo(idInt); - } catch(Exception ex) { + } + catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - } + } // finally return group representation return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo); @@ -174,7 +173,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } @@ -185,8 +184,9 @@ public void storeRepresentation(Representation entity) throws ResourceException try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agent = m_openAcdContext.getAgentById(idInt); - } catch(Exception ex) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed"); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -194,8 +194,9 @@ public void storeRepresentation(Representation entity) throws ResourceException try { updateAgent(agent, agentRestInfo); m_openAcdContext.saveAgent(agent); - } catch(Exception ex) { - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed"); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed", exception.getLocalizedMessage()); return; } @@ -208,12 +209,13 @@ public void storeRepresentation(Representation entity) throws ResourceException try { agent = createOpenAcdAgent(agentRestInfo); m_openAcdContext.saveAgent(agent); - } catch(Exception ex) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent failed"); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, agent.getId(), "Created Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent", agent.getId()); } @@ -231,14 +233,15 @@ public void removeRepresentations() throws ResourceException { try { int idInt = OpenAcdUtilities.getIntFromAttribute(idString); agent = m_openAcdContext.getAgentById(idInt); - } catch (Exception ex) { + } + catch (Exception ex) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteAgent(agent); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, agent.getId(), "Deleted Agent."); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent.", agent.getId()); return; } @@ -369,7 +372,7 @@ public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent1.getName().compareToIgnoreCase(agent2.getName()); + return agent1.getName().compareToIgnoreCase(agent2.getName()); } }); @@ -387,12 +390,12 @@ public int compare(Object object1, Object object2) { break; case SECURITY: - Collections.sort(agents, new Comparator(){ + Collections.sort(agents, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent1.getSecurity().compareToIgnoreCase(agent2.getSecurity()); + return agent1.getSecurity().compareToIgnoreCase(agent2.getSecurity()); } }); @@ -425,7 +428,7 @@ public int compare(Object object1, Object object2) { break; case SECURITY: - Collections.sort(agents, new Comparator(){ + Collections.sort(agents, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; @@ -455,16 +458,16 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI OpenAcdSkill skill; List skillsRestInfo = agentRestInfo.getSkills(); - for(OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); - agent.addSkill(skill); + agent.addSkill(skill); } agent.getQueues().clear(); OpenAcdQueue queue; List queuesRestInfo = agentRestInfo.getQueues(); - for(OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + for (OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { queue = m_openAcdContext.getQueueById(queueRestInfo.getId()); agent.addQueue(queue); } @@ -473,7 +476,7 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI OpenAcdClient client; List clientsRestInfo = agentRestInfo.getClients(); - for(OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + for (OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { client = m_openAcdContext.getClientById(clientRestInfo.getId()); agent.addClient(client); } @@ -504,21 +507,21 @@ private OpenAcdAgent createOpenAcdAgent(OpenAcdAgentRestInfoFull agentRestInfo) Set skills = new LinkedHashSet(); List skillsRestInfo = agentRestInfo.getSkills(); - for(OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); } Set queues = new LinkedHashSet(); List queuesRestInfo = agentRestInfo.getQueues(); - for(OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + for (OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { queues.add(m_openAcdContext.getQueueById(queueRestInfo.getId())); } Set clients = new LinkedHashSet(); List clientsRestInfo = agentRestInfo.getClients(); - for(OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + for (OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { clients.add(m_openAcdContext.getClientById(clientRestInfo.getId())); } @@ -528,6 +531,7 @@ private OpenAcdAgent createOpenAcdAgent(OpenAcdAgentRestInfoFull agentRestInfo) return agent; } + private OpenAcdAgentGroup getAgentGroup(OpenAcdAgentRestInfoFull agentRestInfo) throws ResourceException { OpenAcdAgentGroup agentGroup; int groupId = 0; @@ -535,7 +539,8 @@ private OpenAcdAgentGroup getAgentGroup(OpenAcdAgentRestInfoFull agentRestInfo) try { groupId = agentRestInfo.getGroupId(); agentGroup = m_openAcdContext.getAgentGroupById(groupId); - } catch(Exception ex) { + } + catch (Exception ex) { throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "Agent Group ID " + groupId + " not found."); } @@ -617,4 +622,4 @@ public List getAgents() { public void setOpenAcdContext(OpenAcdContext openAcdContext) { m_openAcdContext = openAcdContext; } -} \ No newline at end of file +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 3eaf2f8c77..ac814c7073 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -181,11 +181,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveClient(client); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, client.getId(), "Updated Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Client", client.getId()); return; } @@ -197,11 +197,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveClient(client); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, client.getId(), "Created Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Client", client.getId()); } @@ -227,7 +227,7 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.deleteClient(client); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, client.getId(), "Deleted Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", client.getId()); return; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index ebd4c41235..6ab3db0dad 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -191,11 +191,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveExtension(line); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, line.getId(), "Updated Line"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Line", line.getId()); return; } @@ -207,11 +207,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveExtension(line); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, line.getId(), "Created Line"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Line", line.getId()); } @@ -237,7 +237,7 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.deleteExtension(line); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, line.getId(), "Deleted Line"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Line", line.getId()); return; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index c5bef5392c..b67b5913ce 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -24,34 +24,35 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.LinkedHashSet; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdQueueGroupsResource extends UserResource { @@ -59,23 +60,21 @@ public class OpenAcdQueueGroupsResource extends UserResource { private Form m_form; // use to define all possible sort fields - private enum SortField - { + private enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -158,13 +157,13 @@ public void storeRepresentation(Representation entity) throws ResourceException OpenAcdQueueGroupRepresentation representation = new OpenAcdQueueGroupRepresentation(entity); OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = representation.getObject(); OpenAcdQueueGroup queueGroup; - + // validate input for update or create ValidationInfo validationInfo = validate(queueGroupRestInfo); if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } @@ -178,7 +177,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing group @@ -187,11 +186,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, queueGroup.getId(), "Updated Queue"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queueGroup.getId()); return; } @@ -203,11 +202,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue Group failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue Group failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, queueGroup.getId(), "Created Queue Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue Group", queueGroup.getId()); } @@ -228,12 +227,12 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } - + m_openAcdContext.deleteQueueGroup(queueGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, queueGroup.getId(), "Deleted Queue Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue Group", queueGroup.getId()); return; } @@ -265,7 +264,7 @@ private OpenAcdQueueGroupRestInfoFull createQueueGroupRestInfo(int id) throws Re skillsRestInfo = createSkillsRestInfo(queueGroup); agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); - + queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); } catch (Exception exception) { @@ -274,7 +273,7 @@ private OpenAcdQueueGroupRestInfoFull createQueueGroupRestInfo(int id) throws Re return queueGroupRestInfo; } - + private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { String tempString; @@ -310,8 +309,8 @@ private MetadataRestInfo addQueueGroups(List queu OpenAcdQueueGroup queueGroup = queueGroups.get(index); skillsRestInfo = createSkillsRestInfo(queueGroup); - agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); - + agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); queueGroupsRestInfo.add(queueGroupRestInfo); } @@ -367,7 +366,7 @@ private void sortGroups(List queueGroups) { switch (sortField) { case NAME: - Collections.sort(queueGroups, new Comparator(){ + Collections.sort(queueGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; @@ -379,7 +378,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(queueGroups, new Comparator(){ + Collections.sort(queueGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; @@ -395,7 +394,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(queueGroups, new Comparator(){ + Collections.sort(queueGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; @@ -407,7 +406,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(queueGroups, new Comparator(){ + Collections.sort(queueGroups, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index b863ac6754..b08ad4c7ea 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -24,39 +24,35 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; -import java.util.Collection; -import java.util.HashSet; import java.util.Collections; import java.util.Comparator; +import java.util.List; import java.util.Set; -import org.jfree.chart.axis.QuarterDateFormat; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; - import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdQueuesResource extends UserResource { @@ -64,23 +60,21 @@ public class OpenAcdQueuesResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField - { + enum SortField { NAME, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -169,10 +163,10 @@ public void storeRepresentation(Representation entity) throws ResourceException if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -183,7 +177,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing @@ -192,11 +186,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, queue.getId(), "Updated Queue"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queue.getId()); return; } @@ -208,11 +202,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, queue.getId(), "Created Queue"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue", queue.getId()); } @@ -234,12 +228,12 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.deleteQueue(queue); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, queue.getId(), "Deleted Queue"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue", queue.getId()); return; } @@ -269,7 +263,7 @@ private OpenAcdQueueRestInfoFull createQueueRestInfo(int id) throws ResourceExce try { OpenAcdQueue queue = m_openAcdContext.getQueueById(id); skillsRestInfo = createSkillsRestInfo(queue); - agentGroupsRestInfo = createAgentGroupsRestInfo(queue); + agentGroupsRestInfo = createAgentGroupsRestInfo(queue); queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupsRestInfo); } catch (Exception exception) { @@ -324,7 +318,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo OpenAcdQueue queue = queues.get(index); skillsRestInfo = createSkillsRestInfo(queue); - agentGroupRestInfo = createAgentGroupsRestInfo(queue); + agentGroupRestInfo = createAgentGroupsRestInfo(queue); queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupRestInfo); queuesRestInfo.add(queueRestInfo); } @@ -348,7 +342,7 @@ private void sortQueues(List queues) { switch (sortField) { case NAME: - Collections.sort(queues, new Comparator(){ + Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; @@ -360,7 +354,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(queues, new Comparator(){ + Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; @@ -376,7 +370,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(queues, new Comparator(){ + Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; @@ -388,7 +382,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(queues, new Comparator(){ + Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 3cdee9b064..193089f8e2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -24,32 +24,32 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; -import java.util.List; import java.util.Collection; -import java.util.HashSet; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; +import java.util.List; import org.restlet.Context; +import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Form; import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.springframework.beans.factory.annotation.Required; -import com.thoughtworks.xstream.XStream; - -import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; public class OpenAcdReleaseCodesResource extends UserResource { @@ -57,23 +57,21 @@ public class OpenAcdReleaseCodesResource extends UserResource { private Form m_form; // use to define all possible sort fields - enum SortField - { + enum SortField { LABEL, DESCRIPTION, NONE; - public static SortField toSortField(String fieldString) - { + public static SortField toSortField(String fieldString) { if (fieldString == null) { return NONE; } try { return valueOf(fieldString.toUpperCase()); - } + } catch (Exception ex) { return NONE; } - } + } } @@ -159,13 +157,13 @@ public void storeRepresentation(Representation entity) throws ResourceException // validate input for update or create ValidationInfo validationInfo = validate(releaseCodeRestInfo); - + if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); - return; + return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -176,7 +174,7 @@ public void storeRepresentation(Representation entity) throws ResourceException } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } // copy values over to existing @@ -185,11 +183,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveReleaseCode(releaseCode); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Release Code failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Release Code failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, releaseCode.getId(), "Updated Release Code"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Release Code", releaseCode.getId()); return; } @@ -201,11 +199,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveReleaseCode(releaseCode); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Release Code failed"); - return; + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Release Code failed", exception.getLocalizedMessage()); + return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, releaseCode.getId(), "Created Release Code"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Release Code", releaseCode.getId()); } @@ -228,7 +226,7 @@ public void removeRepresentations() throws ResourceException { } catch (Exception exception) { OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; + return; } m_openAcdContext.removeReleaseCodes(releaseCodeIds); @@ -249,21 +247,21 @@ public void removeRepresentations() throws ResourceException { // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdReleaseCodeRestInfo restInfo) { ValidationInfo validationInfo = new ValidationInfo(); - + // release code object will allow store of bias other value than -1, 0, or 1, // but then current SipXconfig administrative UI will display nothing for bias name. int bias = restInfo.getBias(); - if ((bias < -1) || (bias >1)) { + if ((bias < -1) || (bias > 1)) { validationInfo.valid = false; validationInfo.message = "Validation Error: Bias must be be -1, 0 or 1"; validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - + return validationInfo; } - + return validationInfo; } - + private OpenAcdReleaseCodeRestInfo createReleaseCodeRestInfo(int id) throws ResourceException { OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; @@ -311,7 +309,7 @@ private void sortReleaseCodes(List releaseCodes) { switch (sortField) { case LABEL: - Collections.sort(releaseCodes, new Comparator(){ + Collections.sort(releaseCodes, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; @@ -323,7 +321,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(releaseCodes, new Comparator(){ + Collections.sort(releaseCodes, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; @@ -339,7 +337,7 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case LABEL: - Collections.sort(releaseCodes, new Comparator(){ + Collections.sort(releaseCodes, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; @@ -351,7 +349,7 @@ public int compare(Object object1, Object object2) { break; case DESCRIPTION: - Collections.sort(releaseCodes, new Comparator(){ + Collections.sort(releaseCodes, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; @@ -458,4 +456,4 @@ public void setOpenAcdContext(OpenAcdContext openAcdContext) { m_openAcdContext = openAcdContext; } -} \ No newline at end of file +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index 9d73f05eff..8108b68fa4 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -142,11 +142,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSettings(setting); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, setting.getId(), "Updated Settings"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Settings", setting.getId()); return; } @@ -158,11 +158,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSettings(setting); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Setting failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, setting.getId(), "Created Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Setting", setting.getId()); } @@ -188,7 +188,7 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.saveSettings(setting); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, setting.getId(), "Deleted Client"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", setting.getId()); return; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 113c0150ea..7c8bd1b084 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -184,11 +184,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkillGroup(skillGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skillGroup.getId(), "Updated Skill Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill Group", skillGroup.getId()); return; } @@ -200,11 +200,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkillGroup(skillGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skillGroup.getId(), "Created Skill Group"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill Group", skillGroup.getId()); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 6cf0d2b578..e94a183f03 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -181,11 +181,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, skill.getId(), "Updated Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); return; } @@ -197,11 +197,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed"); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, skill.getId(), "Created Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); } @@ -227,7 +227,7 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.deleteSkill(skill); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, skill.getId(), "Deleted Skill"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); return; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 3daa0a4ab1..e285851d02 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -1,6 +1,6 @@ /* * - * OpenAcdUtilities.java - Support functionality for OpenAcd Restlets +l * OpenAcdUtilities.java - Support functionality for OpenAcd Restlets * Copyright (C) 2012 PATLive, D. Chang * * This program is free software: you can redistribute it and/or modify @@ -177,7 +177,7 @@ public static void setResponse(Response response, ResponseCode code, String mess setResponseHeader(doc, elementResponse, code, message); - // no data related to result (create function overloads to modify) + // no related data (create function overloads to modify) response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); @@ -188,7 +188,7 @@ public static void setResponse(Response response, ResponseCode code, String mess } } - public static void setResponse(Response response, ResponseCode code, int id, String message) { + public static void setResponse(Response response, ResponseCode code, String message, int id) { try { DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); Document doc = representation.getDocument(); @@ -202,7 +202,7 @@ public static void setResponse(Response response, ResponseCode code, int id, Str setResponseHeader(doc, elementResponse, code, message); - // add data related to result + // add related data Element elementData = doc.createElement("data"); Element elementId = doc.createElement("id"); elementId.appendChild(doc.createTextNode(String.valueOf(id))); @@ -249,6 +249,44 @@ public static Representation getResponseError(Response response, ResponseCode co return null; } + public static void setResponseError(Response response, ResponseCode code, String message, String additionalMessage) { + Representation representation = getResponseError(response, code, message, additionalMessage); + + response.setEntity(representation); + } + + public static Representation getResponseError(Response response, ResponseCode code, String message, String additionalMessage) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add related data + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("additionalMessage"); + elementId.appendChild(doc.createTextNode(additionalMessage)); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { // add standard elements From 1d28d6d1b19f6106f029c19a42a67d687e6b5e9f Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 5 Apr 2012 17:26:33 -0400 Subject: [PATCH 52/99] Add read of Recipes (Todo: create and update of entire Queue and recipe sublists) --- .../rest/OpenAcdQueuesResource.java | 87 +++++++++++++-- .../sipxconfig/rest/OpenAcdUtilities.java | 105 +++++++++++++++++- 2 files changed, 181 insertions(+), 11 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index b08ad4c7ea..122df82c78 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -42,10 +43,16 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeActionRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeConditionRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeStepRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; @@ -259,18 +266,54 @@ private OpenAcdQueueRestInfoFull createQueueRestInfo(int id) throws ResourceExce OpenAcdQueueRestInfoFull queueRestInfo; List skillsRestInfo; List agentGroupsRestInfo; + List recipeStepRestInfo; - try { - OpenAcdQueue queue = m_openAcdContext.getQueueById(id); - skillsRestInfo = createSkillsRestInfo(queue); - agentGroupsRestInfo = createAgentGroupsRestInfo(queue); - queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupsRestInfo); + OpenAcdQueue queue = m_openAcdContext.getQueueById(id); + skillsRestInfo = createSkillsRestInfo(queue); + agentGroupsRestInfo = createAgentGroupsRestInfo(queue); + recipeStepRestInfo = createRecipeStepsRestInfo(queue); + queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupsRestInfo, recipeStepRestInfo); + + return queueRestInfo; + } + + private List createRecipeStepsRestInfo(OpenAcdQueue queue) { + List recipeStepsRestInfo; + OpenAcdRecipeStepRestInfo recipeStepRestInfo; + + Set groupRecipeSteps = queue.getSteps(); + recipeStepsRestInfo = new ArrayList(groupRecipeSteps.size()); + + for (OpenAcdRecipeStep groupRecipeStep : groupRecipeSteps) { + recipeStepRestInfo = new OpenAcdRecipeStepRestInfo(groupRecipeStep, createRecipeActionsRestInfo(groupRecipeStep), createRecipeConditionsRestInfo(groupRecipeStep)); + recipeStepsRestInfo.add(recipeStepRestInfo); } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + return recipeStepsRestInfo; + } + + private OpenAcdRecipeActionRestInfo createRecipeActionsRestInfo(OpenAcdRecipeStep step) { + OpenAcdRecipeActionRestInfo recipeActionRestInfo; + + OpenAcdRecipeAction groupRecipeActions = step.getAction(); + recipeActionRestInfo = new OpenAcdRecipeActionRestInfo(groupRecipeActions); + + return recipeActionRestInfo; + } + + private List createRecipeConditionsRestInfo(OpenAcdRecipeStep step) { + List recipeConditionsRestInfo; + OpenAcdRecipeConditionRestInfo recipeConditionRestInfo; + + + List groupRecipeConditions = step.getConditions(); + recipeConditionsRestInfo = new ArrayList(groupRecipeConditions.size()); + + for (OpenAcdRecipeCondition groupRecipeCondition : groupRecipeConditions) { + recipeConditionRestInfo = new OpenAcdRecipeConditionRestInfo(groupRecipeCondition); + recipeConditionsRestInfo.add(recipeConditionRestInfo); } - return queueRestInfo; + return recipeConditionsRestInfo; } private List createSkillsRestInfo(OpenAcdQueue queue) { @@ -309,6 +352,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo OpenAcdQueueRestInfoFull queueRestInfo; List skillsRestInfo; List agentGroupRestInfo; + List recipeStepRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queues.size()); @@ -319,7 +363,8 @@ private MetadataRestInfo addQueues(List queuesRestInfo skillsRestInfo = createSkillsRestInfo(queue); agentGroupRestInfo = createAgentGroupsRestInfo(queue); - queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupRestInfo); + recipeStepRestInfo = createRecipeStepsRestInfo(queue); + queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupRestInfo, recipeStepRestInfo); queuesRestInfo.add(queueRestInfo); } @@ -409,6 +454,15 @@ private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestI queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); + + queue.getSteps().clear(); + + OpenAcdRecipeStep step; + List recipeStepsRestInfo = queueRestInfo.getSteps(); + for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { + step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); + queue.addStep(step); + } } private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { @@ -422,6 +476,13 @@ private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); + Set steps = new LinkedHashSet(); + List recipeStepsRestInfo = queueRestInfo.getSteps(); + + for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { + steps.add(m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId())); + } + return queue; } @@ -430,7 +491,7 @@ private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfoFull queueRestInfo) int groupId = 0; try { - groupId = queueRestInfo.getId(); + groupId = queueRestInfo.getGroupId(); queueGroup = m_openAcdContext.getQueueGroupById(groupId); } catch (Exception exception) { @@ -460,6 +521,9 @@ protected void configureXStream(XStream xstream) { xstream.alias("queue", OpenAcdQueueRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + xstream.alias("step", OpenAcdRecipeStepRestInfo.class); + xstream.alias("condition", OpenAcdRecipeConditionRestInfo.class); + xstream.alias("action", OpenAcdRecipeActionRestInfo.class); } } @@ -478,6 +542,9 @@ protected void configureXStream(XStream xstream) { xstream.alias("queue", OpenAcdQueueRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + xstream.alias("step", OpenAcdRecipeStepRestInfo.class); + xstream.alias("condition", OpenAcdRecipeConditionRestInfo.class); + xstream.alias("action", OpenAcdRecipeActionRestInfo.class); } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index e285851d02..f2b465ab2d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -35,6 +35,9 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; @@ -491,13 +494,27 @@ public String getGroupName() { } static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { + private final int m_groupId; + private final int m_weight; private final List m_skills; private final List m_agentGroups; + private final List m_steps; - public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups) { + public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups, List steps) { super(queue); + m_groupId = queue.getGroup().getId(); + m_weight = queue.getWeight(); m_skills = skills; m_agentGroups = agentGroups; + m_steps = steps; + } + + public int getGroupId() { + return m_groupId; + } + + public int getWeight() { + return m_weight; } public List getSkills() { @@ -507,6 +524,92 @@ public List getSkills() { public List getAgentGroups() { return m_agentGroups; } + + public List getSteps() { + return m_steps; + } + +// public Set getStep(OpenAcdQueue queue) { +// return queue.getSteps(); +// } + } + + static class OpenAcdRecipeActionRestInfo { + private final String m_action; + private final String m_actionValue; + private final List m_skills; + + public OpenAcdRecipeActionRestInfo(OpenAcdRecipeAction action) { + m_action = action.getAction(); + m_actionValue = action.getActionValue(); + m_skills = action.getAllSkillNames(); + } + + public String getAction() { + return m_action; + } + + public String getActionValue() { + return m_action; + } + + public List getSkills() { + return m_skills; + } + } + + static class OpenAcdRecipeStepRestInfo { + private final int m_id; + private final List m_conditions; + private final OpenAcdRecipeActionRestInfo m_action; + private final String m_frequency; + + public OpenAcdRecipeStepRestInfo(OpenAcdRecipeStep step, OpenAcdRecipeActionRestInfo recipeActionRestInfo, List conditions) { + m_id = step.getId(); + m_conditions = conditions; + m_action = recipeActionRestInfo; + m_frequency = step.getFrequency(); + } + + public int getId() { + return m_id; + } + + public List getCondition() { + return m_conditions; + } + + public OpenAcdRecipeActionRestInfo getAction() { + return m_action; + } + + public String getFrequency() { + return m_frequency; + } + } + + static class OpenAcdRecipeConditionRestInfo { + private final String m_condition; + private final String m_relation; + private final String m_valueCondition; + + public OpenAcdRecipeConditionRestInfo(OpenAcdRecipeCondition condition) { + m_condition = condition.getCondition(); + m_relation = condition.getRelation(); + m_valueCondition = condition.getValueCondition(); + } + + public String getCondition() { + return m_condition; + } + + public String getRelation() { + return m_relation; + } + + public String getValueCondition() { + return m_valueCondition; + } } static class OpenAcdClientRestInfo { From cb3c0832ca754f297dce9e1a189602dc9b342cc0 Mon Sep 17 00:00:00 2001 From: D Chang Date: Fri, 6 Apr 2012 02:01:36 -0400 Subject: [PATCH 53/99] Add more detailed error checking in GET --- .../rest/OpenAcdAgentGroupsResource.java | 117 +++++++++--------- .../rest/OpenAcdAgentsResource.java | 13 +- .../rest/OpenAcdClientsResource.java | 14 ++- .../sipxconfig/rest/OpenAcdLinesResource.java | 14 ++- .../rest/OpenAcdQueueGroupsResource.java | 14 ++- .../rest/OpenAcdQueuesResource.java | 72 +++++++---- .../rest/OpenAcdReleaseCodesResource.java | 14 ++- .../rest/OpenAcdSettingsResource.java | 14 ++- .../rest/OpenAcdSkillGroupsResource.java | 14 ++- .../rest/OpenAcdSkillsResource.java | 14 ++- .../sipxconfig/rest/OpenAcdUtilities.java | 6 +- 11 files changed, 194 insertions(+), 112 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 2f02bddcf9..93654d12b6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -115,19 +114,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdAgentGroupRestInfoFull agentGroupRestInfo; + int idInt; + OpenAcdAgentGroupRestInfoFull agentGroupRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - agentGroupRestInfo = createAgentGroupRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // finally return group representation + try { + agentGroupRestInfo = createAgentGroupRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent Group failed", exception.getLocalizedMessage()); + } + return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); } @@ -200,7 +205,7 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new agent group try { - agentGroup = createOpenAcdAgentGroup(agentGroupRestInfo); + agentGroup = createAgentGroup(agentGroupRestInfo); m_openAcdContext.saveAgentGroup(agentGroup); } catch (Exception exception) { @@ -283,51 +288,6 @@ private OpenAcdAgentGroupRestInfoFull createAgentGroupRestInfo(int id) throws Re return agentGroupRestInfo; } - private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { - String tempString; - - // do not allow empty name - tempString = agentGroupRestInfo.getName(); - if (!tempString.isEmpty()) { - agentGroup.setName(tempString); - } - - agentGroup.setDescription(agentGroupRestInfo.getDescription()); - - // remove all current skills - agentGroup.getSkills().clear(); - - // set skills - OpenAcdSkill skill; - List skillsRestInfo = agentGroupRestInfo.getSkills(); - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); - agentGroup.addSkill(skill); - } - - // remove all current queues - agentGroup.getQueues().clear(); - - // set queues - OpenAcdQueue queue; - List queuesRestInfo = agentGroupRestInfo.getQueues(); - for (OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { - queue = m_openAcdContext.getQueueById(queueRestInfo.getId()); - agentGroup.addQueue(queue); - } - - // remove all current clients - agentGroup.getClients().clear(); - - // set clients - OpenAcdClient client; - List clientsRestInfo = agentGroupRestInfo.getClients(); - for (OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { - client = m_openAcdContext.getClientById(clientRestInfo.getId()); - agentGroup.addClient(client); - } - } - private MetadataRestInfo addAgentGroups(List agentGroupsRestInfo, List agentGroups) { List skillsRestInfo; List queuesRestInfo; @@ -468,24 +428,65 @@ public int compare(Object object1, Object object2) { } } - private OpenAcdAgentGroup createOpenAcdAgentGroup(OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { + private void updateAgentGroup(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { + String tempString; + + // do not allow empty name + tempString = agentGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + agentGroup.setName(tempString); + } + + agentGroup.setDescription(agentGroupRestInfo.getDescription()); + + addLists(agentGroup, agentGroupRestInfo); + } + + private OpenAcdAgentGroup createAgentGroup(OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { OpenAcdAgentGroup agentGroup = new OpenAcdAgentGroup(); // copy fields from rest info agentGroup.setName(agentGroupRestInfo.getName()); agentGroup.setDescription(agentGroupRestInfo.getDescription()); - // add skills - Set skills = new LinkedHashSet(); - List skillsRestInfo = agentGroupRestInfo.getSkills(); + addLists(agentGroup, agentGroupRestInfo); + + return agentGroup; + } + + private void addLists(OpenAcdAgentGroup agentGroup, OpenAcdAgentGroupRestInfoFull agentGroupRestInfo) { + // remove all current skills + agentGroup.getSkills().clear(); + // set skills + OpenAcdSkill skill; + List skillsRestInfo = agentGroupRestInfo.getSkills(); for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + agentGroup.addSkill(skill); } - agentGroup.setSkills(skills); + // remove all current queues + agentGroup.getQueues().clear(); - return agentGroup; + // set queues + OpenAcdQueue queue; + List queuesRestInfo = agentGroupRestInfo.getQueues(); + for (OpenAcdQueueRestInfo queueRestInfo : queuesRestInfo) { + queue = m_openAcdContext.getQueueById(queueRestInfo.getId()); + agentGroup.addQueue(queue); + } + + // remove all current clients + agentGroup.getClients().clear(); + + // set clients + OpenAcdClient client; + List clientsRestInfo = agentGroupRestInfo.getClients(); + for (OpenAcdClientRestInfo clientRestInfo : clientsRestInfo) { + client = m_openAcdContext.getClientById(clientRestInfo.getId()); + agentGroup.addClient(client); + } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index 3728996a09..192c303111 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -123,19 +123,26 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdAgentRestInfoFull agentRestInfo; + int idInt; + OpenAcdAgentRestInfoFull agentRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); agentRestInfo = createAgentRestInfo(idInt); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // finally return group representation + try { + agentRestInfo = createAgentRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent failed", exception.getLocalizedMessage()); + } + return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index ac814c7073..11d5944da7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -108,19 +108,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdClientRestInfo clientRestInfo; + int idInt; + OpenAcdClientRestInfo clientRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - clientRestInfo = createClientRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + clientRestInfo = createClientRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Client failed", exception.getLocalizedMessage()); + } + return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 6ab3db0dad..737e6ec38c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -118,19 +118,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdLineRestInfo lineRestInfo; + int idInt; + OpenAcdLineRestInfo lineRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - lineRestInfo = createLineRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + lineRestInfo = createLineRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Line failed", exception.getLocalizedMessage()); + } + return new OpenAcdLineRepresentation(variant.getMediaType(), lineRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index b67b5913ce..13c6e420e2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -113,19 +113,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdQueueGroupRestInfoFull queueGroupRestInfo; + int idInt; + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueGroupRestInfo = createQueueGroupRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // finally return group representation + try { + queueGroupRestInfo = createQueueGroupRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); + } + return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 122df82c78..ae5b8d8d91 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -1,7 +1,7 @@ /* * * OpenAcdQueuesResource.java - A Restlet to read Skill data from OpenACD within SipXecs - * Copyright (C) 2012 PATLive, I. Wesson + * Copyright (C) 2012 PATLive, I. Wesson, D. Waseem, D. Chang * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -120,19 +119,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdQueueRestInfoFull queueRestInfo; + int idInt; + OpenAcdQueueRestInfoFull queueRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - queueRestInfo = createQueueRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + queueRestInfo = createQueueRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); + } + return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); } @@ -220,7 +225,6 @@ public void storeRepresentation(Representation entity) throws ResourceException // DELETE - Delete single Skill // ---------------------------- - // deleteQueue() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { OpenAcdQueue queue; @@ -285,13 +289,13 @@ private List createRecipeStepsRestInfo(OpenAcdQueue q recipeStepsRestInfo = new ArrayList(groupRecipeSteps.size()); for (OpenAcdRecipeStep groupRecipeStep : groupRecipeSteps) { - recipeStepRestInfo = new OpenAcdRecipeStepRestInfo(groupRecipeStep, createRecipeActionsRestInfo(groupRecipeStep), createRecipeConditionsRestInfo(groupRecipeStep)); + recipeStepRestInfo = new OpenAcdRecipeStepRestInfo(groupRecipeStep, createRecipeActionRestInfo(groupRecipeStep), createRecipeConditionsRestInfo(groupRecipeStep)); recipeStepsRestInfo.add(recipeStepRestInfo); } return recipeStepsRestInfo; } - private OpenAcdRecipeActionRestInfo createRecipeActionsRestInfo(OpenAcdRecipeStep step) { + private OpenAcdRecipeActionRestInfo createRecipeActionRestInfo(OpenAcdRecipeStep step) { OpenAcdRecipeActionRestInfo recipeActionRestInfo; OpenAcdRecipeAction groupRecipeActions = step.getAction(); @@ -455,14 +459,7 @@ private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestI queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); - queue.getSteps().clear(); - - OpenAcdRecipeStep step; - List recipeStepsRestInfo = queueRestInfo.getSteps(); - for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { - step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); - queue.addStep(step); - } + addLists(queue, queueRestInfo); } private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { @@ -476,14 +473,45 @@ private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); - Set steps = new LinkedHashSet(); - List recipeStepsRestInfo = queueRestInfo.getSteps(); + addLists(queue, queueRestInfo); - for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { - steps.add(m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId())); + return queue; + } + + private void addLists(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestInfo) { + // remove all skills + queue.getSkills().clear(); + + // set skills + OpenAcdSkill skill; + List skillsRestInfo = queueRestInfo.getSkills(); + for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + queue.addSkill(skill); } - return queue; + // remove all agent groups + queue.getAgentGroups().clear(); + + // set agent groups + OpenAcdAgentGroup agentGroup; + List agentGroupsRestInfo = queueRestInfo.getAgentGroups(); + for (OpenAcdAgentGroupRestInfo agentGroupRestInfo : agentGroupsRestInfo) { + agentGroup = m_openAcdContext.getAgentGroupById(agentGroupRestInfo.getId()); + queue.addAgentGroup(agentGroup); + } + + // remove all current steps + queue.getSteps().clear(); + + // set steps + // TODO: must add addition of conditions and actions + OpenAcdRecipeStep step; + List recipeStepsRestInfo = queueRestInfo.getSteps(); + for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { + step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); + queue.addStep(step); + } } private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 193089f8e2..15f520de86 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -110,19 +110,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; + int idInt; + OpenAcdReleaseCodeRestInfo releaseCodeRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Release Code failed", exception.getLocalizedMessage()); + } + return new OpenAcdReleaseCodeRepresentation(variant.getMediaType(), releaseCodeRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index 8108b68fa4..f31195b948 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -81,19 +81,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdSettingRestInfo settingRestInfo; + int idInt; + OpenAcdSettingRestInfo settingRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - settingRestInfo = createSettingRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + settingRestInfo = createSettingRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); + } + return new OpenAcdSettingRepresentation(variant.getMediaType(), settingRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 7c8bd1b084..bf0db1b5b0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -111,19 +111,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdSkillGroupRestInfo skillGroupRestInfo; + int idInt; + OpenAcdSkillGroupRestInfo skillGroupRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillGroupRestInfo = createSkillGroupRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // return representation + try { + skillGroupRestInfo = createSkillGroupRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skill Group failed", exception.getLocalizedMessage()); + } + return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index e94a183f03..2791ee38e8 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -108,19 +108,25 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - OpenAcdSkillRestInfoFull skillRestInfo; + int idInt; + OpenAcdSkillRestInfoFull skillRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skillRestInfo = createSkillRestInfo(idInt); + idInt = OpenAcdUtilities.getIntFromAttribute(idString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } - // finally return group representation + try { + skillRestInfo = createSkillRestInfo(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + } + return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index f2b465ab2d..ac127a8b74 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -321,13 +321,17 @@ private static void setResponseStatus(Response response, ResponseCode code) { response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); break; + case ERROR_READ_FAILED: + response.setStatus(Status.SERVER_ERROR_INTERNAL); + break; + default: response.setStatus(Status.SUCCESS_OK); } } public static enum ResponseCode { - SUCCESS, SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED + SUCCESS, SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED, ERROR_READ_FAILED } From fe36adb458c6e98d4b7dd22ce3e32ae4baf077b8 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 6 Apr 2012 03:03:19 -0400 Subject: [PATCH 54/99] Add creation and update of Recipe Step conditions and action --- .../rest/OpenAcdQueuesResource.java | 54 ++++++++++++++----- .../sipxconfig/rest/OpenAcdUtilities.java | 12 ++--- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index ae5b8d8d91..273ec44ee0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -273,7 +273,7 @@ private OpenAcdQueueRestInfoFull createQueueRestInfo(int id) throws ResourceExce List recipeStepRestInfo; OpenAcdQueue queue = m_openAcdContext.getQueueById(id); - skillsRestInfo = createSkillsRestInfo(queue); + skillsRestInfo = createSkillsRestInfo(queue.getSkills()); agentGroupsRestInfo = createAgentGroupsRestInfo(queue); recipeStepRestInfo = createRecipeStepsRestInfo(queue); queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupsRestInfo, recipeStepRestInfo); @@ -297,9 +297,11 @@ private List createRecipeStepsRestInfo(OpenAcdQueue q private OpenAcdRecipeActionRestInfo createRecipeActionRestInfo(OpenAcdRecipeStep step) { OpenAcdRecipeActionRestInfo recipeActionRestInfo; + List skillsRestInfo; - OpenAcdRecipeAction groupRecipeActions = step.getAction(); - recipeActionRestInfo = new OpenAcdRecipeActionRestInfo(groupRecipeActions); + // get skills associated with action + skillsRestInfo = createSkillsRestInfo(step.getAction().getSkills()); + recipeActionRestInfo = new OpenAcdRecipeActionRestInfo(step.getAction(), skillsRestInfo); return recipeActionRestInfo; } @@ -320,16 +322,15 @@ private List createRecipeConditionsRestInfo(Open return recipeConditionsRestInfo; } - private List createSkillsRestInfo(OpenAcdQueue queue) { + private List createSkillsRestInfo(Set skills) { List skillsRestInfo; OpenAcdSkillRestInfo skillRestInfo; - // create list of skill restinfos for single group - Set groupSkills = queue.getSkills(); - skillsRestInfo = new ArrayList(groupSkills.size()); + // create list of skill restinfos from set of skills + skillsRestInfo = new ArrayList(skills.size()); - for (OpenAcdSkill groupSkill : groupSkills) { - skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + for (OpenAcdSkill skill : skills) { + skillRestInfo = new OpenAcdSkillRestInfo(skill); skillsRestInfo.add(skillRestInfo); } @@ -365,7 +366,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdQueue queue = queues.get(index); - skillsRestInfo = createSkillsRestInfo(queue); + skillsRestInfo = createSkillsRestInfo(queue.getSkills()); agentGroupRestInfo = createAgentGroupsRestInfo(queue); recipeStepRestInfo = createRecipeStepsRestInfo(queue); queueRestInfo = new OpenAcdQueueRestInfoFull(queue, skillsRestInfo, agentGroupRestInfo, recipeStepRestInfo); @@ -505,11 +506,40 @@ private void addLists(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestInfo queue.getSteps().clear(); // set steps - // TODO: must add addition of conditions and actions OpenAcdRecipeStep step; + OpenAcdRecipeCondition condition; + OpenAcdRecipeAction action; + OpenAcdRecipeActionRestInfo actionRestInfo; + List recipeStepsRestInfo = queueRestInfo.getSteps(); for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { - step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); + //step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); + step = new OpenAcdRecipeStep(); + + // add conditions + for (OpenAcdRecipeConditionRestInfo recipeConditionRestInfo : recipeStepRestInfo.getConditions()) { + condition = new OpenAcdRecipeCondition(); + condition.setCondition(recipeConditionRestInfo.getCondition()); + condition.setRelation(recipeConditionRestInfo.getRelation()); + condition.setValueCondition(recipeConditionRestInfo.getValueCondition()); + + step.addCondition(condition); + } + + // add action + action = new OpenAcdRecipeAction(); + actionRestInfo = recipeStepRestInfo.getAction(); + action.setAction(actionRestInfo.getAction()); + action.setActionValue(actionRestInfo.getActionValue()); + + // set action skills (separate from skills assigned to queue + for (OpenAcdSkillRestInfo skillRestInfo : actionRestInfo.getSkills()) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + action.addSkill(skill); + } + + step.setAction(action); + queue.addStep(step); } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index ac127a8b74..7c3101ee4d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -541,12 +541,12 @@ public List getSteps() { static class OpenAcdRecipeActionRestInfo { private final String m_action; private final String m_actionValue; - private final List m_skills; + private final List m_skills; - public OpenAcdRecipeActionRestInfo(OpenAcdRecipeAction action) { + public OpenAcdRecipeActionRestInfo(OpenAcdRecipeAction action, List skills) { m_action = action.getAction(); m_actionValue = action.getActionValue(); - m_skills = action.getAllSkillNames(); + m_skills = skills; } public String getAction() { @@ -554,10 +554,10 @@ public String getAction() { } public String getActionValue() { - return m_action; + return m_actionValue; } - public List getSkills() { + public List getSkills() { return m_skills; } } @@ -579,7 +579,7 @@ public int getId() { return m_id; } - public List getCondition() { + public List getConditions() { return m_conditions; } From df131b096d8e7f77df316f3e24aa90e2643b801d Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 6 Apr 2012 16:18:23 -0400 Subject: [PATCH 55/99] Made representation xml element names consistent --- .../sipxconfig/rest/OpenAcdSkillGroupsResource.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index bf0db1b5b0..a0be6832eb 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -436,19 +436,19 @@ protected void configureXStream(XStream xstream) { static class OpenAcdSkillGroupsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_skills; + private final List m_groups; - public OpenAcdSkillGroupsBundleRestInfo(List skills, MetadataRestInfo metadata) { + public OpenAcdSkillGroupsBundleRestInfo(List skillGroups, MetadataRestInfo metadata) { m_metadata = metadata; - m_skills = skills; + m_groups = skillGroups; } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getSkills() { - return m_skills; + public List getGroups() { + return m_groups; } } From de45b69869ee9b51093625bc99c10de240afaf0a Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 6 Apr 2012 16:54:51 -0400 Subject: [PATCH 56/99] Complete create, update of recipes --- .../sipxconfig/rest/OpenAcdQueuesResource.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 273ec44ee0..0cc665369a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -135,7 +135,7 @@ public Representation represent(Variant variant) throws ResourceException { queueRestInfo = createQueueRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); } return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); @@ -459,6 +459,7 @@ private void updateQueue(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestI queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); + queue.setWeight(queueRestInfo.getWeight()); addLists(queue, queueRestInfo); } @@ -473,6 +474,7 @@ private OpenAcdQueue createQueue(OpenAcdQueueRestInfoFull queueRestInfo) throws queue.setGroup(queueGroup); queue.setDescription(queueRestInfo.getDescription()); + queue.setWeight(queueRestInfo.getWeight()); addLists(queue, queueRestInfo); @@ -513,10 +515,12 @@ private void addLists(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestInfo List recipeStepsRestInfo = queueRestInfo.getSteps(); for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { - //step = m_openAcdContext.getRecipeStepById(recipeStepRestInfo.getId()); step = new OpenAcdRecipeStep(); + step.setFrequency(recipeStepRestInfo.getFrequency()); + // add conditions + step.getConditions().clear(); for (OpenAcdRecipeConditionRestInfo recipeConditionRestInfo : recipeStepRestInfo.getConditions()) { condition = new OpenAcdRecipeCondition(); condition.setCondition(recipeConditionRestInfo.getCondition()); @@ -542,6 +546,7 @@ private void addLists(OpenAcdQueue queue, OpenAcdQueueRestInfoFull queueRestInfo queue.addStep(step); } + } private OpenAcdQueueGroup getQueueGroup(OpenAcdQueueRestInfoFull queueRestInfo) throws ResourceException { From f133a8a8bc795d58dea3dace60d657abe22f9212 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Mon, 9 Apr 2012 02:30:20 -0400 Subject: [PATCH 57/99] Correct return of error xml on GET --- .../sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java | 2 +- .../sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java | 2 +- .../sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java | 2 +- .../sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java | 2 +- .../org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 93654d12b6..60221167f2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -130,7 +130,7 @@ public Representation represent(Variant variant) throws ResourceException { agentGroupRestInfo = createAgentGroupRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent Group failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent Group failed", exception.getLocalizedMessage()); } return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index 192c303111..ebcdd00824 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -140,7 +140,7 @@ public Representation represent(Variant variant) throws ResourceException { agentRestInfo = createAgentRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent failed", exception.getLocalizedMessage()); } return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 11d5944da7..6c3a995c5b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -124,7 +124,7 @@ public Representation represent(Variant variant) throws ResourceException { clientRestInfo = createClientRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Client failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Client failed", exception.getLocalizedMessage()); } return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 737e6ec38c..cb0d466a93 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -134,7 +134,7 @@ public Representation represent(Variant variant) throws ResourceException { lineRestInfo = createLineRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Line failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Line failed", exception.getLocalizedMessage()); } return new OpenAcdLineRepresentation(variant.getMediaType(), lineRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 13c6e420e2..de8d75ee09 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -129,7 +129,7 @@ public Representation represent(Variant variant) throws ResourceException { queueGroupRestInfo = createQueueGroupRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); } return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 15f520de86..6aa09a98d8 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -126,7 +126,7 @@ public Representation represent(Variant variant) throws ResourceException { releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Release Code failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Release Code failed", exception.getLocalizedMessage()); } return new OpenAcdReleaseCodeRepresentation(variant.getMediaType(), releaseCodeRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index f31195b948..98ed7745c2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -97,7 +97,7 @@ public Representation represent(Variant variant) throws ResourceException { settingRestInfo = createSettingRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); } return new OpenAcdSettingRepresentation(variant.getMediaType(), settingRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index a0be6832eb..dace657603 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -127,7 +127,7 @@ public Representation represent(Variant variant) throws ResourceException { skillGroupRestInfo = createSkillGroupRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skill Group failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skill Group failed", exception.getLocalizedMessage()); } return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 2791ee38e8..64f6964be1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -124,7 +124,7 @@ public Representation represent(Variant variant) throws ResourceException { skillRestInfo = createSkillRestInfo(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); } return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); From 93dc1d47e7baa0d08be6cadb3d0239bc71789e0c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 04:12:23 -0400 Subject: [PATCH 58/99] Add beginnings of Permissions REST API --- .../sipxconfig/rest/PermissionsResource.java | 538 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 18 + 2 files changed, 556 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java new file mode 100644 index 0000000000..16caa4ed54 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -0,0 +1,538 @@ +/* + * + * PermissionsResource.java - A Restlet to read Skill data from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.permission.Permission; +import org.sipfoundry.sipxconfig.permission.PermissionManager; +import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillRepresentation; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class PermissionsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private PermissionManager m_permissionManager; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + NAME, DESCRIPTION, ATOM, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + PermissionRestInfoFull permissionRestInfo = null; + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + idInt = OpenAcdUtilities.getIntFromAttribute(idString); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + } + + try { + permissionRestInfo = createPermissionRestInfo(idInt); + } + catch (Exception exception) { + return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read permissions failed", exception.getLocalizedMessage()); + } + + return new PermissionRepresentation(variant.getMediaType(), permissionRestInfo); + } + + + // if not single, process request for all + List permissions = (List) m_permissionManager.getPermissions(); + List permissionsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortPermissions(permissions); + + // set requested agents groups and get resulting metadata + metadataRestInfo = addPermissions(permissionsRestInfo, permissions); + + // create final restinfo + PermissionsBundleRestInfo permissionsBundleRestInfo = new PermissionsBundleRestInfo(permissionsRestInfo, metadataRestInfo); + + return new PermissionsRepresentation(variant.getMediaType(), permissionsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); + OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); + OpenAcdSkill skill = null; + + // validate input for update or create + ValidationInfo validationInfo = validate(skillRestInfo); + + if (!validationInfo.valid) { + OpenAcdUtilities.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 = OpenAcdUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateSkill(skill, skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + + return; + } + + + // otherwise add new + try { + skill = createSkill(skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + return; + } + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSkill skill; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteSkill(skill); + + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + + return; + } + + // no id string + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + String name = restInfo.getName(); + String atom = restInfo.getAtom(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < atom.length(); i++) { + if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + return validationInfo; + } + + private Permission getPermissionById(int id) throws ResourceException { + Permission foundPermission = null; + + for (Permission permission : m_permissionManager.getPermissions()) { + if (permission.getId().equals(id)) { + foundPermission = permission; + } + } + + if (foundPermission == null) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Id " + id + "not found."); + } + + return foundPermission; + } + + private PermissionRestInfoFull createPermissionRestInfo(int id) throws ResourceException { + PermissionRestInfoFull permissionRestInfo = null; + + Permission permission = getPermissionById(id); + permissionRestInfo = new PermissionRestInfoFull(permission); + + return permissionRestInfo; + } + + private MetadataRestInfo addPermissions(List permissionsRestInfo, List permissions) { + PermissionRestInfoFull permissionRestInfo; + + // determine pagination + PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, permissions.size()); + + // create list of restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + Permission permission = permissions.get(index); + + permissionRestInfo = new PermissionRestInfoFull(permission); + permissionsRestInfo.add(permissionRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortPermissions(List permissions) { + // sort if requested + SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return permission1.getName().compareToIgnoreCase(permission2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return permission1.getDescription().compareToIgnoreCase(permission2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return permission2.getName().compareToIgnoreCase(permission1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return permission2.getDescription().compareToIgnoreCase(permission1.getDescription()); + } + + }); + break; + } + } + } + + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + String tempString; + + // do not allow empty name + tempString = skillRestInfo.getName(); + if (!tempString.isEmpty()) { + skill.setName(tempString); + } + + skill.setDescription(skillRestInfo.getDescription()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + } + + private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + OpenAcdSkill skill = new OpenAcdSkill(); + + // copy fields from rest info + skill.setName(skillRestInfo.getName()); + skill.setDescription(skillRestInfo.getDescription()); + skill.setAtom(skillRestInfo.getAtom()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + + return skill; + } + + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + int groupId = skillRestInfo.getGroupId(); + skillGroup = m_openAcdContext.getSkillGroupById(groupId); + + return skillGroup; + } + + + // REST Representations + // -------------------- + + static class PermissionsRepresentation extends XStreamRepresentation { + + public PermissionsRepresentation(MediaType mediaType, PermissionsBundleRestInfo object) { + super(mediaType, object); + } + + public PermissionsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("permissions", PermissionsBundleRestInfo.class); + xstream.alias("permission", PermissionRestInfoFull.class); + } + } + + static class PermissionRepresentation extends XStreamRepresentation { + + public PermissionRepresentation(MediaType mediaType, PermissionRestInfoFull object) { + super(mediaType, object); + } + + public PermissionRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("permission", PermissionRestInfoFull.class); + } + } + + + // REST info objects + // ----------------- + + static class PermissionsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_permissions; + + public PermissionsBundleRestInfo(List permissions, MetadataRestInfo metadata) { + m_metadata = metadata; + m_permissions = permissions; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getPermissions() { + return m_permissions; + } + } + + static class PermissionRestInfoFull { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_label; + private final boolean m_defaultValue; + + public PermissionRestInfoFull(Permission permission) { + m_id = permission.getId(); + m_name = permission.getName(); + m_description = permission.getDescription(); + m_label = permission.getLabel(); + m_defaultValue = permission.getDefaultValue(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public boolean getDefaultValue() { + return m_defaultValue; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + + @Required + public void setPermissionManager(PermissionManager permissionManager) { + m_permissionManager = permissionManager; + } +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 2398dc14ba..ea374cd08b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -384,4 +384,22 @@ + + + + + + + + + + + + + + + + + + From fcdf2c6dc109f2ce32bc050fb77cf7afaa4c54b3 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 12:44:16 -0400 Subject: [PATCH 59/99] Add Recipes to Queue Groups --- .../rest/OpenAcdQueueGroupsResource.java | 256 +++++++++++++----- .../sipxconfig/rest/OpenAcdUtilities.java | 81 +++--- 2 files changed, 228 insertions(+), 109 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index de8d75ee09..74132cb916 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -35,19 +34,25 @@ import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueGroupRestInfoFull; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeActionRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeConditionRestInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeStepRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -142,7 +147,7 @@ public Representation represent(Variant variant) throws ResourceException { MetadataRestInfo metadataRestInfo; // sort if specified - sortGroups(queueGroups); + sortQueueGroups(queueGroups); // set requested based on pagination and get resulting metadata metadataRestInfo = addQueueGroups(queueGroupsRestInfo, queueGroups); @@ -204,7 +209,7 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new agent group try { - queueGroup = createOpenAcdQueueGroup(queueGroupRestInfo); + queueGroup = createQueueGroup(queueGroupRestInfo); m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { @@ -257,6 +262,22 @@ public void removeRepresentations() throws ResourceException { private ValidationInfo validate(OpenAcdQueueGroupRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + for (int i = 0; i < restInfo.getSteps().size(); i++) { + if (restInfo.getSteps().get(i).getAction() == null) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } return validationInfo; } @@ -264,49 +285,93 @@ private OpenAcdQueueGroupRestInfoFull createQueueGroupRestInfo(int id) throws Re OpenAcdQueueGroupRestInfoFull queueGroupRestInfo; List skillsRestInfo; List agentGroupRestInfo; + List recipeStepRestInfo; - try { - OpenAcdQueueGroup queueGroup = m_openAcdContext.getQueueGroupById(id); + OpenAcdQueueGroup queueGroup = m_openAcdContext.getQueueGroupById(id); + skillsRestInfo = createSkillsRestInfo(queueGroup.getSkills()); + agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + recipeStepRestInfo = createRecipeStepsRestInfo(queueGroup); + queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo, recipeStepRestInfo); - skillsRestInfo = createSkillsRestInfo(queueGroup); - agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + return queueGroupRestInfo; + } - queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); + private List createSkillsRestInfo(Set groupSkills) { + List skillsRestInfo; + OpenAcdSkillRestInfo skillRestInfo; + + // create list of skill restinfos for single group + skillsRestInfo = new ArrayList(groupSkills.size()); + + for (OpenAcdSkill groupSkill : groupSkills) { + skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); + skillsRestInfo.add(skillRestInfo); } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + + return skillsRestInfo; + } + + private List createAgentGroupsRestInfo(OpenAcdQueueGroup queueGroup) { + List agentGroupsRestInfo; + OpenAcdAgentGroupRestInfo agentGroupRestInfo; + + // create list of agent group restinfos for single group + Set groupAgentGroups = queueGroup.getAgentGroups(); + agentGroupsRestInfo = new ArrayList(groupAgentGroups.size()); + + for (OpenAcdAgentGroup groupAgentGroup : groupAgentGroups) { + agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(groupAgentGroup); + agentGroupsRestInfo.add(agentGroupRestInfo); } - return queueGroupRestInfo; + return agentGroupsRestInfo; } - private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { - String tempString; + private List createRecipeStepsRestInfo(OpenAcdQueueGroup queueGroup) { + List recipeStepsRestInfo; + OpenAcdRecipeStepRestInfo recipeStepRestInfo; - // do not allow empty name - tempString = queueGroupRestInfo.getName(); - if (!tempString.isEmpty()) { - queueGroup.setName(tempString); + Set groupRecipeSteps = queueGroup.getSteps(); + recipeStepsRestInfo = new ArrayList(groupRecipeSteps.size()); + + for (OpenAcdRecipeStep groupRecipeStep : groupRecipeSteps) { + recipeStepRestInfo = new OpenAcdRecipeStepRestInfo(groupRecipeStep, createRecipeActionRestInfo(groupRecipeStep), createRecipeConditionsRestInfo(groupRecipeStep)); + recipeStepsRestInfo.add(recipeStepRestInfo); } + return recipeStepsRestInfo; + } - queueGroup.setDescription(queueGroupRestInfo.getDescription()); + private OpenAcdRecipeActionRestInfo createRecipeActionRestInfo(OpenAcdRecipeStep step) { + OpenAcdRecipeActionRestInfo recipeActionRestInfo; + List skillsRestInfo; - // remove all current skills - queueGroup.getSkills().clear(); + // get skills associated with action + skillsRestInfo = createSkillsRestInfo(step.getAction().getSkills()); + recipeActionRestInfo = new OpenAcdRecipeActionRestInfo(step.getAction(), skillsRestInfo); - // set skills - OpenAcdSkill skill; - List skillsRestInfo = queueGroupRestInfo.getSkills(); - for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); - queueGroup.addSkill(skill); + return recipeActionRestInfo; + } + + private List createRecipeConditionsRestInfo(OpenAcdRecipeStep step) { + List recipeConditionsRestInfo; + OpenAcdRecipeConditionRestInfo recipeConditionRestInfo; + + + List groupRecipeConditions = step.getConditions(); + recipeConditionsRestInfo = new ArrayList(groupRecipeConditions.size()); + + for (OpenAcdRecipeCondition groupRecipeCondition : groupRecipeConditions) { + recipeConditionRestInfo = new OpenAcdRecipeConditionRestInfo(groupRecipeCondition); + recipeConditionsRestInfo.add(recipeConditionRestInfo); } + + return recipeConditionsRestInfo; } private MetadataRestInfo addQueueGroups(List queueGroupsRestInfo, List queueGroups) { List skillsRestInfo; List agentGroupRestInfo; - + List recipeStepRestInfo; // determine pagination PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queueGroups.size()); @@ -314,10 +379,11 @@ private MetadataRestInfo addQueueGroups(List queu for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { OpenAcdQueueGroup queueGroup = queueGroups.get(index); - skillsRestInfo = createSkillsRestInfo(queueGroup); + skillsRestInfo = createSkillsRestInfo(queueGroup.getSkills()); agentGroupRestInfo = createAgentGroupsRestInfo(queueGroup); + recipeStepRestInfo = createRecipeStepsRestInfo(queueGroup); - OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo); + OpenAcdQueueGroupRestInfoFull queueGroupRestInfo = new OpenAcdQueueGroupRestInfoFull(queueGroup, skillsRestInfo, agentGroupRestInfo, recipeStepRestInfo); queueGroupsRestInfo.add(queueGroupRestInfo); } @@ -326,39 +392,7 @@ private MetadataRestInfo addQueueGroups(List queu return metadata; } - private List createSkillsRestInfo(OpenAcdQueueGroup queueGroup) { - List skillsRestInfo; - OpenAcdSkillRestInfo skillRestInfo; - - // create list of skill restinfos for single group - Set groupSkills = queueGroup.getSkills(); - skillsRestInfo = new ArrayList(groupSkills.size()); - - for (OpenAcdSkill groupSkill : groupSkills) { - skillRestInfo = new OpenAcdSkillRestInfo(groupSkill); - skillsRestInfo.add(skillRestInfo); - } - - return skillsRestInfo; - } - - private List createAgentGroupsRestInfo(OpenAcdQueueGroup queueGroup) { - List agentGroupsRestInfo; - OpenAcdAgentGroupRestInfo agentGroupRestInfo; - - // create list of agent group restinfos for single group - Set groupAgentGroups = queueGroup.getAgentGroups(); - agentGroupsRestInfo = new ArrayList(groupAgentGroups.size()); - - for (OpenAcdAgentGroup groupAgentGroup : groupAgentGroups) { - agentGroupRestInfo = new OpenAcdAgentGroupRestInfo(groupAgentGroup); - agentGroupsRestInfo.add(agentGroupRestInfo); - } - - return agentGroupsRestInfo; - } - - private void sortGroups(List queueGroups) { + private void sortQueueGroups(List queueGroups) { // sort groups if requested SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); @@ -426,24 +460,100 @@ public int compare(Object object1, Object object2) { } } - private OpenAcdQueueGroup createOpenAcdQueueGroup(OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { + private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { + String tempString; + + // do not allow empty name + tempString = queueGroupRestInfo.getName(); + if (!tempString.isEmpty()) { + queueGroup.setName(tempString); + } + + queueGroup.setDescription(queueGroupRestInfo.getDescription()); + + // remove all current skills + queueGroup.getSkills().clear(); + + addLists(queueGroup, queueGroupRestInfo); + } + + private OpenAcdQueueGroup createQueueGroup(OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { OpenAcdQueueGroup queueGroup = new OpenAcdQueueGroup(); // copy fields from rest info queueGroup.setName(queueGroupRestInfo.getName()); queueGroup.setDescription(queueGroupRestInfo.getDescription()); - // add skills - Set skills = new LinkedHashSet(); - List skillsRestInfo = queueGroupRestInfo.getSkills(); + addLists(queueGroup, queueGroupRestInfo); + return queueGroup; + } + + private void addLists(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRestInfoFull queueGroupRestInfo) { + // remove all skills + queueGroup.getSkills().clear(); + + // set skills + OpenAcdSkill skill; + List skillsRestInfo = queueGroupRestInfo.getSkills(); for (OpenAcdSkillRestInfo skillRestInfo : skillsRestInfo) { - skills.add(m_openAcdContext.getSkillById(skillRestInfo.getId())); + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + queueGroup.addSkill(skill); } - queueGroup.setSkills(skills); + // remove all agent groups + queueGroup.getAgentGroups().clear(); - return queueGroup; + // set agent groups + OpenAcdAgentGroup agentGroup; + List agentGroupsRestInfo = queueGroupRestInfo.getAgentGroups(); + for (OpenAcdAgentGroupRestInfo agentGroupRestInfo : agentGroupsRestInfo) { + agentGroup = m_openAcdContext.getAgentGroupById(agentGroupRestInfo.getId()); + queueGroup.addAgentGroup(agentGroup); + } + + // remove all current steps + queueGroup.getSteps().clear(); + + // set steps + OpenAcdRecipeStep step; + OpenAcdRecipeCondition condition; + OpenAcdRecipeAction action; + OpenAcdRecipeActionRestInfo actionRestInfo; + + List recipeStepsRestInfo = queueGroupRestInfo.getSteps(); + for (OpenAcdRecipeStepRestInfo recipeStepRestInfo : recipeStepsRestInfo) { + step = new OpenAcdRecipeStep(); + step.setFrequency(recipeStepRestInfo.getFrequency()); + + + // add conditions + step.getConditions().clear(); + for (OpenAcdRecipeConditionRestInfo recipeConditionRestInfo : recipeStepRestInfo.getConditions()) { + condition = new OpenAcdRecipeCondition(); + condition.setCondition(recipeConditionRestInfo.getCondition()); + condition.setRelation(recipeConditionRestInfo.getRelation()); + condition.setValueCondition(recipeConditionRestInfo.getValueCondition()); + + step.addCondition(condition); + } + + // add action + action = new OpenAcdRecipeAction(); + actionRestInfo = recipeStepRestInfo.getAction(); + action.setAction(actionRestInfo.getAction()); + action.setActionValue(actionRestInfo.getActionValue()); + + // set action skills (separate from skills assigned to queue + for (OpenAcdSkillRestInfo skillRestInfo : actionRestInfo.getSkills()) { + skill = m_openAcdContext.getSkillById(skillRestInfo.getId()); + action.addSkill(skill); + } + + step.setAction(action); + + queueGroup.addStep(step); + } } @@ -466,6 +576,9 @@ protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdQueueGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + xstream.alias("step", OpenAcdRecipeStepRestInfo.class); + xstream.alias("condition", OpenAcdRecipeConditionRestInfo.class); + xstream.alias("action", OpenAcdRecipeActionRestInfo.class); } } @@ -484,6 +597,9 @@ protected void configureXStream(XStream xstream) { xstream.alias("group", OpenAcdQueueGroupRestInfoFull.class); xstream.alias("skill", OpenAcdSkillRestInfo.class); xstream.alias("agentGroup", OpenAcdAgentGroupRestInfo.class); + xstream.alias("step", OpenAcdRecipeStepRestInfo.class); + xstream.alias("condition", OpenAcdRecipeConditionRestInfo.class); + xstream.alias("action", OpenAcdRecipeActionRestInfo.class); } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 7c3101ee4d..1657670ed9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -532,10 +532,49 @@ public List getAgentGroups() { public List getSteps() { return m_steps; } + } + + + static class OpenAcdQueueGroupRestInfoFull { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + private final List m_agentGroups; + private final List m_steps; + + public OpenAcdQueueGroupRestInfoFull(OpenAcdQueueGroup queueGroup, List skills, List agentGroups, List steps) { + m_name = queueGroup.getName(); + m_id = queueGroup.getId(); + m_description = queueGroup.getDescription(); + m_skills = skills; + m_agentGroups = agentGroups; + m_steps = steps; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + + public List getAgentGroups() { + return m_agentGroups; + } -// public Set getStep(OpenAcdQueue queue) { -// return queue.getSteps(); -// } + public List getSteps() { + return m_steps; + } } static class OpenAcdRecipeActionRestInfo { @@ -695,42 +734,6 @@ public List getClients() { } } - static class OpenAcdQueueGroupRestInfoFull { - private final String m_name; - private final int m_id; - private final String m_description; - private final List m_skills; - private final List m_agentGroups; - - public OpenAcdQueueGroupRestInfoFull(OpenAcdQueueGroup queueGroup, List skills, List agentGroups) { - m_name = queueGroup.getName(); - m_id = queueGroup.getId(); - m_description = queueGroup.getDescription(); - m_skills = skills; - m_agentGroups = agentGroups; - } - - public String getName() { - return m_name; - } - - public int getId() { - return m_id; - } - - public String getDescription() { - return m_description; - } - - public List getSkills() { - return m_skills; - } - - public List getAgentGroups() { - return m_agentGroups; - } - } - static class OpenAcdReleaseCodeRestInfo { private final int m_id; private final String m_label; From b89a00bb3cabea1bc0405c14aa362f34a02c87c3 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 13:37:34 -0400 Subject: [PATCH 60/99] Add validation --- .../sipxconfig/rest/OpenAcdLinesResource.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index cb0d466a93..94b903882d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -46,6 +46,7 @@ import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -263,6 +264,42 @@ public void removeRepresentations() throws ResourceException { private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + String ext = restInfo.getExtension(); + String did = restInfo.getDIDNumber(); + String alias = restInfo.getAlias(); + for (int i = 0; i < name.length(); i++) { + if (name.charAt(i) == ' ') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, underscores, and symbols"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < ext.length(); i++) { + if ((!Character.isDigit(ext.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Extension' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < did.length(); i++) { + if ((!Character.isDigit(did.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'DID Number' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < alias.length(); i++) { + if ((!Character.isDigit(alias.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Alias' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + return validationInfo; } From 6d3cd4c0b913836b6ed9c3abbe945f76fe1ce92c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 15:19:12 -0400 Subject: [PATCH 61/99] Add Permissions REST API --- .../sipxconfig/rest/OpenAcdUtilities.java | 30 +++ .../sipxconfig/rest/PermissionsResource.java | 188 ++++++------------ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 4 +- 3 files changed, 95 insertions(+), 127 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java index 1657670ed9..fe3843cc37 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java @@ -221,6 +221,36 @@ public static void setResponse(Response response, ResponseCode code, String mess } } + public static void setResponse(Response response, ResponseCode code, String message, String id) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add related data + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("id"); + elementId.appendChild(doc.createTextNode(id)); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public static void setResponseError(Response response, ResponseCode code, String message) { Representation representation = getResponseError(response, code, message); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index 16caa4ed54..3ab661996a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -33,20 +33,13 @@ import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.sipfoundry.sipxconfig.permission.Permission; import org.sipfoundry.sipxconfig.permission.PermissionManager; -import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillRepresentation; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -55,7 +48,6 @@ public class PermissionsResource extends UserResource { - private OpenAcdContext m_openAcdContext; private PermissionManager m_permissionManager; private Form m_form; @@ -113,20 +105,13 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { // process request for single - int idInt; + // Permissions do not use Id, so must key off Name PermissionRestInfoFull permissionRestInfo = null; - String idString = (String) getRequest().getAttributes().get("id"); + String nameString = (String) getRequest().getAttributes().get("name"); - if (idString != null) { + if (nameString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); - } - catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - } - - try { - permissionRestInfo = createPermissionRestInfo(idInt); + permissionRestInfo = createPermissionRestInfo(nameString); } catch (Exception exception) { return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read permissions failed", exception.getLocalizedMessage()); @@ -137,7 +122,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all - List permissions = (List) m_permissionManager.getPermissions(); + List permissions = new ArrayList(m_permissionManager.getPermissions()); List permissionsRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; @@ -160,12 +145,12 @@ public Representation represent(Variant variant) throws ResourceException { @Override public void storeRepresentation(Representation entity) throws ResourceException { // get from request body - OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); - OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); - OpenAcdSkill skill = null; + PermissionRepresentation representation = new PermissionRepresentation(entity); + PermissionRestInfoFull permissionRestInfo = representation.getObject(); + Permission permission = null; // validate input for update or create - ValidationInfo validationInfo = validate(skillRestInfo); + ValidationInfo validationInfo = validate(permissionRestInfo); if (!validationInfo.valid) { OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); @@ -174,29 +159,28 @@ public void storeRepresentation(Representation entity) throws ResourceException // if have id then update single - String idString = (String) getRequest().getAttributes().get("id"); + String nameString = (String) getRequest().getAttributes().get("name"); - if (idString != null) { + if (nameString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + permission = m_permissionManager.getPermissionByName(nameString); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); return; } // copy values over to existing try { - updateSkill(skill, skillRestInfo); - m_openAcdContext.saveSkill(skill); + updatePermission(permission, permissionRestInfo); + m_permissionManager.saveCallPermission(permission); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Permission failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Permission", permission.getName()); return; } @@ -204,15 +188,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new try { - skill = createSkill(skillRestInfo); - m_openAcdContext.saveSkill(skill); + permission = createPermission(permissionRestInfo); + m_permissionManager.saveCallPermission(permission); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Permission failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Permission", permission.getName()); } @@ -221,30 +205,29 @@ public void storeRepresentation(Representation entity) throws ResourceException @Override public void removeRepresentations() throws ResourceException { - OpenAcdSkill skill; + Permission permission; // get id then delete single - String idString = (String) getRequest().getAttributes().get("id"); + String nameString = (String) getRequest().getAttributes().get("name"); - if (idString != null) { + if (nameString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + permission = m_permissionManager.getPermissionByName(nameString); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); return; } - m_openAcdContext.deleteSkill(skill); + m_permissionManager.deleteCallPermission(permission); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Permission", permission.getName()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "Name value missing"); } @@ -255,51 +238,16 @@ public void removeRepresentations() throws ResourceException { // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create - private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + private ValidationInfo validate(PermissionRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); - String name = restInfo.getName(); - String atom = restInfo.getAtom(); - - for (int i = 0; i < name.length(); i++) { - if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - - for (int i = 0; i < atom.length(); i++) { - if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - return validationInfo; } - private Permission getPermissionById(int id) throws ResourceException { - Permission foundPermission = null; - - for (Permission permission : m_permissionManager.getPermissions()) { - if (permission.getId().equals(id)) { - foundPermission = permission; - } - } - - if (foundPermission == null) { - throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Id " + id + "not found."); - } - - return foundPermission; - } - - private PermissionRestInfoFull createPermissionRestInfo(int id) throws ResourceException { + private PermissionRestInfoFull createPermissionRestInfo(String name) throws ResourceException { PermissionRestInfoFull permissionRestInfo = null; - Permission permission = getPermissionById(id); + Permission permission = m_permissionManager.getPermissionByName(name); permissionRestInfo = new PermissionRestInfoFull(permission); return permissionRestInfo; @@ -392,46 +340,33 @@ public int compare(Object object1, Object object2) { } } - private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; + private void updatePermission(Permission permission, PermissionRestInfoFull permissionRestInfo) { String tempString; - // do not allow empty name - tempString = skillRestInfo.getName(); + // do not allow empty label + tempString = permissionRestInfo.getLabel(); if (!tempString.isEmpty()) { - skill.setName(tempString); + permission.setLabel(tempString); } - skill.setDescription(skillRestInfo.getDescription()); - - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + permission.setDescription(permissionRestInfo.getDescription()); + permission.setDefaultValue(permissionRestInfo.getDefaultValue()); } - private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { - OpenAcdSkillGroup skillGroup; - OpenAcdSkill skill = new OpenAcdSkill(); + private Permission createPermission(PermissionRestInfoFull permissionRestInfo) throws ResourceException { + Permission permission = new Permission(); // copy fields from rest info - skill.setName(skillRestInfo.getName()); - skill.setDescription(skillRestInfo.getDescription()); - skill.setAtom(skillRestInfo.getAtom()); + permission.setLabel(permissionRestInfo.getLabel()); + permission.setDescription(permissionRestInfo.getDescription()); + permission.setDefaultValue(permissionRestInfo.getDefaultValue()); - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + // only available is custom call types + permission.setType(Permission.Type.CALL); - return skill; + return permission; } - private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; - int groupId = skillRestInfo.getGroupId(); - skillGroup = m_openAcdContext.getSkillGroupById(groupId); - - return skillGroup; - } - - // REST Representations // -------------------- @@ -491,28 +426,30 @@ public List getPermissions() { } static class PermissionRestInfoFull { - private final int m_id; private final String m_name; - private final String m_description; private final String m_label; + private final String m_description; private final boolean m_defaultValue; + private final Permission.Type m_type; + private final boolean m_builtIn; public PermissionRestInfoFull(Permission permission) { - m_id = permission.getId(); m_name = permission.getName(); - m_description = permission.getDescription(); m_label = permission.getLabel(); + m_description = permission.getDescription(); m_defaultValue = permission.getDefaultValue(); - } - - public int getId() { - return m_id; + m_type = permission.getType(); + m_builtIn = permission.isBuiltIn(); } public String getName() { return m_name; } + public String getLabel() { + return m_label; + } + public String getDescription() { return m_description; } @@ -520,17 +457,20 @@ public String getDescription() { public boolean getDefaultValue() { return m_defaultValue; } + + public Permission.Type getType() { + return m_type; + } + + public boolean getBuiltIn() { + return m_builtIn; + } } // Injected objects // ---------------- - @Required - public void setOpenAcdContext(OpenAcdContext openAcdContext) { - m_openAcdContext = openAcdContext; - } - @Required public void setPermissionManager(PermissionManager permissionManager) { m_permissionManager = permissionManager; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index ea374cd08b..1e0928b763 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -386,7 +386,6 @@ - @@ -395,11 +394,10 @@ - - + From 21401022444d1885ff553eb5a73ba439c4ebfe1c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 15:22:07 -0400 Subject: [PATCH 62/99] Change OpenAcdUtilities class to generic RestUtilities --- .../rest/OpenAcdAgentGroupsResource.java | 50 +- .../rest/OpenAcdAgentsResource.java | 50 +- .../rest/OpenAcdClientsResource.java | 44 +- .../sipxconfig/rest/OpenAcdLinesResource.java | 46 +- .../rest/OpenAcdQueueGroupsResource.java | 54 +- .../rest/OpenAcdQueuesResource.java | 52 +- .../rest/OpenAcdReleaseCodesResource.java | 42 +- .../rest/OpenAcdSettingsResource.java | 34 +- .../rest/OpenAcdSkillGroupsResource.java | 42 +- .../rest/OpenAcdSkillsResource.java | 44 +- .../sipxconfig/rest/PermissionsResource.java | 32 +- .../sipxconfig/rest/RestUtilities.java | 893 ++++++++++++++++++ 12 files changed, 1138 insertions(+), 245 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 60221167f2..3d9218e432 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -42,15 +42,15 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdAgentGroupRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -120,17 +120,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { agentGroupRestInfo = createAgentGroupRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent Group failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent Group failed", exception.getLocalizedMessage()); } return new OpenAcdAgentGroupRepresentation(variant.getMediaType(), agentGroupRestInfo); @@ -169,7 +169,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(agentGroupRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -179,11 +179,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -193,11 +193,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgentGroup(agentGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent Group", agentGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent Group", agentGroup.getId()); return; } @@ -209,11 +209,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgentGroup(agentGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent Group", agentGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent Group", agentGroup.getId()); } @@ -229,23 +229,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); agentGroup = m_openAcdContext.getAgentGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteAgentGroup(agentGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent Group", agentGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent Group", agentGroup.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -294,7 +294,7 @@ private MetadataRestInfo addAgentGroups(List agen List clientsRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agentGroups.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, agentGroups.size()); // create list of group restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -362,7 +362,7 @@ private List createClientsRestInfo(OpenAcdAgentGroup agen private void sortGroups(List agentGroups) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index ebcdd00824..ac3eb2e2d0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -47,15 +47,15 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdAgentRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -129,18 +129,18 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); agentRestInfo = createAgentRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { agentRestInfo = createAgentRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Agent failed", exception.getLocalizedMessage()); } return new OpenAcdAgentRepresentation(variant.getMediaType(), agentRestInfo); @@ -179,7 +179,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(agentRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -189,11 +189,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); agent = m_openAcdContext.getAgentById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -203,11 +203,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgent(agent); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Agent failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Agent"); return; } @@ -218,11 +218,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveAgent(agent); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Agent failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent", agent.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Agent", agent.getId()); } @@ -238,23 +238,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); agent = m_openAcdContext.getAgentById(idInt); } catch (Exception ex) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteAgent(agent); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent.", agent.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Agent.", agent.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value is missing."); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value is missing."); } @@ -292,7 +292,7 @@ private MetadataRestInfo addAgents(List agentsRestInfo List clientsRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, agents.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, agents.size()); // create list of agent restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -361,7 +361,7 @@ private List createClientRestInfo(OpenAcdAgent agent) { private void sortAgents(List agents) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 6c3a995c5b..162e29105d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -39,12 +39,12 @@ import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -114,17 +114,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { clientRestInfo = createClientRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Client failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Client failed", exception.getLocalizedMessage()); } return new OpenAcdClientRepresentation(variant.getMediaType(), clientRestInfo); @@ -163,7 +163,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(clientRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -173,11 +173,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); client = m_openAcdContext.getClientById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -187,11 +187,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveClient(client); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Client failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Client", client.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Client", client.getId()); return; } @@ -203,11 +203,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveClient(client); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Client failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Client", client.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Client", client.getId()); } @@ -223,23 +223,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); client = m_openAcdContext.getClientById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteClient(client); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", client.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", client.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -284,7 +284,7 @@ private MetadataRestInfo addClients(List clientsRestInfo, OpenAcdClientRestInfo clientRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, clients.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, clients.size()); // create list of client restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -301,7 +301,7 @@ private MetadataRestInfo addClients(List clientsRestInfo, private void sortClients(List clients) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 94b903882d..2382437537 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -42,13 +42,13 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdLine; import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdClientRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdClientRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdQueueRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -125,17 +125,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { lineRestInfo = createLineRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Line failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Line failed", exception.getLocalizedMessage()); } return new OpenAcdLineRepresentation(variant.getMediaType(), lineRestInfo); @@ -174,7 +174,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(lineRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -184,11 +184,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); line = (OpenAcdLine) m_openAcdContext.getExtensionById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -198,11 +198,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveExtension(line); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Line failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Line", line.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Line", line.getId()); return; } @@ -214,11 +214,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveExtension(line); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Line failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Line", line.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Line", line.getId()); } @@ -234,23 +234,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); line = (OpenAcdLine) m_openAcdContext.getExtensionById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteExtension(line); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Line", line.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Line", line.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -399,7 +399,7 @@ private MetadataRestInfo addLines(List linesRestInfo, List< OpenAcdLineRestInfo lineRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, lines.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, lines.size()); // create list of line restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -416,7 +416,7 @@ private MetadataRestInfo addLines(List linesRestInfo, List< private void sortLines(List lines) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 74132cb916..53a6e08c37 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -44,17 +44,17 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueGroupRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeActionRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeConditionRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeStepRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdQueueGroupRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeActionRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeConditionRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeStepRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -124,17 +124,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { queueGroupRestInfo = createQueueGroupRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); } return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); @@ -173,7 +173,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(queueGroupRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -183,11 +183,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); queueGroup = m_openAcdContext.getQueueGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -197,11 +197,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queueGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queueGroup.getId()); return; } @@ -213,11 +213,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue Group", queueGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue Group", queueGroup.getId()); } @@ -233,23 +233,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); queueGroup = m_openAcdContext.getQueueGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteQueueGroup(queueGroup); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue Group", queueGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue Group", queueGroup.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -373,7 +373,7 @@ private MetadataRestInfo addQueueGroups(List queu List agentGroupRestInfo; List recipeStepRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queueGroups.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, queueGroups.size()); // create list of group restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -394,7 +394,7 @@ private MetadataRestInfo addQueueGroups(List queu private void sortQueueGroups(List queueGroups) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 0cc665369a..0c9e83c11a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -46,16 +46,16 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdAgentGroupRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdQueueRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeActionRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeConditionRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdRecipeStepRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdAgentGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdQueueRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeActionRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeConditionRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeStepRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -125,17 +125,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { queueRestInfo = createQueueRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); } return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); @@ -174,7 +174,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(queueRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -184,11 +184,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); queue = m_openAcdContext.getQueueById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -198,11 +198,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queue.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queue.getId()); return; } @@ -214,11 +214,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue", queue.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue", queue.getId()); } @@ -234,23 +234,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); queue = m_openAcdContext.getQueueById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteQueue(queue); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue", queue.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue", queue.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -360,7 +360,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo List recipeStepRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, queues.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, queues.size()); // create list of queue restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -380,7 +380,7 @@ private MetadataRestInfo addQueues(List queuesRestInfo private void sortQueues(List queues) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 6aa09a98d8..59397ecb23 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -41,12 +41,12 @@ import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdReleaseCodeRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdReleaseCodeRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -116,17 +116,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { releaseCodeRestInfo = createReleaseCodeRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Release Code failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Release Code failed", exception.getLocalizedMessage()); } return new OpenAcdReleaseCodeRepresentation(variant.getMediaType(), releaseCodeRestInfo); @@ -165,7 +165,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(releaseCodeRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -175,11 +175,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); releaseCode = m_openAcdContext.getReleaseCodeById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -189,11 +189,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveReleaseCode(releaseCode); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Release Code failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Release Code failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Release Code", releaseCode.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Release Code", releaseCode.getId()); return; } @@ -205,11 +205,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveReleaseCode(releaseCode); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Release Code failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Release Code failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Release Code", releaseCode.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Release Code", releaseCode.getId()); } @@ -227,11 +227,11 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); releaseCodeIds.add(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -241,7 +241,7 @@ public void removeRepresentations() throws ResourceException { } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -286,7 +286,7 @@ private MetadataRestInfo addReleaseCodes(List releas OpenAcdReleaseCodeRestInfo releaseCodeRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, releaseCodes.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, releaseCodes.size()); // create list of releaseCode restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -303,7 +303,7 @@ private MetadataRestInfo addReleaseCodes(List releas private void sortReleaseCodes(List releaseCodes) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index 98ed7745c2..8b9e6bcf4f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -34,9 +34,9 @@ import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSettingRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSettingRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -87,17 +87,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { settingRestInfo = createSettingRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); } return new OpenAcdSettingRepresentation(variant.getMediaType(), settingRestInfo); @@ -134,11 +134,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); setting = m_openAcdContext.getSettings(); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -148,11 +148,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSettings(setting); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Settings", setting.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Settings", setting.getId()); return; } @@ -164,11 +164,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSettings(setting); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Setting failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Setting failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Setting", setting.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Setting", setting.getId()); } @@ -184,23 +184,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); setting = m_openAcdContext.getSettings(); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.saveSettings(setting); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", setting.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", setting.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -225,7 +225,7 @@ private MetadataRestInfo addSettings(OpenAcdSettingRestInfo settingsRestInfo, Op OpenAcdSettingRestInfo settingRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, 1); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, 1); // create metadata about agent groups MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index dace657603..45bb4a4639 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -42,12 +42,12 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillGroupRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -117,17 +117,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { skillGroupRestInfo = createSkillGroupRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skill Group failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skill Group failed", exception.getLocalizedMessage()); } return new OpenAcdSkillGroupRepresentation(variant.getMediaType(), skillGroupRestInfo); @@ -166,7 +166,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(skillGroupRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -176,11 +176,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); skillGroup = m_openAcdContext.getSkillGroupById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -190,11 +190,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkillGroup(skillGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill Group", skillGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill Group", skillGroup.getId()); return; } @@ -206,11 +206,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkillGroup(skillGroup); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill Group failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill Group", skillGroup.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill Group", skillGroup.getId()); } @@ -229,11 +229,11 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); skillGroupIds.add(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -243,7 +243,7 @@ public void removeRepresentations() throws ResourceException { } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -288,7 +288,7 @@ private MetadataRestInfo addSkillGroups(List skillsRe OpenAcdSkillGroupRestInfo skillRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skillGroups.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skillGroups.size()); // create list of skill restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -305,7 +305,7 @@ private MetadataRestInfo addSkillGroups(List skillsRe private void sortSkillGroups(List skillGroups) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 64f6964be1..87fc6ee5ed 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -39,12 +39,12 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.OpenAcdSkillRestInfoFull; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ResponseCode; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -114,17 +114,17 @@ public Representation represent(Variant variant) throws ResourceException { if (idString != null) { try { - idInt = OpenAcdUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { skillRestInfo = createSkillRestInfo(idInt); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); } return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); @@ -163,7 +163,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(skillRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -173,11 +173,11 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -187,11 +187,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); return; } @@ -203,11 +203,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveSkill(skill); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); } @@ -223,23 +223,23 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { - int idInt = OpenAcdUtilities.getIntFromAttribute(idString); + int idInt = RestUtilities.getIntFromAttribute(idString); skill = m_openAcdContext.getSkillById(idInt); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteSkill(skill); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -288,7 +288,7 @@ private MetadataRestInfo addSkills(List skillsRestInfo OpenAcdSkillRestInfoFull skillRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, skills.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skills.size()); // create list of skill restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -305,7 +305,7 @@ private MetadataRestInfo addSkills(List skillsRestInfo private void sortSkills(List skills) { // sort groups if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index 3ab661996a..9d6a637443 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -38,10 +38,10 @@ import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.permission.Permission; import org.sipfoundry.sipxconfig.permission.PermissionManager; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.SortInfo; -import org.sipfoundry.sipxconfig.rest.OpenAcdUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -114,7 +114,7 @@ public Representation represent(Variant variant) throws ResourceException { permissionRestInfo = createPermissionRestInfo(nameString); } catch (Exception exception) { - return OpenAcdUtilities.getResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_READ_FAILED, "Read permissions failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read permissions failed", exception.getLocalizedMessage()); } return new PermissionRepresentation(variant.getMediaType(), permissionRestInfo); @@ -153,7 +153,7 @@ public void storeRepresentation(Representation entity) throws ResourceException ValidationInfo validationInfo = validate(permissionRestInfo); if (!validationInfo.valid) { - OpenAcdUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); + RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } @@ -166,7 +166,7 @@ public void storeRepresentation(Representation entity) throws ResourceException permission = m_permissionManager.getPermissionByName(nameString); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); return; } @@ -176,11 +176,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_permissionManager.saveCallPermission(permission); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Permission failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Permission failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Permission", permission.getName()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Permission", permission.getName()); return; } @@ -192,11 +192,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_permissionManager.saveCallPermission(permission); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Permission failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Permission failed", exception.getLocalizedMessage()); return; } - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_CREATED, "Created Permission", permission.getName()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Permission", permission.getName()); } @@ -215,19 +215,19 @@ public void removeRepresentations() throws ResourceException { permission = m_permissionManager.getPermissionByName(nameString); } catch (Exception exception) { - OpenAcdUtilities.setResponseError(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Name " + nameString + " not found."); return; } m_permissionManager.deleteCallPermission(permission); - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Permission", permission.getName()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Permission", permission.getName()); return; } // no id string - OpenAcdUtilities.setResponse(getResponse(), OpenAcdUtilities.ResponseCode.ERROR_MISSING_INPUT, "Name value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "Name value missing"); } @@ -257,7 +257,7 @@ private MetadataRestInfo addPermissions(List permissions PermissionRestInfoFull permissionRestInfo; // determine pagination - PaginationInfo paginationInfo = OpenAcdUtilities.calculatePagination(m_form, permissions.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, permissions.size()); // create list of restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { @@ -274,7 +274,7 @@ private MetadataRestInfo addPermissions(List permissions private void sortPermissions(List permissions) { // sort if requested - SortInfo sortInfo = OpenAcdUtilities.calculateSorting(m_form); + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { return; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java new file mode 100644 index 0000000000..2828ef1076 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -0,0 +1,893 @@ +/* + * +l * OpenAcdUtilities.java - Support functionality for OpenAcd Restlets + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import java.io.IOException; +import java.util.List; + +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Response; +import org.restlet.data.Status; +import org.restlet.resource.DomRepresentation; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; +import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; +import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; +import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; +import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class RestUtilities { + + public static int getIntFromAttribute(String attributeString) throws ResourceException { + int intFromAttribute; + + // attempt to parse attribute provided as an id + try { + intFromAttribute = Integer.parseInt(attributeString); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attribute " + attributeString + " invalid."); + } + + return intFromAttribute; + } + + public static PaginationInfo calculatePagination(Form form, int totalResults) { + PaginationInfo paginationInfo = new PaginationInfo(); + paginationInfo.totalResults = totalResults; + + // must specify both PageNumber and ResultsPerPage together + String pageNumberString = form.getFirstValue("page"); + String resultsPerPageString = form.getFirstValue("pagesize"); + + // attempt to parse pagination values from request + try { + paginationInfo.pageNumber = Integer.parseInt(pageNumberString); + paginationInfo.resultsPerPage = Integer.parseInt(resultsPerPageString); + } + catch (Exception exception) { + // default 0 for nothing + paginationInfo.pageNumber = 0; + paginationInfo.resultsPerPage = 0; + } + + // check for outrageous values or lack of parameters + if ((paginationInfo.pageNumber < 1) || (paginationInfo.resultsPerPage < 1)) { + paginationInfo.pageNumber = 0; + paginationInfo.resultsPerPage = 0; + paginationInfo.paginate = false; + } + else { + paginationInfo.paginate = true; + } + + + // do we have to paginate? + if (paginationInfo.paginate) { + paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; + + // check if only one page + // if (resultsPerPage >= totalResults) { + if (paginationInfo.totalPages == 1) { + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + // design decision: should the resultsPerPage actually be set to totalResults? + // since totalResults are already available preserve call value + } + else { + // check if specified page number is on or beyoned last page (then use last page) + if (paginationInfo.pageNumber >= paginationInfo.totalPages) { + paginationInfo.pageNumber = paginationInfo.totalPages; + paginationInfo.startIndex = (paginationInfo.totalPages - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + } + else { + paginationInfo.startIndex = (paginationInfo.pageNumber - 1) * paginationInfo.resultsPerPage; + paginationInfo.endIndex = paginationInfo.startIndex + paginationInfo.resultsPerPage - 1; + } + } + } + else { + // default values assuming no pagination + paginationInfo.startIndex = 0; + paginationInfo.endIndex = paginationInfo.totalResults - 1; + paginationInfo.pageNumber = 1; + paginationInfo.totalPages = 1; + paginationInfo.resultsPerPage = paginationInfo.totalResults; + } + + return paginationInfo; + } + + public static SortInfo calculateSorting(Form form) { + SortInfo sortInfo = new SortInfo(); + + String sortDirectionString = form.getFirstValue("sortdir"); + String sortFieldString = form.getFirstValue("sortby"); + + // check for invalid input + if ((sortDirectionString == null) || (sortFieldString == null)) { + sortInfo.sort = false; + return sortInfo; + } + + if ((sortDirectionString.isEmpty()) || (sortFieldString.isEmpty())) { + sortInfo.sort = false; + return sortInfo; + } + + sortInfo.sort = true; + + // assume forward if get anything else but "reverse" + if (sortDirectionString.toLowerCase().equals("reverse")) { + sortInfo.directionForward = false; + } + else { + sortInfo.directionForward = true; + } + + // tough to type-check this one + sortInfo.sortField = sortFieldString; + + return sortInfo; + } + + + // XML Response functions + // ---------------------- + + public static void setResponse(Response response, ResponseCode code, String message) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // no related data (create function overloads to modify) + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void setResponse(Response response, ResponseCode code, String message, int id) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add related data + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("id"); + elementId.appendChild(doc.createTextNode(String.valueOf(id))); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void setResponse(Response response, ResponseCode code, String message, String id) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add related data + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("id"); + elementId.appendChild(doc.createTextNode(id)); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void setResponseError(Response response, ResponseCode code, String message) { + Representation representation = getResponseError(response, code, message); + + response.setEntity(representation); + } + + public static Representation getResponseError(Response response, ResponseCode code, String message) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + public static void setResponseError(Response response, ResponseCode code, String message, String additionalMessage) { + Representation representation = getResponseError(response, code, message, additionalMessage); + + response.setEntity(representation); + } + + public static Representation getResponseError(Response response, ResponseCode code, String message, String additionalMessage) { + try { + DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); + Document doc = representation.getDocument(); + + // set response status + setResponseStatus(response, code); + + // create root node + Element elementResponse = doc.createElement("response"); + doc.appendChild(elementResponse); + + setResponseHeader(doc, elementResponse, code, message); + + // add related data + Element elementData = doc.createElement("data"); + Element elementId = doc.createElement("additionalMessage"); + elementId.appendChild(doc.createTextNode(additionalMessage)); + elementData.appendChild(elementId); + elementResponse.appendChild(elementData); + + return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); + + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + + private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { + + // add standard elements + Element elementCode = doc.createElement("code"); + elementCode.appendChild(doc.createTextNode(code.toString())); + elementResponse.appendChild(elementCode); + + Element elementMessage = doc.createElement("message"); + elementMessage.appendChild(doc.createTextNode(message)); + elementResponse.appendChild(elementMessage); + } + + private static void setResponseStatus(Response response, ResponseCode code) { + // set response status based on code + switch (code) { + case SUCCESS_CREATED: + response.setStatus(Status.SUCCESS_CREATED); + break; + + case ERROR_MISSING_INPUT: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + + case ERROR_BAD_INPUT: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + + case ERROR_WRITE_FAILED: + response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + break; + + case ERROR_READ_FAILED: + response.setStatus(Status.SERVER_ERROR_INTERNAL); + break; + + default: + response.setStatus(Status.SUCCESS_OK); + } + } + + public static enum ResponseCode { + SUCCESS, SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED, ERROR_READ_FAILED + } + + + // Data objects + // ------------ + + public static class PaginationInfo { + Boolean paginate = false; + int pageNumber = 0; + int resultsPerPage = 0; + int totalPages = 0; + int totalResults = 0; + int startIndex = 0; + int endIndex = 0; + } + + public static class SortInfo { + Boolean sort = false; + Boolean directionForward = true; + String sortField = ""; + } + + public static class ValidationInfo { + Boolean valid = true; + String message = "Valid"; + ResponseCode responseCode = ResponseCode.SUCCESS; + } + + + // Common Rest Info objects + // ------------------------ + + static class MetadataRestInfo { + private final int m_totalResults; + private final int m_currentPage; + private final int m_totalPages; + private final int m_resultsPerPage; + + public MetadataRestInfo(PaginationInfo paginationInfo) { + m_totalResults = paginationInfo.totalResults; + m_currentPage = paginationInfo.pageNumber; + m_totalPages = paginationInfo.totalPages; + m_resultsPerPage = paginationInfo.resultsPerPage; + } + + public int getTotalResults() { + return m_totalResults; + } + + public int getCurrentPage() { + return m_currentPage; + } + + public int getTotalPages() { + return m_totalPages; + } + + public int getResultsPerPage() { + return m_resultsPerPage; + } + } + + static class OpenAcdSkillRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdSkillRestInfo(OpenAcdSkill skill) { + m_id = skill.getId(); + m_name = skill.getName(); + m_description = skill.getDescription(); + m_groupName = skill.getGroupName(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + static class OpenAcdSkillRestInfoFull extends OpenAcdSkillRestInfo { + private final String m_atom; + private final int m_groupId; + + public OpenAcdSkillRestInfoFull(OpenAcdSkill skill) { + super(skill); + m_atom = skill.getAtom(); + m_groupId = skill.getGroup().getId(); + } + + public String getAtom() { + return m_atom; + } + + public int getGroupId() { + return m_groupId; + } + } + + static class OpenAcdSkillGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdSkillGroupRestInfo(OpenAcdSkillGroup skillGroup) { + m_id = skillGroup.getId(); + m_name = skillGroup.getName(); + m_description = skillGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + static class OpenAcdQueueRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_groupName; + + public OpenAcdQueueRestInfo(OpenAcdQueue queue) { + m_id = queue.getId(); + m_name = queue.getName(); + m_description = queue.getDescription(); + m_groupName = queue.getQueueGroup(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getGroupName() { + return m_groupName; + } + } + + static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { + private final int m_groupId; + private final int m_weight; + private final List m_skills; + private final List m_agentGroups; + private final List m_steps; + + public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups, List steps) { + super(queue); + m_groupId = queue.getGroup().getId(); + m_weight = queue.getWeight(); + m_skills = skills; + m_agentGroups = agentGroups; + m_steps = steps; + } + + public int getGroupId() { + return m_groupId; + } + + public int getWeight() { + return m_weight; + } + + public List getSkills() { + return m_skills; + } + + public List getAgentGroups() { + return m_agentGroups; + } + + public List getSteps() { + return m_steps; + } + } + + + static class OpenAcdQueueGroupRestInfoFull { + private final String m_name; + private final int m_id; + private final String m_description; + private final List m_skills; + private final List m_agentGroups; + private final List m_steps; + + public OpenAcdQueueGroupRestInfoFull(OpenAcdQueueGroup queueGroup, List skills, List agentGroups, List steps) { + m_name = queueGroup.getName(); + m_id = queueGroup.getId(); + m_description = queueGroup.getDescription(); + m_skills = skills; + m_agentGroups = agentGroups; + m_steps = steps; + } + + public String getName() { + return m_name; + } + + public int getId() { + return m_id; + } + + public String getDescription() { + return m_description; + } + + public List getSkills() { + return m_skills; + } + + public List getAgentGroups() { + return m_agentGroups; + } + + public List getSteps() { + return m_steps; + } + } + + static class OpenAcdRecipeActionRestInfo { + private final String m_action; + private final String m_actionValue; + private final List m_skills; + + public OpenAcdRecipeActionRestInfo(OpenAcdRecipeAction action, List skills) { + m_action = action.getAction(); + m_actionValue = action.getActionValue(); + m_skills = skills; + } + + public String getAction() { + return m_action; + } + + public String getActionValue() { + return m_actionValue; + } + + public List getSkills() { + return m_skills; + } + } + + static class OpenAcdRecipeStepRestInfo { + private final int m_id; + private final List m_conditions; + private final OpenAcdRecipeActionRestInfo m_action; + private final String m_frequency; + + public OpenAcdRecipeStepRestInfo(OpenAcdRecipeStep step, OpenAcdRecipeActionRestInfo recipeActionRestInfo, List conditions) { + m_id = step.getId(); + m_conditions = conditions; + m_action = recipeActionRestInfo; + m_frequency = step.getFrequency(); + } + + public int getId() { + return m_id; + } + + public List getConditions() { + return m_conditions; + } + + public OpenAcdRecipeActionRestInfo getAction() { + return m_action; + } + + public String getFrequency() { + return m_frequency; + } + } + + static class OpenAcdRecipeConditionRestInfo { + private final String m_condition; + private final String m_relation; + private final String m_valueCondition; + + public OpenAcdRecipeConditionRestInfo(OpenAcdRecipeCondition condition) { + m_condition = condition.getCondition(); + m_relation = condition.getRelation(); + m_valueCondition = condition.getValueCondition(); + } + + public String getCondition() { + return m_condition; + } + + public String getRelation() { + return m_relation; + } + + public String getValueCondition() { + return m_valueCondition; + } + } + + static class OpenAcdClientRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + private final String m_identity; + + public OpenAcdClientRestInfo(OpenAcdClient client) { + m_id = client.getId(); + m_name = client.getName(); + m_description = client.getDescription(); + m_identity = client.getIdentity(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public String getIdentity() { + return m_identity; + } + } + + static class OpenAcdAgentGroupRestInfo { + private final int m_id; + private final String m_name; + private final String m_description; + + public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup) { + m_id = agentGroup.getId(); + m_name = agentGroup.getName(); + m_description = agentGroup.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + } + + static class OpenAcdAgentGroupRestInfoFull extends OpenAcdAgentGroupRestInfo { + private final List m_skills; + private final List m_queues; + private final List m_clients; + + public OpenAcdAgentGroupRestInfoFull(OpenAcdAgentGroup agentGroup, List skills, List queues, List clients) { + super(agentGroup); + m_skills = skills; + m_queues = queues; + m_clients = clients; + } + + public List getSkills() { + return m_skills; + } + + public List getQueues() { + return m_queues; + } + + public List getClients() { + return m_clients; + } + } + + static class OpenAcdReleaseCodeRestInfo { + private final int m_id; + private final String m_label; + private final String m_description; + private final int m_bias; + + public OpenAcdReleaseCodeRestInfo(OpenAcdReleaseCode releaseCode) { + m_id = releaseCode.getId(); + m_label = releaseCode.getLabel(); + m_bias = releaseCode.getBias(); + m_description = releaseCode.getDescription(); + } + + public int getId() { + return m_id; + } + + public String getLabel() { + return m_label; + } + + public String getDescription() { + return m_description; + } + + public int getBias() { + return m_bias; + } + } + + static class OpenAcdAgentRestInfoFull { + + private final int m_id; + private final int m_userId; + private final String m_userName; + private final String m_firstName; + private final String m_lastName; + private final int m_groupId; + private final String m_groupName; + private final String m_security; + private final String m_password; + private final List m_skills; + private final List m_queues; + private final List m_clients; + + public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List skills, List queues, List clients) { + m_id = agent.getId(); + m_firstName = agent.getFirstName(); + m_lastName = agent.getLastName(); + m_userId = agent.getUser().getId(); + m_userName = agent.getUser().getName(); + m_groupId = agent.getGroup().getId(); + m_groupName = agent.getGroup().getName(); + m_security = agent.getSecurity(); + m_password = ""; // only used on updates, not rest get + m_skills = skills; + m_queues = queues; + m_clients = clients; + } + + public int getId() { + return m_id; + } + + public String getFirstName() { + return m_firstName; + } + + public String getLastName() { + return m_lastName; + } + + public int getUserId() { + return m_userId; + } + + public String getUserName() { + return m_userName; + } + + public int getGroupId() { + return m_groupId; + } + + public String getGroupName() { + return m_groupName; + } + + public String getSecurity() { + return m_security; + } + + public String getPassword() { + return m_password; + } + + public List getSkills() { + return m_skills; + } + + public List getQueues() { + return m_queues; + } + + public List getClients() { + return m_clients; + } + } + + static class OpenAcdSettingRestInfo { + private final int m_id; + private final String m_logLevel; + + public OpenAcdSettingRestInfo(OpenAcdSettings setting) { + m_id = setting.getId(); + m_logLevel = setting.getLogLevel(); + } + + public int getId() { + return m_id; + } + + public String getLogLevel() { + return m_logLevel; + } + } +} From 7e0ee1e9c4914d3bfde9c22e9fd2c28221f2575a Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 15:27:48 -0400 Subject: [PATCH 63/99] Change OpenAcdUtilities to be generic RestUtilities --- .../sipxconfig/rest/OpenAcdUtilities.java | 893 ------------------ .../sipxconfig/rest/PermissionsResource.java | 43 +- .../sipxconfig/rest/RestUtilities.java | 47 + 3 files changed, 48 insertions(+), 935 deletions(-) delete mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java deleted file mode 100644 index fe3843cc37..0000000000 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdUtilities.java +++ /dev/null @@ -1,893 +0,0 @@ -/* - * -l * OpenAcdUtilities.java - Support functionality for OpenAcd Restlets - * Copyright (C) 2012 PATLive, D. Chang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -package org.sipfoundry.sipxconfig.rest; - -import java.io.IOException; -import java.util.List; - -import org.restlet.data.Form; -import org.restlet.data.MediaType; -import org.restlet.data.Response; -import org.restlet.data.Status; -import org.restlet.resource.DomRepresentation; -import org.restlet.resource.Representation; -import org.restlet.resource.ResourceException; -import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; -import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueue; -import org.sipfoundry.sipxconfig.openacd.OpenAcdQueueGroup; -import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeAction; -import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; -import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; -import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -public class OpenAcdUtilities { - - public static int getIntFromAttribute(String attributeString) throws ResourceException { - int intFromAttribute; - - // attempt to parse attribute provided as an id - try { - intFromAttribute = Integer.parseInt(attributeString); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attribute " + attributeString + " invalid."); - } - - return intFromAttribute; - } - - public static PaginationInfo calculatePagination(Form form, int totalResults) { - PaginationInfo paginationInfo = new PaginationInfo(); - paginationInfo.totalResults = totalResults; - - // must specify both PageNumber and ResultsPerPage together - String pageNumberString = form.getFirstValue("page"); - String resultsPerPageString = form.getFirstValue("pagesize"); - - // attempt to parse pagination values from request - try { - paginationInfo.pageNumber = Integer.parseInt(pageNumberString); - paginationInfo.resultsPerPage = Integer.parseInt(resultsPerPageString); - } - catch (Exception exception) { - // default 0 for nothing - paginationInfo.pageNumber = 0; - paginationInfo.resultsPerPage = 0; - } - - // check for outrageous values or lack of parameters - if ((paginationInfo.pageNumber < 1) || (paginationInfo.resultsPerPage < 1)) { - paginationInfo.pageNumber = 0; - paginationInfo.resultsPerPage = 0; - paginationInfo.paginate = false; - } - else { - paginationInfo.paginate = true; - } - - - // do we have to paginate? - if (paginationInfo.paginate) { - paginationInfo.totalPages = ((paginationInfo.totalResults - 1) / paginationInfo.resultsPerPage) + 1; - - // check if only one page - // if (resultsPerPage >= totalResults) { - if (paginationInfo.totalPages == 1) { - paginationInfo.startIndex = 0; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - paginationInfo.pageNumber = 1; - // design decision: should the resultsPerPage actually be set to totalResults? - // since totalResults are already available preserve call value - } - else { - // check if specified page number is on or beyoned last page (then use last page) - if (paginationInfo.pageNumber >= paginationInfo.totalPages) { - paginationInfo.pageNumber = paginationInfo.totalPages; - paginationInfo.startIndex = (paginationInfo.totalPages - 1) * paginationInfo.resultsPerPage; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - } - else { - paginationInfo.startIndex = (paginationInfo.pageNumber - 1) * paginationInfo.resultsPerPage; - paginationInfo.endIndex = paginationInfo.startIndex + paginationInfo.resultsPerPage - 1; - } - } - } - else { - // default values assuming no pagination - paginationInfo.startIndex = 0; - paginationInfo.endIndex = paginationInfo.totalResults - 1; - paginationInfo.pageNumber = 1; - paginationInfo.totalPages = 1; - paginationInfo.resultsPerPage = paginationInfo.totalResults; - } - - return paginationInfo; - } - - public static SortInfo calculateSorting(Form form) { - SortInfo sortInfo = new SortInfo(); - - String sortDirectionString = form.getFirstValue("sortdir"); - String sortFieldString = form.getFirstValue("sortby"); - - // check for invalid input - if ((sortDirectionString == null) || (sortFieldString == null)) { - sortInfo.sort = false; - return sortInfo; - } - - if ((sortDirectionString.isEmpty()) || (sortFieldString.isEmpty())) { - sortInfo.sort = false; - return sortInfo; - } - - sortInfo.sort = true; - - // assume forward if get anything else but "reverse" - if (sortDirectionString.toLowerCase().equals("reverse")) { - sortInfo.directionForward = false; - } - else { - sortInfo.directionForward = true; - } - - // tough to type-check this one - sortInfo.sortField = sortFieldString; - - return sortInfo; - } - - - // XML Response functions - // ---------------------- - - public static void setResponse(Response response, ResponseCode code, String message) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, code); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, code, message); - - // no related data (create function overloads to modify) - - response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); - - } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static void setResponse(Response response, ResponseCode code, String message, int id) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, code); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, code, message); - - // add related data - Element elementData = doc.createElement("data"); - Element elementId = doc.createElement("id"); - elementId.appendChild(doc.createTextNode(String.valueOf(id))); - elementData.appendChild(elementId); - elementResponse.appendChild(elementData); - - response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); - - } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static void setResponse(Response response, ResponseCode code, String message, String id) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, code); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, code, message); - - // add related data - Element elementData = doc.createElement("data"); - Element elementId = doc.createElement("id"); - elementId.appendChild(doc.createTextNode(id)); - elementData.appendChild(elementId); - elementResponse.appendChild(elementData); - - response.setEntity(new DomRepresentation(MediaType.TEXT_XML, doc)); - - } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static void setResponseError(Response response, ResponseCode code, String message) { - Representation representation = getResponseError(response, code, message); - - response.setEntity(representation); - } - - public static Representation getResponseError(Response response, ResponseCode code, String message) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, code); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, code, message); - - return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); - - } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return null; - } - - public static void setResponseError(Response response, ResponseCode code, String message, String additionalMessage) { - Representation representation = getResponseError(response, code, message, additionalMessage); - - response.setEntity(representation); - } - - public static Representation getResponseError(Response response, ResponseCode code, String message, String additionalMessage) { - try { - DomRepresentation representation = new DomRepresentation(MediaType.TEXT_XML); - Document doc = representation.getDocument(); - - // set response status - setResponseStatus(response, code); - - // create root node - Element elementResponse = doc.createElement("response"); - doc.appendChild(elementResponse); - - setResponseHeader(doc, elementResponse, code, message); - - // add related data - Element elementData = doc.createElement("data"); - Element elementId = doc.createElement("additionalMessage"); - elementId.appendChild(doc.createTextNode(additionalMessage)); - elementData.appendChild(elementId); - elementResponse.appendChild(elementData); - - return representation; // new DomRepresentation(MediaType.TEXT_XML, doc); - - } - catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return null; - } - - private static void setResponseHeader(Document doc, Element elementResponse, ResponseCode code, String message) { - - // add standard elements - Element elementCode = doc.createElement("code"); - elementCode.appendChild(doc.createTextNode(code.toString())); - elementResponse.appendChild(elementCode); - - Element elementMessage = doc.createElement("message"); - elementMessage.appendChild(doc.createTextNode(message)); - elementResponse.appendChild(elementMessage); - } - - private static void setResponseStatus(Response response, ResponseCode code) { - // set response status based on code - switch (code) { - case SUCCESS_CREATED: - response.setStatus(Status.SUCCESS_CREATED); - break; - - case ERROR_MISSING_INPUT: - response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); - break; - - case ERROR_BAD_INPUT: - response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); - break; - - case ERROR_WRITE_FAILED: - response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); - break; - - case ERROR_READ_FAILED: - response.setStatus(Status.SERVER_ERROR_INTERNAL); - break; - - default: - response.setStatus(Status.SUCCESS_OK); - } - } - - public static enum ResponseCode { - SUCCESS, SUCCESS_CREATED, SUCCESS_UPDATED, SUCCESS_DELETED, ERROR_MISSING_INPUT, ERROR_BAD_INPUT, ERROR_WRITE_FAILED, ERROR_READ_FAILED - } - - - // Data objects - // ------------ - - public static class PaginationInfo { - Boolean paginate = false; - int pageNumber = 0; - int resultsPerPage = 0; - int totalPages = 0; - int totalResults = 0; - int startIndex = 0; - int endIndex = 0; - } - - public static class SortInfo { - Boolean sort = false; - Boolean directionForward = true; - String sortField = ""; - } - - public static class ValidationInfo { - Boolean valid = true; - String message = "Valid"; - ResponseCode responseCode = ResponseCode.SUCCESS; - } - - - // Common Rest Info objects - // ------------------------ - - static class MetadataRestInfo { - private final int m_totalResults; - private final int m_currentPage; - private final int m_totalPages; - private final int m_resultsPerPage; - - public MetadataRestInfo(PaginationInfo paginationInfo) { - m_totalResults = paginationInfo.totalResults; - m_currentPage = paginationInfo.pageNumber; - m_totalPages = paginationInfo.totalPages; - m_resultsPerPage = paginationInfo.resultsPerPage; - } - - public int getTotalResults() { - return m_totalResults; - } - - public int getCurrentPage() { - return m_currentPage; - } - - public int getTotalPages() { - return m_totalPages; - } - - public int getResultsPerPage() { - return m_resultsPerPage; - } - } - - static class OpenAcdSkillRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdSkillRestInfo(OpenAcdSkill skill) { - m_id = skill.getId(); - m_name = skill.getName(); - m_description = skill.getDescription(); - m_groupName = skill.getGroupName(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - - static class OpenAcdSkillRestInfoFull extends OpenAcdSkillRestInfo { - private final String m_atom; - private final int m_groupId; - - public OpenAcdSkillRestInfoFull(OpenAcdSkill skill) { - super(skill); - m_atom = skill.getAtom(); - m_groupId = skill.getGroup().getId(); - } - - public String getAtom() { - return m_atom; - } - - public int getGroupId() { - return m_groupId; - } - } - - static class OpenAcdSkillGroupRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - - public OpenAcdSkillGroupRestInfo(OpenAcdSkillGroup skillGroup) { - m_id = skillGroup.getId(); - m_name = skillGroup.getName(); - m_description = skillGroup.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - } - - static class OpenAcdQueueRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_groupName; - - public OpenAcdQueueRestInfo(OpenAcdQueue queue) { - m_id = queue.getId(); - m_name = queue.getName(); - m_description = queue.getDescription(); - m_groupName = queue.getQueueGroup(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getGroupName() { - return m_groupName; - } - } - - static class OpenAcdQueueRestInfoFull extends OpenAcdQueueRestInfo { - private final int m_groupId; - private final int m_weight; - private final List m_skills; - private final List m_agentGroups; - private final List m_steps; - - public OpenAcdQueueRestInfoFull(OpenAcdQueue queue, List skills, List agentGroups, List steps) { - super(queue); - m_groupId = queue.getGroup().getId(); - m_weight = queue.getWeight(); - m_skills = skills; - m_agentGroups = agentGroups; - m_steps = steps; - } - - public int getGroupId() { - return m_groupId; - } - - public int getWeight() { - return m_weight; - } - - public List getSkills() { - return m_skills; - } - - public List getAgentGroups() { - return m_agentGroups; - } - - public List getSteps() { - return m_steps; - } - } - - - static class OpenAcdQueueGroupRestInfoFull { - private final String m_name; - private final int m_id; - private final String m_description; - private final List m_skills; - private final List m_agentGroups; - private final List m_steps; - - public OpenAcdQueueGroupRestInfoFull(OpenAcdQueueGroup queueGroup, List skills, List agentGroups, List steps) { - m_name = queueGroup.getName(); - m_id = queueGroup.getId(); - m_description = queueGroup.getDescription(); - m_skills = skills; - m_agentGroups = agentGroups; - m_steps = steps; - } - - public String getName() { - return m_name; - } - - public int getId() { - return m_id; - } - - public String getDescription() { - return m_description; - } - - public List getSkills() { - return m_skills; - } - - public List getAgentGroups() { - return m_agentGroups; - } - - public List getSteps() { - return m_steps; - } - } - - static class OpenAcdRecipeActionRestInfo { - private final String m_action; - private final String m_actionValue; - private final List m_skills; - - public OpenAcdRecipeActionRestInfo(OpenAcdRecipeAction action, List skills) { - m_action = action.getAction(); - m_actionValue = action.getActionValue(); - m_skills = skills; - } - - public String getAction() { - return m_action; - } - - public String getActionValue() { - return m_actionValue; - } - - public List getSkills() { - return m_skills; - } - } - - static class OpenAcdRecipeStepRestInfo { - private final int m_id; - private final List m_conditions; - private final OpenAcdRecipeActionRestInfo m_action; - private final String m_frequency; - - public OpenAcdRecipeStepRestInfo(OpenAcdRecipeStep step, OpenAcdRecipeActionRestInfo recipeActionRestInfo, List conditions) { - m_id = step.getId(); - m_conditions = conditions; - m_action = recipeActionRestInfo; - m_frequency = step.getFrequency(); - } - - public int getId() { - return m_id; - } - - public List getConditions() { - return m_conditions; - } - - public OpenAcdRecipeActionRestInfo getAction() { - return m_action; - } - - public String getFrequency() { - return m_frequency; - } - } - - static class OpenAcdRecipeConditionRestInfo { - private final String m_condition; - private final String m_relation; - private final String m_valueCondition; - - public OpenAcdRecipeConditionRestInfo(OpenAcdRecipeCondition condition) { - m_condition = condition.getCondition(); - m_relation = condition.getRelation(); - m_valueCondition = condition.getValueCondition(); - } - - public String getCondition() { - return m_condition; - } - - public String getRelation() { - return m_relation; - } - - public String getValueCondition() { - return m_valueCondition; - } - } - - static class OpenAcdClientRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - private final String m_identity; - - public OpenAcdClientRestInfo(OpenAcdClient client) { - m_id = client.getId(); - m_name = client.getName(); - m_description = client.getDescription(); - m_identity = client.getIdentity(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - - public String getIdentity() { - return m_identity; - } - } - - static class OpenAcdAgentGroupRestInfo { - private final int m_id; - private final String m_name; - private final String m_description; - - public OpenAcdAgentGroupRestInfo(OpenAcdAgentGroup agentGroup) { - m_id = agentGroup.getId(); - m_name = agentGroup.getName(); - m_description = agentGroup.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getName() { - return m_name; - } - - public String getDescription() { - return m_description; - } - } - - static class OpenAcdAgentGroupRestInfoFull extends OpenAcdAgentGroupRestInfo { - private final List m_skills; - private final List m_queues; - private final List m_clients; - - public OpenAcdAgentGroupRestInfoFull(OpenAcdAgentGroup agentGroup, List skills, List queues, List clients) { - super(agentGroup); - m_skills = skills; - m_queues = queues; - m_clients = clients; - } - - public List getSkills() { - return m_skills; - } - - public List getQueues() { - return m_queues; - } - - public List getClients() { - return m_clients; - } - } - - static class OpenAcdReleaseCodeRestInfo { - private final int m_id; - private final String m_label; - private final String m_description; - private final int m_bias; - - public OpenAcdReleaseCodeRestInfo(OpenAcdReleaseCode releaseCode) { - m_id = releaseCode.getId(); - m_label = releaseCode.getLabel(); - m_bias = releaseCode.getBias(); - m_description = releaseCode.getDescription(); - } - - public int getId() { - return m_id; - } - - public String getLabel() { - return m_label; - } - - public String getDescription() { - return m_description; - } - - public int getBias() { - return m_bias; - } - } - - static class OpenAcdAgentRestInfoFull { - - private final int m_id; - private final int m_userId; - private final String m_userName; - private final String m_firstName; - private final String m_lastName; - private final int m_groupId; - private final String m_groupName; - private final String m_security; - private final String m_password; - private final List m_skills; - private final List m_queues; - private final List m_clients; - - public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List skills, List queues, List clients) { - m_id = agent.getId(); - m_firstName = agent.getFirstName(); - m_lastName = agent.getLastName(); - m_userId = agent.getUser().getId(); - m_userName = agent.getUser().getName(); - m_groupId = agent.getGroup().getId(); - m_groupName = agent.getGroup().getName(); - m_security = agent.getSecurity(); - m_password = ""; // only used on updates, not rest get - m_skills = skills; - m_queues = queues; - m_clients = clients; - } - - public int getId() { - return m_id; - } - - public String getFirstName() { - return m_firstName; - } - - public String getLastName() { - return m_lastName; - } - - public int getUserId() { - return m_userId; - } - - public String getUserName() { - return m_userName; - } - - public int getGroupId() { - return m_groupId; - } - - public String getGroupName() { - return m_groupName; - } - - public String getSecurity() { - return m_security; - } - - public String getPassword() { - return m_password; - } - - public List getSkills() { - return m_skills; - } - - public List getQueues() { - return m_queues; - } - - public List getClients() { - return m_clients; - } - } - - static class OpenAcdSettingRestInfo { - private final int m_id; - private final String m_logLevel; - - public OpenAcdSettingRestInfo(OpenAcdSettings setting) { - m_id = setting.getId(); - m_logLevel = setting.getLogLevel(); - } - - public int getId() { - return m_id; - } - - public String getLogLevel() { - return m_logLevel; - } - } -} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index 9d6a637443..c79768202f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -40,6 +40,7 @@ import org.sipfoundry.sipxconfig.permission.PermissionManager; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PermissionRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -425,48 +426,6 @@ public List getPermissions() { } } - static class PermissionRestInfoFull { - private final String m_name; - private final String m_label; - private final String m_description; - private final boolean m_defaultValue; - private final Permission.Type m_type; - private final boolean m_builtIn; - - public PermissionRestInfoFull(Permission permission) { - m_name = permission.getName(); - m_label = permission.getLabel(); - m_description = permission.getDescription(); - m_defaultValue = permission.getDefaultValue(); - m_type = permission.getType(); - m_builtIn = permission.isBuiltIn(); - } - - public String getName() { - return m_name; - } - - public String getLabel() { - return m_label; - } - - public String getDescription() { - return m_description; - } - - public boolean getDefaultValue() { - return m_defaultValue; - } - - public Permission.Type getType() { - return m_type; - } - - public boolean getBuiltIn() { - return m_builtIn; - } - } - // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 2828ef1076..87fe49baae 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -42,6 +42,7 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.permission.Permission; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -394,6 +395,52 @@ public static class ValidationInfo { // Common Rest Info objects // ------------------------ + static class PermissionRestInfoFull { + private final String m_name; + private final String m_label; + private final String m_description; + private final boolean m_defaultValue; + private final Permission.Type m_type; + private final boolean m_builtIn; + + public PermissionRestInfoFull(Permission permission) { + m_name = permission.getName(); + m_label = permission.getLabel(); + m_description = permission.getDescription(); + m_defaultValue = permission.getDefaultValue(); + m_type = permission.getType(); + m_builtIn = permission.isBuiltIn(); + } + + public String getName() { + return m_name; + } + + public String getLabel() { + return m_label; + } + + public String getDescription() { + return m_description; + } + + public boolean getDefaultValue() { + return m_defaultValue; + } + + public Permission.Type getType() { + return m_type; + } + + public boolean getBuiltIn() { + return m_builtIn; + } + } + + + // Common OpenACD Rest Info objects + // ------------------------ + static class MetadataRestInfo { private final int m_totalResults; private final int m_currentPage; From 17ac2f481712a4f35f87255b9abef57e6c3e22ad Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 15:47:44 -0400 Subject: [PATCH 64/99] Clean up sort enum --- .../src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index c79768202f..2b3b2edfab 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -54,7 +54,7 @@ public class PermissionsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, ATOM, NONE; + NAME, DESCRIPTION, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { From f674a687269eb35de46e74779b0b58a60a176de9 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 10 Apr 2012 18:14:39 -0400 Subject: [PATCH 65/99] Add Branch REST API, start User and User Group REST API --- .../sipxconfig/rest/BranchesResource.java | 467 +++++++++++++++ .../rest/OpenAcdQueueGroupsResource.java | 3 - .../sipxconfig/rest/RestUtilities.java | 75 +++ .../sipxconfig/rest/UserGroupsResource.java | 508 ++++++++++++++++ .../sipxconfig/rest/UsersResource.java | 553 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 6 files changed, 1619 insertions(+), 3 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java new file mode 100644 index 0000000000..1be36774db --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java @@ -0,0 +1,467 @@ +/* + * + * BranchesResource.java - A Restlet to read Branch data from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.branch.Branch; +import org.sipfoundry.sipxconfig.branch.BranchManager; +import org.sipfoundry.sipxconfig.phonebook.Address; +import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class BranchesResource extends UserResource { + + private BranchManager m_branchManager; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Branch + // ------------------------------------ + + @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 branches = m_branchManager.getBranches(); + List branchesRestInfo = new ArrayList(); + 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); + } + + + // PUT - Update or Add single Branch + // --------------------------------- + + @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()); + } + + + // DELETE - Delete single Branch + // ----------------------------- + + @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 branchIds = new ArrayList(); + 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"); + } + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(BranchRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + + private BranchRestInfoFull createBranchRestInfo(int id) throws ResourceException { + BranchRestInfoFull branchRestInfo = null; + + Branch branch = m_branchManager.getBranch(id); + branchRestInfo = new BranchRestInfoFull(branch); + + return branchRestInfo; + } + + private MetadataRestInfo addBranches(List branchesRestInfo, List branches) { + BranchRestInfoFull branchRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, branches.size()); + + // create list of skill restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + Branch branch = branches.get(index); + + branchRestInfo = new BranchRestInfoFull(branch); + branchesRestInfo.add(branchRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortBranches(List branches) { + // sort if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return branch1.getName().compareToIgnoreCase(branch2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return branch1.getDescription().compareToIgnoreCase(branch2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return branch2.getName().compareToIgnoreCase(branch1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return branch2.getDescription().compareToIgnoreCase(branch1.getDescription()); + } + + }); + break; + } + } + } + + 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); + } + + 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 Address getAddress(BranchRestInfoFull branchRestInfo) { + Address address = new Address(); + + address.setCity(branchRestInfo.getAddress().getCity()); + address.setCountry(branchRestInfo.getAddress().getCountry()); + address.setOfficeDesignation(branchRestInfo.getAddress().getOfficeDesignation()); + address.setState(branchRestInfo.getAddress().getState()); + address.setStreet(branchRestInfo.getAddress().getStreet()); + address.setZip(branchRestInfo.getAddress().getZip()); + + return address; + } + + + // REST Representations + // -------------------- + + static class BranchesRepresentation extends XStreamRepresentation { + + public BranchesRepresentation(MediaType mediaType, BranchesBundleRestInfo object) { + super(mediaType, object); + } + + public BranchesRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("branch", BranchesBundleRestInfo.class); + xstream.alias("branch", BranchRestInfoFull.class); + } + } + + static class BranchRepresentation extends XStreamRepresentation { + + public BranchRepresentation(MediaType mediaType, BranchRestInfoFull object) { + super(mediaType, object); + } + + public BranchRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("branch", BranchRestInfoFull.class); + } + } + + + // REST info objects + // ----------------- + + static class BranchesBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_branches; + + public BranchesBundleRestInfo(List branches, MetadataRestInfo metadata) { + m_metadata = metadata; + m_branches = branches; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getBranches() { + return m_branches; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setBranchManager(BranchManager branchManager) { + m_branchManager = branchManager; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 53a6e08c37..848b22c7e2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -471,9 +471,6 @@ private void updateQueueGroup(OpenAcdQueueGroup queueGroup, OpenAcdQueueGroupRes queueGroup.setDescription(queueGroupRestInfo.getDescription()); - // remove all current skills - queueGroup.getSkills().clear(); - addLists(queueGroup, queueGroupRestInfo); } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 87fe49baae..54489cf41d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -30,6 +30,7 @@ import org.restlet.resource.DomRepresentation; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; +import org.sipfoundry.sipxconfig.branch.Branch; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; @@ -43,6 +44,8 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.sipfoundry.sipxconfig.permission.Permission; +import org.sipfoundry.sipxconfig.phonebook.Address; +import org.sipfoundry.sipxconfig.setting.Group; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -437,6 +440,78 @@ public boolean getBuiltIn() { } } + static class BranchRestInfoFull { + private final int m_id; + private final String m_name; + private final String m_description; + private final Address m_address; + private final String m_phoneNumber; + private final String m_faxNumber; + + public BranchRestInfoFull(Branch branch) { + m_id = branch.getId(); + m_name = branch.getName(); + m_description = branch.getDescription(); + m_address = branch.getAddress(); + m_phoneNumber = branch.getPhoneNumber(); + m_faxNumber = branch.getFaxNumber(); + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public Address getAddress() { + return m_address; + } + + public String getPhoneNumber() { + return m_phoneNumber; + } + + public String getFaxNumber() { + return m_faxNumber; + } + } + + static class UserGroupRestInfoFull { + private final int m_id; + private final String m_name; + private final String m_description; + private final BranchRestInfoFull m_branch; + + public UserGroupRestInfoFull(Group userGroup, BranchRestInfoFull branchRestInfo) { + m_id = userGroup.getId(); + m_name = userGroup.getName(); + m_description = userGroup.getDescription(); + m_branch = branchRestInfo; + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public String getDescription() { + return m_description; + } + + public BranchRestInfoFull getBranch() { + return m_branch; + } + } + // Common OpenACD Rest Info objects // ------------------------ diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java new file mode 100644 index 0000000000..7a29e3bb3e --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java @@ -0,0 +1,508 @@ +/* + * + * UserGroupsResource.java - A Restlet to read User Group data from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillsBundleRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.rest.UsersResource.UserRestInfoFull; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class UserGroupsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + NAME, DESCRIPTION, ATOM, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + OpenAcdSkillRestInfoFull skillRestInfo = 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 { + skillRestInfo = createSkillRestInfo(idInt); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + } + + return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); + } + + + // if not single, process request for all + List skills = m_openAcdContext.getSkills(); + List skillsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortSkills(skills); + + // set requested agents groups and get resulting metadata + metadataRestInfo = addSkills(skillsRestInfo, skills); + + // create final restinfo + OpenAcdSkillsBundleRestInfo skillsBundleRestInfo = new OpenAcdSkillsBundleRestInfo(skillsRestInfo, metadataRestInfo); + + return new OpenAcdSkillsRepresentation(variant.getMediaType(), skillsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); + OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); + OpenAcdSkill skill = null; + + // validate input for update or create + ValidationInfo validationInfo = validate(skillRestInfo); + + 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); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateSkill(skill, skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + + return; + } + + + // otherwise add new + try { + skill = createSkill(skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSkill skill; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = RestUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteSkill(skill); + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + + return; + } + + // no id string + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + String name = restInfo.getName(); + String atom = restInfo.getAtom(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < atom.length(); i++) { + if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + return validationInfo; + } + + private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { + OpenAcdSkillRestInfoFull skillRestInfo = null; + + OpenAcdSkill skill = m_openAcdContext.getSkillById(id); + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + + return skillRestInfo; + } + + private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { + OpenAcdSkillRestInfoFull skillRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skills.size()); + + // create list of skill restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdSkill skill = skills.get(index); + + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + skillsRestInfo.add(skillRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortSkills(List skills) { + // sort groups if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getName().compareToIgnoreCase(skill2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); + } + + }); + break; + + + case ATOM: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getName().compareToIgnoreCase(skill1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); + } + + }); + break; + + case ATOM: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + } + + }); + break; + } + } + } + + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + String tempString; + + // do not allow empty name + tempString = skillRestInfo.getName(); + if (!tempString.isEmpty()) { + skill.setName(tempString); + } + + skill.setDescription(skillRestInfo.getDescription()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + } + + private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + OpenAcdSkill skill = new OpenAcdSkill(); + + // copy fields from rest info + skill.setName(skillRestInfo.getName()); + skill.setDescription(skillRestInfo.getDescription()); + skill.setAtom(skillRestInfo.getAtom()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + + return skill; + } + + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + int groupId = skillRestInfo.getGroupId(); + skillGroup = m_openAcdContext.getSkillGroupById(groupId); + + return skillGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdSkillsRepresentation extends XStreamRepresentation { + + public OpenAcdSkillsRepresentation(MediaType mediaType, OpenAcdSkillsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + } + } + + static class OpenAcdSkillRepresentation extends XStreamRepresentation { + + public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfoFull object) { + super(mediaType, object); + } + + public OpenAcdSkillRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + } + } + + + // REST info objects + // ----------------- + + static class UsersBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_users; + + public UsersBundleRestInfo(List users, MetadataRestInfo metadata) { + m_metadata = metadata; + m_users = users; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getSkills() { + return m_users; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java new file mode 100644 index 0000000000..d8ea4da19a --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java @@ -0,0 +1,553 @@ +/* + * + * UsersResource.java - A Restlet to read User data from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; +import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; +import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillsBundleRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class UsersResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + NAME, DESCRIPTION, ATOM, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + OpenAcdSkillRestInfoFull skillRestInfo = 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 { + skillRestInfo = createSkillRestInfo(idInt); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + } + + return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); + } + + + // if not single, process request for all + List skills = m_openAcdContext.getSkills(); + List skillsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortSkills(skills); + + // set requested agents groups and get resulting metadata + metadataRestInfo = addSkills(skillsRestInfo, skills); + + // create final restinfo + OpenAcdSkillsBundleRestInfo skillsBundleRestInfo = new OpenAcdSkillsBundleRestInfo(skillsRestInfo, metadataRestInfo); + + return new OpenAcdSkillsRepresentation(variant.getMediaType(), skillsBundleRestInfo); + } + + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); + OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); + OpenAcdSkill skill = null; + + // validate input for update or create + ValidationInfo validationInfo = validate(skillRestInfo); + + 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); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateSkill(skill, skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + + return; + } + + + // otherwise add new + try { + skill = createSkill(skillRestInfo); + m_openAcdContext.saveSkill(skill); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdSkill skill; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = RestUtilities.getIntFromAttribute(idString); + skill = m_openAcdContext.getSkillById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteSkill(skill); + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + + return; + } + + // no id string + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + String name = restInfo.getName(); + String atom = restInfo.getAtom(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int i = 0; i < atom.length(); i++) { + if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + return validationInfo; + } + + private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { + OpenAcdSkillRestInfoFull skillRestInfo = null; + + OpenAcdSkill skill = m_openAcdContext.getSkillById(id); + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + + return skillRestInfo; + } + + private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { + OpenAcdSkillRestInfoFull skillRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skills.size()); + + // create list of skill restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdSkill skill = skills.get(index); + + skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + skillsRestInfo.add(skillRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortSkills(List skills) { + // sort groups if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getName().compareToIgnoreCase(skill2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); + } + + }); + break; + + + case ATOM: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getName().compareToIgnoreCase(skill1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); + } + + }); + break; + + case ATOM: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + } + + }); + break; + } + } + } + + private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + String tempString; + + // do not allow empty name + tempString = skillRestInfo.getName(); + if (!tempString.isEmpty()) { + skill.setName(tempString); + } + + skill.setDescription(skillRestInfo.getDescription()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + } + + private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { + OpenAcdSkillGroup skillGroup; + OpenAcdSkill skill = new OpenAcdSkill(); + + // copy fields from rest info + skill.setName(skillRestInfo.getName()); + skill.setDescription(skillRestInfo.getDescription()); + skill.setAtom(skillRestInfo.getAtom()); + + skillGroup = getSkillGroup(skillRestInfo); + skill.setGroup(skillGroup); + + return skill; + } + + private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { + OpenAcdSkillGroup skillGroup; + int groupId = skillRestInfo.getGroupId(); + skillGroup = m_openAcdContext.getSkillGroupById(groupId); + + return skillGroup; + } + + + // REST Representations + // -------------------- + + static class OpenAcdSkillsRepresentation extends XStreamRepresentation { + + public OpenAcdSkillsRepresentation(MediaType mediaType, OpenAcdSkillsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdSkillsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + } + } + + static class OpenAcdSkillRepresentation extends XStreamRepresentation { + + public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfoFull object) { + super(mediaType, object); + } + + public OpenAcdSkillRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + } + } + + + // REST info objects + // ----------------- + + static class UsersBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_users; + + public UsersBundleRestInfo(List users, MetadataRestInfo metadata) { + m_metadata = metadata; + m_users = users; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getSkills() { + return m_users; + } + } + + static class UserRestInfoFull { + private final int m_id; + private final String m_userName; // also called "User ID" in gui + private final String m_lastName; + private final String m_firstName; + private final String m_pin; + private final String m_sipPassword; + + // user groups + + public UserRestInfoFull(User user) { + m_id = user.getId(); + m_userName = user.getUserName(); + m_lastName = user.getLastName(); + m_firstName = user.getFirstName(); + m_pin = "*"; // pin is hardcoded to never display but must still be submitted + m_sipPassword = user.getSipPassword(); + } + + public int getId() { + return m_id; + } + + public String getUserName() { + return m_userName; + } + + public String getLastName() { + return m_lastName; + } + + public String getFirstName() { + return m_firstName; + } + + public String getPin() { + return m_pin; + } + + public String getSipPassword() { + return m_sipPassword; + } + } + + + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 1e0928b763..a2678c35e6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -400,4 +400,20 @@ + + + + + + + + + + + + + + + + From 26ea93aa232e3ae02262db8e4c9880af5ce73b31 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 12 Apr 2012 10:49:09 -0400 Subject: [PATCH 66/99] Fix OpenACD settings to only allow update, remove extraneous functionality --- .../rest/OpenAcdSettingsResource.java | 199 ++---------------- .../sipxconfig/rest/RestUtilities.java | 18 -- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 24 ++- 3 files changed, 38 insertions(+), 203 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index 8b9e6bcf4f..eea6a9e739 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -24,19 +24,14 @@ import static org.restlet.data.MediaType.TEXT_XML; import org.restlet.Context; -import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; -import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; -import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSettingRestInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; @@ -44,16 +39,12 @@ public class OpenAcdSettingsResource extends UserResource { private OpenAcdContext m_openAcdContext; - private Form m_form; @Override public void init(Context context, Request request, Response response) { super.init(context, request, response); getVariants().add(new Variant(TEXT_XML)); getVariants().add(new Variant(APPLICATION_JSON)); - - // pull parameters from url - m_form = getRequest().getResourceRef().getQueryAsForm(); } @@ -80,196 +71,49 @@ public boolean allowDelete() { @Override public Representation represent(Variant variant) throws ResourceException { - // process request for single - int idInt; - OpenAcdSettingRestInfo settingRestInfo = 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 { - settingRestInfo = createSettingRestInfo(idInt); - } - catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); - } - - return new OpenAcdSettingRepresentation(variant.getMediaType(), settingRestInfo); - } - - // if not single, process request for list OpenAcdSettings settings = m_openAcdContext.getSettings(); OpenAcdSettingRestInfo settingsRestInfo = new OpenAcdSettingRestInfo(settings); - MetadataRestInfo metadataRestInfo; - // set requested records and get resulting metadata - metadataRestInfo = addSettings(settingsRestInfo, settings); - - // create final restinfo - OpenAcdSettingsBundleRestInfo settingsBundleRestInfo = new OpenAcdSettingsBundleRestInfo(settingsRestInfo, metadataRestInfo); - - return new OpenAcdSettingsRepresentation(variant.getMediaType(), settingsBundleRestInfo); + return new OpenAcdSettingRepresentation(variant.getMediaType(), settingsRestInfo); } - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update single Setting + // --------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { // get from request body OpenAcdSettingRepresentation representation = new OpenAcdSettingRepresentation(entity); OpenAcdSettingRestInfo settingRestInfo = representation.getObject(); - OpenAcdSettings setting; - - // if have id then update single - String idString = (String) getRequest().getAttributes().get("id"); - - if (idString != null) { - try { - int idInt = RestUtilities.getIntFromAttribute(idString); - setting = m_openAcdContext.getSettings(); - } - catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; - } - - // copy values over to existing - try { - updateSetting(setting, settingRestInfo); - m_openAcdContext.saveSettings(setting); - } - catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed", exception.getLocalizedMessage()); - return; - } - - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Settings", setting.getId()); - - return; - } + OpenAcdSettings settings; - - // otherwise add new + // assign new setting try { - setting = createSetting(settingRestInfo); - m_openAcdContext.saveSettings(setting); + settings = m_openAcdContext.getSettings(); + updateSettings(settings, settingRestInfo); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Setting failed", exception.getLocalizedMessage()); - return; - } - - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Setting", setting.getId()); - } - - - // DELETE - Delete single Skill - // ---------------------------- - - @Override - public void removeRepresentations() throws ResourceException { - OpenAcdSettings setting; - - // get id then delete single - String idString = (String) getRequest().getAttributes().get("id"); - - if (idString != null) { - try { - int idInt = RestUtilities.getIntFromAttribute(idString); - setting = m_openAcdContext.getSettings(); - } - catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; - } - - m_openAcdContext.saveSettings(setting); - - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", setting.getId()); - + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Assign Setting failed", exception.getLocalizedMessage()); return; } - // no id string - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Assigned Setting", settings.getId()); } // Helper functions // ---------------- - private OpenAcdSettingRestInfo createSettingRestInfo(int id) throws ResourceException { - OpenAcdSettingRestInfo settingRestInfo; - - try { - OpenAcdSettings setting = m_openAcdContext.getSettings(); - settingRestInfo = new OpenAcdSettingRestInfo(setting); - } - catch (Exception exception) { - throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); - } - - return settingRestInfo; - } - - private MetadataRestInfo addSettings(OpenAcdSettingRestInfo settingsRestInfo, OpenAcdSettings settings) { - OpenAcdSettingRestInfo settingRestInfo; - - // determine pagination - PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, 1); - - // create metadata about agent groups - MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); - return metadata; - } - - private void updateSetting(OpenAcdSettings setting, OpenAcdSettingRestInfo settingRestInfo) throws ResourceException { - String tempString; - - // do not allow empty name - setting.setSettingValue("openacd-config/log_level", setting.getLogLevel()); - - } - - private OpenAcdSettings createSetting(OpenAcdSettingRestInfo settingRestInfo) throws ResourceException { - OpenAcdSettings setting = new OpenAcdSettings(); - - // copy fields from rest info - setting.setSettingValue("openacd-config/log_level", setting.getLogLevel()); - - return setting; + private void updateSettings(OpenAcdSettings settings, OpenAcdSettingRestInfo settingRestInfo) throws ResourceException { + settings.setSettingValue("openacd-config/log_level", settingRestInfo.getLogLevel()); } // REST Representations // -------------------- - static class OpenAcdSettingsRepresentation extends XStreamRepresentation { - - public OpenAcdSettingsRepresentation(MediaType mediaType, OpenAcdSettingsBundleRestInfo object) { - super(mediaType, object); - } - - public OpenAcdSettingsRepresentation(Representation representation) { - super(representation); - } - - @Override - protected void configureXStream(XStream xstream) { - xstream.alias("openacd-setting", OpenAcdSettingsBundleRestInfo.class); - xstream.alias("setting", OpenAcdSettingRestInfo.class); - } - } - static class OpenAcdSettingRepresentation extends XStreamRepresentation { public OpenAcdSettingRepresentation(MediaType mediaType, OpenAcdSettingRestInfo object) { @@ -290,24 +134,25 @@ protected void configureXStream(XStream xstream) { // REST info objects // ----------------- - static class OpenAcdSettingsBundleRestInfo { - private final MetadataRestInfo m_metadata; - private final OpenAcdSettingRestInfo m_settings; + static class OpenAcdSettingRestInfo { + private final int m_id; + private final String m_logLevel; - public OpenAcdSettingsBundleRestInfo(OpenAcdSettingRestInfo settings, MetadataRestInfo metadata) { - m_metadata = metadata; - m_settings = settings; + public OpenAcdSettingRestInfo(OpenAcdSettings setting) { + m_id = setting.getId(); + m_logLevel = setting.getLogLevel(); } - public MetadataRestInfo getMetadata() { - return m_metadata; + public int getId() { + return m_id; } - public OpenAcdSettingRestInfo getSettings() { - return m_settings; + public String getLogLevel() { + return m_logLevel; } } + // Injected objects // ---------------- diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 54489cf41d..1bca58d308 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -40,7 +40,6 @@ import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeCondition; import org.sipfoundry.sipxconfig.openacd.OpenAcdRecipeStep; import org.sipfoundry.sipxconfig.openacd.OpenAcdReleaseCode; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSettings; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; import org.sipfoundry.sipxconfig.permission.Permission; @@ -995,21 +994,4 @@ public List getClients() { } } - static class OpenAcdSettingRestInfo { - private final int m_id; - private final String m_logLevel; - - public OpenAcdSettingRestInfo(OpenAcdSettings setting) { - m_id = setting.getId(); - m_logLevel = setting.getLogLevel(); - } - - public int getId() { - return m_id; - } - - public String getLogLevel() { - return m_logLevel; - } - } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index a2678c35e6..5fdc1d3104 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -376,14 +376,6 @@ - - - - - - - - @@ -416,4 +408,20 @@ + + + + + + + + + + + + + + + + From d4749c7c9c55581ec4a2280433c0e44860ad6c1e Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 12 Apr 2012 12:00:08 -0400 Subject: [PATCH 67/99] Add save of setting --- .../org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java | 1 + 1 file changed, 1 insertion(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index eea6a9e739..caccf1ad47 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -93,6 +93,7 @@ public void storeRepresentation(Representation entity) throws ResourceException try { settings = m_openAcdContext.getSettings(); updateSettings(settings, settingRestInfo); + m_openAcdContext.saveSettings(settings); } catch (Exception exception) { RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Assign Setting failed", exception.getLocalizedMessage()); From cd62601a165faa7ea121e4f6892ef2b51a00e207 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 12 Apr 2012 12:04:38 -0400 Subject: [PATCH 68/99] Remove delete permission --- .../sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index caccf1ad47..8e68fa0e8d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -61,10 +61,6 @@ public boolean allowPut() { return true; } - @Override - public boolean allowDelete() { - return true; - } // GET - Retrieve all and single Client // ----------------------------------- From b380b59c07f7c3dc36da7f769a1168a280173271 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 12 Apr 2012 15:07:46 -0400 Subject: [PATCH 69/99] Add User Groups REST API --- .../sipxconfig/rest/UserGroupsResource.java | 294 +++++++++--------- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 2 + 2 files changed, 145 insertions(+), 151 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java index 7a29e3bb3e..e75a7225c1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java @@ -36,29 +36,30 @@ import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillsBundleRestInfo; +import org.sipfoundry.sipxconfig.branch.Branch; +import org.sipfoundry.sipxconfig.branch.BranchManager; +import org.sipfoundry.sipxconfig.common.CoreContext; +import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; -import org.sipfoundry.sipxconfig.rest.UsersResource.UserRestInfoFull; +import org.sipfoundry.sipxconfig.setting.Group; +import org.sipfoundry.sipxconfig.setting.SettingDao; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; public class UserGroupsResource extends UserResource { - private OpenAcdContext m_openAcdContext; + private SettingDao m_settingContext; // saveGroup is not available through corecontext + private BranchManager m_branchManager; private Form m_form; // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, ATOM, NONE; + NAME, DESCRIPTION, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -111,7 +112,7 @@ public boolean allowDelete() { public Representation represent(Variant variant) throws ResourceException { // process request for single int idInt; - OpenAcdSkillRestInfoFull skillRestInfo = null; + UserGroupRestInfoFull userGroupRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { @@ -123,46 +124,46 @@ public Representation represent(Variant variant) throws ResourceException { } try { - skillRestInfo = createSkillRestInfo(idInt); + userGroupRestInfo = createUserGroupRestInfo(idInt); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read User Group failed", exception.getLocalizedMessage()); } - return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); + return new UserGroupRepresentation(variant.getMediaType(), userGroupRestInfo); } // if not single, process request for all - List skills = m_openAcdContext.getSkills(); - List skillsRestInfo = new ArrayList(); + List userGroups = getCoreContext().getGroups(); // settingsContext.getGroups() requires Resource string value + + List userGroupsRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; - // sort groups if specified - sortSkills(skills); + // sort if specified + sortUserGroups(userGroups); - // set requested agents groups and get resulting metadata - metadataRestInfo = addSkills(skillsRestInfo, skills); + // set requested items and get resulting metadata + metadataRestInfo = addUserGroups(userGroupsRestInfo, userGroups); // create final restinfo - OpenAcdSkillsBundleRestInfo skillsBundleRestInfo = new OpenAcdSkillsBundleRestInfo(skillsRestInfo, metadataRestInfo); + UserGroupsBundleRestInfo userGroupsBundleRestInfo = new UserGroupsBundleRestInfo(userGroupsRestInfo, metadataRestInfo); - return new OpenAcdSkillsRepresentation(variant.getMediaType(), skillsBundleRestInfo); + return new UserGroupsRepresentation(variant.getMediaType(), userGroupsBundleRestInfo); } - // PUT - Update or Add single Skill // -------------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { // get from request body - OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); - OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); - OpenAcdSkill skill = null; + UserGroupRepresentation representation = new UserGroupRepresentation(entity); + UserGroupRestInfoFull userGroupRestInfo = representation.getObject(); + Group userGroup = null; // validate input for update or create - ValidationInfo validationInfo = validate(skillRestInfo); + ValidationInfo validationInfo = validate(userGroupRestInfo); if (!validationInfo.valid) { RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); @@ -176,7 +177,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { int idInt = RestUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + userGroup = m_settingContext.getGroup(idInt); } catch (Exception exception) { RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); @@ -185,15 +186,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // copy values over to existing try { - updateSkill(skill, skillRestInfo); - m_openAcdContext.saveSkill(skill); + updateUserGroup(userGroup, userGroupRestInfo); + m_settingContext.saveGroup(userGroup); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update User Group failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated User Group", userGroup.getId()); return; } @@ -201,15 +202,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new try { - skill = createSkill(skillRestInfo); - m_openAcdContext.saveSkill(skill); + userGroup = createUserGroup(userGroupRestInfo); + m_settingContext.saveGroup(userGroup); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create User Group failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created User Group", userGroup.getId()); } @@ -218,24 +219,27 @@ public void storeRepresentation(Representation entity) throws ResourceException @Override public void removeRepresentations() throws ResourceException { - OpenAcdSkill skill; + Group userGroup; + int idInt; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = RestUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + idInt = RestUtilities.getIntFromAttribute(idString); + userGroup = m_settingContext.getGroup(idInt); } catch (Exception exception) { RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } - m_openAcdContext.deleteSkill(skill); + List userGroupIds = new ArrayList(); + userGroupIds.add(idInt); + m_settingContext.deleteGroups(userGroupIds); - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted User Group", userGroup.getId()); return; } @@ -252,61 +256,64 @@ public void removeRepresentations() throws ResourceException { // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create - private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + private ValidationInfo validate(UserGroupRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); - String name = restInfo.getName(); - String atom = restInfo.getAtom(); + return validationInfo; + } - for (int i = 0; i < name.length(); i++) { - if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } + private UserGroupRestInfoFull createUserGroupRestInfo(int id) { + Group group = m_settingContext.getGroup(id); - for (int i = 0; i < atom.length(); i++) { - if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } + return createUserGroupRestInfo(group); + } + + private UserGroupRestInfoFull createUserGroupRestInfo(Group group) { + UserGroupRestInfoFull userGroupRestInfo = null; + BranchRestInfoFull branchRestInfo = null; + Branch branch = null; + + // group may not have branch assigned + branch = group.getBranch(); + if (branch != null) { + branchRestInfo = createBranchRestInfo(branch.getId()); } - return validationInfo; + userGroupRestInfo = new UserGroupRestInfoFull(group, branchRestInfo); + + return userGroupRestInfo; } - private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { - OpenAcdSkillRestInfoFull skillRestInfo = null; + private BranchRestInfoFull createBranchRestInfo(int id) { + BranchRestInfoFull branchRestInfo = null; - OpenAcdSkill skill = m_openAcdContext.getSkillById(id); - skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + Branch branch = m_branchManager.getBranch(id); + branchRestInfo = new BranchRestInfoFull(branch); - return skillRestInfo; + return branchRestInfo; } - private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { - OpenAcdSkillRestInfoFull skillRestInfo; + private MetadataRestInfo addUserGroups(List userGroupsRestInfo, List userGroups) { + UserGroupRestInfoFull userGroupRestInfo; // determine pagination - PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skills.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, userGroups.size()); - // create list of skill restinfos + // create list of restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { - OpenAcdSkill skill = skills.get(index); + Group userGroup = userGroups.get(index); - skillRestInfo = new OpenAcdSkillRestInfoFull(skill); - skillsRestInfo.add(skillRestInfo); + userGroupRestInfo = createUserGroupRestInfo(userGroup); + userGroupsRestInfo.add(userGroupRestInfo); } - // create metadata about agent groups + // create metadata about restinfos MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); return metadata; } - private void sortSkills(List skills) { - // sort groups if requested + private void sortUserGroups(List userGroups) { + // sort if requested SortInfo sortInfo = RestUtilities.calculateSorting(m_form); if (!sortInfo.sort) { @@ -319,37 +326,24 @@ private void sortSkills(List skills) { switch (sortField) { case NAME: - Collections.sort(skills, new Comparator() { + Collections.sort(userGroups, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getName().compareToIgnoreCase(skill2.getName()); + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group1.getName().compareToIgnoreCase(group2.getName()); } }); break; case DESCRIPTION: - Collections.sort(skills, new Comparator() { - - public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); - } - - }); - break; - - - case ATOM: - Collections.sort(skills, new Comparator() { + Collections.sort(userGroups, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group1.getDescription().compareToIgnoreCase(group2.getDescription()); } }); @@ -360,36 +354,24 @@ public int compare(Object object1, Object object2) { // must be reverse switch (sortField) { case NAME: - Collections.sort(skills, new Comparator() { + Collections.sort(userGroups, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getName().compareToIgnoreCase(skill1.getName()); + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group2.getName().compareToIgnoreCase(group1.getName()); } }); break; case DESCRIPTION: - Collections.sort(skills, new Comparator() { - - public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); - } - - }); - break; - - case ATOM: - Collections.sort(skills, new Comparator() { + Collections.sort(userGroups, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group2.getDescription().compareToIgnoreCase(group1.getDescription()); } }); @@ -398,79 +380,84 @@ public int compare(Object object1, Object object2) { } } - private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; + private void updateUserGroup(Group userGroup, UserGroupRestInfoFull userGroupRestInfo) { + Branch branch; String tempString; // do not allow empty name - tempString = skillRestInfo.getName(); + tempString = userGroupRestInfo.getName(); if (!tempString.isEmpty()) { - skill.setName(tempString); + userGroup.setName(tempString); } - skill.setDescription(skillRestInfo.getDescription()); + userGroup.setDescription(userGroupRestInfo.getDescription()); - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + branch = getBranch(userGroupRestInfo); + userGroup.setBranch(branch); } - private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { - OpenAcdSkillGroup skillGroup; - OpenAcdSkill skill = new OpenAcdSkill(); + private Group createUserGroup(UserGroupRestInfoFull userGroupRestInfo) { + Branch branch = null; + Group userGroup = new Group(); // copy fields from rest info - skill.setName(skillRestInfo.getName()); - skill.setDescription(skillRestInfo.getDescription()); - skill.setAtom(skillRestInfo.getAtom()); + userGroup.setName(userGroupRestInfo.getName()); + userGroup.setDescription(userGroupRestInfo.getDescription()); - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + // apparently there is a special Resource value for user groups + userGroup.setResource(CoreContext.USER_GROUP_RESOURCE_ID); - return skill; + branch = getBranch(userGroupRestInfo); + userGroup.setBranch(branch); + + return userGroup; } - private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; - int groupId = skillRestInfo.getGroupId(); - skillGroup = m_openAcdContext.getSkillGroupById(groupId); + private Branch getBranch(UserGroupRestInfoFull userGroupRestInfo) { + Branch branch = null; + BranchRestInfoFull branchRestInfo = userGroupRestInfo.getBranch(); - return skillGroup; + if (branchRestInfo != null) { + branch = m_branchManager.getBranch(branchRestInfo.getId()); + } + + return branch; } // REST Representations // -------------------- - static class OpenAcdSkillsRepresentation extends XStreamRepresentation { + static class UserGroupsRepresentation extends XStreamRepresentation { - public OpenAcdSkillsRepresentation(MediaType mediaType, OpenAcdSkillsBundleRestInfo object) { + public UserGroupsRepresentation(MediaType mediaType, UserGroupsBundleRestInfo object) { super(mediaType, object); } - public OpenAcdSkillsRepresentation(Representation representation) { + public UserGroupsRepresentation(Representation representation) { super(representation); } @Override protected void configureXStream(XStream xstream) { - xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); - xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + xstream.alias("user-group", UserGroupsBundleRestInfo.class); + xstream.alias("userGroup", UserGroupRestInfoFull.class); } } - static class OpenAcdSkillRepresentation extends XStreamRepresentation { + static class UserGroupRepresentation extends XStreamRepresentation { - public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfoFull object) { + public UserGroupRepresentation(MediaType mediaType, UserGroupRestInfoFull object) { super(mediaType, object); } - public OpenAcdSkillRepresentation(Representation representation) { + public UserGroupRepresentation(Representation representation) { super(representation); } @Override protected void configureXStream(XStream xstream) { - xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + xstream.alias("userGroup", UserGroupRestInfoFull.class); } } @@ -478,21 +465,21 @@ protected void configureXStream(XStream xstream) { // REST info objects // ----------------- - static class UsersBundleRestInfo { + static class UserGroupsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_users; + private final List m_userGroups; - public UsersBundleRestInfo(List users, MetadataRestInfo metadata) { + public UserGroupsBundleRestInfo(List userGroups, MetadataRestInfo metadata) { m_metadata = metadata; - m_users = users; + m_userGroups = userGroups; } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getSkills() { - return m_users; + public List getUserGroups() { + return m_userGroups; } } @@ -501,8 +488,13 @@ public List getSkills() { // ---------------- @Required - public void setOpenAcdContext(OpenAcdContext openAcdContext) { - m_openAcdContext = openAcdContext; + public void setSettingDao(SettingDao settingContext) { + m_settingContext = settingContext; + } + + @Required + public void setBranchManager(BranchManager branchManager) { + m_branchManager = branchManager; } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 5fdc1d3104..6d4776ceb7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -410,6 +410,7 @@ + @@ -418,6 +419,7 @@ + From 667416197164a140f33547e6002099b3e7f45967 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 12 Apr 2012 22:00:14 -0400 Subject: [PATCH 70/99] Add User Group Permissions REST API --- .../sipxconfig/rest/RestUtilities.java | 48 +- .../rest/UserGroupPermissionsResource.java | 497 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 18 + 3 files changed, 559 insertions(+), 4 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 1bca58d308..41f67da3dd 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -481,17 +481,15 @@ public String getFaxNumber() { } } - static class UserGroupRestInfoFull { + static class UserGroupRestInfo { private final int m_id; private final String m_name; private final String m_description; - private final BranchRestInfoFull m_branch; - public UserGroupRestInfoFull(Group userGroup, BranchRestInfoFull branchRestInfo) { + public UserGroupRestInfo(Group userGroup) { m_id = userGroup.getId(); m_name = userGroup.getName(); m_description = userGroup.getDescription(); - m_branch = branchRestInfo; } public int getId() { @@ -505,12 +503,54 @@ public String getName() { public String getDescription() { return m_description; } + } + + static class UserGroupRestInfoFull extends UserGroupRestInfo { + private final BranchRestInfoFull m_branch; + + public UserGroupRestInfoFull(Group userGroup, BranchRestInfoFull branchRestInfo) { + super(userGroup); + + m_branch = branchRestInfo; + } public BranchRestInfoFull getBranch() { return m_branch; } } + static class SettingBooleanRestInfo { + private final String m_name; + private final Boolean m_value; + + public SettingBooleanRestInfo(String name, Boolean value) { + m_name = name; + m_value = value; + } + + public String getName() { + return m_name; + } + + public Boolean getValue() { + return m_value; + } + } + + static class UserGroupPermissionRestInfoFull extends UserGroupRestInfo { + private final List m_settings; + + public UserGroupPermissionRestInfoFull(Group userGroup, List settingsRestInfo) { + super(userGroup); + + m_settings = settingsRestInfo; + } + + public List getSettings() { + return m_settings; + } + } + // Common OpenACD Rest Info objects // ------------------------ diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java new file mode 100644 index 0000000000..5099a4595b --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -0,0 +1,497 @@ +/* + * + * UserGroupPermissionsResource.java - A Restlet to read User Group data with Permissions from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.branch.Branch; +import org.sipfoundry.sipxconfig.branch.BranchManager; +import org.sipfoundry.sipxconfig.common.CoreContext; +import org.sipfoundry.sipxconfig.permission.PermissionName; +import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SettingBooleanRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupPermissionRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupRestInfoFull; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.setting.Group; +import org.sipfoundry.sipxconfig.setting.SettingDao; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class UserGroupPermissionsResource extends UserResource { + + private SettingDao m_settingContext; // saveGroup is not available through corecontext + private BranchManager m_branchManager; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Skill + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + UserGroupPermissionRestInfoFull userGroupPermissionRestInfo = 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 { + userGroupPermissionRestInfo = createUserGroupPermissionRestInfo(idInt); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read User Group failed", exception.getLocalizedMessage()); + } + + return new UserGroupPermissionRepresentation(variant.getMediaType(), userGroupPermissionRestInfo); + } + + + // if not single, process request for all + List userGroups = getCoreContext().getGroups(); // settingsContext.getGroups() requires Resource string value + + List userGroupPermissionsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort if specified + sortUserGroups(userGroups); + + // set requested items and get resulting metadata + metadataRestInfo = addUserGroups(userGroupPermissionsRestInfo, userGroups); + + // create final restinfo + UserGroupPermissionsBundleRestInfo userGroupPermissionsBundleRestInfo = new UserGroupPermissionsBundleRestInfo(userGroupPermissionsRestInfo, metadataRestInfo); + + return new UserGroupPermissionsRepresentation(variant.getMediaType(), userGroupPermissionsBundleRestInfo); + } + + // PUT - Update or Add single Skill + // -------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + UserGroupPermissionRepresentation representation = new UserGroupPermissionRepresentation(entity); + UserGroupPermissionRestInfoFull userGroupPermissionRestInfo = representation.getObject(); + Group userGroup = null; + + // validate input for update or create + ValidationInfo validationInfo = validate(userGroupPermissionRestInfo); + + 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); + userGroup = m_settingContext.getGroup(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateUserGroupPermission(userGroup, userGroupPermissionRestInfo); + m_settingContext.saveGroup(userGroup); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update User Group Permissions failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated User Group Permissions", userGroup.getId()); + + return; + } + + + // otherwise add new + try { + userGroup = createUserGroupPermission(userGroupPermissionRestInfo); + m_settingContext.saveGroup(userGroup); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create User Group Permissions failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created User Group permissions", userGroup.getId()); + } + + + // DELETE - Delete single Skill + // ---------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + Group userGroup; + int idInt; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + idInt = RestUtilities.getIntFromAttribute(idString); + userGroup = m_settingContext.getGroup(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + List userGroupIds = new ArrayList(); + userGroupIds.add(idInt); + m_settingContext.deleteGroups(userGroupIds); + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted User Group Permissions", userGroup.getId()); + + return; + } + + // no id string + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(UserGroupPermissionRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + + private UserGroupPermissionRestInfoFull createUserGroupPermissionRestInfo(int id) { + Group group = m_settingContext.getGroup(id); + + return createUserGroupPermissionRestInfo(group); + } + + private UserGroupPermissionRestInfoFull createUserGroupPermissionRestInfo(Group group) { + UserGroupPermissionRestInfoFull userGroupPermissionRestInfo = null; + List settings = new ArrayList(); + + settings.add(createSettingRestInfo(group)); + + userGroupPermissionRestInfo = new UserGroupPermissionRestInfoFull(group, settings); + + return userGroupPermissionRestInfo; + } + + private SettingBooleanRestInfo createSettingRestInfo(Group group) { + SettingBooleanRestInfo settingRestInfo = null; + String valueString; + Boolean value; + + //value = (Boolean) group.getSettingTypedValue(new BooleanSetting(), PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); + valueString = group.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); + value = Boolean.parseBoolean(valueString); + + settingRestInfo = new SettingBooleanRestInfo(PermissionName.SUBSCRIBE_TO_PRESENCE.getName(), value); + + return settingRestInfo; + } + + private MetadataRestInfo addUserGroups(List userGroupPermissionsRestInfo, List userGroups) { + UserGroupPermissionRestInfoFull userGroupPermissionRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, userGroups.size()); + + // create list of restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + Group userGroup = userGroups.get(index); + + userGroupPermissionRestInfo = createUserGroupPermissionRestInfo(userGroup); + userGroupPermissionsRestInfo.add(userGroupPermissionRestInfo); + } + + // create metadata about restinfos + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortUserGroups(List userGroups) { + // sort if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(userGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group1.getName().compareToIgnoreCase(group2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(userGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group1.getDescription().compareToIgnoreCase(group2.getDescription()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(userGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group2.getName().compareToIgnoreCase(group1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(userGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + Group group1 = (Group) object1; + Group group2 = (Group) object2; + return group2.getDescription().compareToIgnoreCase(group1.getDescription()); + } + + }); + break; + } + } + } + + private void updateUserGroupPermission(Group userGroup, UserGroupPermissionRestInfoFull userGroupPermissionRestInfo) { + String tempString; + + // do not allow empty name + tempString = userGroupPermissionRestInfo.getName(); + if (!tempString.isEmpty()) { + userGroup.setName(tempString); + } + + userGroup.setDescription(userGroupPermissionRestInfo.getDescription()); + } + + private Group createUserGroupPermission(UserGroupPermissionRestInfoFull userGroupPermissionRestInfo) { + Group userGroup = new Group(); + + // copy fields from rest info + userGroup.setName(userGroupPermissionRestInfo.getName()); + userGroup.setDescription(userGroupPermissionRestInfo.getDescription()); + + // apparently there is a special Resource value for user groups + userGroup.setResource(CoreContext.USER_GROUP_RESOURCE_ID); + + return userGroup; + } + + private Branch getBranch(UserGroupRestInfoFull userGroupRestInfo) { + Branch branch = null; + BranchRestInfoFull branchRestInfo = userGroupRestInfo.getBranch(); + + if (branchRestInfo != null) { + branch = m_branchManager.getBranch(branchRestInfo.getId()); + } + + return branch; + } + + + // REST Representations + // -------------------- + + static class UserGroupPermissionsRepresentation extends XStreamRepresentation { + + public UserGroupPermissionsRepresentation(MediaType mediaType, UserGroupPermissionsBundleRestInfo object) { + super(mediaType, object); + } + + public UserGroupPermissionsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("user-group-permission", UserGroupPermissionsBundleRestInfo.class); + xstream.alias("userGroup", UserGroupPermissionRestInfoFull.class); + xstream.alias("setting", SettingBooleanRestInfo.class); + } + } + + static class UserGroupPermissionRepresentation extends XStreamRepresentation { + + public UserGroupPermissionRepresentation(MediaType mediaType, UserGroupPermissionRestInfoFull object) { + super(mediaType, object); + } + + public UserGroupPermissionRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("userGroup", UserGroupPermissionRestInfoFull.class); + xstream.alias("setting", SettingBooleanRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class UserGroupPermissionsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_userGroupPermissions; + + public UserGroupPermissionsBundleRestInfo(List userGroupPermissions, MetadataRestInfo metadata) { + m_metadata = metadata; + m_userGroupPermissions = userGroupPermissions; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getUserGroupPermissions() { + return m_userGroupPermissions; + } + } + + + // Injected objects + // ---------------- + + @Required + public void setSettingDao(SettingDao settingContext) { + m_settingContext = settingContext; + } + + @Required + public void setBranchManager(BranchManager branchManager) { + m_branchManager = branchManager; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 6d4776ceb7..30c99eaf90 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -426,4 +426,22 @@ + + + + + + + + + + + + + + + + + + From a2b1124cdeea8671f33690e56a955d8cb8c244a4 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 10:52:44 -0400 Subject: [PATCH 71/99] Add basic input validation --- .../rest/OpenAcdQueueGroupsResource.java | 28 +++++++++++-- .../rest/OpenAcdQueuesResource.java | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 848b22c7e2..3cdf56ab0f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -52,7 +52,6 @@ import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeStepRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -268,16 +267,39 @@ private ValidationInfo validate(OpenAcdQueueGroupRestInfoFull restInfo) { if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; } } for (int i = 0; i < restInfo.getSteps().size(); i++) { if (restInfo.getSteps().get(i).getAction() == null) { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Action' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + if (!restInfo.getSteps().get(i).getAction().getAction().equals("announce")) { + for (int j = 0; j < restInfo.getSteps().get(i).getAction().getActionValue().length(); j++) { + char c = restInfo.getSteps().get(i).getAction().getActionValue().charAt(j); + if (!Character.isDigit(c) && c != '*') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action Value' must only contain numbers and *"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + } + } + for (int k = 0; k < restInfo.getSteps().get(i).getConditions().size(); k++) { + if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name"))) { + for (int j = 0; j < restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().length(); j++) { + char c = restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().charAt(j); + if (!Character.isDigit(c) && c != '*') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' must only contain numbers and *"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + } + } } } + return validationInfo; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 0c9e83c11a..03e5f7fb46 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -263,6 +263,45 @@ public void removeRepresentations() throws ResourceException { private ValidationInfo validate(OpenAcdQueueRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + String name = restInfo.getName(); + + for (int i = 0; i < name.length(); i++) { + if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + } + for (int i = 0; i < restInfo.getSteps().size(); i++) { + if (restInfo.getSteps().get(i).getAction() == null) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action' must only contain letters, numbers, dashes, and underscores"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + if (!restInfo.getSteps().get(i).getAction().getAction().equals("announce")) { + for (int j = 0; j < restInfo.getSteps().get(i).getAction().getActionValue().length(); j++) { + char c = restInfo.getSteps().get(i).getAction().getActionValue().charAt(j); + if (!Character.isDigit(c) && c != '*') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action Value' must only contain numbers and *"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + } + } + for (int k = 0; k < restInfo.getSteps().get(i).getConditions().size(); k++) { + if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name"))) { + for (int j = 0; j < restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().length(); j++) { + char c = restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().charAt(j); + if (!Character.isDigit(c) && c != '*') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' must only contain numbers and *"; + validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + } + } + } + } + } + return validationInfo; } From 707b04bea3f3c40494bf1f3d4a54e7ece2aebb5c Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 11:06:50 -0400 Subject: [PATCH 72/99] Correct return code for update --- .../org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java index 8e68fa0e8d..c9191d7ae0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSettingsResource.java @@ -96,7 +96,7 @@ public void storeRepresentation(Representation entity) throws ResourceException return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Assigned Setting", settings.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Assigned Setting", settings.getId()); } From 5b065fd1a42cb72b7451655ea605bbb4485d5c1d Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 16:13:58 -0400 Subject: [PATCH 73/99] Add validation --- .../rest/OpenAcdQueueGroupsResource.java | 100 +++++++++++++++--- .../rest/OpenAcdQueuesResource.java | 98 ++++++++++++++--- 2 files changed, 167 insertions(+), 31 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 3cdf56ab0f..724ca9d034 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -24,6 +24,7 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -52,6 +53,7 @@ import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeStepRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -126,14 +128,14 @@ public Representation represent(Variant variant) throws ResourceException { idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { queueGroupRestInfo = createQueueGroupRestInfo(idInt); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), ResponseCode.ERROR_READ_FAILED, "Read Queue Group failed", exception.getLocalizedMessage()); } return new OpenAcdQueueGroupRepresentation(variant.getMediaType(), queueGroupRestInfo); @@ -186,7 +188,7 @@ public void storeRepresentation(Representation entity) throws ResourceException queueGroup = m_openAcdContext.getQueueGroupById(idInt); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -196,11 +198,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueueGroup(queueGroup); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_WRITE_FAILED, "Update Queue Group failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queueGroup.getId()); + RestUtilities.setResponse(getResponse(), ResponseCode.SUCCESS_UPDATED, "Updated Queue", queueGroup.getId()); return; } @@ -236,19 +238,19 @@ public void removeRepresentations() throws ResourceException { queueGroup = m_openAcdContext.getQueueGroupById(idInt); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } m_openAcdContext.deleteQueueGroup(queueGroup); - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Queue Group", queueGroup.getId()); + RestUtilities.setResponse(getResponse(), ResponseCode.SUCCESS_DELETED, "Deleted Queue Group", queueGroup.getId()); return; } // no id string - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + RestUtilities.setResponse(getResponse(), ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); } @@ -260,40 +262,106 @@ public void removeRepresentations() throws ResourceException { // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdQueueGroupRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + List relation = Arrays.asList("is", "isNot"); + List condition1 = Arrays.asList("available_agents", "eligible_agents", "calls_queued", "queue_position", "hour", "weekday", "client_calls_queued"); + List condition2 = Arrays.asList("ticks", "client", "media_type", "caller_id", "caller_name"); + List equalityRelation = Arrays.asList("is", "greater", "less"); + List mediaValues = Arrays.asList("voice", "email", "voicemail", "chat"); String name = restInfo.getName(); + // rest mods the hours with 24 to give a new hour, so allow over 23 + for (int i = 0; i < name.length(); i++) { if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } + for (int i = 0; i < restInfo.getSteps().size(); i++) { - if (restInfo.getSteps().get(i).getAction() == null) { + if (restInfo.getSteps().get(i).getAction().getAction().isEmpty()) { validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Action' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.message = "Validation Error: 'Action' cannot be empty and must only contain numbers and *"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + + if (restInfo.getSteps().get(i).getAction().getAction().equals("announce") || restInfo.getSteps().get(i).getAction().getAction().equals("set_priority")) { + if (restInfo.getSteps().get(i).getAction().getActionValue().isEmpty()) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action Value' cannot be empty and must only contain numbers and *"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int j = 0; j < m_openAcdContext.getClients().size(); j++) { + if (m_openAcdContext.getClients().get(j).getIdentity().equals(restInfo.getSteps().get(i).getAction().getActionValue())) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Client Does not Exist"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } } - if (!restInfo.getSteps().get(i).getAction().getAction().equals("announce")) { + + if (restInfo.getSteps().get(i).getAction().getAction().equals("set_priority")) { for (int j = 0; j < restInfo.getSteps().get(i).getAction().getActionValue().length(); j++) { char c = restInfo.getSteps().get(i).getAction().getActionValue().charAt(j); if (!Character.isDigit(c) && c != '*') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Action Value' must only contain numbers and *"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } } + for (int k = 0; k < restInfo.getSteps().get(i).getConditions().size(); k++) { - if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name"))) { + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().isEmpty() || restInfo.getSteps().get(i).getConditions().get(k).getRelation().isEmpty() || restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().isEmpty()) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Condtion' cannot be empty"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + + } + + if (condition2.contains(restInfo.getSteps().get(i).getConditions().get(k).getCondition())) { + if (!(relation.contains(restInfo.getSteps().get(i).getConditions().get(k).getRelation()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Relation' must only be 'is' or 'isNot'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + + } + + if (condition1.contains(restInfo.getSteps().get(i).getConditions().get(k).getCondition())) { + if (!(equalityRelation.contains(restInfo.getSteps().get(i).getConditions().get(k).getRelation()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Relation' must only be 'is', 'greater', or 'less'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("media_type")) { + if (!(mediaValues.contains(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' can only be 'voice', 'email', 'voicemail', or 'chat'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("weekday")) { + if (Integer.parseInt(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()) < 1 || Integer.parseInt(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()) > 7) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' must be between 1 and 7"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("media_type") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("client"))) { for (int j = 0; j < restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().length(); j++) { char c = restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().charAt(j); if (!Character.isDigit(c) && c != '*') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Value Condition' must only contain numbers and *"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 03e5f7fb46..51257e4651 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -24,6 +24,7 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -54,6 +55,7 @@ import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdRecipeStepRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.springframework.beans.factory.annotation.Required; @@ -128,14 +130,14 @@ public Representation represent(Variant variant) throws ResourceException { idInt = RestUtilities.getIntFromAttribute(idString); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return RestUtilities.getResponseError(getResponse(), ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); } try { queueRestInfo = createQueueRestInfo(idInt); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), ResponseCode.ERROR_READ_FAILED, "Read Queue failed", exception.getLocalizedMessage()); } return new OpenAcdQueueRepresentation(variant.getMediaType(), queueRestInfo); @@ -188,7 +190,7 @@ public void storeRepresentation(Representation entity) throws ResourceException queue = m_openAcdContext.getQueueById(idInt); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } @@ -198,11 +200,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_WRITE_FAILED, "Update Queue failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Queue", queue.getId()); + RestUtilities.setResponse(getResponse(), ResponseCode.SUCCESS_UPDATED, "Updated Queue", queue.getId()); return; } @@ -214,11 +216,11 @@ public void storeRepresentation(Representation entity) throws ResourceException m_openAcdContext.saveQueue(queue); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), ResponseCode.ERROR_WRITE_FAILED, "Create Queue failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Queue", queue.getId()); + RestUtilities.setResponse(getResponse(), ResponseCode.SUCCESS_CREATED, "Created Queue", queue.getId()); } @@ -262,40 +264,106 @@ public void removeRepresentations() throws ResourceException { // may create another validation function if different rules needed for update v. create private ValidationInfo validate(OpenAcdQueueRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); + List relation = Arrays.asList("is", "isNot"); + List condition1 = Arrays.asList("available_agents", "eligible_agents", "calls_queued", "queue_position", "hour", "weekday", "client_calls_queued"); + List condition2 = Arrays.asList("ticks", "client", "media_type", "caller_id", "caller_name"); + List equalityRelation = Arrays.asList("is", "greater", "less"); + List mediaValues = Arrays.asList("voice", "email", "voicemail", "chat"); String name = restInfo.getName(); + // rest mods the hours with 24 to give a new hour, so allow over 23 + for (int i = 0; i < name.length(); i++) { if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } + for (int i = 0; i < restInfo.getSteps().size(); i++) { - if (restInfo.getSteps().get(i).getAction() == null) { + if (restInfo.getSteps().get(i).getAction().getAction().isEmpty()) { validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Action' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.message = "Validation Error: 'Action' cannot be empty and must only contain numbers and *"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + + if (restInfo.getSteps().get(i).getAction().getAction().equals("announce") || restInfo.getSteps().get(i).getAction().getAction().equals("set_priority")) { + if (restInfo.getSteps().get(i).getAction().getActionValue().isEmpty()) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Action Value' cannot be empty and must only contain numbers and *"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + for (int j = 0; j < m_openAcdContext.getClients().size(); j++) { + if (m_openAcdContext.getClients().get(j).getIdentity().equals(restInfo.getSteps().get(i).getAction().getActionValue())) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: Client Does not Exist"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } } - if (!restInfo.getSteps().get(i).getAction().getAction().equals("announce")) { + + if (restInfo.getSteps().get(i).getAction().getAction().equals("set_priority")) { for (int j = 0; j < restInfo.getSteps().get(i).getAction().getActionValue().length(); j++) { char c = restInfo.getSteps().get(i).getAction().getActionValue().charAt(j); if (!Character.isDigit(c) && c != '*') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Action Value' must only contain numbers and *"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } } + for (int k = 0; k < restInfo.getSteps().get(i).getConditions().size(); k++) { - if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name"))) { + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().isEmpty() || restInfo.getSteps().get(i).getConditions().get(k).getRelation().isEmpty() || restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().isEmpty()) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Condtion' cannot be empty"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + + } + + if (condition2.contains(restInfo.getSteps().get(i).getConditions().get(k).getCondition())) { + if (!(relation.contains(restInfo.getSteps().get(i).getConditions().get(k).getRelation()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Relation' must only be 'is' or 'isNot'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + + } + + if (condition1.contains(restInfo.getSteps().get(i).getConditions().get(k).getCondition())) { + if (!(equalityRelation.contains(restInfo.getSteps().get(i).getConditions().get(k).getRelation()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Relation' must only be 'is', 'greater', or 'less'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("media_type")) { + if (!(mediaValues.contains(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' can only be 'voice', 'email', 'voicemail', or 'chat'"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("weekday")) { + if (Integer.parseInt(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()) < 1 || Integer.parseInt(restInfo.getSteps().get(i).getConditions().get(k).getValueCondition()) > 7) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Value Condition' must be between 1 and 7"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + + if (!(restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_id") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("caller_name") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("media_type") || restInfo.getSteps().get(i).getConditions().get(k).getCondition().equals("client"))) { for (int j = 0; j < restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().length(); j++) { char c = restInfo.getSteps().get(i).getConditions().get(k).getValueCondition().charAt(j); if (!Character.isDigit(c) && c != '*') { validationInfo.valid = false; validationInfo.message = "Validation Error: 'Value Condition' must only contain numbers and *"; - validationInfo.responseCode = RestUtilities.ResponseCode.ERROR_BAD_INPUT; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } } From 623e3320cc2084fa9816f1adf2e316cbbb4086c6 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 17:58:30 -0400 Subject: [PATCH 74/99] Correct response value on delete --- .../sipxconfig/rest/OpenAcdSkillGroupsResource.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 45bb4a4639..6b15006433 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -152,8 +152,8 @@ public Representation represent(Variant variant) throws ResourceException { } - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update or Create single + // ----------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { @@ -214,8 +214,8 @@ public void storeRepresentation(Representation entity) throws ResourceException } - // DELETE - Delete single Skill - // ---------------------------- + // DELETE - Delete single + // ---------------------- // deleteSkillGroup() not available from openAcdContext @Override @@ -223,13 +223,14 @@ public void removeRepresentations() throws ResourceException { // for some reason skill groups are deleted by providing collection of ids, not by // providing skill group object Collection skillGroupIds = new HashSet(); + int idInt; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { try { - int idInt = RestUtilities.getIntFromAttribute(idString); + idInt = RestUtilities.getIntFromAttribute(idString); skillGroupIds.add(idInt); } catch (Exception exception) { @@ -239,6 +240,8 @@ public void removeRepresentations() throws ResourceException { m_openAcdContext.removeSkillGroups(skillGroupIds); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill Group", idInt); + return; } From d042a6e608b58361c0b508fd97a6ae45e3d8214d Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 17:59:54 -0400 Subject: [PATCH 75/99] Add OpenACD Dial Strings REST API --- .../rest/OpenAcdDialStringsResource.java | 540 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 18 + 2 files changed, 558 insertions(+) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java new file mode 100644 index 0000000000..fcaac73510 --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java @@ -0,0 +1,540 @@ +/* + * + * OpenAcdDialStringsResource.java - A Restlet to read DialString data from OpenACD within SipXecs + * Copyright (C) 2012 PATLive, D. Waseem + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.data.Status; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.freeswitch.FreeswitchAction; +import org.sipfoundry.sipxconfig.openacd.OpenAcdCommand; +import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class OpenAcdDialStringsResource extends UserResource { + + private OpenAcdContext m_openAcdContext; + private Form m_form; + + // use to define all possible sort fields + enum SortField { + NAME, DESCRIPTION, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + @Override + public boolean allowDelete() { + return true; + } + + // GET - Retrieve all and single Client + // ----------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + OpenAcdDialStringRestInfo dialStringRestInfo = 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 { + dialStringRestInfo = createDialStringRestInfo(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Settings failed", exception.getLocalizedMessage()); + } + + return new OpenAcdDialStringRepresentation(variant.getMediaType(), dialStringRestInfo); + } + + // if not single, process request for list + List dialStrings = new ArrayList(m_openAcdContext.getCommands()); + List dialStringsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort groups if specified + sortDialStrings(dialStrings); + + // set requested records and get resulting metadata + metadataRestInfo = addDialStrings(dialStringsRestInfo, dialStrings); + + // create final restinfo + OpenAcdDialStringsBundleRestInfo dialStringsBundleRestInfo = new OpenAcdDialStringsBundleRestInfo(dialStringsRestInfo, metadataRestInfo); + + return new OpenAcdDialStringsRepresentation(variant.getMediaType(), dialStringsBundleRestInfo); + } + + + // PUT - Update or Add single Dial String + // -------------------------------------- + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + OpenAcdDialStringRepresentation representation = new OpenAcdDialStringRepresentation(entity); + OpenAcdDialStringRestInfo dialStringRestInfo = representation.getObject(); + OpenAcdCommand dialString = null; + + ValidationInfo validationInfo = validate(dialStringRestInfo); + + 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); + dialString = (OpenAcdCommand) m_openAcdContext.getExtensionById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateDialString(dialString, dialStringRestInfo); + m_openAcdContext.saveExtension(dialString); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Setting failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Settings", dialString.getId()); + + return; + } + + + // otherwise add new + try { + dialString = createDialString(dialStringRestInfo); + m_openAcdContext.saveExtension(dialString); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Dial String failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Setting", dialString.getId()); + } + + + // DELETE - Delete single Dial String + // ---------------------------------- + + @Override + public void removeRepresentations() throws ResourceException { + OpenAcdCommand dialString; + + // get id then delete single + String idString = (String) getRequest().getAttributes().get("id"); + + if (idString != null) { + try { + int idInt = RestUtilities.getIntFromAttribute(idString); + dialString = (OpenAcdCommand) m_openAcdContext.getExtensionById(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + m_openAcdContext.deleteExtension(dialString); + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Client", dialString.getId()); + + return; + } + + // no id string + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(OpenAcdDialStringRestInfo restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + + private OpenAcdDialStringRestInfo createDialStringRestInfo(int id) throws ResourceException { + OpenAcdDialStringRestInfo dialStringRestInfo; + + try { + OpenAcdCommand dialString = (OpenAcdCommand) m_openAcdContext.getExtensionById(id); + dialStringRestInfo = createDialStringRestInfo(dialString); + } + catch (Exception exception) { + throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, "ID " + id + " not found."); + } + + return dialStringRestInfo; + } + + private OpenAcdDialStringRestInfo createDialStringRestInfo(OpenAcdCommand dialString) { + OpenAcdDialStringRestInfo dialStringRestInfo; + List dialStringActionRestInfo; + + dialStringActionRestInfo = createDialStringActionRestInfo(dialString); + dialStringRestInfo = new OpenAcdDialStringRestInfo(dialString, dialStringActionRestInfo); + + return dialStringRestInfo; + + } + + private List createDialStringActionRestInfo(OpenAcdCommand dialString) { + OpenAcdDialStringActionRestInfo dialStringActionRestInfo; + List customActions = new ArrayList(); + + List actions = dialString.getLineActions(); + + for (FreeswitchAction action : actions) { + dialStringActionRestInfo = new OpenAcdDialStringActionRestInfo(action); + customActions.add(dialStringActionRestInfo); + } + + return customActions; + } + + private MetadataRestInfo addDialStrings(List dialStringsRestInfo, List dialStrings) { + OpenAcdDialStringRestInfo dialStringRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, dialStrings.size()); + + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + OpenAcdCommand dialString = dialStrings.get(index); + + dialStringRestInfo = createDialStringRestInfo(dialString); + dialStringsRestInfo.add(dialStringRestInfo); + } + + // create metadata about agent groups + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortDialStrings(List dialStrings) { + // sort groups if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case NAME: + Collections.sort(dialStrings, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdCommand dialString1 = (OpenAcdCommand) object1; + OpenAcdCommand dialString2 = (OpenAcdCommand) object2; + return dialString1.getName().compareToIgnoreCase(dialString2.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(dialStrings, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdCommand dialString1 = (OpenAcdCommand) object1; + OpenAcdCommand dialString2 = (OpenAcdCommand) object2; + return dialString1.getDescription().compareToIgnoreCase(dialString2.getDescription()); + } + + }); + break; + + } + } + else { + // must be reverse + switch (sortField) { + case NAME: + Collections.sort(dialStrings, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdCommand dialString1 = (OpenAcdCommand) object1; + OpenAcdCommand dialString2 = (OpenAcdCommand) object2; + return dialString2.getName().compareToIgnoreCase(dialString1.getName()); + } + + }); + break; + + case DESCRIPTION: + Collections.sort(dialStrings, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdCommand dialString1 = (OpenAcdCommand) object1; + OpenAcdCommand dialString2 = (OpenAcdCommand) object2; + return dialString2.getDescription().compareToIgnoreCase(dialString1.getDescription()); + } + + }); + break; + } + } + } + + private void updateDialString(OpenAcdCommand dialString, OpenAcdDialStringRestInfo dialStringRestInfo) throws ResourceException { + + dialString.setName(dialStringRestInfo.getName()); + dialString.setEnabled(dialStringRestInfo.getEnabled()); + dialString.setDescription(dialStringRestInfo.getDescription()); + dialString.getNumberCondition().setExpression(dialStringRestInfo.getExtension()); + + for (OpenAcdDialStringActionRestInfo actionRestInfo : dialStringRestInfo.getActions()) { + dialString.getNumberCondition().addAction(OpenAcdCommand.createAction(actionRestInfo.getApplication(), actionRestInfo.getApplication())); + } + } + + private OpenAcdCommand createDialString(OpenAcdDialStringRestInfo dialStringRestInfo) throws ResourceException { + OpenAcdCommand dialString = new OpenAcdCommand(); + dialString.addCondition(OpenAcdCommand.createLineCondition()); + String tempString; + + if (!(tempString = dialStringRestInfo.getName()).isEmpty()) { + dialString.setName(tempString); + } + + dialString.setEnabled(dialStringRestInfo.getEnabled()); + dialString.setDescription(dialStringRestInfo.getDescription()); + dialString.getNumberCondition().setExpression(dialStringRestInfo.getExtension()); + + for (OpenAcdDialStringActionRestInfo actionRestInfo : dialStringRestInfo.getActions()) { + dialString.getNumberCondition().addAction(OpenAcdCommand.createAction(actionRestInfo.getApplication(), actionRestInfo.getApplication())); + } + + return dialString; + } + + + // REST Representations + // -------------------- + + static class OpenAcdDialStringsRepresentation extends XStreamRepresentation { + + public OpenAcdDialStringsRepresentation(MediaType mediaType, OpenAcdDialStringsBundleRestInfo object) { + super(mediaType, object); + } + + public OpenAcdDialStringsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("openacd-dial-string", OpenAcdDialStringsBundleRestInfo.class); + xstream.alias("dial-string", OpenAcdDialStringRestInfo.class); + xstream.alias("action", OpenAcdDialStringActionRestInfo.class); + } + } + + static class OpenAcdDialStringRepresentation extends XStreamRepresentation { + + public OpenAcdDialStringRepresentation(MediaType mediaType, OpenAcdDialStringRestInfo dialStringsBundleRestInfo) { + super(mediaType, dialStringsBundleRestInfo); + } + + public OpenAcdDialStringRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("dialString", OpenAcdDialStringRestInfo.class); + xstream.alias("action", OpenAcdDialStringActionRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class OpenAcdDialStringsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_dialStrings; + + public OpenAcdDialStringsBundleRestInfo(List dialStrings, MetadataRestInfo metadata) { + m_metadata = metadata; + m_dialStrings = dialStrings; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getDialStrings() { + return m_dialStrings; + } + } + + static class OpenAcdDialStringRestInfo { + private final int m_id; + private final String m_name; + private final boolean m_enabled; + private final String m_description; + private final String m_extension; + private final List m_actions; + + public OpenAcdDialStringRestInfo(OpenAcdCommand dial, List dialStringActionsRestInfo) { + m_id = dial.getId(); + m_name = dial.getName(); + m_enabled = dial.isEnabled(); + m_description = dial.getDescription(); + m_extension = dial.getExtension(); + m_actions = dialStringActionsRestInfo; + } + + public int getId() { + return m_id; + } + + public String getName() { + return m_name; + } + + public boolean getEnabled() { + return m_enabled; + } + + public String getDescription() { + return m_description; + } + + public String getExtension() { + return m_extension; + } + + public List getActions() { + return m_actions; + } + } + + static class OpenAcdDialStringActionRestInfo { + private final String m_application; + private final String m_data; + + public OpenAcdDialStringActionRestInfo(FreeswitchAction action) { + m_application = action.getApplication(); + m_data = action.getData(); + } + + public String getApplication() { + return m_application; + } + + public String getData() { + return m_data; + } + } + + // Injected objects + // ---------------- + + @Required + public void setOpenAcdContext(OpenAcdContext openAcdContext) { + m_openAcdContext = openAcdContext; + } +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 30c99eaf90..7c54070576 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -430,6 +430,7 @@ + @@ -439,9 +440,26 @@ + + + + + + + + + + + + + + + + + From 38769efe2026b78fff5cca91679040e0897f7d31 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 13 Apr 2012 18:00:28 -0400 Subject: [PATCH 76/99] User Group Permissions development in progress --- .../sipxconfig/rest/RestUtilities.java | 6 +- .../rest/UserGroupPermissionsResource.java | 68 +++++++++++++++++-- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 41f67da3dd..7de8fe3747 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -521,9 +521,9 @@ public BranchRestInfoFull getBranch() { static class SettingBooleanRestInfo { private final String m_name; - private final Boolean m_value; + private final String m_value; - public SettingBooleanRestInfo(String name, Boolean value) { + public SettingBooleanRestInfo(String name, String value) { m_name = name; m_value = value; } @@ -532,7 +532,7 @@ public String getName() { return m_name; } - public Boolean getValue() { + public String getValue() { return m_value; } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java index 5099a4595b..ce33a249af 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -23,6 +23,7 @@ import static org.restlet.data.MediaType.APPLICATION_JSON; import static org.restlet.data.MediaType.TEXT_XML; +import java.security.Permission; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -39,6 +40,8 @@ import org.sipfoundry.sipxconfig.branch.Branch; import org.sipfoundry.sipxconfig.branch.BranchManager; import org.sipfoundry.sipxconfig.common.CoreContext; +import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.permission.PermissionManager; import org.sipfoundry.sipxconfig.permission.PermissionName; import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; @@ -58,6 +61,7 @@ public class UserGroupPermissionsResource extends UserResource { private SettingDao m_settingContext; // saveGroup is not available through corecontext private BranchManager m_branchManager; + private PermissionManager m_permissionManager; private Form m_form; // use to define all possible sort fields @@ -284,14 +288,63 @@ private UserGroupPermissionRestInfoFull createUserGroupPermissionRestInfo(Group private SettingBooleanRestInfo createSettingRestInfo(Group group) { SettingBooleanRestInfo settingRestInfo = null; - String valueString; - Boolean value; + String valueString = "empty"; + boolean value; + List permissions; + + permissions = new ArrayList(m_permissionManager.getCallPermissions()); + + User user = null; + user = getCoreContext().newUser(); + + if (user == null) { + valueString = "user null"; + } + else { + valueString = "getPermissions: "; + for (String permString : user.getPermissions()) { + valueString = valueString + permString + " "; + } + + //user.setSettingValue(PermissionName.findByName("subscribe-to-presence").getPath(), "ENABLE"); + //valueString = valueString + " getName(): " + user.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); + valueString = valueString + " getName(): " + user.getSettingValue(PermissionName.findByName("subscribe-to-presence").getPath()); + } +/* + List temp = new ArrayList(user.getPermissions()); + if (temp == null) { + valueString = "temp null"; + } + + valueString = "getPermissions: "; + for (String permString : user.getPermissions()) { + valueString = valueString + permString + " "; + } + + //valueString = tempString + " - " + user.getSettings().getSetting(tempString).getName(); // + " - " + user.getSettingValue(tempString); + + if (user == null) { + valueString = "user null"; + } + else if (user.getSettings() == null) { + valueString = "getSettings null"; + } + else if (user.getSettings().getSetting(tempString) == null) { + valueString = "getSetting(tempString) null"; + } + else { + valueString = tempString + " - " + user.getSettings().getSetting(tempString).getName(); + } +*/ + + //value = (Boolean) group.getSettingTypedValue(new BooleanSetting(), PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); - valueString = group.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); - value = Boolean.parseBoolean(valueString); + //valueString = group.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); + //value = Boolean.parseBoolean(valueString); + - settingRestInfo = new SettingBooleanRestInfo(PermissionName.SUBSCRIBE_TO_PRESENCE.getName(), value); + settingRestInfo = new SettingBooleanRestInfo(PermissionName.SUBSCRIBE_TO_PRESENCE.getName(), valueString); return settingRestInfo; } @@ -494,4 +547,9 @@ public void setBranchManager(BranchManager branchManager) { m_branchManager = branchManager; } + @Required + public void setPermissionManager(PermissionManager permissionManager) { + m_permissionManager = permissionManager; + } + } From dec689a17bb1495fa7d8e6f74f1261a21e037ca1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 03:56:44 -0400 Subject: [PATCH 77/99] Finish read in User Group Permissions, need update --- .../sipxconfig/rest/RestUtilities.java | 8 +- .../rest/UserGroupPermissionsResource.java | 104 +++++------------- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 2 - 3 files changed, 32 insertions(+), 82 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 7de8fe3747..d1a50bc9a1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -522,10 +522,12 @@ public BranchRestInfoFull getBranch() { static class SettingBooleanRestInfo { private final String m_name; private final String m_value; + private final boolean m_defaultValue; - public SettingBooleanRestInfo(String name, String value) { + public SettingBooleanRestInfo(String name, String value, boolean defaultValue) { m_name = name; m_value = value; + m_defaultValue = defaultValue; } public String getName() { @@ -535,6 +537,10 @@ public String getName() { public String getValue() { return m_value; } + + public boolean getDefaultValue() { + return m_defaultValue; + } } static class UserGroupPermissionRestInfoFull extends UserGroupRestInfo { diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java index ce33a249af..94e0836e75 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -23,8 +23,8 @@ import static org.restlet.data.MediaType.APPLICATION_JSON; import static org.restlet.data.MediaType.TEXT_XML; -import java.security.Permission; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -37,19 +37,14 @@ import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.sipfoundry.sipxconfig.branch.Branch; -import org.sipfoundry.sipxconfig.branch.BranchManager; import org.sipfoundry.sipxconfig.common.CoreContext; -import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.permission.Permission; import org.sipfoundry.sipxconfig.permission.PermissionManager; -import org.sipfoundry.sipxconfig.permission.PermissionName; -import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.SettingBooleanRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupPermissionRestInfoFull; -import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.sipfoundry.sipxconfig.setting.Group; import org.sipfoundry.sipxconfig.setting.SettingDao; @@ -60,7 +55,6 @@ public class UserGroupPermissionsResource extends UserResource { private SettingDao m_settingContext; // saveGroup is not available through corecontext - private BranchManager m_branchManager; private PermissionManager m_permissionManager; private Form m_form; @@ -277,76 +271,44 @@ private UserGroupPermissionRestInfoFull createUserGroupPermissionRestInfo(int id private UserGroupPermissionRestInfoFull createUserGroupPermissionRestInfo(Group group) { UserGroupPermissionRestInfoFull userGroupPermissionRestInfo = null; - List settings = new ArrayList(); - - settings.add(createSettingRestInfo(group)); + List settings; + settings = createSettingsRestInfo(group); userGroupPermissionRestInfo = new UserGroupPermissionRestInfoFull(group, settings); return userGroupPermissionRestInfo; } - private SettingBooleanRestInfo createSettingRestInfo(Group group) { + private List createSettingsRestInfo(Group group) { + List settings = new ArrayList(); SettingBooleanRestInfo settingRestInfo = null; - String valueString = "empty"; - boolean value; - List permissions; + Collection permissions; + String permissionName; + String permissionValue; + boolean defaultValue; - permissions = new ArrayList(m_permissionManager.getCallPermissions()); + permissions = m_permissionManager.getPermissions(); - User user = null; - user = getCoreContext().newUser(); + // settings value for permissions are ENABLE or DISABLE instead of boolean + for (Permission permission : permissions) { + permissionName = permission.getName(); - if (user == null) { - valueString = "user null"; - } - else { - valueString = "getPermissions: "; - for (String permString : user.getPermissions()) { - valueString = valueString + permString + " "; + try { + // empty return means setting is at default (unless error in input to getSettingValue) + //permissionValue = group.getSettingValue(PermissionName.findByName(permissionName).getPath()); + permissionValue = group.getSettingValue(permission.getSettingPath()); + } + catch (Exception exception) { + permissionValue = "GetSettingValue error: " + exception.getLocalizedMessage(); } - //user.setSettingValue(PermissionName.findByName("subscribe-to-presence").getPath(), "ENABLE"); - //valueString = valueString + " getName(): " + user.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); - valueString = valueString + " getName(): " + user.getSettingValue(PermissionName.findByName("subscribe-to-presence").getPath()); - } -/* - List temp = new ArrayList(user.getPermissions()); - if (temp == null) { - valueString = "temp null"; - } + defaultValue = permission.getDefaultValue(); - valueString = "getPermissions: "; - for (String permString : user.getPermissions()) { - valueString = valueString + permString + " "; + settingRestInfo = new SettingBooleanRestInfo(permissionName, permissionValue, defaultValue); + settings.add(settingRestInfo); } - //valueString = tempString + " - " + user.getSettings().getSetting(tempString).getName(); // + " - " + user.getSettingValue(tempString); - - if (user == null) { - valueString = "user null"; - } - else if (user.getSettings() == null) { - valueString = "getSettings null"; - } - else if (user.getSettings().getSetting(tempString) == null) { - valueString = "getSetting(tempString) null"; - } - else { - valueString = tempString + " - " + user.getSettings().getSetting(tempString).getName(); - } -*/ - - - - //value = (Boolean) group.getSettingTypedValue(new BooleanSetting(), PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); - //valueString = group.getSettingValue(PermissionName.SUBSCRIBE_TO_PRESENCE.getPath()); - //value = Boolean.parseBoolean(valueString); - - - settingRestInfo = new SettingBooleanRestInfo(PermissionName.SUBSCRIBE_TO_PRESENCE.getName(), valueString); - - return settingRestInfo; + return settings; } private MetadataRestInfo addUserGroups(List userGroupPermissionsRestInfo, List userGroups) { @@ -461,17 +423,6 @@ private Group createUserGroupPermission(UserGroupPermissionRestInfoFull userGrou return userGroup; } - private Branch getBranch(UserGroupRestInfoFull userGroupRestInfo) { - Branch branch = null; - BranchRestInfoFull branchRestInfo = userGroupRestInfo.getBranch(); - - if (branchRestInfo != null) { - branch = m_branchManager.getBranch(branchRestInfo.getId()); - } - - return branch; - } - // REST Representations // -------------------- @@ -542,11 +493,6 @@ public void setSettingDao(SettingDao settingContext) { m_settingContext = settingContext; } - @Required - public void setBranchManager(BranchManager branchManager) { - m_branchManager = branchManager; - } - @Required public void setPermissionManager(PermissionManager permissionManager) { m_permissionManager = permissionManager; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 7c54070576..01e656db8c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -429,7 +429,6 @@ - @@ -439,7 +438,6 @@ - From fa3ec46ebd875d42750f088b6a828b5476243e59 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 04:03:31 -0400 Subject: [PATCH 78/99] Remove delete --- .../rest/UserGroupPermissionsResource.java | 48 ++----------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java index 94e0836e75..19c85b5dd0 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -101,13 +101,8 @@ public boolean allowPut() { return true; } - @Override - public boolean allowDelete() { - return true; - } - - // GET - Retrieve all and single Skill - // ----------------------------------- + // GET - Retrieve all and single User Group with Permissions + // --------------------------------------------------------- @Override public Representation represent(Variant variant) throws ResourceException { @@ -153,8 +148,8 @@ public Representation represent(Variant variant) throws ResourceException { return new UserGroupPermissionsRepresentation(variant.getMediaType(), userGroupPermissionsBundleRestInfo); } - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update Permissions + // ------------------------ @Override public void storeRepresentation(Representation entity) throws ResourceException { @@ -215,41 +210,6 @@ public void storeRepresentation(Representation entity) throws ResourceException } - // DELETE - Delete single Skill - // ---------------------------- - - @Override - public void removeRepresentations() throws ResourceException { - Group userGroup; - int idInt; - - // get id then delete single - String idString = (String) getRequest().getAttributes().get("id"); - - if (idString != null) { - try { - idInt = RestUtilities.getIntFromAttribute(idString); - userGroup = m_settingContext.getGroup(idInt); - } - catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); - return; - } - - List userGroupIds = new ArrayList(); - userGroupIds.add(idInt); - m_settingContext.deleteGroups(userGroupIds); - - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted User Group Permissions", userGroup.getId()); - - return; - } - - // no id string - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.ERROR_MISSING_INPUT, "ID value missing"); - } - - // Helper functions // ---------------- From 9d7bd451d35ecb0a96845b1db78fb22bc0577ab8 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 10:57:13 -0400 Subject: [PATCH 79/99] Change REST xml naming of groups for consistency --- .../sipxconfig/rest/UserGroupsResource.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java index e75a7225c1..f72ad31d0c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupsResource.java @@ -441,7 +441,7 @@ public UserGroupsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("user-group", UserGroupsBundleRestInfo.class); - xstream.alias("userGroup", UserGroupRestInfoFull.class); + xstream.alias("group", UserGroupRestInfoFull.class); } } @@ -457,7 +457,7 @@ public UserGroupRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("userGroup", UserGroupRestInfoFull.class); + xstream.alias("group", UserGroupRestInfoFull.class); } } @@ -467,19 +467,19 @@ protected void configureXStream(XStream xstream) { static class UserGroupsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_userGroups; + private final List m_groups; public UserGroupsBundleRestInfo(List userGroups, MetadataRestInfo metadata) { m_metadata = metadata; - m_userGroups = userGroups; + m_groups = userGroups; } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getUserGroups() { - return m_userGroups; + public List getGroups() { + return m_groups; } } From 6d4eb104e00ac030edece6419e00ec2ad9a3d39b Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 11:36:43 -0400 Subject: [PATCH 80/99] Delete checks for skills pointing to group and fails if any exist --- .../rest/OpenAcdSkillGroupsResource.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 6b15006433..2d085c6d8f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -217,11 +217,12 @@ public void storeRepresentation(Representation entity) throws ResourceException // DELETE - Delete single // ---------------------- - // deleteSkillGroup() not available from openAcdContext @Override public void removeRepresentations() throws ResourceException { - // for some reason skill groups are deleted by providing collection of ids, not by - // providing skill group object + OpenAcdSkillGroupRestInfo skillGroupRestInfo; + List skills; + + // skill groups are deleted by providing collection of ids, not by providing skill group object Collection skillGroupIds = new HashSet(); int idInt; @@ -238,6 +239,15 @@ public void removeRepresentations() throws ResourceException { return; } + // sipxconfig ui does not allow delete of group with existing skills + skills = m_openAcdContext.getSkills(); + for (OpenAcdSkill skill : skills) { + if (skill.getGroup().getId() == idInt) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Skill " + skill.getName() + " still refers to this group."); + return; + } + } + m_openAcdContext.removeSkillGroups(skillGroupIds); RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill Group", idInt); From c0df1fb04572a909eac22f4a1b4eec0d3780c2ce Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 12:00:02 -0400 Subject: [PATCH 81/99] Correct naming of REST elements --- .../sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java index fcaac73510..9c1b787b73 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java @@ -426,7 +426,7 @@ public OpenAcdDialStringsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("openacd-dial-string", OpenAcdDialStringsBundleRestInfo.class); - xstream.alias("dial-string", OpenAcdDialStringRestInfo.class); + xstream.alias("dialString", OpenAcdDialStringRestInfo.class); xstream.alias("action", OpenAcdDialStringActionRestInfo.class); } } From 69568e06b8d34938a86302b551af4350ec4dc7e7 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Tue, 17 Apr 2012 19:12:36 -0400 Subject: [PATCH 82/99] Add update of Permissions --- .../rest/UserGroupPermissionsResource.java | 51 +++++-------------- 1 file changed, 13 insertions(+), 38 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java index 19c85b5dd0..8bc8b91334 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -37,7 +37,6 @@ import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; -import org.sipfoundry.sipxconfig.common.CoreContext; import org.sipfoundry.sipxconfig.permission.Permission; import org.sipfoundry.sipxconfig.permission.PermissionManager; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; @@ -196,17 +195,8 @@ public void storeRepresentation(Representation entity) throws ResourceException } - // otherwise add new - try { - userGroup = createUserGroupPermission(userGroupPermissionRestInfo); - m_settingContext.saveGroup(userGroup); - } - catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create User Group Permissions failed", exception.getLocalizedMessage()); - return; - } - - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created User Group permissions", userGroup.getId()); + // otherwise error, since no creation of new permissions + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Missing ID"); } @@ -359,28 +349,13 @@ public int compare(Object object1, Object object2) { } private void updateUserGroupPermission(Group userGroup, UserGroupPermissionRestInfoFull userGroupPermissionRestInfo) { - String tempString; + Permission permission; - // do not allow empty name - tempString = userGroupPermissionRestInfo.getName(); - if (!tempString.isEmpty()) { - userGroup.setName(tempString); + // update each permission setting + for (SettingBooleanRestInfo settingRestInfo : userGroupPermissionRestInfo.getSettings()) { + permission = m_permissionManager.getPermissionByName(settingRestInfo.getName()); + userGroup.setSettingValue(permission.getSettingPath(), settingRestInfo.getValue()); } - - userGroup.setDescription(userGroupPermissionRestInfo.getDescription()); - } - - private Group createUserGroupPermission(UserGroupPermissionRestInfoFull userGroupPermissionRestInfo) { - Group userGroup = new Group(); - - // copy fields from rest info - userGroup.setName(userGroupPermissionRestInfo.getName()); - userGroup.setDescription(userGroupPermissionRestInfo.getDescription()); - - // apparently there is a special Resource value for user groups - userGroup.setResource(CoreContext.USER_GROUP_RESOURCE_ID); - - return userGroup; } @@ -400,7 +375,7 @@ public UserGroupPermissionsRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { xstream.alias("user-group-permission", UserGroupPermissionsBundleRestInfo.class); - xstream.alias("userGroup", UserGroupPermissionRestInfoFull.class); + xstream.alias("group", UserGroupPermissionRestInfoFull.class); xstream.alias("setting", SettingBooleanRestInfo.class); } } @@ -417,7 +392,7 @@ public UserGroupPermissionRepresentation(Representation representation) { @Override protected void configureXStream(XStream xstream) { - xstream.alias("userGroup", UserGroupPermissionRestInfoFull.class); + xstream.alias("group", UserGroupPermissionRestInfoFull.class); xstream.alias("setting", SettingBooleanRestInfo.class); } } @@ -428,19 +403,19 @@ protected void configureXStream(XStream xstream) { static class UserGroupPermissionsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_userGroupPermissions; + private final List m_permissions; public UserGroupPermissionsBundleRestInfo(List userGroupPermissions, MetadataRestInfo metadata) { m_metadata = metadata; - m_userGroupPermissions = userGroupPermissions; + m_permissions = userGroupPermissions; } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getUserGroupPermissions() { - return m_userGroupPermissions; + public List getPermissions() { + return m_permissions; } } From 5fc7662b0a00fc6f672d8da1b48ee9b3c1420e7e Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Wed, 18 Apr 2012 02:27:51 -0400 Subject: [PATCH 83/99] Complete update of permissions, change REST element names --- .../org/sipfoundry/sipxconfig/rest/RestUtilities.java | 8 ++++---- .../sipxconfig/rest/UserGroupPermissionsResource.java | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index d1a50bc9a1..be1dd93842 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -544,16 +544,16 @@ public boolean getDefaultValue() { } static class UserGroupPermissionRestInfoFull extends UserGroupRestInfo { - private final List m_settings; + private final List m_permissions; public UserGroupPermissionRestInfoFull(Group userGroup, List settingsRestInfo) { super(userGroup); - m_settings = settingsRestInfo; + m_permissions = settingsRestInfo; } - public List getSettings() { - return m_settings; + public List getPermissions() { + return m_permissions; } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java index 8bc8b91334..25072f5b90 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserGroupPermissionsResource.java @@ -352,7 +352,7 @@ private void updateUserGroupPermission(Group userGroup, UserGroupPermissionRestI Permission permission; // update each permission setting - for (SettingBooleanRestInfo settingRestInfo : userGroupPermissionRestInfo.getSettings()) { + for (SettingBooleanRestInfo settingRestInfo : userGroupPermissionRestInfo.getPermissions()) { permission = m_permissionManager.getPermissionByName(settingRestInfo.getName()); userGroup.setSettingValue(permission.getSettingPath(), settingRestInfo.getValue()); } @@ -403,19 +403,19 @@ protected void configureXStream(XStream xstream) { static class UserGroupPermissionsBundleRestInfo { private final MetadataRestInfo m_metadata; - private final List m_permissions; + private final List m_groups; public UserGroupPermissionsBundleRestInfo(List userGroupPermissions, MetadataRestInfo metadata) { m_metadata = metadata; - m_permissions = userGroupPermissions; + m_groups = userGroupPermissions; } public MetadataRestInfo getMetadata() { return m_metadata; } - public List getPermissions() { - return m_permissions; + public List getGroups() { + return m_groups; } } From c302f25fd8aa424283808e457a5fb40207e877a1 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 19 Apr 2012 12:56:05 -0400 Subject: [PATCH 84/99] Add User Permissions REST API --- .../rest/UserPermissionsResource.java | 473 ++++++++++++++++++ .../sipfoundry/sipxconfig/rest/rest.beans.xml | 30 +- 2 files changed, 496 insertions(+), 7 deletions(-) create mode 100644 sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java new file mode 100644 index 0000000000..bd0a300e5c --- /dev/null +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java @@ -0,0 +1,473 @@ +/* + * + * UserGroupPermissionsResource.java - A Restlet to read User Group data with Permissions from SipXecs + * Copyright (C) 2012 PATLive, D. Chang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +package org.sipfoundry.sipxconfig.rest; + +import static org.restlet.data.MediaType.APPLICATION_JSON; +import static org.restlet.data.MediaType.TEXT_XML; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.restlet.Context; +import org.restlet.data.Form; +import org.restlet.data.MediaType; +import org.restlet.data.Request; +import org.restlet.data.Response; +import org.restlet.resource.Representation; +import org.restlet.resource.ResourceException; +import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.permission.Permission; +import org.sipfoundry.sipxconfig.permission.PermissionManager; +import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SettingBooleanRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.springframework.beans.factory.annotation.Required; + +import com.thoughtworks.xstream.XStream; + +public class UserPermissionsResource extends UserResource { + + //private SettingDao m_settingContext; // saveGroup is not available through corecontext + private PermissionManager m_permissionManager; + private Form m_form; + + // use to define all possible sort fields + private enum SortField { + LASTNAME, FIRSTNAME, NONE; + + public static SortField toSortField(String fieldString) { + if (fieldString == null) { + return NONE; + } + + try { + return valueOf(fieldString.toUpperCase()); + } + catch (Exception ex) { + return NONE; + } + } + } + + + @Override + public void init(Context context, Request request, Response response) { + super.init(context, request, response); + getVariants().add(new Variant(TEXT_XML)); + getVariants().add(new Variant(APPLICATION_JSON)); + + // pull parameters from url + m_form = getRequest().getResourceRef().getQueryAsForm(); + } + + + // Allowed REST operations + // ----------------------- + + @Override + public boolean allowGet() { + return true; + } + + @Override + public boolean allowPut() { + return true; + } + + // GET - Retrieve all and single User with Permissions + // --------------------------------------------------- + + @Override + public Representation represent(Variant variant) throws ResourceException { + // process request for single + int idInt; + UserPermissionRestInfoFull userPermissionRestInfo = 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 { + userPermissionRestInfo = createUserPermissionRestInfo(idInt); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read User failed", exception.getLocalizedMessage()); + } + + return new UserPermissionRepresentation(variant.getMediaType(), userPermissionRestInfo); + } + + + // if not single, process request for all + List users = getCoreContext().getSharedUsers(); // no GetUsers() in coreContext, instead some subgroups + + List userPermissionsRestInfo = new ArrayList(); + MetadataRestInfo metadataRestInfo; + + // sort if specified + sortUsers(users); + + // set requested items and get resulting metadata + metadataRestInfo = addUsers(userPermissionsRestInfo, users); + + // create final restinfo + UserPermissionsBundleRestInfo userPermissionsBundleRestInfo = new UserPermissionsBundleRestInfo(userPermissionsRestInfo, metadataRestInfo); + + return new UserPermissionsRepresentation(variant.getMediaType(), userPermissionsBundleRestInfo); + } + + + // PUT - Update Permissions + // ------------------------ + + @Override + public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body + UserPermissionRepresentation representation = new UserPermissionRepresentation(entity); + UserPermissionRestInfoFull userPermissionRestInfo = representation.getObject(); + User user = null; + + // validate input for update or create + ValidationInfo validationInfo = validate(userPermissionRestInfo); + + 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); + user = getCoreContext().getUser(idInt); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); + return; + } + + // copy values over to existing + try { + updateUserPermission(user, userPermissionRestInfo); + getCoreContext().saveUser(user); + } + catch (Exception exception) { + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update User Permissions failed", exception.getLocalizedMessage()); + return; + } + + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated User Permissions", user.getId()); + + return; + } + + + // otherwise error, since no creation of new permissions + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Missing ID"); + } + + + // Helper functions + // ---------------- + + // basic interface level validation of data provided through REST interface for creation or + // update + // may also contain clean up of input data + // may create another validation function if different rules needed for update v. create + private ValidationInfo validate(UserPermissionRestInfoFull restInfo) { + ValidationInfo validationInfo = new ValidationInfo(); + + return validationInfo; + } + + private UserPermissionRestInfoFull createUserPermissionRestInfo(int id) { + User user = getCoreContext().getUser(id); + + return createUserPermissionRestInfo(user); + } + + private UserPermissionRestInfoFull createUserPermissionRestInfo(User user) { + UserPermissionRestInfoFull userPermissionRestInfo = null; + List settings; + + settings = createSettingsRestInfo(user); + userPermissionRestInfo = new UserPermissionRestInfoFull(user, settings); + + return userPermissionRestInfo; + } + + private List createSettingsRestInfo(User user) { + List settings = new ArrayList(); + SettingBooleanRestInfo settingRestInfo = null; + Collection permissions; + String permissionName; + String permissionValue; + boolean defaultValue; + + permissions = m_permissionManager.getPermissions(); + + // settings value for permissions are ENABLE or DISABLE instead of boolean + for (Permission permission : permissions) { + permissionName = permission.getName(); + + try { + // empty return means setting is at default (unless error in input to getSettingValue) + //permissionValue = group.getSettingValue(PermissionName.findByName(permissionName).getPath()); + permissionValue = user.getSettingValue(permission.getSettingPath()); + } + catch (Exception exception) { + permissionValue = "GetSettingValue error: " + exception.getLocalizedMessage(); + } + + defaultValue = permission.getDefaultValue(); + + settingRestInfo = new SettingBooleanRestInfo(permissionName, permissionValue, defaultValue); + settings.add(settingRestInfo); + } + + return settings; + } + + private MetadataRestInfo addUsers(List userPermissionsRestInfo, List users) { + UserPermissionRestInfoFull userPermissionRestInfo; + + // determine pagination + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, users.size()); + + // create list of restinfos + for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { + User user = users.get(index); + + userPermissionRestInfo = createUserPermissionRestInfo(user); + userPermissionsRestInfo.add(userPermissionRestInfo); + } + + // create metadata about restinfos + MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); + return metadata; + } + + private void sortUsers(List users) { + // sort if requested + SortInfo sortInfo = RestUtilities.calculateSorting(m_form); + + if (!sortInfo.sort) { + return; + } + + SortField sortField = SortField.toSortField(sortInfo.sortField); + + if (sortInfo.directionForward) { + + switch (sortField) { + case LASTNAME: + Collections.sort(users, new Comparator() { + + public int compare(Object object1, Object object2) { + User user1 = (User) object1; + User user2 = (User) object2; + return user1.getLastName().compareToIgnoreCase(user2.getLastName()); + } + + }); + break; + + case FIRSTNAME: + Collections.sort(users, new Comparator() { + + public int compare(Object object1, Object object2) { + User user1 = (User) object1; + User user2 = (User) object2; + return user1.getFirstName().compareToIgnoreCase(user2.getFirstName()); + } + + }); + break; + } + } + else { + // must be reverse + switch (sortField) { + case LASTNAME: + Collections.sort(users, new Comparator() { + + public int compare(Object object1, Object object2) { + User user1 = (User) object1; + User user2 = (User) object2; + return user2.getLastName().compareToIgnoreCase(user1.getLastName()); + } + + }); + break; + + case FIRSTNAME: + Collections.sort(users, new Comparator() { + + public int compare(Object object1, Object object2) { + User user1 = (User) object1; + User user2 = (User) object2; + return user2.getFirstName().compareToIgnoreCase(user1.getFirstName()); + } + + }); + break; + } + } + } + + private void updateUserPermission(User user, UserPermissionRestInfoFull userPermissionRestInfo) { + Permission permission; + + // update each permission setting + for (SettingBooleanRestInfo settingRestInfo : userPermissionRestInfo.getPermissions()) { + permission = m_permissionManager.getPermissionByName(settingRestInfo.getName()); + user.setSettingValue(permission.getSettingPath(), settingRestInfo.getValue()); + } + } + + + // REST Representations + // -------------------- + + static class UserPermissionsRepresentation extends XStreamRepresentation { + + public UserPermissionsRepresentation(MediaType mediaType, UserPermissionsBundleRestInfo object) { + super(mediaType, object); + } + + public UserPermissionsRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("user-permission", UserPermissionsBundleRestInfo.class); + xstream.alias("user", UserPermissionRestInfoFull.class); + xstream.alias("setting", SettingBooleanRestInfo.class); + } + } + + static class UserPermissionRepresentation extends XStreamRepresentation { + + public UserPermissionRepresentation(MediaType mediaType, UserPermissionRestInfoFull object) { + super(mediaType, object); + } + + public UserPermissionRepresentation(Representation representation) { + super(representation); + } + + @Override + protected void configureXStream(XStream xstream) { + xstream.alias("user", UserPermissionRestInfoFull.class); + xstream.alias("setting", SettingBooleanRestInfo.class); + } + } + + + // REST info objects + // ----------------- + + static class UserPermissionsBundleRestInfo { + private final MetadataRestInfo m_metadata; + private final List m_users; + + public UserPermissionsBundleRestInfo(List userPermissions, MetadataRestInfo metadata) { + m_metadata = metadata; + m_users = userPermissions; + } + + public MetadataRestInfo getMetadata() { + return m_metadata; + } + + public List getUsers() { + return m_users; + } + } + + static class UserRestInfo { + private final int m_id; + private final String m_lastName; + private final String m_firstName; + + public UserRestInfo(User user) { + m_id = user.getId(); + m_lastName = user.getLastName(); + m_firstName = user.getFirstName(); + } + + public int getId() { + return m_id; + } + + public String getLastName() { + return m_lastName; + } + + public String getFirstName() { + return m_firstName; + } + } + + static class UserPermissionRestInfoFull extends UserRestInfo { + private final List m_permissions; + + public UserPermissionRestInfoFull(User user, List settingsRestInfo) { + super(user); + + m_permissions = settingsRestInfo; + } + + public List getPermissions() { + return m_permissions; + } + } + + + // Injected objects + // ---------------- + +// @Required +// public void setSettingDao(SettingDao settingContext) { +// m_settingContext = settingContext; +// } + + @Required + public void setPermissionManager(PermissionManager permissionManager) { + m_permissionManager = permissionManager; + } + +} diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 01e656db8c..565dbca2f6 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -375,6 +375,22 @@ + + + + + + + + + + + + + + + + @@ -444,20 +460,20 @@ - + - + - - - + + + - + - + From e5fd6c05b859e1ffaf0000f1933affd07a0948fc Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 19 Apr 2012 13:45:35 -0400 Subject: [PATCH 85/99] Finish User Permissions for all users, need filtering by branch, ids --- .../org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java index bd0a300e5c..120dd3ef9e 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java @@ -128,7 +128,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all - List users = getCoreContext().getSharedUsers(); // no GetUsers() in coreContext, instead some subgroups + List users = getCoreContext().loadUsersByPage(1, 10); // no GetUsers() in coreContext, instead some subgroups List userPermissionsRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; From 32ea3678c0d63f973d41ed0df81e953ec3529fd6 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 19 Apr 2012 13:58:42 -0400 Subject: [PATCH 86/99] Correct limit on User page size --- .../org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java index 120dd3ef9e..61f28ae095 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java @@ -128,7 +128,7 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, process request for all - List users = getCoreContext().loadUsersByPage(1, 10); // no GetUsers() in coreContext, instead some subgroups + List users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups List userPermissionsRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; From c9bf26368f0445e8b8e1e5216a6350b403efed66 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Thu, 19 Apr 2012 16:18:39 -0400 Subject: [PATCH 87/99] Add filter by Branch --- .../rest/UserPermissionsResource.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java index 61f28ae095..24fa34e207 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java @@ -51,7 +51,6 @@ public class UserPermissionsResource extends UserResource { - //private SettingDao m_settingContext; // saveGroup is not available through corecontext private PermissionManager m_permissionManager; private Form m_form; @@ -127,8 +126,27 @@ public Representation represent(Variant variant) throws ResourceException { } - // if not single, process request for all - List users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups + // if not single, check if need to filter list + List users; + Collection userIds; + String branchIdString = m_form.getFirstValue("branch"); + int branchId; + + if ((branchIdString != null) && (branchIdString != "")) { + try { + branchId = RestUtilities.getIntFromAttribute(branchIdString); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Branch ID " + branchIdString + " not found."); + } + + userIds = getCoreContext().getBranchMembersByPage(branchId, 0, getCoreContext().getBranchMembersCount(branchId)); + users = getUsers(userIds); + } + else { + // process request for all + users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups + } List userPermissionsRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; @@ -145,7 +163,6 @@ public Representation represent(Variant variant) throws ResourceException { return new UserPermissionsRepresentation(variant.getMediaType(), userPermissionsBundleRestInfo); } - // PUT - Update Permissions // ------------------------ @@ -347,6 +364,17 @@ public int compare(Object object1, Object object2) { } } + private List getUsers(Collection userIds) { + List users; + + users = new ArrayList(); + for (int userId : userIds) { + users.add(getCoreContext().getUser(userId)); + } + + return users; + } + private void updateUserPermission(User user, UserPermissionRestInfoFull userPermissionRestInfo) { Permission permission; @@ -460,11 +488,6 @@ public List getPermissions() { // Injected objects // ---------------- -// @Required -// public void setSettingDao(SettingDao settingContext) { -// m_settingContext = settingContext; -// } - @Required public void setPermissionManager(PermissionManager permissionManager) { m_permissionManager = permissionManager; From 9eb73e469cf5ab126c739e2e255ee3fc7f801f18 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 20 Apr 2012 01:01:04 -0400 Subject: [PATCH 88/99] Add Users REST API, still need aliases and GET single --- .../sipxconfig/rest/RestUtilities.java | 24 +- .../sipxconfig/rest/UsersResource.java | 357 +++++++++++------- .../sipfoundry/sipxconfig/rest/rest.beans.xml | 16 + 3 files changed, 249 insertions(+), 148 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index be1dd93842..14fc49ac23 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -439,21 +439,15 @@ public boolean getBuiltIn() { } } - static class BranchRestInfoFull { + static class BranchRestInfo { private final int m_id; private final String m_name; private final String m_description; - private final Address m_address; - private final String m_phoneNumber; - private final String m_faxNumber; - public BranchRestInfoFull(Branch branch) { + public BranchRestInfo(Branch branch) { m_id = branch.getId(); m_name = branch.getName(); m_description = branch.getDescription(); - m_address = branch.getAddress(); - m_phoneNumber = branch.getPhoneNumber(); - m_faxNumber = branch.getFaxNumber(); } public int getId() { @@ -467,6 +461,20 @@ public String getName() { public String getDescription() { return m_description; } + } + + static class BranchRestInfoFull extends BranchRestInfo { + private final Address m_address; + private final String m_phoneNumber; + private final String m_faxNumber; + + public BranchRestInfoFull(Branch branch) { + super(branch); + + m_address = branch.getAddress(); + m_phoneNumber = branch.getPhoneNumber(); + m_faxNumber = branch.getFaxNumber(); + } public Address getAddress() { return m_address; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java index d8ea4da19a..79b918faca 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java @@ -24,9 +24,12 @@ import static org.restlet.data.MediaType.TEXT_XML; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.restlet.Context; import org.restlet.data.Form; @@ -36,29 +39,29 @@ import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; +import org.sipfoundry.sipxconfig.branch.Branch; +import org.sipfoundry.sipxconfig.branch.BranchManager; import org.sipfoundry.sipxconfig.common.User; -import org.sipfoundry.sipxconfig.openacd.OpenAcdContext; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkill; -import org.sipfoundry.sipxconfig.openacd.OpenAcdSkillGroup; -import org.sipfoundry.sipxconfig.rest.OpenAcdSkillsResource.OpenAcdSkillsBundleRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.OpenAcdSkillRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; -import org.sipfoundry.sipxconfig.rest.RestUtilities.ResponseCode; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; +import org.sipfoundry.sipxconfig.setting.Group; import org.springframework.beans.factory.annotation.Required; import com.thoughtworks.xstream.XStream; public class UsersResource extends UserResource { - private OpenAcdContext m_openAcdContext; + private BranchManager m_branchManager; private Form m_form; // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, ATOM, NONE; + USERNAME, LASTNAME, FIRSTNAME, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -104,14 +107,14 @@ public boolean allowDelete() { return true; } - // GET - Retrieve all and single Skill - // ----------------------------------- + // GET - Retrieve all and single User + // ---------------------------------- @Override public Representation represent(Variant variant) throws ResourceException { // process request for single int idInt; - OpenAcdSkillRestInfoFull skillRestInfo = null; + UserRestInfoFull userRestInfo = null; String idString = (String) getRequest().getAttributes().get("id"); if (idString != null) { @@ -123,46 +126,66 @@ public Representation represent(Variant variant) throws ResourceException { } try { - skillRestInfo = createSkillRestInfo(idInt); + userRestInfo = createUserRestInfo(idInt); } catch (Exception exception) { - return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read Skills failed", exception.getLocalizedMessage()); + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_READ_FAILED, "Read User failed", exception.getLocalizedMessage()); } - return new OpenAcdSkillRepresentation(variant.getMediaType(), skillRestInfo); + return new UserRepresentation(variant.getMediaType(), userRestInfo); } - // if not single, process request for all - List skills = m_openAcdContext.getSkills(); - List skillsRestInfo = new ArrayList(); + // if not single, check if need to filter list + List users; + + Collection userIds; + String branchIdString = m_form.getFirstValue("branch"); + int branchId; + + if ((branchIdString != null) && (branchIdString != "")) { + try { + branchId = RestUtilities.getIntFromAttribute(branchIdString); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "Branch ID " + branchIdString + " not found."); + } + + userIds = getCoreContext().getBranchMembersByPage(branchId, 0, getCoreContext().getBranchMembersCount(branchId)); + users = getUsers(userIds); + } + else { + // process request for all + users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups + } + + List usersRestInfo = new ArrayList(); MetadataRestInfo metadataRestInfo; - // sort groups if specified - sortSkills(skills); + // sort if specified + sortUsers(users); - // set requested agents groups and get resulting metadata - metadataRestInfo = addSkills(skillsRestInfo, skills); + // set requested items and get resulting metadata + metadataRestInfo = addUsers(usersRestInfo, users); // create final restinfo - OpenAcdSkillsBundleRestInfo skillsBundleRestInfo = new OpenAcdSkillsBundleRestInfo(skillsRestInfo, metadataRestInfo); + UsersBundleRestInfo usersBundleRestInfo = new UsersBundleRestInfo(usersRestInfo, metadataRestInfo); - return new OpenAcdSkillsRepresentation(variant.getMediaType(), skillsBundleRestInfo); + return new UsersRepresentation(variant.getMediaType(), usersBundleRestInfo); } - - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update or Add single User + // ------------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { // get from request body - OpenAcdSkillRepresentation representation = new OpenAcdSkillRepresentation(entity); - OpenAcdSkillRestInfoFull skillRestInfo = representation.getObject(); - OpenAcdSkill skill = null; + UserRepresentation representation = new UserRepresentation(entity); + UserRestInfoFull userRestInfo = representation.getObject(); + User user = null; // validate input for update or create - ValidationInfo validationInfo = validate(skillRestInfo); + ValidationInfo validationInfo = validate(userRestInfo); if (!validationInfo.valid) { RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); @@ -176,7 +199,7 @@ public void storeRepresentation(Representation entity) throws ResourceException if (idString != null) { try { int idInt = RestUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + user = getCoreContext().getUser(idInt); } catch (Exception exception) { RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); @@ -185,15 +208,15 @@ public void storeRepresentation(Representation entity) throws ResourceException // copy values over to existing try { - updateSkill(skill, skillRestInfo); - m_openAcdContext.saveSkill(skill); + updateUser(user, userRestInfo); + getCoreContext().saveUser(user); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Update User failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "Updated User", user.getId()); return; } @@ -201,24 +224,24 @@ public void storeRepresentation(Representation entity) throws ResourceException // otherwise add new try { - skill = createSkill(skillRestInfo); - m_openAcdContext.saveSkill(skill); + user = createUser(userRestInfo); + getCoreContext().saveUser(user); } catch (Exception exception) { - RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create Skill failed", exception.getLocalizedMessage()); + RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_WRITE_FAILED, "Create User failed", exception.getLocalizedMessage()); return; } - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_CREATED, "Created User", user.getId()); } - // DELETE - Delete single Skill - // ---------------------------- + // DELETE - Delete single User + // --------------------------- @Override public void removeRepresentations() throws ResourceException { - OpenAcdSkill skill; + User user; // get id then delete single String idString = (String) getRequest().getAttributes().get("id"); @@ -226,16 +249,16 @@ public void removeRepresentations() throws ResourceException { if (idString != null) { try { int idInt = RestUtilities.getIntFromAttribute(idString); - skill = m_openAcdContext.getSkillById(idInt); + user = getCoreContext().getUser(idInt); } catch (Exception exception) { RestUtilities.setResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + idString + " not found."); return; } - m_openAcdContext.deleteSkill(skill); + getCoreContext().deleteUser(user); - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted Skill", skill.getId()); + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_DELETED, "Deleted User", user.getId()); return; } @@ -252,60 +275,71 @@ public void removeRepresentations() throws ResourceException { // update // may also contain clean up of input data // may create another validation function if different rules needed for update v. create - private ValidationInfo validate(OpenAcdSkillRestInfoFull restInfo) { + private ValidationInfo validate(UserRestInfoFull restInfo) { ValidationInfo validationInfo = new ValidationInfo(); - String name = restInfo.getName(); - String atom = restInfo.getAtom(); + return validationInfo; + } - for (int i = 0; i < name.length(); i++) { - if ((!Character.isLetterOrDigit(name.charAt(i)) && !(Character.getType(name.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && name.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: Skill Group 'Name' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } + private UserRestInfoFull createUserRestInfo(int id) { + User user = getCoreContext().getUser(id); + + return createUserRestInfo(user); + } - for (int i = 0; i < atom.length(); i++) { - if ((!Character.isLetterOrDigit(atom.charAt(i)) && !(Character.getType(atom.charAt(i)) == Character.CONNECTOR_PUNCTUATION)) && atom.charAt(i) != '-') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Atom' must only contain letters, numbers, dashes, and underscores"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + private UserRestInfoFull createUserRestInfo(User user) { + UserRestInfoFull userRestInfo = null; + UserGroupRestInfo userGroupRestInfo = null; + List userGroupsRestInfo = new ArrayList(); + Set groups = null; + BranchRestInfo branchRestInfo = null; + Branch branch = null; + + groups = user.getGroups(); + + // user does not necessarily have any groups + if ((groups != null) && (!groups.isEmpty())) { + for (Group group : groups) { + userGroupRestInfo = new UserGroupRestInfo(group); + userGroupsRestInfo.add(userGroupRestInfo); } } - return validationInfo; - } + branch = user.getBranch(); - private OpenAcdSkillRestInfoFull createSkillRestInfo(int id) throws ResourceException { - OpenAcdSkillRestInfoFull skillRestInfo = null; + // user does not necessarily have branch + if (branch != null) { + branchRestInfo = new BranchRestInfo(branch); + } - OpenAcdSkill skill = m_openAcdContext.getSkillById(id); - skillRestInfo = new OpenAcdSkillRestInfoFull(skill); + userRestInfo = new UserRestInfoFull(user, userGroupsRestInfo, branchRestInfo); - return skillRestInfo; + return userRestInfo; } - private MetadataRestInfo addSkills(List skillsRestInfo, List skills) { - OpenAcdSkillRestInfoFull skillRestInfo; + private MetadataRestInfo addUsers(List usersRestInfo, List users) { + UserRestInfoFull userRestInfo; + User user; // determine pagination - PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, skills.size()); + PaginationInfo paginationInfo = RestUtilities.calculatePagination(m_form, users.size()); + // create list of skill restinfos for (int index = paginationInfo.startIndex; index <= paginationInfo.endIndex; index++) { - OpenAcdSkill skill = skills.get(index); + user = users.get(index); - skillRestInfo = new OpenAcdSkillRestInfoFull(skill); - skillsRestInfo.add(skillRestInfo); + userRestInfo = createUserRestInfo(user); + usersRestInfo.add(userRestInfo); } + // create metadata about agent groups MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); return metadata; } - private void sortSkills(List skills) { + private void sortUsers(List users) { // sort groups if requested SortInfo sortInfo = RestUtilities.calculateSorting(m_form); @@ -318,38 +352,38 @@ private void sortSkills(List skills) { if (sortInfo.directionForward) { switch (sortField) { - case NAME: - Collections.sort(skills, new Comparator() { + case USERNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getName().compareToIgnoreCase(skill2.getName()); + User user1 = (User) object1; + User user2 = (User) object2; + return user1.getUserName().compareToIgnoreCase(user2.getUserName()); } }); break; - case DESCRIPTION: - Collections.sort(skills, new Comparator() { + case LASTNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); + User user1 = (User) object1; + User user2 = (User) object2; + return user1.getLastName().compareToIgnoreCase(user2.getLastName()); } }); break; - case ATOM: - Collections.sort(skills, new Comparator() { + case FIRSTNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + User user1 = (User) object1; + User user2 = (User) object2; + return user1.getFirstName().compareToIgnoreCase(user2.getFirstName()); } }); @@ -359,37 +393,37 @@ public int compare(Object object1, Object object2) { else { // must be reverse switch (sortField) { - case NAME: - Collections.sort(skills, new Comparator() { + case USERNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getName().compareToIgnoreCase(skill1.getName()); + User user1 = (User) object1; + User user2 = (User) object2; + return user2.getUserName().compareToIgnoreCase(user1.getUserName()); } }); break; - case DESCRIPTION: - Collections.sort(skills, new Comparator() { + case LASTNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); + User user1 = (User) object1; + User user2 = (User) object2; + return user2.getLastName().compareToIgnoreCase(user1.getLastName()); } }); break; - case ATOM: - Collections.sort(skills, new Comparator() { + case FIRSTNAME: + Collections.sort(users, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + User user1 = (User) object1; + User user2 = (User) object2; + return user2.getFirstName().compareToIgnoreCase(user1.getFirstName()); } }); @@ -398,79 +432,110 @@ public int compare(Object object1, Object object2) { } } - private void updateSkill(OpenAcdSkill skill, OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; + private List getUsers(Collection userIds) { + List users; + + users = new ArrayList(); + for (int userId : userIds) { + users.add(getCoreContext().getUser(userId)); + } + + return users; + } + + private void updateUser(User user, UserRestInfoFull userRestInfo) { + Set userGroups = new HashSet(); + Branch branch; String tempString; - // do not allow empty name - tempString = skillRestInfo.getName(); + // do not allow empty username + tempString = userRestInfo.getUserName(); if (!tempString.isEmpty()) { - skill.setName(tempString); + user.setUserName(tempString); } - skill.setDescription(skillRestInfo.getDescription()); + user.setLastName(userRestInfo.getLastName()); + user.setFirstName(userRestInfo.getFirstName()); + user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); + user.setSipPassword(userRestInfo.getSipPassword()); + + userGroups = new HashSet(getUserGroups(userRestInfo)); + user.setGroups(userGroups); - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); + user.setBranch(branch); } - private OpenAcdSkill createSkill(OpenAcdSkillRestInfoFull skillRestInfo) throws ResourceException { - OpenAcdSkillGroup skillGroup; - OpenAcdSkill skill = new OpenAcdSkill(); + private User createUser(UserRestInfoFull userRestInfo) { + User user = new User(); + Set userGroups = new HashSet(); + Branch branch; + + user.setUserName(userRestInfo.getUserName()); + user.setLastName(userRestInfo.getLastName()); + user.setFirstName(userRestInfo.getFirstName()); + user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); + user.setSipPassword(userRestInfo.getSipPassword()); - // copy fields from rest info - skill.setName(skillRestInfo.getName()); - skill.setDescription(skillRestInfo.getDescription()); - skill.setAtom(skillRestInfo.getAtom()); + userGroups = new HashSet(getUserGroups(userRestInfo)); + user.setGroups(userGroups); - skillGroup = getSkillGroup(skillRestInfo); - skill.setGroup(skillGroup); + branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); + user.setBranch(branch); - return skill; + return user; } - private OpenAcdSkillGroup getSkillGroup(OpenAcdSkillRestInfoFull skillRestInfo) { - OpenAcdSkillGroup skillGroup; - int groupId = skillRestInfo.getGroupId(); - skillGroup = m_openAcdContext.getSkillGroupById(groupId); + private List getUserGroups(UserRestInfoFull userRestInfo) { + List userGroups = new ArrayList(); + Group userGroup; + + for (UserGroupRestInfo userGroupRestInfo : userRestInfo.getGroups()) { + userGroup = getCoreContext().getGroupById(userGroupRestInfo.getId()); + userGroups.add(userGroup); + } - return skillGroup; + return userGroups; } // REST Representations // -------------------- - static class OpenAcdSkillsRepresentation extends XStreamRepresentation { + static class UsersRepresentation extends XStreamRepresentation { - public OpenAcdSkillsRepresentation(MediaType mediaType, OpenAcdSkillsBundleRestInfo object) { + public UsersRepresentation(MediaType mediaType, UsersBundleRestInfo object) { super(mediaType, object); } - public OpenAcdSkillsRepresentation(Representation representation) { + public UsersRepresentation(Representation representation) { super(representation); } @Override protected void configureXStream(XStream xstream) { - xstream.alias("openacd-skill", OpenAcdSkillsBundleRestInfo.class); - xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + xstream.alias("user", UsersBundleRestInfo.class); + xstream.alias("user", UserRestInfoFull.class); + xstream.alias("group", UserGroupRestInfo.class); + xstream.alias("branch", BranchRestInfoFull.class); } } - static class OpenAcdSkillRepresentation extends XStreamRepresentation { + static class UserRepresentation extends XStreamRepresentation { - public OpenAcdSkillRepresentation(MediaType mediaType, OpenAcdSkillRestInfoFull object) { + public UserRepresentation(MediaType mediaType, UserRestInfoFull object) { super(mediaType, object); } - public OpenAcdSkillRepresentation(Representation representation) { + public UserRepresentation(Representation representation) { super(representation); } @Override protected void configureXStream(XStream xstream) { - xstream.alias("skill", OpenAcdSkillRestInfoFull.class); + xstream.alias("group", UserGroupRestInfo.class); + xstream.alias("user", UserRestInfoFull.class); + xstream.alias("branch", BranchRestInfoFull.class); } } @@ -503,16 +568,20 @@ static class UserRestInfoFull { private final String m_firstName; private final String m_pin; private final String m_sipPassword; + private final List m_groups; + private final BranchRestInfo m_branch; - // user groups + // user groups, branch, aliases - public UserRestInfoFull(User user) { + public UserRestInfoFull(User user, List userGroupsRestInfo, BranchRestInfo branchRestInfo) { m_id = user.getId(); m_userName = user.getUserName(); m_lastName = user.getLastName(); m_firstName = user.getFirstName(); m_pin = "*"; // pin is hardcoded to never display but must still be submitted m_sipPassword = user.getSipPassword(); + m_groups = userGroupsRestInfo; + m_branch = branchRestInfo; } public int getId() { @@ -538,6 +607,14 @@ public String getPin() { public String getSipPassword() { return m_sipPassword; } + + public List getGroups() { + return m_groups; + } + + public BranchRestInfo getBranch() { + return m_branch; + } } @@ -546,8 +623,8 @@ public String getSipPassword() { // ---------------- @Required - public void setOpenAcdContext(OpenAcdContext openAcdContext) { - m_openAcdContext = openAcdContext; + public void setBranchManager(BranchManager branchManager) { + m_branchManager = branchManager; } } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml index 565dbca2f6..db4682867b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/rest.beans.xml @@ -460,6 +460,22 @@ + + + + + + + + + + + + + + + + From e1b5437902420f13a808e3308a042cf629482092 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 20 Apr 2012 04:32:14 -0400 Subject: [PATCH 89/99] Add GET using list of User IDs --- .../sipxconfig/rest/RestUtilities.java | 75 +++++++ .../rest/UserPermissionsResource.java | 20 ++ .../sipxconfig/rest/UsersResource.java | 187 ++++++++++-------- 3 files changed, 205 insertions(+), 77 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 14fc49ac23..9a0efe5b98 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -31,6 +31,7 @@ import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.sipfoundry.sipxconfig.branch.Branch; +import org.sipfoundry.sipxconfig.common.User; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgent; import org.sipfoundry.sipxconfig.openacd.OpenAcdAgentGroup; import org.sipfoundry.sipxconfig.openacd.OpenAcdClient; @@ -565,6 +566,80 @@ public List getPermissions() { } } + static class UserRestInfoFull { + private final int m_id; + private final String m_userName; // also called "User ID" in gui + private final String m_lastName; + private final String m_firstName; + private final String m_pin; + private final String m_sipPassword; + private final List m_groups; + private final BranchRestInfo m_branch; + private final List m_aliases; + + + public UserRestInfoFull(User user, List userGroupsRestInfo, BranchRestInfo branchRestInfo, List aliasesRestInfo) { + m_id = user.getId(); + m_userName = user.getUserName(); + m_lastName = user.getLastName(); + m_firstName = user.getFirstName(); + m_pin = ""; // pin is hardcoded to never display but must still be submitted + m_sipPassword = user.getSipPassword(); + m_groups = userGroupsRestInfo; + m_branch = branchRestInfo; + m_aliases = aliasesRestInfo; + } + + public int getId() { + return m_id; + } + + public String getUserName() { + return m_userName; + } + + public String getLastName() { + return m_lastName; + } + + public String getFirstName() { + return m_firstName; + } + + public String getPin() { + return m_pin; + } + + public String getSipPassword() { + return m_sipPassword; + } + + public List getGroups() { + return m_groups; + } + + public BranchRestInfo getBranch() { + return m_branch; + } + + public List getAliases() { + return m_aliases; + } + } + + static class AliasRestInfo { + + private final String m_alias; + + public AliasRestInfo(String alias) { + m_alias = alias; + } + + public String getAlias() { + return m_alias; + } + } + // Common OpenACD Rest Info objects // ------------------------ diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java index 24fa34e207..4e2ffcf2fa 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UserPermissionsResource.java @@ -129,7 +129,9 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, check if need to filter list List users; Collection userIds; + String branchIdString = m_form.getFirstValue("branch"); + String idListString = m_form.getFirstValue("ids"); int branchId; if ((branchIdString != null) && (branchIdString != "")) { @@ -143,6 +145,24 @@ public Representation represent(Variant variant) throws ResourceException { userIds = getCoreContext().getBranchMembersByPage(branchId, 0, getCoreContext().getBranchMembersCount(branchId)); users = getUsers(userIds); } + else if ((idListString != null) && (!idListString.isEmpty())) { + // searching by id list + String[] idArray = idListString.split(","); + + users = new ArrayList(); + User user; + for (String id : idArray) { + try { + idInt = RestUtilities.getIntFromAttribute(id); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + id + " not found."); + } + + user = getCoreContext().getUser(idInt); + users.add(user); + } + } else { // process request for all users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java index 79b918faca..0950e429f1 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java @@ -27,27 +27,31 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.TreeSet; import org.restlet.Context; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; +import org.restlet.data.Status; import org.restlet.resource.Representation; import org.restlet.resource.ResourceException; import org.restlet.resource.Variant; import org.sipfoundry.sipxconfig.branch.Branch; import org.sipfoundry.sipxconfig.branch.BranchManager; import org.sipfoundry.sipxconfig.common.User; +import org.sipfoundry.sipxconfig.rest.RestUtilities.AliasRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.BranchRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.MetadataRestInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.PaginationInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.SortInfo; import org.sipfoundry.sipxconfig.rest.RestUtilities.UserGroupRestInfo; +import org.sipfoundry.sipxconfig.rest.RestUtilities.UserRestInfoFull; import org.sipfoundry.sipxconfig.rest.RestUtilities.ValidationInfo; import org.sipfoundry.sipxconfig.setting.Group; import org.springframework.beans.factory.annotation.Required; @@ -138,12 +142,14 @@ public Representation represent(Variant variant) throws ResourceException { // if not single, check if need to filter list List users; - Collection userIds; + String branchIdString = m_form.getFirstValue("branch"); + String idListString = m_form.getFirstValue("ids"); int branchId; - if ((branchIdString != null) && (branchIdString != "")) { + // check if searching by branch + if ((branchIdString != null) && (!branchIdString.isEmpty())) { try { branchId = RestUtilities.getIntFromAttribute(branchIdString); } @@ -154,6 +160,24 @@ public Representation represent(Variant variant) throws ResourceException { userIds = getCoreContext().getBranchMembersByPage(branchId, 0, getCoreContext().getBranchMembersCount(branchId)); users = getUsers(userIds); } + else if ((idListString != null) && (!idListString.isEmpty())) { + // searching by id list + String[] idArray = idListString.split(","); + + users = new ArrayList(); + User user; + for (String id : idArray) { + try { + idInt = RestUtilities.getIntFromAttribute(id); + } + catch (Exception exception) { + return RestUtilities.getResponseError(getResponse(), RestUtilities.ResponseCode.ERROR_BAD_INPUT, "ID " + id + " not found."); + } + + user = getCoreContext().getUser(idInt); + users.add(user); + } + } else { // process request for all users = getCoreContext().loadUsersByPage(1, getCoreContext().getAllUsersCount()); // no GetUsers() in coreContext, instead some subgroups @@ -281,9 +305,13 @@ private ValidationInfo validate(UserRestInfoFull restInfo) { return validationInfo; } - private UserRestInfoFull createUserRestInfo(int id) { + private UserRestInfoFull createUserRestInfo(int id) throws ResourceException { User user = getCoreContext().getUser(id); + if (user == null) { + throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No user with id " + id); + } + return createUserRestInfo(user); } @@ -294,6 +322,9 @@ private UserRestInfoFull createUserRestInfo(User user) { Set groups = null; BranchRestInfo branchRestInfo = null; Branch branch = null; + AliasRestInfo aliasRestInfo = null; + List aliasesRestInfo = new ArrayList(); + Set aliases = null; groups = user.getGroups(); @@ -312,7 +343,17 @@ private UserRestInfoFull createUserRestInfo(User user) { branchRestInfo = new BranchRestInfo(branch); } - userRestInfo = new UserRestInfoFull(user, userGroupsRestInfo, branchRestInfo); + aliases = user.getAliases(); + + // user does not necessarily have any aliases + if (aliases != null) { + for (String alias : aliases) { + aliasRestInfo = new AliasRestInfo(alias); + aliasesRestInfo.add(aliasRestInfo); + } + } + + userRestInfo = new UserRestInfoFull(user, userGroupsRestInfo, branchRestInfo, aliasesRestInfo); return userRestInfo; } @@ -444,7 +485,6 @@ private List getUsers(Collection userIds) { } private void updateUser(User user, UserRestInfoFull userRestInfo) { - Set userGroups = new HashSet(); Branch branch; String tempString; @@ -456,38 +496,76 @@ private void updateUser(User user, UserRestInfoFull userRestInfo) { user.setLastName(userRestInfo.getLastName()); user.setFirstName(userRestInfo.getFirstName()); - user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); user.setSipPassword(userRestInfo.getSipPassword()); - userGroups = new HashSet(getUserGroups(userRestInfo)); - user.setGroups(userGroups); + // if pin is empty do not save + if (!userRestInfo.getPin().isEmpty()) { + user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); + } - branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); - user.setBranch(branch); + // user may not have any groups + List userGroupsRestInfo = userRestInfo.getGroups(); + if (userGroupsRestInfo != null) { + user.setGroups(createUserGroups(userRestInfo)); + } + else { + user.setGroups(null); + } + + // user may not have a branch + if (userRestInfo.getBranch() != null) { + branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); + user.setBranch(branch); + } + else { + user.setBranch(null); + } + + // user may not have any aliases + if (userRestInfo.getAliases() != null) { + user.setAliases(createAliases(userRestInfo)); + } + else { + user.setAliases(null); + } } private User createUser(UserRestInfoFull userRestInfo) { - User user = new User(); - Set userGroups = new HashSet(); + User user = getCoreContext().newUser(); Branch branch; user.setUserName(userRestInfo.getUserName()); user.setLastName(userRestInfo.getLastName()); user.setFirstName(userRestInfo.getFirstName()); - user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); user.setSipPassword(userRestInfo.getSipPassword()); - userGroups = new HashSet(getUserGroups(userRestInfo)); - user.setGroups(userGroups); + // if pin is empty do not save + if (!userRestInfo.getPin().isEmpty()) { + user.setPin(userRestInfo.getPin(), getCoreContext().getAuthorizationRealm()); + } + + // user may not have any groups + List userGroupsRestInfo = userRestInfo.getGroups(); + if (userGroupsRestInfo != null) { + user.setGroups(createUserGroups(userRestInfo)); + } + + // user may not have a branch + if (userRestInfo.getBranch() != null) { + branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); + user.setBranch(branch); + } - branch = m_branchManager.getBranch(userRestInfo.getBranch().getId()); - user.setBranch(branch); + // user may not have any aliases + if (userRestInfo.getAliases() != null) { + user.setAliases(createAliases(userRestInfo)); + } return user; } - private List getUserGroups(UserRestInfoFull userRestInfo) { - List userGroups = new ArrayList(); + private Set createUserGroups(UserRestInfoFull userRestInfo) { + Set userGroups = new TreeSet(); Group userGroup; for (UserGroupRestInfo userGroupRestInfo : userRestInfo.getGroups()) { @@ -498,6 +576,16 @@ private List getUserGroups(UserRestInfoFull userRestInfo) { return userGroups; } + private Set createAliases(UserRestInfoFull userRestInfo) { + Set aliases = new LinkedHashSet(); + + for (AliasRestInfo aliasRestInfo : userRestInfo.getAliases()) { + aliases.add(aliasRestInfo.getAlias()); + } + + return aliases; + } + // REST Representations // -------------------- @@ -518,6 +606,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("user", UserRestInfoFull.class); xstream.alias("group", UserGroupRestInfo.class); xstream.alias("branch", BranchRestInfoFull.class); + xstream.alias("alias", AliasRestInfo.class); } } @@ -536,6 +625,7 @@ protected void configureXStream(XStream xstream) { xstream.alias("group", UserGroupRestInfo.class); xstream.alias("user", UserRestInfoFull.class); xstream.alias("branch", BranchRestInfoFull.class); + xstream.alias("alias", AliasRestInfo.class); } } @@ -561,63 +651,6 @@ public List getSkills() { } } - static class UserRestInfoFull { - private final int m_id; - private final String m_userName; // also called "User ID" in gui - private final String m_lastName; - private final String m_firstName; - private final String m_pin; - private final String m_sipPassword; - private final List m_groups; - private final BranchRestInfo m_branch; - - // user groups, branch, aliases - - public UserRestInfoFull(User user, List userGroupsRestInfo, BranchRestInfo branchRestInfo) { - m_id = user.getId(); - m_userName = user.getUserName(); - m_lastName = user.getLastName(); - m_firstName = user.getFirstName(); - m_pin = "*"; // pin is hardcoded to never display but must still be submitted - m_sipPassword = user.getSipPassword(); - m_groups = userGroupsRestInfo; - m_branch = branchRestInfo; - } - - public int getId() { - return m_id; - } - - public String getUserName() { - return m_userName; - } - - public String getLastName() { - return m_lastName; - } - - public String getFirstName() { - return m_firstName; - } - - public String getPin() { - return m_pin; - } - - public String getSipPassword() { - return m_sipPassword; - } - - public List getGroups() { - return m_groups; - } - - public BranchRestInfo getBranch() { - return m_branch; - } - } - - // Injected objects // ---------------- From 38efb8a45c0419dc77b0ddac4f00df1e7d9fd793 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 20 Apr 2012 04:41:22 -0400 Subject: [PATCH 90/99] Finish Creation of Dial String --- .../sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java index 9c1b787b73..70965f8d7f 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java @@ -390,7 +390,7 @@ private void updateDialString(OpenAcdCommand dialString, OpenAcdDialStringRestIn } private OpenAcdCommand createDialString(OpenAcdDialStringRestInfo dialStringRestInfo) throws ResourceException { - OpenAcdCommand dialString = new OpenAcdCommand(); + OpenAcdCommand dialString = m_openAcdContext.newOpenAcdCommand(); dialString.addCondition(OpenAcdCommand.createLineCondition()); String tempString; From 957d9a97f180af5b95700f149f050460ba51c895 Mon Sep 17 00:00:00 2001 From: "D. Chang" Date: Fri, 20 Apr 2012 11:48:15 -0400 Subject: [PATCH 91/99] Correct comments --- .../sipxconfig/rest/OpenAcdClientsResource.java | 12 ++++++------ .../sipxconfig/rest/OpenAcdDialStringsResource.java | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java index 162e29105d..6ed2edfea7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdClientsResource.java @@ -103,7 +103,7 @@ public boolean allowDelete() { } // GET - Retrieve all and single Client - // ----------------------------------- + // ------------------------------------ @Override public Representation represent(Variant variant) throws ResourceException { @@ -149,8 +149,8 @@ public Representation represent(Variant variant) throws ResourceException { } - // PUT - Update or Add single Skill - // -------------------------------- + // PUT - Update or Add single Client + // --------------------------------- @Override public void storeRepresentation(Representation entity) throws ResourceException { @@ -211,8 +211,8 @@ public void storeRepresentation(Representation entity) throws ResourceException } - // DELETE - Delete single Skill - // ---------------------------- + // DELETE - Delete single Client + // ----------------------------- @Override public void removeRepresentations() throws ResourceException { @@ -294,7 +294,7 @@ private MetadataRestInfo addClients(List clientsRestInfo, clientsRestInfo.add(clientRestInfo); } - // create metadata about agent groups + // create metadata about results MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); return metadata; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java index 70965f8d7f..4e8342ddd4 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdDialStringsResource.java @@ -99,8 +99,8 @@ public boolean allowDelete() { return true; } - // GET - Retrieve all and single Client - // ----------------------------------- + // GET - Retrieve all and single Dial Strings + // ------------------------------------------ @Override public Representation represent(Variant variant) throws ResourceException { @@ -303,7 +303,7 @@ private MetadataRestInfo addDialStrings(List dialStri dialStringsRestInfo.add(dialStringRestInfo); } - // create metadata about agent groups + // create metadata about results MetadataRestInfo metadata = new MetadataRestInfo(paginationInfo); return metadata; } From 74651e89497aee7b3e807dc8161e94067193fde0 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Fri, 27 Apr 2012 14:20:09 -0400 Subject: [PATCH 92/99] Add email field --- .../src/org/sipfoundry/sipxconfig/rest/RestUtilities.java | 5 +++++ .../src/org/sipfoundry/sipxconfig/rest/UsersResource.java | 2 ++ 2 files changed, 7 insertions(+) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 9a0efe5b98..4dc9f3a44b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -573,6 +573,7 @@ static class UserRestInfoFull { private final String m_firstName; private final String m_pin; private final String m_sipPassword; + private final String m_emailAddress; // this is actually from "Contact Information" tab. Maybe create separate API later private final List m_groups; private final BranchRestInfo m_branch; private final List m_aliases; @@ -585,6 +586,7 @@ public UserRestInfoFull(User user, List userGroupsRestInfo, B m_firstName = user.getFirstName(); m_pin = ""; // pin is hardcoded to never display but must still be submitted m_sipPassword = user.getSipPassword(); + m_emailAddress = user.getEmailAddress(); m_groups = userGroupsRestInfo; m_branch = branchRestInfo; m_aliases = aliasesRestInfo; @@ -614,6 +616,9 @@ public String getSipPassword() { return m_sipPassword; } + public String getEmailAddress() { + return m_emailAddress; + } public List getGroups() { return m_groups; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java index 0950e429f1..abb46e4ff9 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/UsersResource.java @@ -497,6 +497,7 @@ private void updateUser(User user, UserRestInfoFull userRestInfo) { user.setLastName(userRestInfo.getLastName()); user.setFirstName(userRestInfo.getFirstName()); user.setSipPassword(userRestInfo.getSipPassword()); + user.setEmailAddress(userRestInfo.getEmailAddress()); // if pin is empty do not save if (!userRestInfo.getPin().isEmpty()) { @@ -538,6 +539,7 @@ private User createUser(UserRestInfoFull userRestInfo) { user.setLastName(userRestInfo.getLastName()); user.setFirstName(userRestInfo.getFirstName()); user.setSipPassword(userRestInfo.getSipPassword()); + user.setEmailAddress(userRestInfo.getEmailAddress()); // if pin is empty do not save if (!userRestInfo.getPin().isEmpty()) { From 14fe401076924bec64cb7d5baee4a427cba2e4e6 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Sat, 12 May 2012 15:07:03 -0400 Subject: [PATCH 93/99] Save phone and fax numbers when creating/updating --- .../org/sipfoundry/sipxconfig/rest/BranchesResource.java | 4 ++++ .../sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java index 1be36774db..faf41b0bd2 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java @@ -364,6 +364,8 @@ private void updateBranch(Branch branch, BranchRestInfoFull branchRestInfo) { } branch.setDescription(branchRestInfo.getDescription()); + branch.setPhoneNumber(branchRestInfo.getPhoneNumber()); + branch.setFaxNumber(branchRestInfo.getFaxNumber()); address = getAddress(branchRestInfo); branch.setAddress(address); @@ -376,6 +378,8 @@ private Branch createBranch(BranchRestInfoFull branchRestInfo) throws ResourceEx // copy fields from rest info branch.setName(branchRestInfo.getName()); branch.setDescription(branchRestInfo.getDescription()); + branch.setPhoneNumber(branchRestInfo.getPhoneNumber()); + branch.setFaxNumber(branchRestInfo.getFaxNumber()); address = getAddress(branchRestInfo); branch.setAddress(address); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 2382437537..c1c5b5b261 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -165,6 +165,7 @@ public Representation represent(Variant variant) throws ResourceException { @Override public void storeRepresentation(Representation entity) throws ResourceException { + // get from request body OpenAcdLineRepresentation representation = new OpenAcdLineRepresentation(entity); OpenAcdLineRestInfo lineRestInfo = representation.getObject(); @@ -178,6 +179,10 @@ public void storeRepresentation(Representation entity) throws ResourceException return; } + // DEBUG + RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "after validate", 4); + if (true ) return; + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -283,7 +288,7 @@ private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } - + for (int i = 0; i < did.length(); i++) { if ((!Character.isDigit(did.charAt(i)))) { validationInfo.valid = false; From 451db7ec3ce5765b0d04c6bb9308fc08ddf06864 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Sat, 12 May 2012 15:15:06 -0400 Subject: [PATCH 94/99] Clean up leftover debug code --- .../sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index c1c5b5b261..2382437537 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -165,7 +165,6 @@ public Representation represent(Variant variant) throws ResourceException { @Override public void storeRepresentation(Representation entity) throws ResourceException { - // get from request body OpenAcdLineRepresentation representation = new OpenAcdLineRepresentation(entity); OpenAcdLineRestInfo lineRestInfo = representation.getObject(); @@ -179,10 +178,6 @@ public void storeRepresentation(Representation entity) throws ResourceException return; } - // DEBUG - RestUtilities.setResponse(getResponse(), RestUtilities.ResponseCode.SUCCESS_UPDATED, "after validate", 4); - if (true ) return; - // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -288,7 +283,7 @@ private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; } } - + for (int i = 0; i < did.length(); i++) { if ((!Character.isDigit(did.charAt(i)))) { validationInfo.valid = false; From 142de04a63d1421cf191c25228ff9f07493b4c3b Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Wed, 16 May 2012 19:58:48 -0400 Subject: [PATCH 95/99] Add sorting on various fields --- .../sipxconfig/rest/BranchesResource.java | 58 +++++++- .../rest/OpenAcdAgentGroupsResource.java | 8 +- .../rest/OpenAcdAgentsResource.java | 137 +++++++++++++++++- .../sipxconfig/rest/OpenAcdLinesResource.java | 106 +++++++++++++- .../rest/OpenAcdQueueGroupsResource.java | 8 +- .../rest/OpenAcdQueuesResource.java | 34 ++++- .../rest/OpenAcdReleaseCodesResource.java | 8 +- .../rest/OpenAcdSkillGroupsResource.java | 12 +- .../rest/OpenAcdSkillsResource.java | 38 ++++- .../sipxconfig/rest/PermissionsResource.java | 58 +++++++- .../sipxconfig/rest/RestUtilities.java | 10 ++ 11 files changed, 425 insertions(+), 52 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java index faf41b0bd2..be9f61bbd7 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/BranchesResource.java @@ -55,7 +55,7 @@ public class BranchesResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, NONE; + NAME, DESCRIPTION, CITY, OFFICEDESIGNATION, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -298,13 +298,37 @@ private void sortBranches(List branches) { if (sortInfo.directionForward) { switch (sortField) { + case CITY: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return RestUtilities.compareIgnoreCaseNullSafe(branch1.getAddress().getCity(), branch2.getAddress().getCity()); + } + + }); + break; + + case OFFICEDESIGNATION: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return RestUtilities.compareIgnoreCaseNullSafe(branch1.getAddress().getOfficeDesignation(), branch2.getAddress().getOfficeDesignation()); + } + + }); + break; + case NAME: Collections.sort(branches, new Comparator() { public int compare(Object object1, Object object2) { Branch branch1 = (Branch) object1; Branch branch2 = (Branch) object2; - return branch1.getName().compareToIgnoreCase(branch2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(branch1.getName(),branch2.getName()); } }); @@ -316,7 +340,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { Branch branch1 = (Branch) object1; Branch branch2 = (Branch) object2; - return branch1.getDescription().compareToIgnoreCase(branch2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(branch1.getDescription(),branch2.getDescription()); } }); @@ -326,13 +350,37 @@ public int compare(Object object1, Object object2) { else { // must be reverse switch (sortField) { + case CITY: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return RestUtilities.compareIgnoreCaseNullSafe(branch2.getAddress().getCity(), branch1.getAddress().getCity()); + } + + }); + break; + + case OFFICEDESIGNATION: + Collections.sort(branches, new Comparator() { + + public int compare(Object object1, Object object2) { + Branch branch1 = (Branch) object1; + Branch branch2 = (Branch) object2; + return RestUtilities.compareIgnoreCaseNullSafe(branch2.getAddress().getOfficeDesignation(), branch1.getAddress().getOfficeDesignation()); + } + + }); + break; + case NAME: Collections.sort(branches, new Comparator() { public int compare(Object object1, Object object2) { Branch branch1 = (Branch) object1; Branch branch2 = (Branch) object2; - return branch2.getName().compareToIgnoreCase(branch1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(branch2.getName(),branch1.getName()); } }); @@ -344,7 +392,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { Branch branch1 = (Branch) object1; Branch branch2 = (Branch) object2; - return branch2.getDescription().compareToIgnoreCase(branch1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(branch2.getDescription(),branch1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java index 3d9218e432..ed580afc8d 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentGroupsResource.java @@ -379,7 +379,7 @@ private void sortGroups(List agentGroups) { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getName().compareToIgnoreCase(agentGroup2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(agentGroup1.getName(), agentGroup2.getName()); } }); @@ -391,7 +391,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup1.getDescription().compareToIgnoreCase(agentGroup2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(agentGroup1.getDescription(),agentGroup2.getDescription()); } }); @@ -407,7 +407,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getName().compareToIgnoreCase(agentGroup1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(agentGroup2.getName(), agentGroup1.getName()); } }); @@ -419,7 +419,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgentGroup agentGroup1 = (OpenAcdAgentGroup) object1; OpenAcdAgentGroup agentGroup2 = (OpenAcdAgentGroup) object2; - return agentGroup2.getDescription().compareToIgnoreCase(agentGroup1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(agentGroup2.getDescription(),agentGroup1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index ac3eb2e2d0..7f7eb2e248 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -69,7 +69,7 @@ public class OpenAcdAgentsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, GROUP, SECURITY, NONE; + NAME, GROUP, SECURITY, USERNAME, FIRSTNAME, LASTNAME, BRANCH, EXTENSION, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -379,7 +379,7 @@ public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent1.getName().compareToIgnoreCase(agent2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getName(), agent2.getName()); } }); @@ -391,7 +391,7 @@ public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent1.getAgentGroup().compareToIgnoreCase(agent2.getAgentGroup()); + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getAgentGroup(), agent2.getAgentGroup()); } }); break; @@ -402,7 +402,67 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent1.getSecurity().compareToIgnoreCase(agent2.getSecurity()); + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getUser().getGroupsNames(), agent2.getUser().getGroupsNames()); + } + + }); + break; + + case USERNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getUser().getUserName(), agent2.getUser().getUserName()); + } + + }); + break; + + case FIRSTNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getFirstName(), agent2.getFirstName()); + } + + }); + break; + + case LASTNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getLastName(), agent2.getLastName()); + } + + }); + break; + + case BRANCH: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getUser().getBranch().getName(), agent2.getUser().getBranch().getName()); + } + + }); + break; + + case EXTENSION: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent1.getUser().getExtension(true), agent2.getUser().getExtension(true)); } }); @@ -418,8 +478,10 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent2.getName().compareToIgnoreCase(agent1.getName()); + + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getName(), agent1.getName()); } + }); break; @@ -429,7 +491,7 @@ public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent2.getAgentGroup().compareToIgnoreCase(agent1.getAgentGroup()); + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getAgentGroup(), agent1.getAgentGroup()); } }); break; @@ -440,8 +502,69 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdAgent agent1 = (OpenAcdAgent) object1; OpenAcdAgent agent2 = (OpenAcdAgent) object2; - return agent2.getSecurity().compareToIgnoreCase(agent1.getSecurity()); + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getUser().getGroupsNames(), agent1.getUser().getGroupsNames()); } + + }); + break; + + case USERNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getUser().getUserName(), agent1.getUser().getUserName()); + } + + }); + break; + + case FIRSTNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getFirstName(), agent1.getFirstName()); + } + + }); + break; + + case LASTNAME: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getLastName(), agent1.getLastName()); + } + + }); + break; + + case BRANCH: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getUser().getBranch().getName(), agent1.getUser().getBranch().getName()); + } + + }); + break; + + case EXTENSION: + Collections.sort(agents, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdAgent agent1 = (OpenAcdAgent) object1; + OpenAcdAgent agent2 = (OpenAcdAgent) object2; + return RestUtilities.compareIgnoreCaseNullSafe(agent2.getUser().getExtension(true), agent1.getUser().getExtension(true)); + } + }); break; } diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 2382437537..309ee0ec30 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -66,7 +66,7 @@ public class OpenAcdLinesResource extends UserResource { // use to define all possible sort fields enum SortField { - NAME, DESCRIPTION, NONE; + NAME, DESCRIPTION, EXTENSION, DIDNUMBER, QUEUE, CLIENT, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -433,7 +433,7 @@ private void sortLines(List lines) { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; OpenAcdLine line2 = (OpenAcdLine) object2; - return line1.getName().compareToIgnoreCase(line2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(line1.getName(), line2.getName()); } }); @@ -445,7 +445,55 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; OpenAcdLine line2 = (OpenAcdLine) object2; - return line1.getDescription().compareToIgnoreCase(line2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(line1.getDescription(), line2.getDescription()); + } + + }); + break; + + case EXTENSION: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(line1.getExtension(), line2.getExtension()); + } + + }); + break; + + case DIDNUMBER: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(line1.getDid(), line2.getDid()); + } + + }); + break; + + case QUEUE: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(createLineRestInfo(line1).m_actions.getQueue().getName(), createLineRestInfo(line2).m_actions.getQueue().getName()); + } + + }); + break; + + case CLIENT: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(createLineRestInfo(line1).m_actions.getClient().getIdentity(), createLineRestInfo(line2).m_actions.getClient().getIdentity()); } }); @@ -462,7 +510,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; OpenAcdLine line2 = (OpenAcdLine) object2; - return line2.getName().compareToIgnoreCase(line1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(line2.getName(), line1.getName()); } }); @@ -474,7 +522,55 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdLine line1 = (OpenAcdLine) object1; OpenAcdLine line2 = (OpenAcdLine) object2; - return line2.getDescription().compareToIgnoreCase(line1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(line2.getDescription(), line1.getDescription()); + } + + }); + break; + + case EXTENSION: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(line2.getExtension(), line1.getExtension()); + } + + }); + break; + + case DIDNUMBER: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(line2.getDid(), line1.getDid()); + } + + }); + break; + + case QUEUE: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(createLineRestInfo(line2).m_actions.getQueue().getName(), createLineRestInfo(line1).m_actions.getQueue().getName()); + } + + }); + break; + + case CLIENT: + Collections.sort(lines, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdLine line1 = (OpenAcdLine) object1; + OpenAcdLine line2 = (OpenAcdLine) object2; + return RestUtilities.compareIgnoreCaseNullSafe(createLineRestInfo(line2).m_actions.getClient().getIdentity(), createLineRestInfo(line1).m_actions.getClient().getIdentity()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java index 724ca9d034..b6d4917e8c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueueGroupsResource.java @@ -501,7 +501,7 @@ private void sortQueueGroups(List queueGroups) { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; - return queueGroup1.getName().compareToIgnoreCase(queueGroup2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(queueGroup1.getName(), queueGroup2.getName()); } }); @@ -513,7 +513,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; - return queueGroup1.getDescription().compareToIgnoreCase(queueGroup2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(queueGroup1.getDescription(), queueGroup2.getDescription()); } }); @@ -529,7 +529,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; - return queueGroup2.getName().compareToIgnoreCase(queueGroup1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(queueGroup2.getName(), queueGroup1.getName()); } }); @@ -541,7 +541,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdQueueGroup queueGroup1 = (OpenAcdQueueGroup) object1; OpenAcdQueueGroup queueGroup2 = (OpenAcdQueueGroup) object2; - return queueGroup2.getDescription().compareToIgnoreCase(queueGroup1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(queueGroup2.getDescription(), queueGroup1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java index 51257e4651..04e2a510fc 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdQueuesResource.java @@ -69,7 +69,7 @@ public class OpenAcdQueuesResource extends UserResource { // use to define all possible sort fields enum SortField { - NAME, DESCRIPTION, NONE; + NAME, GROUPNAME, DESCRIPTION, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -498,13 +498,25 @@ private void sortQueues(List queues) { if (sortInfo.directionForward) { switch (sortField) { + case GROUPNAME: + Collections.sort(queues, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return RestUtilities.compareIgnoreCaseNullSafe(queue1.getGroup().getName(), queue2.getGroup().getName()); + } + + }); + break; + case NAME: Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; OpenAcdQueue queue2 = (OpenAcdQueue) object2; - return queue1.getName().compareToIgnoreCase(queue2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(queue1.getName(), queue2.getName()); } }); @@ -516,7 +528,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; OpenAcdQueue queue2 = (OpenAcdQueue) object2; - return queue1.getDescription().compareToIgnoreCase(queue2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(queue1.getDescription(), queue2.getDescription()); } }); @@ -526,13 +538,25 @@ public int compare(Object object1, Object object2) { else { // must be reverse switch (sortField) { + case GROUPNAME: + Collections.sort(queues, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdQueue queue1 = (OpenAcdQueue) object1; + OpenAcdQueue queue2 = (OpenAcdQueue) object2; + return RestUtilities.compareIgnoreCaseNullSafe(queue2.getGroup().getName(), queue1.getGroup().getName()); + } + + }); + break; + case NAME: Collections.sort(queues, new Comparator() { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; OpenAcdQueue queue2 = (OpenAcdQueue) object2; - return queue2.getName().compareToIgnoreCase(queue1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(queue2.getName(), queue1.getName()); } }); @@ -544,7 +568,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdQueue queue1 = (OpenAcdQueue) object1; OpenAcdQueue queue2 = (OpenAcdQueue) object2; - return queue2.getDescription().compareToIgnoreCase(queue1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(queue2.getDescription(), queue1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java index 59397ecb23..441621654c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdReleaseCodesResource.java @@ -320,7 +320,7 @@ private void sortReleaseCodes(List releaseCodes) { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; - return releaseCode1.getLabel().compareToIgnoreCase(releaseCode2.getLabel()); + return RestUtilities.compareIgnoreCaseNullSafe(releaseCode1.getLabel(), releaseCode2.getLabel()); } }); @@ -332,7 +332,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; - return releaseCode1.getDescription().compareToIgnoreCase(releaseCode2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(releaseCode1.getDescription(), releaseCode2.getDescription()); } }); @@ -348,7 +348,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; - return releaseCode2.getLabel().compareToIgnoreCase(releaseCode1.getLabel()); + return RestUtilities.compareIgnoreCaseNullSafe(releaseCode2.getLabel(), releaseCode1.getLabel()); } }); @@ -360,7 +360,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdReleaseCode releaseCode1 = (OpenAcdReleaseCode) object1; OpenAcdReleaseCode releaseCode2 = (OpenAcdReleaseCode) object2; - return releaseCode2.getDescription().compareToIgnoreCase(releaseCode1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(releaseCode2.getDescription(), releaseCode1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 2d085c6d8f..727cea198b 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -335,7 +335,7 @@ private void sortSkillGroups(List skillGroups) { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; - return skillGroup1.getName().compareToIgnoreCase(skillGroup2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(skillGroup1.getName(), skillGroup2.getName()); } }); @@ -347,7 +347,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; - return skillGroup1.getDescription().compareToIgnoreCase(skillGroup2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(skillGroup1.getDescription(), skillGroup2.getDescription()); } }); @@ -361,9 +361,9 @@ public int compare(Object object1, Object object2) { Collections.sort(skillGroups, new Comparator() { public int compare(Object object1, Object object2) { - OpenAcdSkill skill1 = (OpenAcdSkill) object1; - OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getName().compareToIgnoreCase(skill1.getName()); + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return RestUtilities.compareIgnoreCaseNullSafe(skillGroup2.getName(), skillGroup1.getName()); } }); @@ -375,7 +375,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; - return skillGroup2.getDescription().compareToIgnoreCase(skillGroup1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(skillGroup2.getDescription(), skillGroup1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java index 87fc6ee5ed..ee532028d3 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillsResource.java @@ -56,7 +56,7 @@ public class OpenAcdSkillsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, ATOM, NONE; + NAME, DESCRIPTION, ATOM, GROUPNAME, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -322,7 +322,19 @@ private void sortSkills(List skills) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getName().compareToIgnoreCase(skill2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(skill1.getName(), skill2.getName()); + } + + }); + break; + + case GROUPNAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return RestUtilities.compareIgnoreCaseNullSafe(skill1.getGroupName(), skill2.getGroupName()); } }); @@ -334,7 +346,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getDescription().compareToIgnoreCase(skill2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(skill1.getDescription(), skill2.getDescription()); } }); @@ -347,7 +359,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill1.getAtom().compareToIgnoreCase(skill2.getAtom()); + return RestUtilities.compareIgnoreCaseNullSafe(skill1.getAtom(), skill2.getAtom()); } }); @@ -363,7 +375,19 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getName().compareToIgnoreCase(skill1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(skill2.getName(), skill1.getName()); + } + + }); + break; + + case GROUPNAME: + Collections.sort(skills, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkill skill1 = (OpenAcdSkill) object1; + OpenAcdSkill skill2 = (OpenAcdSkill) object2; + return RestUtilities.compareIgnoreCaseNullSafe(skill2.getGroupName(), skill1.getGroupName()); } }); @@ -375,7 +399,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getDescription().compareToIgnoreCase(skill1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(skill2.getDescription(), skill1.getDescription()); } }); @@ -387,7 +411,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { OpenAcdSkill skill1 = (OpenAcdSkill) object1; OpenAcdSkill skill2 = (OpenAcdSkill) object2; - return skill2.getAtom().compareToIgnoreCase(skill1.getAtom()); + return RestUtilities.compareIgnoreCaseNullSafe(skill2.getAtom(), skill1.getAtom()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index 2b3b2edfab..07c8b33edc 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -54,7 +54,7 @@ public class PermissionsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, NONE; + NAME, DESCRIPTION, LABEL, BUILTIN, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -286,13 +286,37 @@ private void sortPermissions(List permissions) { if (sortInfo.directionForward) { switch (sortField) { + case LABEL: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return RestUtilities.compareIgnoreCaseNullSafe(permission1.getLabel(),permission2.getLabel()); + } + + }); + break; + + case BUILTIN: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission1.isBuiltIn()),Boolean.toString(permission2.isBuiltIn())); + } + + }); + break; + case NAME: Collections.sort(permissions, new Comparator() { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return permission1.getName().compareToIgnoreCase(permission2.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(permission1.getName(), permission2.getName()); } }); @@ -304,7 +328,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return permission1.getDescription().compareToIgnoreCase(permission2.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(permission1.getDescription(), permission2.getDescription()); } }); @@ -314,13 +338,37 @@ public int compare(Object object1, Object object2) { else { // must be reverse switch (sortField) { + case LABEL: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return RestUtilities.compareIgnoreCaseNullSafe(permission2.getLabel(),permission1.getLabel()); + } + + }); + break; + + case BUILTIN: + Collections.sort(permissions, new Comparator() { + + public int compare(Object object1, Object object2) { + Permission permission1 = (Permission) object1; + Permission permission2 = (Permission) object2; + return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission2.isBuiltIn()),Boolean.toString(permission1.isBuiltIn())); + } + + }); + break; + case NAME: Collections.sort(permissions, new Comparator() { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return permission2.getName().compareToIgnoreCase(permission1.getName()); + return RestUtilities.compareIgnoreCaseNullSafe(permission2.getName(), permission1.getName()); } }); @@ -332,7 +380,7 @@ public int compare(Object object1, Object object2) { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return permission2.getDescription().compareToIgnoreCase(permission1.getDescription()); + return RestUtilities.compareIgnoreCaseNullSafe(permission2.getDescription(), permission1.getDescription()); } }); diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index 4dc9f3a44b..e1db78f19a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -166,6 +166,16 @@ public static SortInfo calculateSorting(Form form) { return sortInfo; } + public static int compareIgnoreCaseNullSafe (String left, String right) + { + if (left == null) + left = ""; + if (right == null) + right = ""; + + return left.compareToIgnoreCase(right); + } + // XML Response functions // ---------------------- From 7fcee1d9bf8e554f54edfb87d5066d1658e96d93 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Fri, 18 May 2012 14:07:20 -0400 Subject: [PATCH 96/99] Add sort by numberskills --- .../rest/OpenAcdSkillGroupsResource.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java index 727cea198b..d18a4f4c7a 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdSkillGroupsResource.java @@ -59,7 +59,7 @@ public class OpenAcdSkillGroupsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, NONE; + NAME, DESCRIPTION, NUMBERSKILLS, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -352,6 +352,18 @@ public int compare(Object object1, Object object2) { }); break; + + case NUMBERSKILLS: + Collections.sort(skillGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return RestUtilities.compareIgnoreCaseNullSafe(String.valueOf(skillGroup1.getSkills().size()), String.valueOf(skillGroup2.getSkills().size())); + } + + }); + break; } } else { @@ -380,6 +392,18 @@ public int compare(Object object1, Object object2) { }); break; + + case NUMBERSKILLS: + Collections.sort(skillGroups, new Comparator() { + + public int compare(Object object1, Object object2) { + OpenAcdSkillGroup skillGroup1 = (OpenAcdSkillGroup) object1; + OpenAcdSkillGroup skillGroup2 = (OpenAcdSkillGroup) object2; + return RestUtilities.compareIgnoreCaseNullSafe(String.valueOf(skillGroup2.getSkills().size()), String.valueOf(skillGroup1.getSkills().size())); + } + + }); + break; } } } From d0c9d96e917ee09f58d73a0719800f99db108a03 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Mon, 21 May 2012 14:20:33 -0400 Subject: [PATCH 97/99] Correct error in create and update due to bad ValidationInfo checking --- .../sipxconfig/rest/OpenAcdLinesResource.java | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java index 309ee0ec30..2aa5f0f349 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdLinesResource.java @@ -172,13 +172,13 @@ public void storeRepresentation(Representation entity) throws ResourceException // validate input for update or create ValidationInfo validationInfo = validate(lineRestInfo); - + if (!validationInfo.valid) { RestUtilities.setResponseError(getResponse(), validationInfo.responseCode, validationInfo.message); return; } - + // if have id then update single String idString = (String) getRequest().getAttributes().get("id"); @@ -268,38 +268,47 @@ private ValidationInfo validate(OpenAcdLineRestInfo restInfo) { String ext = restInfo.getExtension(); String did = restInfo.getDIDNumber(); String alias = restInfo.getAlias(); - for (int i = 0; i < name.length(); i++) { - if (name.charAt(i) == ' ') { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, underscores, and symbols"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - - for (int i = 0; i < ext.length(); i++) { - if ((!Character.isDigit(ext.charAt(i)))) { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Extension' must only contain numbers"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - - for (int i = 0; i < did.length(); i++) { - if ((!Character.isDigit(did.charAt(i)))) { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'DID Number' must only contain numbers"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - - for (int i = 0; i < alias.length(); i++) { - if ((!Character.isDigit(alias.charAt(i)))) { - validationInfo.valid = false; - validationInfo.message = "Validation Error: 'Alias' must only contain numbers"; - validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; - } - } - + + if (!StringUtils.isEmpty(name)) { + for (int i = 0; i < name.length(); i++) { + if (name.charAt(i) == ' ') { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Name' must only contain letters, numbers, dashes, underscores, and symbols"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + } + + if (!StringUtils.isEmpty(ext)) { + for (int i = 0; i < ext.length(); i++) { + if ((!Character.isDigit(ext.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Extension' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + } + + if (!StringUtils.isEmpty(did)) { + for (int i = 0; i < did.length(); i++) { + if ((!Character.isDigit(did.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'DID Number' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + } + + if (!StringUtils.isEmpty(alias)) { + for (int i = 0; i < alias.length(); i++) { + if ((!Character.isDigit(alias.charAt(i)))) { + validationInfo.valid = false; + validationInfo.message = "Validation Error: 'Alias' must only contain numbers"; + validationInfo.responseCode = ResponseCode.ERROR_BAD_INPUT; + } + } + } + return validationInfo; } From e1ec77d06f520ec5f4049c20e14d748d00e5465f Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Mon, 21 May 2012 15:16:45 -0400 Subject: [PATCH 98/99] Change sort on builtIn to sort on defaultValue --- .../sipxconfig/rest/PermissionsResource.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java index 07c8b33edc..cf40a8077c 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/PermissionsResource.java @@ -54,7 +54,7 @@ public class PermissionsResource extends UserResource { // use to define all possible sort fields private enum SortField { - NAME, DESCRIPTION, LABEL, BUILTIN, NONE; + NAME, DESCRIPTION, LABEL, DEFAULTVALUE, NONE; public static SortField toSortField(String fieldString) { if (fieldString == null) { @@ -298,13 +298,13 @@ public int compare(Object object1, Object object2) { }); break; - case BUILTIN: + case DEFAULTVALUE: Collections.sort(permissions, new Comparator() { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission1.isBuiltIn()),Boolean.toString(permission2.isBuiltIn())); + return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission1.getDefaultValue()),Boolean.toString(permission2.getDefaultValue())); } }); @@ -350,13 +350,13 @@ public int compare(Object object1, Object object2) { }); break; - case BUILTIN: + case DEFAULTVALUE: Collections.sort(permissions, new Comparator() { public int compare(Object object1, Object object2) { Permission permission1 = (Permission) object1; Permission permission2 = (Permission) object2; - return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission2.isBuiltIn()),Boolean.toString(permission1.isBuiltIn())); + return RestUtilities.compareIgnoreCaseNullSafe(Boolean.toString(permission2.getDefaultValue()),Boolean.toString(permission1.getDefaultValue())); } }); From 113455c94e55c2dcb76f90f5d04c735860853002 Mon Sep 17 00:00:00 2001 From: Daniel Chang Date: Mon, 21 May 2012 21:42:25 -0400 Subject: [PATCH 99/99] remove "password" field from OpenACDAgent (does not actually exist, was incorrectly setting user.pintoken on update) --- .../sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java | 5 ----- .../src/org/sipfoundry/sipxconfig/rest/RestUtilities.java | 6 ------ 2 files changed, 11 deletions(-) diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java index 7f7eb2e248..3e1f5b0ffe 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/OpenAcdAgentsResource.java @@ -579,11 +579,6 @@ private void updateAgent(OpenAcdAgent agent, OpenAcdAgentRestInfoFull agentRestI agent.setSecurity(agentRestInfo.getSecurity()); - // only update password if it is not empty (since caller cannot obtain password to pass back for updating) - if (!agentRestInfo.getPassword().isEmpty()) { - agent.getUser().setPintoken(agentRestInfo.getPassword()); - } - agent.getSkills().clear(); OpenAcdSkill skill; diff --git a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java index e1db78f19a..4a699872df 100644 --- a/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java +++ b/sipXconfig/web/src/org/sipfoundry/sipxconfig/rest/RestUtilities.java @@ -1069,7 +1069,6 @@ static class OpenAcdAgentRestInfoFull { private final int m_groupId; private final String m_groupName; private final String m_security; - private final String m_password; private final List m_skills; private final List m_queues; private final List m_clients; @@ -1083,7 +1082,6 @@ public OpenAcdAgentRestInfoFull(OpenAcdAgent agent, List s m_groupId = agent.getGroup().getId(); m_groupName = agent.getGroup().getName(); m_security = agent.getSecurity(); - m_password = ""; // only used on updates, not rest get m_skills = skills; m_queues = queues; m_clients = clients; @@ -1121,10 +1119,6 @@ public String getSecurity() { return m_security; } - public String getPassword() { - return m_password; - } - public List getSkills() { return m_skills; }