Permalink
Browse files

photo updates

  • Loading branch information...
1 parent 53e6c2b commit f0965b12d2284996d845cb79478f3068ad8d76e1 @mdhari committed Nov 12, 2011
@@ -19,13 +19,18 @@
@XmlRootElement
public class AlbumResource {
+
+
/**
* MH: Apparently if you make these public, you don't need getters and
* setters because JAXB can set it correctly. But its usually bad practice
* to expose all properties of a class publicly
* <p>
* Make sure you DO make getters and setters, JAXB needs them
*/
+
+ private String photo_id;
+
private String album_id;
private String album_name;
private String album_description;
@@ -66,6 +71,25 @@ public Map getMap() {
if (!Utils.isEmptyString(created_date))
map.put("createdDate", created_date);
+
+ if (!Utils.isEmptyString(photo_id))
+ map.put("photo_id", photo_id);
+
+ if (!Utils.isEmptyString(time_taken))
+ map.put("time_taken", time_taken);
+
+ if (!Utils.isEmptyString(latitude))
+ map.put("latitude", latitude);
+
+ if (!Utils.isEmptyString(longitude))
+ map.put("longitude", longitude);
+
+ if (!Utils.isEmptyString(created_by))
+ map.put("created_by", created_by);
+
+ if (!Utils.isEmptyString(description))
+ map.put("description", description);
+
return map;
@@ -79,6 +103,31 @@ public AlbumResource(String album_name, String album_description,
this.user_id = user_id;
this.created_date = created_date;
}
+
+ /**
+ * for photo addition to db
+ * @param photo_id
+ * @param album_id
+ * @param user_id
+ * @param time_taken
+ * @param latitude
+ * @param longitude
+ * @param created_by
+ * @param description
+ */
+ public AlbumResource(String photo_id, String album_id, String user_id,
+ String time_taken, String latitude, String longitude,
+ String created_by, String description) {
+
+ this.photo_id = photo_id;
+ this.album_id = album_id;
+ this.user_id = user_id;
+ this.time_taken = time_taken;
+ this.latitude = latitude;
+ this.longitude = longitude;
+ this.created_by = created_by;
+ this.description = description;
+ }
public boolean hasNull() {
if (Utils.isEmptyString(album_name)
@@ -206,17 +255,26 @@ public String getDescription() {
return description;
}
+ public void setPhoto_id(String photo_id) {
+ this.photo_id = photo_id;
+ }
+
+ public String getPhoto_id() {
+ return photo_id;
+ }
+
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
- return "AlbumResource [album_id=" + album_id + ", album_name="
- + album_name + ", album_description=" + album_description
- + ", user_id=" + user_id + ", created_date=" + created_date
- + ", time_taken=" + time_taken + ", latitude=" + latitude
- + ", longitude=" + longitude + ", created_by=" + created_by
- + ", description=" + description + "]";
+ return "AlbumResource [photo_id=" + photo_id + ", album_id=" + album_id
+ + ", album_name=" + album_name + ", album_description="
+ + album_description + ", user_id=" + user_id
+ + ", created_date=" + created_date + ", time_taken="
+ + time_taken + ", latitude=" + latitude + ", longitude="
+ + longitude + ", created_by=" + created_by + ", description="
+ + description + "]";
}
}
@@ -2,16 +2,21 @@
+import java.util.HashMap;
+import java.util.Map;
+
import javax.xml.bind.annotation.XmlRootElement;
+import edu.sjsu.carbonated.util.Utils;
+
@XmlRootElement
public class PhotoResource {
/**
* MH: Apparently if you make these public, you don't need getters
* and setters because JAXB can set it correctly. But its usually
* bad practice to expose all properties of a class publicly
*/
-
+ private String photo_id;
private String user_id;
private String time_taken;
private String latitude;
@@ -20,6 +25,46 @@
private String created_by;
private String description;
+
+ /**
+ * Less maintenance for storing in the DB
+ *
+ * @return Map
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public Map getMap() {
+
+ Map map = new HashMap();
+
+ if (!Utils.isEmptyString(photo_id))
+ map.put("photo_id", photo_id);
+
+ if (!Utils.isEmptyString(user_id))
+ map.put("user_id", user_id);
+
+ if (!Utils.isEmptyString(time_taken))
+ map.put("time_taken", time_taken);
+
+ if (!Utils.isEmptyString(latitude))
+ map.put("latitude", latitude);
+
+ if (!Utils.isEmptyString(longitude))
+ map.put("longitude", longitude);
+
+ if (!Utils.isEmptyString(album_id))
+ map.put("album_id", album_id);
+
+ if (!Utils.isEmptyString(created_by))
+ map.put("created_by", created_by);
+
+ if (!Utils.isEmptyString(description))
+ map.put("description", description);
+
+
+ return map;
+
+ }
+
public PhotoResource(){
// JAXB needs this
}
@@ -116,15 +161,24 @@ public String getUser_id() {
return user_id;
}
+ public void setPhoto_id(String photo_id) {
+ this.photo_id = photo_id;
+ }
+
+ public String getPhoto_id() {
+ return photo_id;
+ }
+
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
- return "PhotoResource [user_id=" + user_id + ", time_taken="
- + time_taken + ", latitude=" + latitude + ", longitude="
- + longitude + ", album_id=" + album_id + ", created_by="
- + created_by + ", description=" + description + "]";
+ return "PhotoResource [photo_id=" + photo_id + ", user_id=" + user_id
+ + ", time_taken=" + time_taken + ", latitude=" + latitude
+ + ", longitude=" + longitude + ", album_id=" + album_id
+ + ", created_by=" + created_by + ", description=" + description
+ + "]";
}
@@ -21,40 +21,43 @@
* @date: Nov 6, 2011
*/
-import com.mongodb.Mongo;
-import com.mongodb.DBCollection;
+import java.net.UnknownHostException;
+import java.util.Set;
+
import com.mongodb.BasicDBObject;
-import com.mongodb.DBObject;
-import com.mongodb.DBCursor;
import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.DBCursor;
+import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
import edu.sjsu.carbonated.data.AlbumResource;
-
-import java.lang.reflect.Field;
-import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.List;
+import edu.sjsu.carbonated.data.PhotoResource;
public class MongoDBAlbum {
- protected static final String sCollection = "Album";
+ protected static final String albumCollection = "Album";
+ protected static final String photoCollection = "Photo";
protected static final String sDB = "OneVillage";
protected static final String sHost = "localhost";
+ protected static final String photo_url_location = "http://localhost:8080/albums/";
+
+
Mongo m = null;
DB db = null;
- DBCollection collection = null;
+ DBCollection albumColl = null;
+ DBCollection photoColl = null;
+
public MongoDBAlbum() {
try {
m = new Mongo(sHost);
DB db = m.getDB(sDB);
- collection = db.getCollection(sCollection);
+ albumColl = db.getCollection(albumCollection);
+ photoColl = db.getCollection(photoCollection);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -74,7 +77,7 @@ public void printCollections() {
}
public void truncateDBCollection() {
- collection.drop();
+ albumColl.drop();
}
/**
@@ -89,15 +92,24 @@ public void addAlbum(AlbumResource albumRes) {
BasicDBObject info = new BasicDBObject();
info.putAll(albumRes.getMap());
- collection.insert(info);
+ albumColl.insert(info);
}
+
+ public void addPhotoToAlbum(AlbumResource albumRes){
+
+ BasicDBObject info = new BasicDBObject();
+ info.putAll(albumRes.getMap());
+ info.put("photo_url", photo_url_location+albumRes.getAlbum_id()+"/"+albumRes.getPhoto_id() +".png");
+ photoColl.insert(info);
+
+ }
public String getAllAlbums() {
String toReturn = "[";
- DBCursor cur = collection.find(new BasicDBObject(), new BasicDBObject("_id",0));
+ DBCursor cur = albumColl.find(new BasicDBObject(), new BasicDBObject("_id",0));
while (cur.hasNext()) {
toReturn += cur.next();
@@ -114,7 +126,7 @@ public String getAlbumsByUser(String user_id){
String toReturn = "[";
- DBCursor cur = collection.find(new BasicDBObject("user_id",user_id),new BasicDBObject("_id",0));
+ DBCursor cur = albumColl.find(new BasicDBObject("user_id",user_id),new BasicDBObject("_id",0));
while (cur.hasNext()) {
toReturn += cur.next();
@@ -143,7 +155,20 @@ public void updateAlbum(String album_id, AlbumResource albumRes) {
query.put("album_id", album_id);
- collection.update(query, update);
+ albumColl.update(query, update);
+
+ }
+
+ public void updatePhoto(String album_id, String photo_id, AlbumResource albumRes) {
+
+ //BasicDBObject query = new BasicDBObject("$and", new BasicDBObject("album_id",album_id).append("photo_id", photo_id));
+ BasicDBObject query = new BasicDBObject("$and", JSON.parse("[{\"album_id\":\""+album_id+"\"},{\"photo_id\":\""+photo_id+"\"}]"));
+
+ BasicDBObject update = new BasicDBObject("$set", new BasicDBObject(
+ albumRes.getMap()));
+
+ db.getCollection(albumCollection);
+ photoColl.update(query, update);
}
@@ -160,7 +185,17 @@ public void removeAlbum(String album_id, String user_id) {
removeQuery.put("album_id", album_id);
- collection.remove(removeQuery);
+ albumColl.remove(removeQuery);
+
+ }
+
+ public void removePhoto(String user_id,String photo_id) {
+
+ BasicDBObject removeQuery = new BasicDBObject();
+
+ removeQuery.put("photo_id", photo_id);
+
+ photoColl.remove(removeQuery);
}
Oops, something went wrong.

0 comments on commit f0965b1

Please sign in to comment.