List of usage examples for javax.persistence EntityManager close
public void close();
From source file:org.noorganization.instalist.server.api.TaggedProductResource.java
/** * Get a list of tagged products.// w w w .ja va 2s . co m * @param _groupId The id of the group containing various tagged products. * @param _changedSince Limits the result to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100. Optional. */ @GET @TokenSecured @Produces({ "application/json" }) public Response getTaggedProducts(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<TaggedProduct> taggedProducts; List<DeletedObject> deletedTaggedProducts; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<TaggedProduct> taggedProductQuery = manager.createQuery( "select tp from " + "TaggedProduct tp where tp.group = :group and tp.updated > :updated", TaggedProduct.class); taggedProductQuery.setParameter("group", group); taggedProductQuery.setParameter("updated", changedSince); taggedProducts = taggedProductQuery.getResultList(); TypedQuery<DeletedObject> deletedTaggedProductQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); deletedTaggedProductQuery.setParameter("group", group); deletedTaggedProductQuery.setParameter("updated", changedSince); deletedTaggedProductQuery.setParameter("type", DeletedObject.Type.TAGGEDPRODUCT); deletedTaggedProducts = deletedTaggedProductQuery.getResultList(); } else { taggedProducts = new ArrayList<TaggedProduct>(group.getTaggedProducts()); TypedQuery<DeletedObject> deletedIngredientsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedIngredientsQuery.setParameter("group", group); deletedIngredientsQuery.setParameter("type", DeletedObject.Type.TAGGEDPRODUCT); deletedTaggedProducts = deletedIngredientsQuery.getResultList(); } manager.close(); ArrayList<TaggedProductInfo> rtn = new ArrayList<TaggedProductInfo>( taggedProducts.size() + deletedTaggedProducts.size()); for (TaggedProduct current : taggedProducts) { TaggedProductInfo toAdd = new TaggedProductInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setTagUUID(current.getTag().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : deletedTaggedProducts) { TaggedProductInfo toAdd = new TaggedProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.ListResource.java
/** * Get a list of shopping-lists./*from w ww . j a va 2s .c o m*/ * @param _groupId The id of the group, containing the lists. * @param _changedSince Requests only the elements that changed since the given date. ISO 8601 * time e.g. 2016-01-19T11:54:07+0100 */ @GET @TokenSecured @Produces({ "application/json" }) public Response getLists(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<ShoppingList> foundLists; List<DeletedObject> foundDeleted; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<ShoppingList> foundListsQuery = manager.createQuery( "select sl from " + "ShoppingList sl where sl.group = :group and sl.updated > :updated", ShoppingList.class); foundListsQuery.setParameter("group", group); foundListsQuery.setParameter("updated", changedSince); foundLists = foundListsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager .createQuery("select do from" + " DeletedObject do where do.group = :group and " + "do.updated > :updated and do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("updated", changedSince); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.LIST); foundDeleted = foundDeletedListsQuery.getResultList(); } else { TypedQuery<ShoppingList> foundListsQuery = manager .createQuery("select sl from " + "ShoppingList sl where sl.group = :group", ShoppingList.class); foundListsQuery.setParameter("group", group); foundLists = foundListsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.LIST); foundDeleted = foundDeletedListsQuery.getResultList(); } manager.close(); ArrayList<ListInfo> rtn = new ArrayList<ListInfo>(foundLists.size() + foundDeleted.size()); for (ShoppingList current : foundLists) { ListInfo toAdd = new ListInfo(); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); if (current.getCategory() != null) toAdd.setCategoryUUID(current.getCategory().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(false); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { ListInfo toAdd = new ListInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:org.noorganization.instalist.server.api.RecipeResource.java
/** * Get a single recipe./*from ww w . j a va 2s . c om*/ * @param _groupId The id of the group containing the recipe. * @param _recipeUUID The uuid of the requested recipe. */ @GET @TokenSecured @Path("{recipeuuid}") @Produces({ "application/json" }) public Response getRecipe(@PathParam("groupid") int _groupId, @PathParam("recipeuuid") String _recipeUUID) throws Exception { UUID toFind; try { toFind = UUID.fromString(_recipeUUID); } catch (IllegalArgumentException _e) { return ResponseFactory.generateBadRequest(CommonEntity.INVALID_UUID); } EntityManager manager = DatabaseHelper.getInstance().getManager(); IRecipeController recipeController = ControllerFactory.getRecipeController(manager); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); Recipe current = recipeController.findByGroupAndUUID(group, toFind); if (current == null) { if (recipeController.findDeletedByGroupAndUUID(group, toFind) == null) { manager.close(); return ResponseFactory.generateNotFound(new Error().withMessage("Recipe was not " + "found.")); } manager.close(); return ResponseFactory.generateGone(new Error().withMessage("Recipe was deleted " + "before.")); } manager.close(); RecipeInfo rtn = new RecipeInfo().withDeleted(false); rtn.setUUID(current.getUUID()); rtn.setName(current.getName()); rtn.setLastChanged(Date.from(current.getUpdated())); return ResponseFactory.generateOK(rtn); }
From source file:com.epam.training.taranovski.web.project.repository.implementation.VacancyRepositoryImplementation.java
@Override public Vacancy getById(int id) { EntityManager em = entityManagerFactory.createEntityManager(); Vacancy vacancy = null;// ww w. j a va 2s .co m try { em.getTransaction().begin(); vacancy = em.find(Vacancy.class, id); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(VacancyRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return vacancy; }
From source file:com.epam.training.taranovski.web.project.repository.implementation.EmployeeRepositoryImplementation.java
@Override public Employee getById(int id) { EntityManager em = entityManagerFactory.createEntityManager(); Employee employee = null;/*from w ww .j a va2s . c o m*/ try { em.getTransaction().begin(); employee = em.find(Employee.class, id); em.getTransaction().commit(); } catch (RuntimeException e) { Logger.getLogger(EmployeeRepositoryImplementation.class.getName()).info(e); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } return employee; }
From source file:org.noorganization.instalist.server.api.EntryResource.java
/** * Get a list of listEntries./* www.j a v a 2 s .c o m*/ * @param _groupId The id of the group containing various list-entries. * @param _changedSince Limits the result to elements that changed since the given date. ISO * 8601 time e.g. 2016-01-19T11:54:07+0100 */ @GET @TokenSecured @Produces({ "application/json" }) public Response getEntries(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<ListEntry> foundEntries; List<DeletedObject> foundDeleted; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<ListEntry> foundEntriesQuery = manager.createQuery( "select le from " + "ListEntry le where le.group = :group and le.updated > :updated", ListEntry.class); foundEntriesQuery.setParameter("group", group); foundEntriesQuery.setParameter("updated", changedSince); foundEntries = foundEntriesQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedEntriesQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and " + "do.type = :type", DeletedObject.class); foundDeletedEntriesQuery.setParameter("group", group); foundDeletedEntriesQuery.setParameter("updated", changedSince); foundDeletedEntriesQuery.setParameter("type", DeletedObject.Type.LISTENTRY); foundDeleted = foundDeletedEntriesQuery.getResultList(); } else { foundEntries = new ArrayList<ListEntry>(group.getListEntries()); TypedQuery<DeletedObject> deletedEntriesQuery = manager.createQuery( "select do from " + "DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); deletedEntriesQuery.setParameter("group", group); deletedEntriesQuery.setParameter("type", DeletedObject.Type.LISTENTRY); foundDeleted = deletedEntriesQuery.getResultList(); } manager.close(); ArrayList<EntryInfo> rtn = new ArrayList<EntryInfo>(foundEntries.size() + foundDeleted.size()); for (ListEntry current : foundEntries) { EntryInfo toAdd = new EntryInfo().withDeleted(false); toAdd.setUUID(current.getUUID()); toAdd.setProductUUID(current.getProduct().getUUID()); toAdd.setListUUID(current.getList().getUUID()); toAdd.setAmount(current.getAmount()); toAdd.setPriority(current.getPriority()); toAdd.setStruck(current.getStruck()); toAdd.setLastChanged(Date.from(current.getUpdated())); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { EntryInfo toAdd = new EntryInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:de.zib.gndms.infra.system.GNDMSystemDirectory.java
@SuppressWarnings({ "MethodWithTooExceptionsDeclared", "OverloadedMethodsWithSameNumberOfParameters" }) public TaskAction newTaskAction(final @NotNull EntityManagerFactory emf, final @NotNull String offerTypeKey) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { EntityManager em = emf.createEntityManager(); try {/*from ww w .ja v a 2s. c o m*/ return newTaskAction(em, offerTypeKey); } finally { if (!em.isOpen()) em.close(); } }
From source file:org.noorganization.instalist.server.api.ProductResource.java
/** * Get a list of products.// w ww. j a v a 2s .co m * @param _groupId The id of the group containing the products. * @param _changedSince Optional. Limits the request to the elements that changed since the * given date. ISO 8601 time e.g. "2016-01-19T11:54:07+0100". */ @GET @TokenSecured @Produces({ "application/json" }) public Response getProducts(@PathParam("groupid") int _groupId, @QueryParam("changedsince") String _changedSince) throws Exception { List<Product> foundProducts; List<DeletedObject> foundDeleted; EntityManager manager = DatabaseHelper.getInstance().getManager(); DeviceGroup group = manager.find(DeviceGroup.class, _groupId); if (_changedSince != null) { Instant changedSince; try { changedSince = ISO8601Utils.parse(_changedSince, new ParsePosition(0)).toInstant(); } catch (ParseException _e) { manager.close(); return ResponseFactory.generateBadRequest(CommonEntity.INVALID_CHANGEDATE); } TypedQuery<Product> foundProductsQuery = manager.createQuery( "select p from " + "Product p where p.group = :group and p.updated > :updated", Product.class); foundProductsQuery.setParameter("group", group); foundProductsQuery.setParameter("updated", changedSince); foundProducts = foundProductsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedListsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.updated > :updated and" + " do.type = :type", DeletedObject.class); foundDeletedListsQuery.setParameter("group", group); foundDeletedListsQuery.setParameter("updated", changedSince); foundDeletedListsQuery.setParameter("type", DeletedObject.Type.PRODUCT); foundDeleted = foundDeletedListsQuery.getResultList(); } else { TypedQuery<Product> foundProductsQuery = manager .createQuery("select p from " + "Product p where p.group = :group", Product.class); foundProductsQuery.setParameter("group", group); foundProducts = foundProductsQuery.getResultList(); TypedQuery<DeletedObject> foundDeletedProductsQuery = manager.createQuery( "select do " + "from DeletedObject do where do.group = :group and do.type = :type", DeletedObject.class); foundDeletedProductsQuery.setParameter("group", group); foundDeletedProductsQuery.setParameter("type", DeletedObject.Type.PRODUCT); foundDeleted = foundDeletedProductsQuery.getResultList(); } manager.close(); ArrayList<ProductInfo> rtn = new ArrayList<ProductInfo>(foundProducts.size() + foundDeleted.size()); for (Product current : foundProducts) { ProductInfo toAdd = new ProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setName(current.getName()); toAdd.setDefaultAmount(current.getDefaultAmount()); toAdd.setStepAmount(current.getStepAmount()); if (current.getUnit() != null) toAdd.setUnitUUID(current.getUnit().getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(false); rtn.add(toAdd); } for (DeletedObject current : foundDeleted) { ProductInfo toAdd = new ProductInfo(); toAdd.setUUID(current.getUUID()); toAdd.setLastChanged(Date.from(current.getUpdated())); toAdd.setDeleted(true); rtn.add(toAdd); } return ResponseFactory.generateOK(rtn); }
From source file:in.bookmylab.jpa.JpaDAO.java
public void saveProfile(UserProfile profile) { EntityManager em = emf.createEntityManager(); try {/*from ww w . jav a 2 s . c o m*/ em.getTransaction().begin(); if (profile.profileId == null) { em.persist(profile); } else { em.merge(profile); } em.getTransaction().commit(); } finally { em.close(); } }
From source file:in.bookmylab.jpa.JpaDAO.java
public User getUserByLogin(String email) { User u = null;//from w w w . j av a 2 s . com EntityManager em = emf.createEntityManager(); try { Query q = em.createNamedQuery("User.findByEmail"); List<User> l = q.setParameter("email", email).getResultList(); if (l.size() == 1) { u = l.get(0); } } finally { em.clear(); em.close(); } return u; }