Prefixed id with _ so Raw Packet Table will work with cursor adapters.
[APRS-X.git] / src / com / dryerzinia / aprs_x / db / table / RawPacketTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.dryerzinia.aprs_x.db.table;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
 
import com.dryerzinia.aprs_x.data_model.packet.RawPacket;
 
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
 
public class RawPacketTable {
 
        public static final String TABLE_RAW_PACKET = "packet_master";
 
        public static final String COLUMN_ID = "id";
 
        public static final String COLUMN_TIME_STAMP = "time_stamp";
 
        public static final String COLUMN_SOURCE_ADDRESS = "source_address";
        public static final String COLUMN_DESTINATION_ADDRESS = "desination_address";
 
        public static final String COLUMN_REPEATER_1_ADDRESS = "repeater_1_address";
        public static final String COLUMN_REPEATER_2_ADDRESS = "repeater_2_address";
        public static final String COLUMN_REPEATER_3_ADDRESS = "repeater_3_address";
        public static final String COLUMN_REPEATER_4_ADDRESS = "repeater_4_address";
        public static final String COLUMN_REPEATER_5_ADDRESS = "repeater_5_address";
        public static final String COLUMN_REPEATER_6_ADDRESS = "repeater_6_address";
        public static final String COLUMN_REPEATER_7_ADDRESS = "repeater_7_address";
        public static final String COLUMN_REPEATER_8_ADDRESS = "repeater_8_address";
 
        public static final String COLUMN_PACKET_TYPE = "packet_type";
        public static final String COLUMN_PACKET_TYPE_DATA_ID = "packet_type_data_id";
 
        public static final String COLUMN_RAW_DATA = "raw_rata";
 
        public static final String TABLE_CREATE =
                  "create table " + TABLE_RAW_PACKET
                + "("
 
                + COLUMN_ID + " INTEGER primary key autoincrement, "
 
                + COLUMN_TIME_STAMP + " INTEGER not null, "
 
                + COLUMN_SOURCE_ADDRESS + " TEXT not null, "
                + COLUMN_DESTINATION_ADDRESS + " TEXT not null, "
 
                + COLUMN_REPEATER_1_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_2_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_3_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_4_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_5_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_6_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_7_ADDRESS + " TEXT, "
                + COLUMN_REPEATER_8_ADDRESS + " TEXT, "
 
                + COLUMN_PACKET_TYPE + " INTEGER, "
                + COLUMN_PACKET_TYPE_DATA_ID + " INTEGER, "
 
                + COLUMN_RAW_DATA + " BLOB"
 
                + ");";
 
        private static final HashSet<String> availableColumns =
                new HashSet<String>(
                        Arrays.asList(new String[]{
 
                                COLUMN_ID,
 
                                COLUMN_TIME_STAMP,
 
                                COLUMN_SOURCE_ADDRESS,
                                COLUMN_DESTINATION_ADDRESS,
 
                                COLUMN_REPEATER_1_ADDRESS,
                                COLUMN_REPEATER_2_ADDRESS,
                                COLUMN_REPEATER_3_ADDRESS,
                                COLUMN_REPEATER_4_ADDRESS,
                                COLUMN_REPEATER_5_ADDRESS,
                                COLUMN_REPEATER_6_ADDRESS,
                                COLUMN_REPEATER_7_ADDRESS,
                                COLUMN_REPEATER_8_ADDRESS,
 
                                COLUMN_PACKET_TYPE,
                                COLUMN_PACKET_TYPE_DATA_ID,
 
                                COLUMN_RAW_DATA
                        })
                );
        
        public RawPacketTable(){
 
                //
 
        }
 
        public static void onCreate(SQLiteDatabase db){
 
                db.execSQL(TABLE_CREATE);
 
        }
 
        public static void onUpgrade(SQLiteDatabase db){
 
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_RAW_PACKET);
                onCreate(db);
 
        }
 
        public static ContentValues getContentValues(RawPacket pkt){
 
                ContentValues values = new ContentValues();
 
                values.put(RawPacketTable.COLUMN_TIME_STAMP, pkt.getTimeStamp());
 
                values.put(RawPacketTable.COLUMN_SOURCE_ADDRESS, pkt.getSourceAddress());
                values.put(RawPacketTable.COLUMN_DESTINATION_ADDRESS, pkt.getDestinationAddress());
 
                List<String> repeaterAddresses = pkt.getRepeaterAddress();
 
                if(repeaterAddresses.size() >= 1)
                        values.put(RawPacketTable.COLUMN_REPEATER_1_ADDRESS, repeaterAddresses.get(0));
                if(repeaterAddresses.size() >= 2)
                        values.put(RawPacketTable.COLUMN_REPEATER_2_ADDRESS, repeaterAddresses.get(1));
                if(repeaterAddresses.size() >= 3)
                        values.put(RawPacketTable.COLUMN_REPEATER_3_ADDRESS, repeaterAddresses.get(2));
                if(repeaterAddresses.size() >= 4)
                        values.put(RawPacketTable.COLUMN_REPEATER_4_ADDRESS, repeaterAddresses.get(3));
                if(repeaterAddresses.size() >= 5)
                        values.put(RawPacketTable.COLUMN_REPEATER_5_ADDRESS, repeaterAddresses.get(4));
                if(repeaterAddresses.size() >= 6)
                        values.put(RawPacketTable.COLUMN_REPEATER_6_ADDRESS, repeaterAddresses.get(5));
                if(repeaterAddresses.size() >= 7)
                        values.put(RawPacketTable.COLUMN_REPEATER_7_ADDRESS, repeaterAddresses.get(6));
                if(repeaterAddresses.size() >= 8)
                        values.put(RawPacketTable.COLUMN_REPEATER_8_ADDRESS, repeaterAddresses.get(7));
 
                values.put(RawPacketTable.COLUMN_RAW_DATA, pkt.getRawData());
 
                return values;
 
        }
 
        public static void checkColumns(String[] projection){
 
                if(projection != null) {
 
                        HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection));
 
                        if(!availableColumns.containsAll(requestedColumns)){
                                throw new IllegalArgumentException("Unknown columns in projection");
                        }
 
                }
 
        }
 
}