org.restcomm.connect.dao.mybatis.MybatisAccountsDao Maven / Gradle / Ivy
/*
* 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
*
*/
package org.restcomm.connect.dao.mybatis;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.joda.time.DateTime;
import org.restcomm.connect.commons.annotations.concurrency.ThreadSafe;
import org.restcomm.connect.dao.AccountsDao;
import org.restcomm.connect.dao.entities.Account;
import org.restcomm.connect.commons.dao.Sid;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.restcomm.connect.dao.DaoUtils.readAccountStatus;
import static org.restcomm.connect.dao.DaoUtils.readAccountType;
import static org.restcomm.connect.dao.DaoUtils.readDateTime;
import static org.restcomm.connect.dao.DaoUtils.readSid;
import static org.restcomm.connect.dao.DaoUtils.readString;
import static org.restcomm.connect.dao.DaoUtils.readUri;
import static org.restcomm.connect.dao.DaoUtils.writeAccountStatus;
import static org.restcomm.connect.dao.DaoUtils.writeAccountType;
import static org.restcomm.connect.dao.DaoUtils.writeDateTime;
import static org.restcomm.connect.dao.DaoUtils.writeSid;
import static org.restcomm.connect.dao.DaoUtils.writeUri;
/**
* @author [email protected] (Thomas Quintana)
*/
@ThreadSafe
public final class MybatisAccountsDao implements AccountsDao {
private static final String namespace = "org.mobicents.servlet.sip.restcomm.dao.AccountsDao.";
private Integer accountRecursionDepth = 4; // maximum value for recursive account queries
private final SqlSessionFactory sessions;
public MybatisAccountsDao(final SqlSessionFactory sessions) {
super();
this.sessions = sessions;
}
public void setAccountRecursionDepth(Integer accountRecursionDepth) {
this.accountRecursionDepth = accountRecursionDepth;
}
@Override
public void addAccount(final Account account) {
final SqlSession session = sessions.openSession();
try {
session.insert(namespace + "addAccount", toMap(account));
session.commit();
} finally {
session.close();
}
}
@Override
public Account getAccount(final Sid sid) {
return getAccount(namespace + "getAccount", sid.toString());
}
@Override
public Account getAccount(final String name) {
Account account = null;
account = getAccount(namespace + "getAccountByFriendlyName", name);
if (account == null) {
account = getAccount(namespace + "getAccountByEmail", name);
}
if (account == null) {
account = getAccount(namespace + "getAccount", name);
}
return account;
}
@Override
public Account getAccountToAuthenticate(final String name) {
Account account = null;
account = getAccount(namespace + "getAccountByEmail", name);
if (account == null) {
account = getAccount(namespace + "getAccount", name);
}
return account;
}
private Account getAccount(final String selector, final Object parameters) {
final SqlSession session = sessions.openSession();
try {
final Map result = session.selectOne(selector, parameters);
if (result != null) {
return toAccount(result);
} else {
return null;
}
} finally {
session.close();
}
}
@Override
public List getChildAccounts(final Sid parentSid) {
final SqlSession session = sessions.openSession();
try {
final List © 2015 - 2025 Weber Informatics LLC | Privacy Policy