Java tutorial
/* ======================================================================== * Copyright 2014 Arquidalgos * * Licensed under the MIT, The MIT License (MIT) * Copyright (c) 2014 Arquidalgos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ======================================================================== Source generated by CrudMaker version 1.0.0.201408112050 */ package co.edu.uniandes.csw.Arquidalgos.usuario.persistence; import co.edu.uniandes.csw.Arquidalgos.usuario.logic.dto.UsuarioDTO; import co.edu.uniandes.csw.Arquidalgos.usuario.persistence.api._IUsuarioPersistence; import co.edu.uniandes.csw.Arquidalgos.usuario.persistence.converter.UsuarioConverter; import co.edu.uniandes.csw.Arquidalgos.usuario.persistence.entity.UsuarioEntity; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; public abstract class _UsuarioPersistence extends MongoConfig implements _IUsuarioPersistence { @PersistenceContext(unitName = "TuMeOpPU") protected EntityManager entityManager; public UsuarioDTO createUsuario(UsuarioDTO user) { // UsuarioEntity entity=UsuarioConverter.persistenceDTO2Entity(usuario); // entityManager.persist(entity); // return UsuarioConverter.entity2PersistenceDTO(entity); DBCollection coll = null; BasicDBObject doc = null; UsuarioDTO usuario = (UsuarioDTO) user; coll = db.getCollection("UsuarioDTO"); doc = new BasicDBObject(); doc.append("name", usuario.getName()); doc.append("contrasena", usuario.getContrasena()); doc.append("email", usuario.getEmail()); doc.append("facebookId", usuario.getFacebookId()); coll.insert(doc); return user; } @SuppressWarnings("unchecked") public List<UsuarioDTO> getUsuarios() { // Query q = entityManager.createQuery("select u from UsuarioEntity u"); // return UsuarioConverter.entity2PersistenceDTOList(q.getResultList()); List listaUsuarios = new ArrayList(); DBCollection coll = db.getCollection("UsuarioDTO"); BasicDBObject query = new BasicDBObject(); DBCursor cursor = coll.find(query); while (cursor.hasNext()) { DBObject dBObject = cursor.next(); UsuarioDTO user = new UsuarioDTO(); user.setName((String) dBObject.get("name")); user.setContrasena((String) dBObject.get("contrasena")); user.setEmail((String) dBObject.get("email")); user.setFacebookId((String) dBObject.get("facebookId")); //TODO imprimir usuarios listaUsuarios.add(user); } cursor.close(); return listaUsuarios; } public UsuarioDTO getUsuario(Long id) { return UsuarioConverter.entity2PersistenceDTO(entityManager.find(UsuarioEntity.class, id)); } public void deleteUsuario(Long id) { UsuarioEntity entity = entityManager.find(UsuarioEntity.class, id); entityManager.remove(entity); } public void deleteUsuarioNoSql(String id) { DBCollection coll = null; BasicDBObject doc = new BasicDBObject(); coll = db.getCollection("UsuarioDTO"); doc.append("facebookId", id); coll.remove(doc); } public void updateUsuario(UsuarioDTO detail) { // UsuarioEntity entity=entityManager.merge(UsuarioConverter.persistenceDTO2Entity(detail)); // UsuarioConverter.entity2PersistenceDTO(entity); DBCollection col = null; DBObject query = null; BasicDBObject doc = null; DBObject update = new BasicDBObject(); col = db.getCollection("UsuarioDTO"); String id = ((UsuarioDTO) detail).getFacebookId(); query = new BasicDBObject("id", id); doc = toDoc(detail); update.put("$set", doc); col.update(query, update); } private BasicDBObject toDoc(UsuarioDTO user) { return new BasicDBObject() .append("name", user.getName()).append("contrasena", user.getContrasena()) .append("email", user.getEmail()).append("facebookId", user.getFacebookId()); } }