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