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

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

Images can be sent from the file new file browser.

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