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