@Override public Boolean demandeOwner(String restaurantId, Integer OwnerId) { Restaurant restaurant = factoryServiceLocal.getRestaurantEjb().findById(restaurantId, Restaurant.class); Owner owner = factoryServiceLocal.getOwnerEjb().findById(OwnerId, Owner.class); if (restaurant == null || owner == null || restaurant instanceof Complaint) { return false; } String jpql = "select e from ValidateOwnerRestaurant e where e.restaurant.id =:param"; Query query = entityManager.createQuery(jpql); query.setParameter("param", restaurantId); ValidateOwnerRestaurant ownerRestaurant = null; try { ownerRestaurant = (ValidateOwnerRestaurant) query.getSingleResult(); } catch (Exception ee) { } String code = ""; if (ownerRestaurant == null) { code = SendMail.generateCode(); factoryServiceLocal .getValidateOwnerRestaurantEjb() .add(new ValidateOwnerRestaurant(code, new Date(), null, restaurant, owner)); } else code = ownerRestaurant.getCode(); SendMail.sendMail(restaurant.getEmail(), code); return true; }
public Boolean verifAndAssignRestaurant(String code, Integer IdOwner) { Owner owner = factoryServiceLocal.getOwnerEjb().findById(IdOwner, Owner.class); String jpql = "select e from ValidateOwnerRestaurant e where e.code =:code and e.owner.id=:owner"; Query sql = entityManager.createQuery(jpql); sql.setParameter("code", code); sql.setParameter("owner", IdOwner); ValidateOwnerRestaurant ownerRestaurant = (ValidateOwnerRestaurant) sql.getSingleResult(); if (ownerRestaurant == null) return false; else { System.out.println(ownerRestaurant); Restaurant restaurant = factoryServiceLocal .getRestaurantEjb() .findById(ownerRestaurant.getRestaurant().getId(), Restaurant.class); factoryServiceLocal.getValidateOwnerRestaurantEjb().delete(ownerRestaurant); factoryServiceLocal.getRestaurantEjb().delete(restaurant); restaurant = new Complaint( restaurant.getId(), restaurant.getName(), restaurant.getAddress(), restaurant.getEmail(), restaurant.getCategory(), null, null); restaurant.setOwner(owner); factoryServiceLocal.getRestaurantEjb().add(restaurant); ownerRestaurant.getRestaurant().setOwner(owner); return true; } }