Ignore:
Timestamp:
Mar 29, 2011, 8:21:25 PM (13 years ago)
Author:
mlg
Message:

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/ImageService.java

    r9893 r9919  
    11package fr.mael.jiwigo.service;
    22
    3 import java.io.File;
    43import java.io.IOException;
    54import java.security.NoSuchAlgorithmException;
    65import java.util.List;
    76
    8 import fr.mael.jiwigo.dao.ImageDao;
    97import fr.mael.jiwigo.om.Image;
    108import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    119import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    1210import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException;
    13 import fr.mael.jiwigo.transverse.session.SessionManager;
    14 import fr.mael.jiwigo.transverse.util.ImagesUtil;
    15 import fr.mael.jiwigo.transverse.util.Tools;
    1611
    17 /*
    18  *  jiwigo-ws-api Piwigo webservice access Api
    19  *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
    20  *                All Rights Reserved
    21  *
    22  *  This library is free software. It comes without any warranty, to
    23  *  the extent permitted by applicable law. You can redistribute it
    24  *  and/or modify it under the terms of the Do What The Fuck You Want
    25  *  To Public License, Version 2, as published by Sam Hocevar. See
    26  *  http://sam.zoy.org/wtfpl/COPYING for more details.
    27  */
    28 /**
    29  *
    30 
    31  * @author mael
    32  *
    33  */
    34 public class ImageService {
    35 
    36     /**
    37      * Logger
    38      */
    39     public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
    40             .getLog(ImageService.class);
    41 
    42     /**
    43      * Singleton
    44      */
    45     private static ImageService instance;
    46 
    47     private SessionManager sessionManager;
    48 
    49     /**
    50      * @return the singleton
    51      */
    52     public static ImageService getInstance(SessionManager sessionManager) {
    53         if (instance == null) {
    54             instance = new ImageService(sessionManager);
    55         }
    56         return instance;
    57     }
    58 
    59     /**
    60      * private constructor to use a singleton
    61      */
    62     private ImageService(SessionManager sessionManager) {
    63         this.sessionManager = sessionManager;
    64     }
     12public interface ImageService {
    6513
    6614    /**
     
    7220     */
    7321    public List<Image> listByCategory(Integer categoryId, boolean rafraichir) throws IOException,
    74             ProxyAuthenticationException {
    75         return ImageDao.getInstance(sessionManager).listByCategory(categoryId, rafraichir);
    76     }
     22            ProxyAuthenticationException;
    7723
    7824    /**
     
    9238    public boolean create(String filePath, Integer idCategory, Integer originalWidth, Integer originalHeight,
    9339            Double chunckSize, Integer privacyLevel) throws IOException, NoSuchAlgorithmException,
    94             FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException {
    95         File originalFile = new File(filePath);
    96         //get the byte array of the original file, to keep metadata
    97         byte[] bytesFichierOriginal = Tools.getBytesFromFile(originalFile);
    98 
    99         //resize the picture (or not)
    100         boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", originalWidth, originalHeight);
    101         //create the thumbnail
    102         ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
    103         //get the thumbnail
    104         File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
    105         File originale = null;
    106         if (originaleRedimensionnee) {
    107             originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
    108             //if the original file has been resized, we put the metadata in the resized file
    109             //I use here a try catch because if the original file isn't a jpeg
    110             //the methode Outil.enrich will fail, but the procedure has to continue
    111             try {
    112                 byte[] fichierEnrichi = Tools.enrich(bytesFichierOriginal, Tools.getBytesFromFile(new File(System
    113                         .getProperty("java.io.tmpdir")
    114                         + "/originale.jpg")));
    115                 Tools.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
    116             } catch (Exception e) {
    117             }
    118         } else {
    119             originale = originalFile;
    120 
    121         }
    122         Image image = new Image();
    123         image.setName(getImageName(filePath));
    124         image.setThumbnail(thumbnail);
    125         image.setOriginale(originale);
    126         image.setIdCategory(idCategory);
    127         image.setPrivacyLevel(String.valueOf(privacyLevel));
    128         //now we call the dao to send the image to the webservice
    129         return ImageDao.getInstance(sessionManager).create(image, chunckSize * 1000000);
    130     }
     40            FileAlreadyExistsException, ProxyAuthenticationException, WrongChunkSizeException;
    13141
    13242    /**
     
    13848     * @throws ProxyAuthenticationException
    13949     */
    140     public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException {
    141         return ImageDao.getInstance(sessionManager).addTags(image.getIdentifier(), tagId);
    142     }
     50    public boolean addTags(Image image, String tagId) throws IOException, ProxyAuthenticationException;
    14351
    14452    /**
     
    14957     * @throws ProxyAuthenticationException
    15058     */
    151     public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException {
    152         return ImageDao.getInstance(sessionManager).search(queryString);
    153     }
    154 
    155     /**
    156      * Deletes the file extension
    157      * @param path
    158      * @return
    159      */
    160     private String getImageName(String path) {
    161         File fichier = new File(path);
    162         StringBuffer name = new StringBuffer(fichier.getName());
    163         return (name.delete(name.lastIndexOf("."), name.length())).toString();
    164 
    165     }
     59    public List<Image> search(String queryString) throws IOException, ProxyAuthenticationException;
    16660
    16761}
Note: See TracChangeset for help on using the changeset viewer.