
org.sample.reservation.service.AdminService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of business Show documentation
Show all versions of business Show documentation
Sample application business layer
The newest version!
/*
* @Copyright 2010, MechSoft
* MechSoft, Mechanical and Software Solutions
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.sample.reservation.service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.commons.validator.GenericValidator;
import org.defne.jpa.EntityManagerUtil;
import org.defne.service.Message;
import org.defne.service.MessageFactory;
import org.defne.service.annotation.Operation;
import org.defne.service.annotation.Service;
import org.defne.utility.Utility;
import org.defne.utility.exception.DefneException;
import org.defne.utility.log.ILogger;
import org.defne.utility.log.LoggerProvider;
import org.sample.reservation.entity.Hotel;
import org.sample.reservation.entity.Reservation;
import org.sample.reservation.entity.User;
@Service
@SuppressWarnings("unchecked")
public class AdminService
{
private static final ILogger logger = LoggerProvider.getLogProvider(AdminService.class);
@Operation
public static Message createNewHotel(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
String name = inBag.getMessageParameter(String.class, "NAME");
int star = inBag.getMessageParameter(Integer.class, "STAR");
String city = inBag.getMessageParameter(String.class, "CITY");
String country = inBag.getMessageParameter(String.class, "COUNTRY");
if(GenericValidator.isBlankOrNull(name) ||
GenericValidator.isBlankOrNull(city) ||
GenericValidator.isBlankOrNull(country))
{
logger.info("Some of the parameters are missing to define hotel.");
}
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
Hotel hotel = new Hotel();
hotel.setCity(city);
hotel.setCountry(country);
hotel.setName(name);
hotel.setStar(star);
entityManager.persist(hotel);
}catch(Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getHotels(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
Query query = entityManager.createQuery("select h from Hotel h");
List hotels = query.getResultList();
oBag.putMessageParameter("HOTELS", hotels);
}catch(Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getUsers(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
Query query = entityManager.createQuery("select u from User u");
List users = query.getResultList();
oBag.putMessageParameter("USERS", users);
}catch(Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getHotelWithId(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
int id = inBag.getMessageParameter(Integer.class, "ID");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
Hotel hotel = entityManager.find(Hotel.class, id);
oBag.putMessageParameter("HOTEL", hotel);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getReservationsWithHotel(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
int hotelId = inBag.getMessageParameter(Integer.class , "ID");
Query query = entityManager.createQuery("select u from User u join fetch u.reservations r where r.id=:id");
query.setParameter("id", hotelId);
List users = query.getResultList();
oBag.putMessageParameter("RESERVATIONS", users);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getReservationsWithUser(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newMessage();
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
int userId = inBag.getMessageParameter(Integer.class, "ID");
Query query = entityManager.createQuery("select u from User u where u.id=:id");
query.setParameter("id", userId);
User user = (User)query.getSingleResult();
Set hotels = new HashSet();
Set reservations = user.getReservations();
for(Reservation reserve : reservations)
{
hotels.add(reserve.getHotel());
}
oBag.putMessageParameter("RESERVATIONS", hotels);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message updateHotel(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
String name = inBag.getMessageParameter(String.class, "NAME");
int star = inBag.getMessageParameter(Integer.class, "STAR");
String city = inBag.getMessageParameter(String.class, "CITY");
String country = inBag.getMessageParameter(String.class, "COUNTRY");
int id = inBag.getMessageParameter(Integer.class, "ID");
Hotel hotel = entityManager.find(Hotel.class, id);
hotel.setName(name);
hotel.setStar(star);
hotel.setCountry(country);
hotel.setCity(city);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message deleteHotel(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
int id = inBag.getMessageParameter(Integer.class, "ID");
Hotel hotel = entityManager.find(Hotel.class, id);
entityManager.remove(hotel);
Query query = entityManager.createQuery("select r from Reservation r where r.hotel.id=:id");
query.setParameter("id",hotel.getId());
List res = query.getResultList();
for(Reservation r : res)
{
entityManager.remove(r);
}
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy