1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.ImageTypeException;
18  import com.liferay.portal.NoSuchImageException;
19  import com.liferay.portal.image.Hook;
20  import com.liferay.portal.image.HookFactory;
21  import com.liferay.portal.kernel.exception.PortalException;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.image.ImageBag;
24  import com.liferay.portal.kernel.image.ImageProcessorUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.PropsKeys;
30  import com.liferay.portal.model.Image;
31  import com.liferay.portal.model.impl.ImageImpl;
32  import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
33  import com.liferay.portal.util.PropsUtil;
34  
35  import java.awt.image.RenderedImage;
36  
37  import java.io.File;
38  import java.io.FileInputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  
42  import java.util.Arrays;
43  import java.util.Date;
44  import java.util.List;
45  
46  /**
47   * <a href="ImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Julio Camarero
51   */
52  public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
53  
54      public void afterPropertiesSet() {
55          ClassLoader classLoader = getClass().getClassLoader();
56  
57          try {
58              InputStream is = classLoader.getResourceAsStream(
59                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
60  
61              if (is == null) {
62                  _log.error("Default spacer is not available");
63              }
64  
65              _defaultSpacer = getImage(is);
66          }
67          catch (Exception ioe) {
68              _log.error(
69                  "Unable to configure the default spacer: " + ioe.getMessage());
70          }
71  
72          try {
73              InputStream is = classLoader.getResourceAsStream(
74                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
75  
76              if (is == null) {
77                  _log.error("Default company logo is not available");
78              }
79  
80              _defaultCompanyLogo = getImage(is);
81          }
82          catch (Exception ioe) {
83              _log.error(
84                  "Unable to configure the default company logo: " +
85                      ioe.getMessage());
86          }
87  
88          try {
89              InputStream is = classLoader.getResourceAsStream(
90                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
91  
92              if (is == null) {
93                  _log.error("Default organization logo is not available");
94              }
95  
96              _defaultOrganizationLogo = getImage(is);
97          }
98          catch (Exception ioe) {
99              _log.error(
100                 "Unable to configure the default organization logo: " +
101                     ioe.getMessage());
102         }
103 
104         try {
105             InputStream is = classLoader.getResourceAsStream(
106                 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
107 
108             if (is == null) {
109                 _log.error("Default user female portrait is not available");
110             }
111 
112             _defaultUserFemalePortrait = getImage(is);
113         }
114         catch (Exception ioe) {
115             _log.error(
116                 "Unable to configure the default user female portrait: " +
117                     ioe.getMessage());
118         }
119 
120         try {
121             InputStream is = classLoader.getResourceAsStream(
122                 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
123 
124             if (is == null) {
125                 _log.error("Default user male portrait is not available");
126             }
127 
128             _defaultUserMalePortrait = getImage(is);
129         }
130         catch (Exception ioe) {
131             _log.error(
132                 "Unable to configure the default user male portrait: " +
133                     ioe.getMessage());
134         }
135     }
136 
137     public void deleteImage(long imageId)
138         throws PortalException, SystemException {
139 
140         try {
141             if (imageId > 0) {
142                 Image image = getImage(imageId);
143 
144                 imagePersistence.remove(imageId);
145 
146                 Hook hook = HookFactory.getInstance();
147 
148                 hook.deleteImage(image);
149             }
150         }
151         catch (NoSuchImageException nsie) {
152         }
153     }
154 
155     public Image getCompanyLogo(long imageId) {
156         Image image = getImage(imageId);
157 
158         if (image == null) {
159             image = getDefaultCompanyLogo();
160         }
161 
162         return image;
163     }
164 
165     public Image getDefaultCompanyLogo() {
166         return _defaultCompanyLogo;
167     }
168 
169     public Image getDefaultOrganizationLogo() {
170         return _defaultOrganizationLogo;
171     }
172 
173     public Image getDefaultSpacer() {
174         return _defaultSpacer;
175     }
176 
177     public Image getDefaultUserFemalePortrait() {
178         return _defaultUserFemalePortrait;
179     }
180 
181     public Image getDefaultUserMalePortrait() {
182         return _defaultUserMalePortrait;
183     }
184 
185     public Image getImage(long imageId) {
186         try {
187             if (imageId > 0) {
188                 return imagePersistence.findByPrimaryKey(imageId);
189             }
190         }
191         catch (Exception e) {
192             if (_log.isWarnEnabled()) {
193                 _log.warn(
194                     "Unable to get image " + imageId + ": " + e.getMessage());
195             }
196         }
197 
198         return null;
199     }
200 
201     public Image getImage(byte[] bytes)
202         throws PortalException, SystemException {
203 
204         return getImage(null, bytes);
205     }
206 
207     public Image getImage(File file) throws PortalException, SystemException {
208         try {
209             return getImage(new FileInputStream(file));
210         }
211         catch (IOException ioe) {
212             throw new SystemException(ioe);
213         }
214     }
215 
216     public Image getImage(InputStream is)
217         throws PortalException, SystemException {
218 
219         return getImage(is, null);
220     }
221 
222     public Image getImageOrDefault(long imageId) {
223         Image image = getImage(imageId);
224 
225         if (image == null) {
226             image = getDefaultSpacer();
227         }
228 
229         return image;
230     }
231 
232     public List<Image> getImages() throws SystemException {
233         return imagePersistence.findAll();
234     }
235 
236     public List<Image> getImages(int start, int end) throws SystemException {
237         return imagePersistence.findAll(start, end);
238     }
239 
240     public List<Image> getImagesBySize(int size) throws SystemException {
241         return imagePersistence.findBySize(size);
242     }
243 
244     public boolean isNullOrDefaultSpacer(byte[] bytes) {
245         if ((bytes == null) || (bytes.length == 0) ||
246             (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
247 
248             return true;
249         }
250         else {
251             return false;
252         }
253     }
254 
255     public Image updateImage(long imageId, byte[] bytes)
256         throws PortalException, SystemException {
257 
258         Image image = getImage(bytes);
259 
260         return updateImage(
261             imageId, image.getTextObj(), image.getType(), image.getHeight(),
262             image.getWidth(), image.getSize());
263     }
264 
265     public Image updateImage(long imageId, File file)
266         throws PortalException, SystemException {
267 
268         Image image = getImage(file);
269 
270         return updateImage(
271             imageId, image.getTextObj(), image.getType(), image.getHeight(),
272             image.getWidth(), image.getSize());
273     }
274 
275     public Image updateImage(long imageId, InputStream is)
276         throws PortalException, SystemException {
277 
278         Image image = getImage(is);
279 
280         return updateImage(
281             imageId, image.getTextObj(), image.getType(), image.getHeight(),
282             image.getWidth(), image.getSize());
283     }
284 
285     public Image updateImage(
286             long imageId, byte[] bytes, String type, int height, int width,
287             int size)
288         throws PortalException, SystemException {
289 
290         Image image = imagePersistence.fetchByPrimaryKey(imageId);
291 
292         if (image == null) {
293             image = imagePersistence.create(imageId);
294         }
295 
296         image.setModifiedDate(new Date());
297         image.setType(type);
298         image.setHeight(height);
299         image.setWidth(width);
300         image.setSize(size);
301 
302         Hook hook = HookFactory.getInstance();
303 
304         hook.updateImage(image, type, bytes);
305 
306         imagePersistence.update(image, false);
307 
308         ImageServletTokenUtil.resetToken(imageId);
309 
310         return image;
311     }
312 
313     protected Image getImage(InputStream is, byte[] bytes)
314         throws PortalException, SystemException {
315 
316         try {
317             if (is != null) {
318                 bytes = FileUtil.getBytes(is);
319             }
320 
321             ImageBag imageBag = ImageProcessorUtil.read(bytes);
322 
323             RenderedImage renderedImage = imageBag.getRenderedImage();
324             String type = imageBag.getType();
325 
326             if (renderedImage == null) {
327                 throw new ImageTypeException();
328             }
329 
330             int height = renderedImage.getHeight();
331             int width = renderedImage.getWidth();
332             int size = bytes.length;
333 
334             Image image = new ImageImpl();
335 
336             image.setTextObj(bytes);
337             image.setType(type);
338             image.setHeight(height);
339             image.setWidth(width);
340             image.setSize(size);
341 
342             return image;
343         }
344         catch (IOException ioe) {
345             throw new SystemException(ioe);
346         }
347         finally {
348             if (is != null) {
349                 try {
350                     is.close();
351                 }
352                 catch (IOException ioe) {
353                     if (_log.isWarnEnabled()) {
354                         _log.warn(ioe);
355                     }
356                 }
357             }
358         }
359     }
360 
361     private static Log _log = LogFactoryUtil.getLog(
362         ImageLocalServiceImpl.class);
363 
364     private Image _defaultSpacer;
365     private Image _defaultCompanyLogo;
366     private Image _defaultOrganizationLogo;
367     private Image _defaultUserFemalePortrait;
368     private Image _defaultUserMalePortrait;
369 
370 }