com.zappy.purefit6.service.DayFacadeREST.java Source code

Java tutorial

Introduction

Here is the source code for com.zappy.purefit6.service.DayFacadeREST.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zappy.purefit6.service;

import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
import com.zappy.purefit6.model.Day;
import com.zappy.purefit6.model.DayMeal;
import com.zappy.purefit6.model.Week;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author gianksp
 */
@Stateless
@Path("day")
public class DayFacadeREST extends AbstractFacade<Day> {

    //Log
    private static Logger log = Logger.getLogger(WeekFacadeREST.class.getName());

    //Entity Manager
    @PersistenceContext(unitName = "com.zappy_Purefit6_war_1.0PU")
    private EntityManager em;

    /**
     * Constructor.
     */
    public DayFacadeREST() {
        super(Day.class);
    }

    /**
     * Creates new week program.
     * @param uploadedInputStream   Image file      FORM
     * @param fileDetail            Image details   FORM
     * @param name                  Name            FORM
     */
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void create(@FormDataParam("image") InputStream uploadedInputStream,
            @FormDataParam("image") FormDataContentDisposition fileDetail, @FormDataParam("index") int index,
            @FormDataParam("calories") int calories, @FormDataParam("workoutTime") int workoutTime,
            @FormDataParam("idWeek") int idWeek) {

        try {
            //Copy file
            File destination = new File(
                    FilenameUtils.concat("/home/gianksp/Desktop/test/day", fileDetail.getFileName()));
            FileUtils.copyInputStreamToFile(uploadedInputStream, destination);
            //Create element
            Day day = new Day();
            day.setIndexPos(index);
            day.setCalories(calories);
            day.setWorkoutTime(workoutTime);
            day.setImagePath(destination.getAbsolutePath());
            //Store element
            super.create(day);
        } catch (IOException ex) {
            log.log(Level.SEVERE, "Unable to import program day image " + " .image:" + fileDetail.getName()
                    + " .index:" + index + " .week:" + idWeek, ex);
        } catch (Exception ex) {
            log.log(Level.SEVERE, "Unable to import program day image " + " .index:" + index + " .week:" + idWeek,
                    ex);
        }
    }

    @PUT
    @Path("{id}")
    @Consumes({ "application/xml", "application/json" })
    public void edit(@PathParam("id") Integer id, Day entity) {
        super.edit(entity);
    }

    /**
     * Remove day program given its id.
     * @param id 
     */
    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    /**
     * Get day program by id.
     * @param id
     * @return 
     */
    @GET
    @Path("{id}")
    @Produces({ "application/xml", "application/json" })
    public Day find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    /**
     * Get all day programs.
     * @return 
     */
    @GET
    @Override
    @Produces({ "application/xml", "application/json" })
    public List<Day> findAll() {
        return super.findAll();
    }

    /**
     * Get rank day programs.
     * @param from
     * @param to
     * @return 
     */
    @GET
    @Path("{from}/{to}")
    @Produces({ "application/xml", "application/json" })
    public List<Day> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[] { from, to });
    }

    /**
     * Get total amount of day programs.
     * @return 
     */
    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    /**
    * Get week program by its id.
    * @param id
    * @return 
    */
    @GET
    @Path("{id}/meals")
    @Produces({ "application/json", "application/xml" })
    public Collection<DayMeal> findMealsForDay(@PathParam("id") Integer id) {
        Day day = super.find(id);
        return day.getDayMealCollection();
    }

    /**
     * Get entity manager.
     * @return 
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}