org.croodie.resource.UserServerResource.java Source code

Java tutorial

Introduction

Here is the source code for org.croodie.resource.UserServerResource.java

Source

/**
 *
 * Copyright (c) 2014 Kerby Martino and others. All rights reserved.
 * 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.croodie.resource;

import org.croodie.entity.User;
import org.croodie.persistence.EMF;
import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.util.Date;

@Component("/user/{id}")
@Scope("prototype")
public class UserServerResource extends ServerResource implements UserResource {

    final static Logger logger = LoggerFactory.getLogger(UserServerResource.class);

    @Override
    public Representation createUser(User user) {
        EntityManagerFactory emf = EMF.get();
        EntityManager em = emf.createEntityManager();
        try {
            em.persist(user);
        } catch (Exception ex) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Internal Server Error", ex);
        } finally {
            em.close();
        }
        return new JacksonRepresentation<User>(user);
    }

    @Get
    public Representation retrieveUsers(Representation entity) {
        Form form = new Form(entity);
        String id = form.getFirstValue("id");
        logger.info("Fetching user with id: " + id);
        EntityManagerFactory emf = EMF.get();
        EntityManager em = emf.createEntityManager();
        User user = null;
        try {
            user = em.find(User.class, id);
        } finally {
            em.close();
        }
        return null;
    }

    @Override
    public Representation updateUser(User ref) {
        //        EntityManagerFactory emf = EMF.get();
        //        EntityManager em = emf.createEntityManager();
        //        try {
        //            User user = em.find(User.class, id);
        //            BeanUtils.copyProperties(user, ref);
        //            em.merge(user);
        //        } finally {
        //            em.close();
        //        }
        return new JacksonRepresentation<User>(new User("test", "test", new Date()));
    }

    @Delete
    public Representation deleteUser(Representation entity) {
        Form form = new Form(entity);
        String id = form.getFirstValue("id");
        EntityManagerFactory emf = EMF.get();
        EntityManager em = emf.createEntityManager();
        try {
            User user = em.find(User.class, id);
            em.remove(user);
        } finally {
            em.close();
        }
        return null;
    }

}