-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoitrackDAO.java
More file actions
162 lines (118 loc) · 4.44 KB
/
MoitrackDAO.java
File metadata and controls
162 lines (118 loc) · 4.44 KB
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
160
161
162
import DAO.MongoDAO;
import Entity.Tracker;
import Entity.Tracking;
import Entity.Vehicle;
import com.mongodb.*;
import java.util.ArrayList;
import java.util.Set;
@SuppressWarnings("deprecation")
public class MoitrackDAO implements MongoDAO {
private MongoClient mongoClient;
private DBCollection collection;
private DB database;
private Integer YOUR_PORT = 00000000;
public MoitrackDAO() {
collection = null;
this.mongoClient = new MongoClient("YOUR_HOST", YOUR_PORT);
System.out.println();
database = mongoClient.getDB("YOUR_DATABASE_NAME");
Set<String> set = database.getCollectionNames();
System.out.println("\nCollections: ");
for(String s: set){
System.out.println(s);
}
System.out.println();
}
private void switchCollections(String coll){
this.collection = database.getCollection(coll);
}
/************** Vehicle functionality ***************/
public ArrayList<DBObject> getAllVehicles() {
switchCollections("vehicle");
DBCursor cursor = collection.find();
ArrayList<DBObject> objects = new ArrayList<DBObject>();
while(cursor.hasNext()){
objects.add(cursor.next());
}
return objects;
}
public DBObject getVehicleByRegistrationNumber(String registrationNumber) {
switchCollections("vehicle");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("registrationNumber", registrationNumber);
DBCursor cursor = collection.find(searchQuery);
return cursor.one();
}
public void addVehicle(Vehicle vehicle) {
switchCollections("vehicle");
BasicDBObject document = new BasicDBObject();
document.put("_class", vehicle.get_class());
document.put("name", vehicle.getName());
document.put("imei", vehicle.getImei());
document.put("registrationNumber", vehicle.getRegistrationNumber());
document.put("description", vehicle.getDescription());
document.put("key", vehicle.getKey());
collection.insert(document);
}
public Boolean deleteVehicle(String registrationNumber) {
switchCollections("vehicle");
BasicDBObject document = new BasicDBObject();
document.put("registrationNumber", registrationNumber);
return (collection.remove(document).getN() == 1);
}
public Boolean updateVehicle(Vehicle vehicle) {
switchCollections("vehicle");
BasicDBObject original = new BasicDBObject();
original.put("imei", vehicle.getImei());
BasicDBObject newObject = new BasicDBObject();
newObject.put("_class", vehicle.get_class());
newObject.put("name", vehicle.getName());
newObject.put("model", vehicle.getModel());
newObject.put("key", vehicle.getKey());
newObject.put("registrationNumber", vehicle.getRegistrationNumber());
newObject.put("description", vehicle.getDescription());
return (collection.update(original, newObject).getN() == 1);
}
/************** Tracker functionality ***************/
public ArrayList<DBObject> getAllTracker() {
switchCollections("tracker");
DBCursor cursor = collection.find();
ArrayList<DBObject> objects = new ArrayList<DBObject>();
while(cursor.hasNext()){
objects.add(cursor.next());
}
return objects;
}
public DBObject getTrackerByRegistrationNumber(String registrationNumber) {
return null;
}
public void addTracker(Tracker tracker) {
}
public Boolean deleteTracker(String registrationNumber) {
return null;
}
public Boolean updateTracker(Tracker tracker) {
return null;
}
/************** Tracking functionality ***************/
public ArrayList<DBObject> getAllTracking() {
switchCollections("tracking");
DBCursor cursor = collection.find();
ArrayList<DBObject> objects = new ArrayList<DBObject>();
while(cursor.hasNext()){
objects.add(cursor.next());
}
return objects;
}
public DBObject getTrackingByRegistrationNumber(String registrationNumber) {
return null;
}
public void addTracking(Tracking tracking) {
}
public Boolean deleteTracking(String registrationNumber) {
return null;
}
public Boolean updateTracking(Tracking tracking) {
return null;
}
}