001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.ImageTypeException;
018    import com.liferay.portal.NoSuchImageException;
019    import com.liferay.portal.image.HookFactory;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.image.Hook;
023    import com.liferay.portal.kernel.image.ImageBag;
024    import com.liferay.portal.kernel.image.ImageProcessorUtil;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.util.FileUtil;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.model.Image;
030    import com.liferay.portal.model.impl.ImageImpl;
031    import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
032    import com.liferay.portal.util.PropsUtil;
033    import com.liferay.portal.webserver.WebServerServletTokenUtil;
034    
035    import java.awt.image.RenderedImage;
036    
037    import java.io.File;
038    import java.io.FileInputStream;
039    import java.io.IOException;
040    import java.io.InputStream;
041    
042    import java.util.Arrays;
043    import java.util.Date;
044    import java.util.List;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Julio Camarero
049     */
050    public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
051    
052            @Override
053            public void afterPropertiesSet() {
054                    super.afterPropertiesSet();
055    
056                    ClassLoader classLoader = getClass().getClassLoader();
057    
058                    try {
059                            InputStream is = classLoader.getResourceAsStream(
060                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
061    
062                            if (is == null) {
063                                    _log.error("Default spacer is not available");
064                            }
065    
066                            _defaultSpacer = getImage(is);
067                    }
068                    catch (Exception ioe) {
069                            _log.error(
070                                    "Unable to configure the default spacer: " + ioe.getMessage());
071                    }
072    
073                    try {
074                            InputStream is = classLoader.getResourceAsStream(
075                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
076    
077                            if (is == null) {
078                                    _log.error("Default company logo is not available");
079                            }
080    
081                            _defaultCompanyLogo = getImage(is);
082                    }
083                    catch (Exception ioe) {
084                            _log.error(
085                                    "Unable to configure the default company logo: " +
086                                            ioe.getMessage());
087                    }
088    
089                    try {
090                            InputStream is = classLoader.getResourceAsStream(
091                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
092    
093                            if (is == null) {
094                                    _log.error("Default organization logo is not available");
095                            }
096    
097                            _defaultOrganizationLogo = getImage(is);
098                    }
099                    catch (Exception 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 (Exception 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 (Exception ioe) {
132                            _log.error(
133                                    "Unable to configure the default user male portrait: " +
134                                            ioe.getMessage());
135                    }
136            }
137    
138            @Override
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(
147                                    DatabaseHook.class.getName()) &&
148                            (imagePersistence.getListeners().length == 0)) {
149    
150                            runSQL("delete from Image where imageId = " + imageId);
151    
152                            imagePersistence.clearCache();
153                    }
154                    else {*/
155                            try {
156                                    Image image = getImage(imageId);
157    
158                                    imagePersistence.remove(imageId);
159    
160                                    Hook hook = HookFactory.getInstance();
161    
162                                    hook.deleteImage(image);
163                            }
164                            catch (NoSuchImageException nsie) {
165                            }
166                    //}
167            }
168    
169            public Image getCompanyLogo(long imageId) {
170                    Image image = getImage(imageId);
171    
172                    if (image == null) {
173                            image = getDefaultCompanyLogo();
174                    }
175    
176                    return image;
177            }
178    
179            public Image getDefaultCompanyLogo() {
180                    return _defaultCompanyLogo;
181            }
182    
183            public Image getDefaultOrganizationLogo() {
184                    return _defaultOrganizationLogo;
185            }
186    
187            public Image getDefaultSpacer() {
188                    return _defaultSpacer;
189            }
190    
191            public Image getDefaultUserFemalePortrait() {
192                    return _defaultUserFemalePortrait;
193            }
194    
195            public Image getDefaultUserMalePortrait() {
196                    return _defaultUserMalePortrait;
197            }
198    
199            @Override
200            public Image getImage(long imageId) {
201                    try {
202                            if (imageId > 0) {
203                                    return imagePersistence.findByPrimaryKey(imageId);
204                            }
205                    }
206                    catch (Exception e) {
207                            if (_log.isWarnEnabled()) {
208                                    _log.warn(
209                                            "Unable to get image " + imageId + ": " + e.getMessage());
210                            }
211                    }
212    
213                    return null;
214            }
215    
216            public Image getImage(byte[] bytes)
217                    throws PortalException, SystemException {
218    
219                    return getImage(null, bytes);
220            }
221    
222            public Image getImage(File file) throws PortalException, SystemException {
223                    try {
224                            return getImage(new FileInputStream(file));
225                    }
226                    catch (IOException ioe) {
227                            throw new SystemException(ioe);
228                    }
229            }
230    
231            public Image getImage(InputStream is)
232                    throws PortalException, SystemException {
233    
234                    return getImage(is, null);
235            }
236    
237            public Image getImage(InputStream is, boolean cleanUpStream)
238                    throws PortalException, SystemException {
239    
240                    return getImage(is, null, cleanUpStream);
241            }
242    
243            public Image getImageOrDefault(long imageId) {
244                    Image image = getImage(imageId);
245    
246                    if (image == null) {
247                            image = getDefaultSpacer();
248                    }
249    
250                    return image;
251            }
252    
253            public List<Image> getImages() throws SystemException {
254                    return imagePersistence.findAll();
255            }
256    
257            @Override
258            public List<Image> getImages(int start, int end) throws SystemException {
259                    return imagePersistence.findAll(start, end);
260            }
261    
262            public List<Image> getImagesBySize(int size) throws SystemException {
263                    return imagePersistence.findByLtSize(size);
264            }
265    
266            public boolean isNullOrDefaultSpacer(byte[] bytes) {
267                    if ((bytes == null) || (bytes.length == 0) ||
268                            (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
269    
270                            return true;
271                    }
272                    else {
273                            return false;
274                    }
275            }
276    
277            public Image updateImage(long imageId, byte[] bytes)
278                    throws PortalException, SystemException {
279    
280                    Image image = getImage(bytes);
281    
282                    return updateImage(
283                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
284                            image.getWidth(), image.getSize());
285            }
286    
287            public Image updateImage(long imageId, File file)
288                    throws PortalException, SystemException {
289    
290                    Image image = getImage(file);
291    
292                    return updateImage(
293                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
294                            image.getWidth(), image.getSize());
295            }
296    
297            public Image updateImage(long imageId, InputStream is)
298                    throws PortalException, SystemException {
299    
300                    Image image = getImage(is);
301    
302                    return updateImage(
303                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
304                            image.getWidth(), image.getSize());
305            }
306    
307            public Image updateImage(
308                            long imageId, InputStream is, boolean cleanUpStream)
309                    throws PortalException, SystemException {
310    
311                    Image image = getImage(is, cleanUpStream);
312    
313                    return updateImage(
314                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
315                            image.getWidth(), image.getSize());
316            }
317    
318            public Image updateImage(
319                            long imageId, byte[] bytes, String type, int height, int width,
320                            int size)
321                    throws PortalException, SystemException {
322    
323                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
324    
325                    if (image == null) {
326                            image = imagePersistence.create(imageId);
327                    }
328    
329                    image.setModifiedDate(new Date());
330                    image.setType(type);
331                    image.setHeight(height);
332                    image.setWidth(width);
333                    image.setSize(size);
334    
335                    Hook hook = HookFactory.getInstance();
336    
337                    hook.updateImage(image, type, bytes);
338    
339                    imagePersistence.update(image, false);
340    
341                    WebServerServletTokenUtil.resetToken(imageId);
342    
343                    return image;
344            }
345    
346            protected Image getImage(InputStream is, byte[] bytes)
347                    throws PortalException, SystemException {
348    
349                    return getImage(is, bytes, true);
350            }
351    
352            protected Image getImage(
353                            InputStream is, byte[] bytes, boolean cleanUpStream)
354                    throws PortalException, SystemException {
355    
356                    try {
357                            if (is != null) {
358                                    bytes = FileUtil.getBytes(is, -1, cleanUpStream);
359                            }
360    
361                            if (bytes == null) {
362                                    return null;
363                            }
364    
365                            ImageBag imageBag = ImageProcessorUtil.read(bytes);
366    
367                            RenderedImage renderedImage = imageBag.getRenderedImage();
368                            String type = imageBag.getType();
369    
370                            if (renderedImage == null) {
371                                    throw new ImageTypeException();
372                            }
373    
374                            int height = renderedImage.getHeight();
375                            int width = renderedImage.getWidth();
376                            int size = bytes.length;
377    
378                            Image image = new ImageImpl();
379    
380                            image.setTextObj(bytes);
381                            image.setType(type);
382                            image.setHeight(height);
383                            image.setWidth(width);
384                            image.setSize(size);
385    
386                            return image;
387                    }
388                    catch (IOException ioe) {
389                            throw new SystemException(ioe);
390                    }
391            }
392    
393            private static Log _log = LogFactoryUtil.getLog(
394                    ImageLocalServiceImpl.class);
395    
396            private Image _defaultSpacer;
397            private Image _defaultCompanyLogo;
398            private Image _defaultOrganizationLogo;
399            private Image _defaultUserFemalePortrait;
400            private Image _defaultUserMalePortrait;
401    
402    }