blob: bc90274f12633558bd68a9d0bad0f2275ec18760 [file] [log] [blame]
Alexandre Lisionaad014e2014-01-14 17:45:49 -05001/*
2 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
32package org.sflphone.history;
33
34import android.content.Context;
35import com.j256.ormlite.android.apptools.OpenHelperManager;
Alexandre Lision67c70b42014-01-16 13:57:15 -050036import com.j256.ormlite.stmt.QueryBuilder;
37import com.j256.ormlite.table.TableUtils;
Alexandre Lision945e4612014-01-15 17:40:31 -050038import org.sflphone.model.Conference;
39import org.sflphone.model.SipCall;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050040
41import java.sql.SQLException;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050042import java.util.List;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050043
44public class HistoryManager {
45
46 private Context mContext;
47 private DatabaseHelper historyDBHelper = null;
48
49 public HistoryManager(Context context) {
Alexandre Lisionaad014e2014-01-14 17:45:49 -050050 mContext = context;
Alexandre Lision945e4612014-01-15 17:40:31 -050051 getHelper();
52 }
53
54 public boolean insertNewEntry(Conference toInsert){
55 for (SipCall call : toInsert.getParticipants()) {
56 call.setTimestampEnd_(System.currentTimeMillis() * 1000);
57 HistoryCall persistent = new HistoryCall(call);
58 try {
59 getHelper().getHistoryDao().create(persistent);
60 } catch (SQLException e) {
61 e.printStackTrace();
62 return false;
63 }
64 }
65 return true;
66 }
67
Alexandre Lision183bf452014-01-17 11:21:59 -050068 /*
69 * Necessary when user hang up a call in a Conference
70 * The call creates an HistoryCall, but the conference still goes on
71 */
Alexandre Lision945e4612014-01-15 17:40:31 -050072 public boolean insertNewEntry(SipCall toInsert){
73 return true;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050074 }
75
76 /**
77 * Retrieve helper for our DB
78 */
79 private DatabaseHelper getHelper() {
80 if (historyDBHelper == null) {
81 historyDBHelper = OpenHelperManager.getHelper(mContext, DatabaseHelper.class);
82 }
83 return historyDBHelper;
84 }
85
86 public List<HistoryCall> getAll() throws SQLException {
Alexandre Lision67c70b42014-01-16 13:57:15 -050087
88 QueryBuilder<HistoryCall, Integer> qb = getHelper().getHistoryDao().queryBuilder();
89 qb.orderBy("TIMESTAMP_START", true);
90
91 return getHelper().getHistoryDao().query(qb.prepare());
92 }
93
94 public boolean clearDB() {
95 try {
96 TableUtils.clearTable(getHelper().getConnectionSource(), HistoryCall.class);
97 } catch (SQLException e) {
98 e.printStackTrace();
99 return false;
100 }
101 return true;
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500102 }
103}