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

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

implements feature : the number of thumbnails in the thumbnails viewer is adapted to the size of the frame
not perfect but better
feature:0001838

File size: 5.9 KB
Line 
1package fr.mael.jiwigo.service;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.List;
6import fr.mael.jiwigo.dao.ImageDao;
7import fr.mael.jiwigo.om.Image;
8import fr.mael.jiwigo.transverse.enumeration.PreferencesEnum;
9import fr.mael.jiwigo.transverse.util.ImagesUtil;
10import fr.mael.jiwigo.transverse.util.Messages;
11import fr.mael.jiwigo.transverse.util.Outil;
12import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement;
13import fr.mael.jiwigo.ui.mainframe.MainFrame;
14
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.getLog(ImageService.class);
52
53        /**
54         * Singleton
55         */
56        private static ImageService instance;
57
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        /**
71         * private constructor to use a singleton
72         */
73        private ImageService() {
74
75        }
76
77
78        /**
79         * Lists all images for a category
80         * @param categoryId the id of the category
81         * @param rafraichir true : refresh the list of images
82         * @return the list of images
83         * @throws IOException
84         */
85        public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException {
86                return ImageDao.getInstance().listerParCategory(categoryId, rafraichir);
87        }
88
89
90        /**
91         * Method called to send an image to the server.
92         * @param filePath
93         * @param idCategory
94         * @return
95         * @throws Exception
96         */
97        public boolean creer(String filePath, Integer idCategory) throws Exception {
98                MainFrame.getInstance().setMessage(Messages.getMessage("mainFrame_resizing") + " " + filePath);
99                //get the byte array of the original file, to keep metadata
100                byte[] bytesFichierOriginal = Outil.getBytesFromFile(new File(filePath));
101
102                //size taken from the user's preferences
103                int widthOriginale = Integer.valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
104                int heightOriginale = Integer.valueOf(PreferencesManagement.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(Messages.getMessage("mainFrame_addMetadata") + " " + filePath);
118                        try {
119                                byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal,
120                                                Outil.getBytesFromFile(new File(System.getProperty("java.io.tmpdir") + "/originale.jpg")));
121                                Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
122                        } catch (Exception e) {
123                        }
124                } else {
125                        originale = new File(filePath);
126
127                }
128                Image image = new Image();
129                image.setName(getImageName(filePath));
130                image.setThumbnail(thumbnail);
131                image.setOriginale(originale);
132                image.setIdCategory(idCategory);
133                MainFrame.getInstance().setMessage(Messages.getMessage("mainFrame_sendingFiles") + " " + filePath);
134                //now we call the dao to send the image to the webservice
135                return ImageDao.getInstance().creer(image);
136        }
137
138
139        /**
140         * Add tags to an existing image
141         * @param image the image
142         * @param tagId the ids of the tags
143         * @return true if successful
144         * @throws IOException
145         */
146        public boolean addTags(Image image, String tagId) throws IOException {
147                return ImageDao.getInstance().addTags(image.getIdentifiant(), tagId);
148        }
149
150
151        /**
152         * Search images from a string
153         * @param queryString the string
154         * @return images matching the string
155         * @throws IOException
156         */
157        public List<Image> search(String queryString) throws IOException {
158                return ImageDao.getInstance().search(queryString);
159        }
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.