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