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

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

changes the informations displayed while uploading photos
bug:0001886

File size: 6.0 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                File originalFile = new File(filePath);
99                MainFrame.getInstance().setMessage(Messages.getMessage("mainFrame_resizing") + " " + originalFile.getName());
100                //get the byte array of the original file, to keep metadata
101                byte[] bytesFichierOriginal = Outil.getBytesFromFile(originalFile);
102
103                //size taken from the user's preferences
104                int widthOriginale = Integer.valueOf(PreferencesManagement.getValue(PreferencesEnum.WIDTH_ORIGINALE.getLabel()));
105                int heightOriginale = Integer.valueOf(PreferencesManagement.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(Messages.getMessage("mainFrame_addMetadata") + " " + originalFile.getName());
119                        try {
120                                byte[] fichierEnrichi = Outil.enrich(bytesFichierOriginal,
121                                                Outil.getBytesFromFile(new File(System.getProperty("java.io.tmpdir") + "/originale.jpg")));
122                                Outil.byteToFile(System.getProperty("java.io.tmpdir") + "/originale.jpg", fichierEnrichi);
123                        } catch (Exception e) {
124                        }
125                } else {
126                        originale = originalFile;
127
128                }
129                Image image = new Image();
130                image.setName(getImageName(filePath));
131                image.setThumbnail(thumbnail);
132                image.setOriginale(originale);
133                image.setIdCategory(idCategory);
134                MainFrame.getInstance().setMessage(Messages.getMessage("mainFrame_sendingFiles") + " " + originalFile.getName());
135                //now we call the dao to send the image to the webservice
136                return ImageDao.getInstance().creer(image);
137        }
138
139
140        /**
141         * Add tags to an existing image
142         * @param image the image
143         * @param tagId the ids of the tags
144         * @return true if successful
145         * @throws IOException
146         */
147        public boolean addTags(Image image, String tagId) throws IOException {
148                return ImageDao.getInstance().addTags(image.getIdentifiant(), tagId);
149        }
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        /**
164         * Deletes the file extension
165         * @param path
166         * @return
167         */
168        private String getImageName(String path) {
169                File fichier = new File(path);
170                StringBuffer name = new StringBuffer(fichier.getName());
171                return (name.delete(name.lastIndexOf("."), name.length())).toString();
172
173        }
174
175}
Note: See TracBrowser for help on using the repository browser.