Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue2608 numbers filterd by app sid #2630

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
</if>
<if test="phoneNumber != null">
AND n.phone_number like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND (n.voice_application_sid = #{applicationSid} OR n.sms_application_sid = #{applicationSid} OR n.ussd_application_sid = #{applicationSid} OR n.refer_application_sid = #{applicationSid})
</if>
<choose>
<when test="sortBy == 'phone_number'">
Expand All @@ -90,6 +93,9 @@
<if test="phoneNumber != null">
AND phone_number like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND (voice_application_sid = #{applicationSid} OR sms_application_sid = #{applicationSid} OR ussd_application_sid = #{applicationSid} OR refer_application_sid = #{applicationSid})
</if>

</select>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<if test="phoneNumber != null">
AND "n"."phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("n"."voice_application_sid" = #{applicationSid} OR "n"."sms_application_sid" = #{applicationSid} OR "n"."ussd_application_sid" = #{applicationSid} OR "n"."refer_application_sid" = #{applicationSid})
</if>
<choose>
<when test="sortBy == 'phone_number'">
order by "n"."phone_number" ${sortDirection}
Expand All @@ -92,8 +95,10 @@
<if test="phoneNumber != null">
AND "phone_number" like #{phoneNumber}
</if>

</select>
<if test="applicationSid != null">
AND ("voice_application_sid" = #{applicationSid} OR "sms_application_sid" = #{applicationSid} OR "ussd_application_sid" = #{applicationSid} OR "refer_application_sid" = #{applicationSid})
</if>
</select>


<delete id="removeIncomingPhoneNumber" parameterType="string">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public class IncomingPhoneNumberFilter {
private final String accountSid;
private final String friendlyName;
private final String phoneNumber;
private final String applicationSid;
private final String sortBy;
private final String sortDirection;
private final Integer limit;
private final Integer offset;

public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String phoneNumber, String sortBy,
String sortDirection, Integer limit, Integer offset) {
public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String phoneNumber, String applicationSid, String sortBy,
String sortDirection, Integer limit, Integer offset) {
this.accountSid = accountSid;
this.friendlyName = friendlyName;
// The LIKE keyword uses '%' to match any (including 0) number of characters, and '_' to match exactly one character
Expand All @@ -48,13 +49,14 @@ public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String
}

this.phoneNumber = phoneNumber;
this.applicationSid = applicationSid;
this.sortBy = sortBy;
this.sortDirection = sortDirection;
this.limit = limit;
this.offset = offset;
}

public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String phoneNumber) {
public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String phoneNumber, String applicationSid) {
super();
this.accountSid = accountSid;
this.friendlyName = friendlyName;
Expand All @@ -65,6 +67,7 @@ public IncomingPhoneNumberFilter(String accountSid, String friendlyName, String
phoneNumber = phoneNumber.replaceAll("\\*", "_");
}
this.phoneNumber = phoneNumber;
this.applicationSid = applicationSid;
this.sortBy = null;
this.sortDirection = null;
this.offset = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void createReadUpdateDelete() {
@Test
public void applicationFriendlyNameReturned() {
final IncomingPhoneNumbersDao dao = manager.getIncomingPhoneNumbersDao();
IncomingPhoneNumberFilter incomingPhoneNumberFilter = new IncomingPhoneNumberFilter("ACae6e420f425248d6a26948c17a9e2acf", null, null,"phone_number","ASC",50,0);
IncomingPhoneNumberFilter incomingPhoneNumberFilter = new IncomingPhoneNumberFilter("ACae6e420f425248d6a26948c17a9e2acf", null, null, null, "phone_number","ASC",50,0);
List<IncomingPhoneNumber> phoneNumbers = dao.getIncomingPhoneNumbersByFilter(incomingPhoneNumberFilter);
Assert.assertEquals("Only a single phone number expected",1, phoneNumbers.size());
IncomingPhoneNumber number = phoneNumbers.get(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2014, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* 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 <http://www.gnu.org/licenses/>
*
*/

package org.restcomm.connect.dao.mybatis;

import junit.framework.Assert;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.restcomm.connect.dao.IncomingPhoneNumbersDao;
import org.restcomm.connect.dao.entities.IncomingPhoneNumber;
import org.restcomm.connect.dao.entities.IncomingPhoneNumberFilter;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
* @author [email protected] - Orestis Tsakiridis
*/
public class IncomingPhoneNumbersRetrievalTest extends DaoTest {
private static MybatisDaoManager manager;

@Before
public void before() throws Exception {
sandboxRoot = createTempDir("IncomingPhoneNumbersRetrievalTest");
String mybatisFilesPath = getClass().getResource("/applicationsDao").getFile();
setupSandbox(mybatisFilesPath, sandboxRoot);

String mybatisXmlPath = sandboxRoot.getPath() + "/mybatis_updated.xml";
final InputStream data = new FileInputStream(mybatisXmlPath);
final SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
final SqlSessionFactory factory = builder.build(data);
manager = new MybatisDaoManager();
manager.start(factory);
}

@After
public void after() throws Exception {
manager.shutdown();
removeTempDir(sandboxRoot.getAbsolutePath());
}

@Test
public void retrieveNumbersForApplication() {
IncomingPhoneNumbersDao dao = manager.getIncomingPhoneNumbersDao();
// application AP73926e7113fa4d95981aa96b76eca854 is bound with 3 numbers
IncomingPhoneNumberFilter filter = new IncomingPhoneNumberFilter("ACae6e420f425248d6a26948c17a9e2acf", null, null, "AP73926e7113fa4d95981aa96b76eca854", null, null, 100, 0);
List<IncomingPhoneNumber> numbers = dao.getIncomingPhoneNumbersByFilter(filter);
Assert.assertEquals(3, numbers.size());
// application AP00000000000000000000000000000004 is bound with no numbers
filter = new IncomingPhoneNumberFilter("ACae6e420f425248d6a26948c17a9e2acf", null, null, "AP00000000000000000000000000000004", null, null, 100, 0);
numbers = dao.getIncomingPhoneNumbersByFilter(filter);
Assert.assertEquals(0, numbers.size());
}

@Test
public void countTotalNumbersForApplication() {
IncomingPhoneNumbersDao dao = manager.getIncomingPhoneNumbersDao();
IncomingPhoneNumberFilter filter = new IncomingPhoneNumberFilter("ACae6e420f425248d6a26948c17a9e2acf", null, null, "AP73926e7113fa4d95981aa96b76eca854", null, null, 100, 0);
int total = dao.getTotalIncomingPhoneNumbers(filter);
Assert.assertEquals(3, total);
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ TeleStax, Open Source Cloud Communications
~ Copyright 2011-2014, Telestax Inc and individual contributors
~ by the @authors tag.
~
~ This program is free software: you can redistribute it and/or modify
~ 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 <http://www.gnu.org/licenses/>
~
-->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
@author [email protected] (Thomas Quintana)
Expand Down Expand Up @@ -88,6 +68,9 @@
<if test="phoneNumber != null">
AND "n"."phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("n"."voice_application_sid" = #{applicationSid} OR "n"."sms_application_sid" = #{applicationSid} OR "n"."ussd_application_sid" = #{applicationSid} OR "n"."refer_application_sid" = #{applicationSid})
</if>
<choose>
<when test="sortBy == 'phone_number'">
order by "n"."phone_number" ${sortDirection}
Expand All @@ -112,6 +95,9 @@
<if test="phoneNumber != null">
AND "phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("voice_application_sid" = #{applicationSid} OR "sms_application_sid" = #{applicationSid} OR "ussd_application_sid" = #{applicationSid} OR "refer_application_sid" = #{applicationSid})
</if>

</select>

Expand Down
37 changes: 21 additions & 16 deletions restcomm/restcomm.dao/src/test/resources/incoming-phone-numbers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,37 @@
<if test="phoneNumber != null">
AND "n"."phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("n"."voice_application_sid" = #{applicationSid} OR "n"."sms_application_sid" = #{applicationSid} OR "n"."ussd_application_sid" = #{applicationSid} OR "n"."refer_application_sid" = #{applicationSid})
</if>
<choose>
<when test="sortBy == 'phone_number'">
order by "n"."phone_number" ${sortDirection}
</when>
<when test="sortBy == 'friendly_name'">
order by "n"."friendly_name" ${sortDirection}
</when>
<otherwise>
order by "n"."phone_number" ${sortDirection}
</otherwise>
</choose>
<when test="sortBy == 'phone_number'">
order by "n"."phone_number" ${sortDirection}
</when>
<when test="sortBy == 'friendly_name'">
order by "n"."friendly_name" ${sortDirection}
</when>
<otherwise>
order by "n"."phone_number" ${sortDirection}
</otherwise>
</choose>

LIMIT #{limit} OFFSET #{offset}
</select>

<select id="getTotalIncomingPhoneNumbersByUsingFilters" parameterType="org.restcomm.connect.dao.entities.IncomingPhoneNumberFilter" resultType="int">
SELECT COUNT(*) FROM "restcomm_incoming_phone_numbers" WHERE "account_sid"=#{accountSid}
SELECT COUNT(*) FROM "restcomm_incoming_phone_numbers" WHERE "account_sid"=#{accountSid}

<if test="friendlyName != null">
<if test="friendlyName != null">
AND "friendly_name"=#{friendlyName}
</if>
<if test="phoneNumber != null">
AND "phone_number" like #{phoneNumber}
</if>

</select>
<if test="applicationSid != null">
AND ("voice_application_sid" = #{applicationSid} OR "sms_application_sid" = #{applicationSid} OR "ussd_application_sid" = #{applicationSid} OR "refer_application_sid" = #{applicationSid})
</if>
</select>


<delete id="removeIncomingPhoneNumber" parameterType="string">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ The following query string parameters allow you to limit the list returned. Note
|*Parameter* |*Description*
|PhoneNumber |Only show the incoming phone number resources that match this pattern. You can specify partial numbers and use '*' as a wildcard for any digit.
|FriendlyName |Only show the incoming phone number resources with friendly names that exactly match this name.
|ApplicationSid |Only show the incoming phone number resources associated with the specified application sid.
|Beta |Include phone numbers new to the Restcomm platform. Possible values are either true or false. Default is true.
|============================================================================================================================================================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ protected Response getIncomingPhoneNumbers(final String accountSid, final PhoneN
try{
String phoneNumberFilter = info.getQueryParameters().getFirst("PhoneNumber");
String friendlyNameFilter = info.getQueryParameters().getFirst("FriendlyName");
String applicationSid = info.getQueryParameters().getFirst("ApplicationSid");
String page = info.getQueryParameters().getFirst("Page");
String reverse = info.getQueryParameters().getFirst("Reverse");
String pageSize = info.getQueryParameters().getFirst("PageSize");
Expand All @@ -306,14 +307,14 @@ protected Response getIncomingPhoneNumbers(final String accountSid, final PhoneN
int pageAsInt = Integer.parseInt(page);
int offset = (page == "0") ? 0 : (((pageAsInt - 1) * limit) + limit);
IncomingPhoneNumberFilter incomingPhoneNumberFilter = new IncomingPhoneNumberFilter(accountSid, friendlyNameFilter,
phoneNumberFilter);
phoneNumberFilter, applicationSid);
final int total = dao.getTotalIncomingPhoneNumbers(incomingPhoneNumberFilter);

if (pageAsInt > (total / limit)) {
return status(javax.ws.rs.core.Response.Status.BAD_REQUEST).build();
}

incomingPhoneNumberFilter = new IncomingPhoneNumberFilter(accountSid, friendlyNameFilter, phoneNumberFilter, sortBy,
incomingPhoneNumberFilter = new IncomingPhoneNumberFilter(accountSid, friendlyNameFilter, phoneNumberFilter, applicationSid, sortBy,
reverse, limit, offset);

final List<IncomingPhoneNumber> incomingPhoneNumbers = dao.getIncomingPhoneNumbersByFilter(incomingPhoneNumberFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,55 @@
-->

<!-- along with the DID it returns the application frien-->
<select id="getIncomingPhoneNumbersByFriendlyName" parameterType="string" resultType="hashmap">
<select id="getIncomingPhoneNumbersByFriendlyName" parameterType="org.restcomm.connect.dao.entities.IncomingPhoneNumberFilter" resultType="hashmap">
SELECT
"n".*,
"a_voice"."friendly_name" "voice_application_name",
"a_sms"."friendly_name" "sms_application_name",
"a_ussd"."friendly_name" "ussd_application_name"
"a_ussd"."friendly_name" "ussd_application_name",
"a_refer"."friendly_name" "refer_application_name"
FROM "restcomm_incoming_phone_numbers" "n"
LEFT OUTER JOIN "restcomm_applications" "a_voice" ON "n"."voice_application_sid" = "a_voice"."sid"
LEFT OUTER JOIN "restcomm_applications" "a_sms" ON "n"."sms_application_sid" = "a_sms"."sid"
LEFT OUTER JOIN "restcomm_applications" "a_ussd" ON "n"."ussd_application_sid" = "a_ussd"."sid"
LEFT OUTER JOIN "restcomm_applications" "a_refer" ON "n"."refer_application_sid" = "a_refer"."sid"
WHERE "account_sid"=#{accountSid}
<if test="friendlyName != null">
AND "n"."friendly_name"=#{friendlyName}
</if>
<if test="phoneNumber != null">
AND "n"."phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("n"."voice_application_sid" = #{applicationSid} OR "n"."sms_application_sid" = #{applicationSid} OR "n"."ussd_application_sid" = #{applicationSid} OR "n"."refer_application_sid" = #{applicationSid})
</if>
<choose>
<when test="sortBy == 'phone_number'">
order by "n"."phone_number" ${sortDirection}
</when>
<when test="sortBy == 'friendly_name'">
order by "n"."friendly_name" ${sortDirection}
</when>
<otherwise>
order by "n"."phone_number" ${sortDirection}
</otherwise>
</choose>

LIMIT #{limit} OFFSET #{offset}
</select>

<select id="getTotalIncomingPhoneNumbersByUsingFilters" parameterType="org.restcomm.connect.dao.entities.IncomingPhoneNumberFilter" resultType="int">
SELECT COUNT(*) FROM "restcomm_incoming_phone_numbers" WHERE "account_sid"=#{accountSid}

<if test="friendlyName != null">
AND "friendly_name"=#{friendlyName}
</if>
<if test="phoneNumber != null">
AND "phone_number" like #{phoneNumber}
</if>
<if test="applicationSid != null">
AND ("voice_application_sid" = #{applicationSid} OR "sms_application_sid" = #{applicationSid} OR "ussd_application_sid" = #{applicationSid} OR "refer_application_sid" = #{applicationSid})
</if>
</select>

<delete id="removeIncomingPhoneNumber" parameterType="string">
Expand Down
Loading