Java tutorial
/* * 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.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("week") public class WeekFacadeREST extends AbstractFacade<Week> { //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 WeekFacadeREST() { super(Week.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("name") String name) { try { //Copy file File destination = new File( FilenameUtils.concat("/home/gianksp/Desktop/test/week", fileDetail.getFileName())); FileUtils.copyInputStreamToFile(uploadedInputStream, destination); //Create element Week week = new Week(); week.setName(name); week.setImagePath(destination.getAbsolutePath()); //Store element super.create(week); } catch (IOException ex) { log.log(Level.SEVERE, "Unable to import week program image " + " .name:" + name + " .fileName:" + fileDetail.getName(), ex); } catch (Exception ex) { log.log(Level.SEVERE, "Unable to create week program " + " .name:" + name, ex); } } /** * Edit week program. * @param id Id of week to edit URL * @param uploadedInputStream Image FORM * @param fileDetail Image FORM * @param name Weekly program name */ @PUT @Path("{id}") @Consumes(MediaType.MULTIPART_FORM_DATA) public void edit(@PathParam("id") Integer id, @FormDataParam("image") InputStream uploadedInputStream, @FormDataParam("image") FormDataContentDisposition fileDetail, @FormDataParam("name") String name) { try { //Copy file File destination = new File( FilenameUtils.concat("/home/gianksp/Desktop/test/week", fileDetail.getFileName())); FileUtils.copyInputStreamToFile(uploadedInputStream, destination); //Create element Week week = new Week(); week.setIdWeek(id); week.setName(name); week.setImagePath(destination.getAbsolutePath()); //Store element super.edit(week); } catch (IOException ex) { log.log(Level.SEVERE, "Unable to import week program image " + " .name:" + name + " .fileName:" + fileDetail.getName(), ex); } catch (Exception ex) { log.log(Level.SEVERE, "Unable to create week program " + " .name:" + name, ex); } } /** * Remove week program given its id. * @param id */ @DELETE @Path("{id}") public void remove(@PathParam("id") Integer id) { super.remove(super.find(id)); } /** * Get week program by its id. * @param id * @return */ @GET @Path("{id}") @Produces({ "application/json", "application/xml" }) public Week find(@PathParam("id") Integer id) { return super.find(id); } /** * Find information about all weekly programs. * @return */ @GET @Override @Produces({ "application/json", "application/xml" }) public List<Week> findAll() { return super.findAll(); } /** * Find information about weekly programs given bottom and top ids. * @param from * @param to * @return */ @GET @Path("{from}/{to}") @Produces({ "application/json", "application/xml" }) public List<Week> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) { return super.findRange(new int[] { from, to }); } /** * Get total amount of 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}/days") @Produces({ "application/json", "application/xml" }) public Collection<Day> findDaysForWeek(@PathParam("id") Integer id) { Week week = super.find(id); return week.getDayCollection(); } /** * Get Entity Manager. * @return */ @Override protected EntityManager getEntityManager() { return em; } }