/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ee.tlu.htk.dippler.managers; import ee.tlu.htk.dippler.backoffice.StatusCodes; import ee.tlu.htk.dippler.entities.Image; import ee.tlu.htk.dippler.entities.User; import java.io.StringReader; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.EJB; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.NoResultException; import javax.persistence.NonUniqueResultException; import javax.persistence.PersistenceContext; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import sun.misc.BASE64Encoder; /** * * @author metz */ @Stateless public class ImageManager implements ImageManagerLocal { @PersistenceContext private EntityManager em; @EJB private UserManagerLocal userManager;; public static final Integer LOAD = 1; public static final Integer UPLOAD = 95; private static Unmarshaller unmarshaller = null; private static BASE64Encoder base64Enc = null; enum CourseActions { LOAD, UPLOAD } public ImageManager() { try { final JAXBContext context = JAXBContext.newInstance(Image.class); unmarshaller = context.createUnmarshaller(); } catch (JAXBException ex) { Logger.getLogger(ImageManager.class.getName()).log(Level.SEVERE, null, ex); } base64Enc = new BASE64Encoder(); } @Override public String manageImage(String action, String data, User user) { switch (CourseActions.valueOf(action)) { case LOAD: if (userManager.hasPermission(user, LOAD)) { return this.load(data); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load image"); } case UPLOAD: if (userManager.hasPermission(user, UPLOAD)) { return this.upload(data, user); } else { return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for upload image"); } default: return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, ""); } } public String load(String data) { Image image = findByData(data); if ( image != null ) { return StatusCodes.respondWithData(StatusCodes.SUCCESS, "", marshal(image)); } return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not loaded"); } public String upload(String data, User user) { return StatusCodes.respond(StatusCodes.COURSE_ERROR, "Not upload"); } public Image findById(Long id) throws NoResultException, NonUniqueResultException { if ( id > 0 ) { return em.find(Image.class, id); } return null; } public Image findByData(String data) { Image fakeImage = unMarshal(data); if ( fakeImage != null ) { return findById(fakeImage.getId()); } return null; } private static String toBase64(byte[] bs) { return base64Enc.encode(bs); } public static Image unMarshal(String data) { if ( unmarshaller != null ) { try { final Image courseUNM = (Image) unmarshaller.unmarshal(new StringReader(data)); return courseUNM; } catch(JAXBException e) { //Something went wrong } } return null; } public static String marshal(Image image) { StringBuilder xml = new StringBuilder(""); xml.append("").append(image.getId()).append(""); xml.append("").append(image.getName()).append(""); xml.append(""); xml.append("").append(image.getFiletype()).append(""); xml.append("").append(image.getFilesize()).append(""); xml.append(""); return xml.toString(); } }