
org.sample.reservation.service.UserService 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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
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;
import org.sample.reservation.util.ReservationModel;
@Service
@SuppressWarnings("unchecked")
public class UserService
{
private static final ILogger logger = LoggerProvider.getLogProvider(RegisterService.class);
@Operation
public static Message getUser(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
int id = inBag.getMessageParameter(Integer.class, "ID");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
User user = entityManager.find(User.class, id);
oBag.putMessageParameter("USER", user);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message updateUserInfo(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
int userId = inBag.getMessageParameter(Integer.class, "ID");
String userName = inBag.getMessageParameter(String.class, "USER_NAME");
String password = inBag.getMessageParameter(String.class, "PASSWORD");
String name = inBag.getMessageParameter(String.class, "NAME");
String surname = inBag.getMessageParameter(String.class, "SURNAME");
int age = inBag.getMessageParameter(Integer.class, "AGE");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
logger.info("Updating user with id : " + userId);
User user = entityManager.find(User.class, userId);
user.setName(name);
user.setSurname(surname);
user.setAge(age);
user.setUserName(userName);
user.setPassword(password);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message addReservation(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
int userId = inBag.getMessageParameter(Integer.class, "ID");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
User user = entityManager.find(User.class, userId);
Map reservations = inBag.getMessageParameter(Map.class, "RESERVATIONS");
Set keys = reservations.keySet();
for(String item : keys)
{
ReservationModel rm = reservations.get(item);
Hotel hotel = entityManager.find(Hotel.class, rm.getItem());
Reservation reservation = new Reservation();
user.addHotel(reservation);
reservation.setHotel(hotel);
reservation.setReservationDate(rm.getDate());
entityManager.persist(reservation);
}
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message deleteReservation(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
int reservsitonId = inBag.getMessageParameter(Integer.class, "ID");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
Reservation res = entityManager.find(Reservation.class, reservsitonId);
User user = res.getUser();
user.getReservations().remove(res);
entityManager.remove(res);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
@Operation
public static Message getReservations(Message inBag) throws DefneException
{
Message oBag = MessageFactory.newServiceBag(inBag.isReturnsOutMessage());
try
{
int id = inBag.getMessageParameter(Integer.class, "ID");
EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
User user = entityManager.find(User.class, id);
Set res = user.getReservations();
List l = new ArrayList();
if(res != null)
{
for(Reservation r : res)
{
l.add(r);
}
}
oBag.putMessageParameter("RESERVATIONS", l);
}
catch (Exception e)
{
Utility.throwsDefneException(e);
}
return oBag;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy