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

Last change on this file since 6972 was 6972, checked in by mlg, 14 years ago

New feature : research
Add a research functionnality : it's juste a field on the bottom right of the main frame. To make a research, the user have to type text in that field and press enter. It opens a new tab. A new tab is opened for each research. The tab contains the query string.

File size: 5.7 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     * Logger
49     */
50    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
51            .getLog(ImageService.class);
52
53    private static ImageService instance;
54
55    public static ImageService getInstance() {
56        if (instance == null) {
57            instance = new ImageService();
58        }
59        return instance;
60    }
61
62    private ImageService() {
63
64    }
65
66    public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException {
67        return ImageDao.getInstance().listerParCategory(categoryId, rafraichir);
68    }
69
70    /**
71     * Method called to send an image to the server.
72     
73     * @param filePath
74     * @param idCategory
75     * @return
76     * @throws Exception
77     */
78    public boolean creer(String filePath, Integer idCategory) throws Exception {
79        MainFrame.getInstance().setReussiteMessage(Messages.getMessage("mainFrame_resizing") + " " + filePath);
80        //get the byte array of the original file, to keep metadata
81        byte[] bytesFichierOriginal = Outil.getBytesFromFile(new File(filePath));
82
83        //size taken from the user's preferences
84        int widthOriginale = Integer
85                .valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
86        int heightOriginale = Integer.valueOf(PreferencesManagement
87                .getValue(PreferencesEnum.HEIGHT_ORIGINAL.getLabel()));
88        //resize the picture (or not)
89        boolean originaleRedimensionnee = ImagesUtil.scale(filePath, "originale.jpg", widthOriginale, heightOriginale);
90        //create the thumbnail
91        ImagesUtil.scale(filePath, "thumb.jpg", 120, 90);
92        //get the thumbnail
93        File thumbnail = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg");
94        File originale = null;
95        if (originaleRedimensionnee) {
96            originale = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg");
97            //if the original file has been resized, we put the metadata in the resized file
98            //I use here a try catch because if the original file isn't a jpeg
99            //the methode Outil.enrich will fail, but the procedure has to continue
100            MainFrame.getInstance().setReussiteMessage(Messages.getMessage("mainFrame_addMetadata") + " " + filePath);
101            try {
102                byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal, Outil.getBytesFromFile(new File(System
103                        .getProperty("java.io.tmpdir")
104                        + "/originale.jpg")));
105                Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
106            } catch (Exception e) {
107            }
108        } else {
109            originale = new File(filePath);
110
111        }
112        Image image = new Image();
113        image.setName(getImageName(filePath));
114        image.setThumbnail(thumbnail);
115        image.setOriginale(originale);
116        image.setIdCategory(idCategory);
117        MainFrame.getInstance().setReussiteMessage(Messages.getMessage("mainFrame_sendingFiles") + " " + filePath);
118        //now we call the dao to send the image to the webservice
119        return ImageDao.getInstance().creer(image);
120    }
121
122    /**
123     * Add tags to an existing image
124     * @param image the image
125     * @param tagId the ids of the tags
126     * @return true if successful
127     * @throws IOException
128     */
129    public boolean addTags(Image image, String tagId) throws IOException {
130        return ImageDao.getInstance().addTags(image.getIdentifiant(), tagId);
131    }
132
133    /**
134     * Search images from a string
135     * @param queryString the string
136     * @return images matching the string
137     * @throws IOException
138     */
139    public List<Image> search(String queryString) throws IOException {
140        return ImageDao.getInstance().search(queryString);
141    }
142
143    /**
144     * Deletes the file extension
145     * @param path
146     * @return
147     */
148    private String getImageName(String path) {
149        File fichier = new File(path);
150        StringBuffer name = new StringBuffer(fichier.getName());
151        return (name.delete(name.lastIndexOf("."), name.length())).toString();
152
153    }
154
155}
Note: See TracBrowser for help on using the repository browser.