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