Best Android App InstaSquare Lite for full-size publication of photos in Instagram, Facebook, Twitter. Run the android application on PC. You can through the android emulator Droid4X.

root/trunk/rapidandroid/org.rapidandroid/src/org/rapidandroid/data/controller/ParsedDataReporter.java @ 134

Revision 134, 7.1 KB (checked in by dmyung, 10 months ago)

added some secret sections for FormReview? for debug data injection

but for real stuff, added a settings file that's generated on startup for the first time, and is persisted in the settings directory to set a preference for ACK replies for parse and no parse events.

Line 
1/*
2 *    rapidandroid - SMS gateway for the android platform
3 *    Copyright (C) 2009 Dimagi Inc., UNICEF
4 *
5 *   This program is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19package org.rapidandroid.data.controller;
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.io.IOException;
25import java.text.ParseException;
26import java.util.Calendar;
27import java.util.Date;
28import java.util.zip.GZIPOutputStream;
29
30import org.rapidandroid.data.RapidSmsDBConstants;
31import org.rapidandroid.data.SmsDbHelper;
32import org.rapidsms.java.core.Constants;
33import org.rapidsms.java.core.model.Form;
34import org.rapidsms.java.core.model.Message;
35
36import android.content.Context;
37import android.database.Cursor;
38import android.database.sqlite.SQLiteDatabase;
39import android.os.Environment;
40
41/**
42 *
43 * @author Daniel Myung [email protected]
44 * @created Jan 30, 2009
45 *
46 */
47public class ParsedDataReporter {
48
49        private String[] messageColumns = new String[] { "message_time", "monitor_id", "monitor_phone", "message_text" };
50
51        public synchronized static Date getOldestMessageDate(Context context, Form f) {
52                SmsDbHelper mHelper = new SmsDbHelper(context);
53                Date toReturn = getOldestMessageDate(mHelper, f);
54                mHelper.close();
55                return toReturn;
56
57        }
58
59        public synchronized static Date getOldestMessageDate(SmsDbHelper mHelper, Form f) {
60                // TODO Auto-generated method stub
61                StringBuilder query = new StringBuilder();
62                query.append("select min(rapidandroid_message.time) ");
63                query.append(" from " + RapidSmsDBConstants.FormData.TABLE_PREFIX + f.getPrefix());
64                query.append(" join rapidandroid_message on (");
65                query.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + f.getPrefix());
66                query.append(".message_id = rapidandroid_message._id");
67                query.append(") ");
68
69                SQLiteDatabase db = mHelper.getReadableDatabase();
70                Cursor cr = db.rawQuery(query.toString(), null);
71                if (cr.getCount() == 0) {
72                        cr.close();
73                        db.close();
74                        // this is the caller's responsibility
75                        // mHelper.close();
76                        return Constants.NULLDATE;
77
78                }
79                cr.moveToFirst();
80                String dateString = cr.getString(0);
81
82                if (dateString == null) {
83                        cr.close();
84                        db.close();
85                        // this is the caller's responsibility
86                        // mHelper.close();
87                        return Constants.NULLDATE;
88                }
89
90                Date ret = new Date();
91                try {
92                        ret = Message.SQLDateFormatter.parse(dateString);
93                } catch (ParseException e) {
94                        // TODO Auto-generated catch block
95
96                        e.printStackTrace();
97                        try {
98                                if (cr != null) {
99                                        cr.close();
100                                }
101                                if (db != null) {
102                                        db.close();
103                                }
104                                if (mHelper != null) {
105                                        // this is the caller's responsibility
106                                        // mHelper.close();
107                                }
108                        } catch (Exception ex2) {
109
110                        }
111                }
112                cr.close();
113                db.close();
114                // this is the caller's responsibility
115                // mHelper.close();
116                return ret;
117        }
118
119        public synchronized static void exportFormDataToCSV(Context context, Form f, Calendar startDate, Calendar endDate) {
120                SmsDbHelper mHelper = new SmsDbHelper(context);
121                // build the query
122                StringBuilder query = new StringBuilder();
123                query.append("select " + RapidSmsDBConstants.FormData.TABLE_PREFIX);
124                query.append(f.getPrefix() + ".*");
125                query
126                                .append(", rapidandroid_message.message,rapidandroid_message.time, rapidandroid_monitor._id as monitor_id, rapidandroid_monitor.phone as monitor_phone ");
127                query.append(" from " + RapidSmsDBConstants.FormData.TABLE_PREFIX + f.getPrefix());
128                query.append(" join rapidandroid_message on (");
129                query.append(RapidSmsDBConstants.FormData.TABLE_PREFIX + f.getPrefix());
130                query.append(".message_id = rapidandroid_message._id");
131                query.append(") ");
132
133                query.append(" join rapidandroid_monitor on (");
134                query.append("rapidandroid_monitor._id = rapidandroid_message.monitor_id");
135                query.append(") ");
136
137                query.append("WHERE rapidandroid_message.time > '" + startDate.get(Calendar.YEAR) + "-"
138                                + (startDate.get(Calendar.MONTH) + 1) + "-" + startDate.get(Calendar.DATE) + "' AND ");
139                query.append(" rapidandroid_message.time < '" + endDate.get(Calendar.YEAR) + "-"
140                                + (1 + endDate.get(Calendar.MONTH)) + "-" + endDate.get(Calendar.DATE) + "';");
141
142                Cursor cr = mHelper.getReadableDatabase().rawQuery(query.toString(), null);
143                FileOutputStream fOut = null;
144               
145                try {
146
147                        File sdcard = Environment.getExternalStorageDirectory();
148                        File destinationdir = new File(sdcard, "rapidandroid/exports");
149                        destinationdir.mkdir();
150                        Date now = new Date();
151                        File destinationfile = new File(destinationdir, "formdata_" + f.getPrefix() + now.getYear()
152                                        + now.getMonth() + now.getDate() + "-" + now.getHours() + now.getMinutes() + ".csv");
153                       
154               
155                        destinationfile.createNewFile();
156                        fOut = new FileOutputStream(destinationfile);
157                        String[] cols = cr.getColumnNames();
158                        int colcount = cols.length;
159                        StringBuilder sbrow = new StringBuilder();
160                        for (int i = 0; i < colcount; i++) {
161                                // sbrow.append(cols[i] + ",");
162                                sbrow.append(cols[i]);
163                                if (i < colcount - 1) {
164                                        sbrow.append(",");
165                                } else {
166                                        sbrow.append("\n");
167                                }
168                        }
169                        fOut.write(sbrow.toString().getBytes());
170                        cr.moveToFirst();
171                        do {
172                                sbrow = new StringBuilder();
173                                for (int i = 0; i < colcount; i++) {
174                                        // sbrow.append(cr.getString(i) + ",");
175                                        sbrow.append(cr.getString(i));
176                                        if (i < colcount - 1) {
177                                                sbrow.append(",");
178                                        } else {
179                                                sbrow.append("\n");
180                                        }
181                                }
182                                fOut.write(sbrow.toString().getBytes());
183                        } while (cr.moveToNext());
184                } catch (IOException e) {
185                        // TODO Auto-generated catch block
186                        e.printStackTrace();
187                } finally {
188                        cr.close();
189                        mHelper.close();
190                        if (fOut != null) {
191                                try {
192                                        fOut.close();
193                                } catch (IOException e) {
194                                        // TODO Auto-generated catch block
195                                        e.printStackTrace();
196                                }
197                        }
198                        // compressFile(destinationfile);
199                }
200
201        }
202
203        void compressFile(File rawFile) {
204                FileInputStream fin = null;
205                GZIPOutputStream gz = null;
206                try {
207                        fin = new FileInputStream(rawFile);
208                        FileOutputStream fout = new FileOutputStream(rawFile.getAbsoluteFile() + ".gz");
209                        gz = new GZIPOutputStream(fout);
210                        byte[] buf = new byte[4096];
211                        int readCount;
212                        while ((readCount = fin.read(buf)) != -1) {
213                                gz.write(buf, 0, readCount);
214                        }
215
216                } catch (Exception ex) {
217                } finally {
218                        // Close the BufferedInputStream
219                        try {
220                                if (fin != null)
221                                        fin.close();
222                                if (gz != null)
223                                        gz.close();
224                        } catch (IOException ex) {
225                                ex.printStackTrace();
226                        }
227
228                }
229        }
230}
Note: See TracBrowser for help on using the browser.