All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.defne.samples.hotel.services.HotelService Maven / Gradle / Ivy

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.defne.samples.hotel.services;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.defne.jpa.EntityManagerUtil;
import org.defne.samples.hotel.models.Hotel;
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.exception.DefneException;


@Service(name="HotelService")
public class HotelService {

    @Operation(name="addHotel")
    public static Message addHotel(Message inBag) throws DefneException
    {
        String hotelName = inBag.getMessageParameter(String.class, "HOTEL_NAME");
        EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
        
        Hotel hotel = new Hotel();
        hotel.setName(hotelName);
        
        entityManager.persist(hotel);
        
        return inBag;
    }
    
    @Operation(name="getHotels")
    @SuppressWarnings("unchecked")
    public static Message getHotels(Message inBag) throws DefneException
    {
        Message oBag = MessageFactory.newMessage(inBag.getServiceName(), inBag.getServiceMethodName());
        EntityManager entityManager = EntityManagerUtil.getEntityManagerFromBag(inBag);
        Query query = entityManager.createQuery("select c from Hotel c");
        
        List hotels = (List)query.getResultList();
        if(hotels != null)
        {
            oBag.putMessageParameter("HOTEL_LIST", hotels);
        }
        
        return oBag;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy