/* * file : UserDAO.java * */ import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.ObjectId; import com.sigawe.accounts.db.DBConnection; import com.sigawe.accounts.models.User; import java.util.ArrayList; import java.util.Date; import java.util.List; public class UserDAO { private DBCollection coll; public UserDAO() { coll = DBConnection.getDBCollection("user"); } public void save(User user) { BasicDBObject bdbo = new BasicDBObject(); bdbo.put("username", user.getUsername()); bdbo.put("password", user.getPassword()); bdbo.put("email", user.getEmail()); bdbo.put("name", user.getName()); bdbo.put("address", user.getAddress()); bdbo.put("sex", user.getSex()); bdbo.put("birthDate", user.getBirthDate()); bdbo.put("city", user.getCity()); bdbo.put("state", user.getState()); bdbo.put("status", user.getStatus()); coll.save(bdbo); } public void remove(String username) { BasicDBObject query = new BasicDBObject(); query.put("username", username); coll.remove(query); } public void update(String username, User user) { BasicDBObject query = new BasicDBObject(); query.put("username", username); BasicDBObject bdbo = new BasicDBObject(); bdbo.put("username", user.getUsername()); bdbo.put("password", user.getPassword()); bdbo.put("email", user.getEmail()); bdbo.put("name", user.getName()); bdbo.put("address", user.getAddress()); bdbo.put("sex", user.getSex()); bdbo.put("birthDate", user.getBirthDate()); bdbo.put("city", user.getCity()); bdbo.put("state", user.getState()); bdbo.put("status", user.getStatus()); coll.update(query, bdbo); } public User find(String username) { BasicDBObject query = new BasicDBObject(); query.put("username", username); User user = new User(); DBCursor cur = coll.find(query); while (cur.hasNext()) { DBObject dbo = cur.next(); user.setId((ObjectId) dbo.get("_id")); user.setUsername((String) dbo.get("username")); user.setPassword((String) dbo.get("password")); user.setEmail((String) dbo.get("email")); user.setAddress((String) dbo.get("address")); user.setSex((String) dbo.get("sex")); user.setBirthDate((Date) dbo.get("birthDate")); user.setCity((String) dbo.get("city")); user.setState((String) dbo.get("state")); user.setStatus((String) dbo.get("status")); } return user; } public Listfind() { List users = new ArrayList (); DBCursor cur = coll.find(); while (cur.hasNext()) { DBObject dbo = cur.next(); User user = new User(); user.setId((ObjectId) dbo.get("_id")); user.setUsername((String) dbo.get("username")); user.setPassword((String) dbo.get("password")); user.setEmail((String) dbo.get("email")); user.setName((String) dbo.get("name")); user.setAddress((String) dbo.get("address")); user.setSex((String) dbo.get("sex")); user.setBirthDate((Date) dbo.get("birthDate")); user.setCity((String) dbo.get("city")); user.setState((String) dbo.get("state")); user.setStatus((String) dbo.get("status")); users.add(user); } return users; } public List find(DBObject query) { List users = new ArrayList (); DBCursor cur = coll.find(query); while (cur.hasNext()) { DBObject dbo = cur.next(); User user = new User(); user.setId((ObjectId) dbo.get("_id")); user.setUsername((String) dbo.get("username")); user.setPassword((String) dbo.get("password")); user.setEmail((String) dbo.get("email")); user.setName((String) dbo.get("name")); user.setAddress((String) dbo.get("address")); user.setSex((String) dbo.get("sex")); user.setBirthDate((Date) dbo.get("birthDate")); user.setCity((String) dbo.get("city")); user.setState((String) dbo.get("state")); user.setStatus((String) dbo.get("status")); users.add(user); } return users; } public User findOne() { DBObject dbo = coll.findOne(); User user = new User(); user.setId((ObjectId) dbo.get("_id")); user.setUsername((String) dbo.get("username")); user.setPassword((String) dbo.get("password")); user.setEmail((String) dbo.get("email")); user.setName((String) dbo.get("name")); user.setAddress((String) dbo.get("address")); user.setSex((String) dbo.get("sex")); user.setBirthDate((Date) dbo.get("birthDate")); user.setCity((String) dbo.get("city")); user.setState((String) dbo.get("state")); user.setStatus((String) dbo.get("status")); return user; } } /* * file : User.java * */ import com.mongodb.ObjectId; import java.util.Date; public class User { private ObjectId id; private String username; private String password; private String email; private String name; private String address; private String sex; private Date birthDate; private String city; private String state; private String status; public User() { } public ObjectId getId() { return id; } public void setId(ObjectId id) { this.id = id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
UserDAO and User Model for MongoDB
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment