source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/service/ImageService.java @ 8829

Last change on this file since 8829 was 8829, checked in by mlg, 13 years ago

Changes ImagesManagement access from static to object.

File size: 6.1 KB
Line 
1package fr.mael.jiwigo.service;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.List;
6
7import fr.mael.jiwigo.dao.ImageDao;
8import fr.mael.jiwigo.om.Image;
9import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
10import fr.mael.jiwigo.transverse.util.ImagesUtil;
11import fr.mael.jiwigo.transverse.util.Messages;
12import fr.mael.jiwigo.transverse.util.Outil;
13import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
14import fr.mael.jiwigo.ui.mainframe.MainFrame;
15
16/**
17 *
18   Copyright (c) 2010, Mael
19   All rights reserved.
20
21   Redistribution and use in source and binary forms, with or without
22   modification, are permitted provided that the following conditions are met:
23    * Redistributions of source code must retain the above copyright
24      notice, this list of conditions and the following disclaimer.
25    * Redistributions in binary form must reproduce the above copyright
26      notice, this list of conditions and the following disclaimer in the
27      documentation and/or other materials provided with the distribution.
28    * Neither the name of jiwigo nor the
29      names of its contributors may be used to endorse or promote products
30      derived from this software without specific prior written permission.
31
32   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
36   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   
43 * @author mael
44 *
45 */
46public class ImageService {
47
48    /**
49     * Logger
50     */
51    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
52            .getLog(ImageService.class);
53
54    /**
55     * Singleton
56     */
57    private static ImageService instance;
58
59    /**
60     * @return the singleton
61     */
62    public static ImageService getInstance() {
63        if (instance == null) {
64            instance = new ImageService();
65        }
66        return instance;
67    }
68
69    /**
70     * private constructor to use a singleton
71     */
72    private ImageService() {
73
74    }
75
76    /**
77     * Lists all images for a category
78     * @param categoryId the id of the category
79     * @param rafraichir true : refresh the list of images
80     * @return the list of images
81     * @throws IOException
82     */
83    public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException {
84        return ImageDao.getInstance().listerParCategory(categoryId, rafraichir);
85    }
86
87    /**
88     * Method called to send an image to the server.
89     * @param filePath
90     * @param idCategory
91     * @return
92     * @throws Exception
93     */
94    public boolean creer(String filePath, Integer idCategory) throws Exception {
95        File originalFile = new File(filePath);
96        MainFrame.getInstance().setMessage(Messages.getMessage("mainFrame_resizing") + " " + originalFile.getName());
97        //get the byte array of the original file, to keep metadata
98        byte[] bytesFichierOriginal = Outil.getBytesFromFile(originalFile);
99
100        //size taken from the user's preferences
101        int widthOriginale = Integer
102                .valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
103        int heightOriginale = Integer.valueOf(PreferencesManagement
104                .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
105        //resize the picture (or not)
106        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", widthOriginale, heightOriginale);
107        //create the thumbnail
108        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
109        //get the thumbnail
110        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
111        File originale = null;
112        if (originaleRedimensionnee) {
113            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
114            //if the original file has been resized, we put the metadata in the resized file
115            //I use here a try catch because if the original file isn't a jpeg
116            //the methode Outil.enrich will fail, but the procedure has to continue
117            MainFrame.getInstance().setMessage(
118                    Messages.getMessage("mainFrame_addMetadata") + " " + originalFile.getName());
119            try {
120                byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal, Outil.getBytesFromFile(new File(System
121                        .getProperty("java.io.tmpdir")
122                        + "/originale.jpg")));
123                Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
124            } catch (Exception e) {
125            }
126        } else {
127            originale = originalFile;
128
129        }
130        Image image = new Image();
131        image.setName(getImageName(filePath));
132        image.setThumbnail(thumbnail);
133        image.setOriginale(originale);
134        image.setIdCategory(idCategory);
135        MainFrame.getInstance()
136                .setMessage(Messages.getMessage("mainFrame_sendingFiles") + " " + originalFile.getName());
137        //now we call the dao to send the image to the webservice
138        return ImageDao.getInstance().creer(image);
139    }
140
141    /**
142     * Add tags to an existing image
143     * @param image the image
144     * @param tagId the ids of the tags
145     * @return true if successful
146     * @throws IOException
147     */
148    public boolean addTags(Image image, String tagId) throws IOException {
149        return ImageDao.getInstance().addTags(image.getIdentifiant(), tagId);
150    }
151
152    /**
153     * Search images from a string
154     * @param queryString the string
155     * @return images matching the string
156     * @throws IOException
157     */
158    public List<Image> search(String queryString) throws IOException {
159        return ImageDao.getInstance().search(queryString);
160    }
161
162    /**
163     * Deletes the file extension
164     * @param path
165     * @return
166     */
167    private String getImageName(String path) {
168        File fichier = new File(path);
169        StringBuffer name = new StringBuffer(fichier.getName());
170        return (name.delete(name.lastIndexOf("."), name.length())).toString();
171
172    }
173
174}
Note: See TracBrowser for help on using the repository browser.