001    /**
002     * Copyright (c) 2000-2012 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.ImageToolUtil;
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 = 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            public Image getImage(byte[] bytes)
200                    throws PortalException, SystemException {
201    
202                    return getImage(null, bytes);
203            }
204    
205            public Image getImage(File file) throws PortalException, SystemException {
206                    try {
207                            return getImage(new FileInputStream(file));
208                    }
209                    catch (IOException ioe) {
210                            throw new SystemException(ioe);
211                    }
212            }
213    
214            public Image getImage(InputStream is)
215                    throws PortalException, SystemException {
216    
217                    return getImage(is, null);
218            }
219    
220            public Image getImage(InputStream is, boolean cleanUpStream)
221                    throws PortalException, SystemException {
222    
223                    return getImage(is, null, cleanUpStream);
224            }
225    
226            @Override
227            public Image getImage(long imageId) {
228                    try {
229                            if (imageId > 0) {
230                                    return imagePersistence.findByPrimaryKey(imageId);
231                            }
232                    }
233                    catch (Exception e) {
234                            if (_log.isWarnEnabled()) {
235                                    _log.warn(
236                                            "Unable to get image " + imageId + ": " + e.getMessage());
237                            }
238                    }
239    
240                    return null;
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(
288                            long imageId, byte[] bytes, String type, int height, int width,
289                            int size)
290                    throws PortalException, SystemException {
291    
292                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
293    
294                    if (image == null) {
295                            image = imagePersistence.create(imageId);
296                    }
297    
298                    image.setModifiedDate(new Date());
299                    image.setType(type);
300                    image.setHeight(height);
301                    image.setWidth(width);
302                    image.setSize(size);
303    
304                    Hook hook = HookFactory.getInstance();
305    
306                    hook.updateImage(image, type, bytes);
307    
308                    imagePersistence.update(image, false);
309    
310                    WebServerServletTokenUtil.resetToken(imageId);
311    
312                    return image;
313            }
314    
315            public Image updateImage(long imageId, File file)
316                    throws PortalException, SystemException {
317    
318                    Image image = getImage(file);
319    
320                    return updateImage(
321                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
322                            image.getWidth(), image.getSize());
323            }
324    
325            public Image updateImage(long imageId, InputStream is)
326                    throws PortalException, SystemException {
327    
328                    Image image = getImage(is);
329    
330                    return updateImage(
331                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
332                            image.getWidth(), image.getSize());
333            }
334    
335            public Image updateImage(
336                            long imageId, InputStream is, boolean cleanUpStream)
337                    throws PortalException, SystemException {
338    
339                    Image image = getImage(is, cleanUpStream);
340    
341                    return updateImage(
342                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
343                            image.getWidth(), image.getSize());
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 = ImageToolUtil.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 _defaultCompanyLogo;
397            private Image _defaultOrganizationLogo;
398            private Image _defaultSpacer;
399            private Image _defaultUserFemalePortrait;
400            private Image _defaultUserMalePortrait;
401    
402    }