blob: 70108e1f86685d6573299c3b28a28431cfe02371 [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
68 public boolean insertNewEntry(SipCall toInsert){
69 return true;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050070 }
71
72 /**
73 * Retrieve helper for our DB
74 */
75 private DatabaseHelper getHelper() {
76 if (historyDBHelper == null) {
77 historyDBHelper = OpenHelperManager.getHelper(mContext, DatabaseHelper.class);
78 }
79 return historyDBHelper;
80 }
81
82 public List<HistoryCall> getAll() throws SQLException {
Alexandre Lision67c70b42014-01-16 13:57:15 -050083
84 QueryBuilder<HistoryCall, Integer> qb = getHelper().getHistoryDao().queryBuilder();
85 qb.orderBy("TIMESTAMP_START", true);
86
87 return getHelper().getHistoryDao().query(qb.prepare());
88 }
89
90 public boolean clearDB() {
91 try {
92 TableUtils.clearTable(getHelper().getConnectionSource(), HistoryCall.class);
93 } catch (SQLException e) {
94 e.printStackTrace();
95 return false;
96 }
97 return true;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050098 }
99}