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     * @author Shuyang Zhou
050     */
051    public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
052    
053            @Override
054            public void afterPropertiesSet() {
055                    super.afterPropertiesSet();
056    
057                    ClassLoader classLoader = getClassLoader();
058    
059                    try {
060                            InputStream is = classLoader.getResourceAsStream(
061                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
062    
063                            if (is == null) {
064                                    _log.error("Default spacer is not available");
065                            }
066    
067                            _defaultSpacer = getImage(is);
068                    }
069                    catch (Exception e) {
070                            _log.error(
071                                    "Unable to configure the default spacer: " + e.getMessage());
072                    }
073    
074                    try {
075                            InputStream is = classLoader.getResourceAsStream(
076                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
077    
078                            if (is == null) {
079                                    _log.error("Default company logo is not available");
080                            }
081    
082                            _defaultCompanyLogo = getImage(is);
083                    }
084                    catch (Exception e) {
085                            _log.error(
086                                    "Unable to configure the default company logo: " +
087                                            e.getMessage());
088                    }
089    
090                    try {
091                            InputStream is = classLoader.getResourceAsStream(
092                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
093    
094                            if (is == null) {
095                                    _log.error("Default organization logo is not available");
096                            }
097    
098                            _defaultOrganizationLogo = getImage(is);
099                    }
100                    catch (Exception e) {
101                            _log.error(
102                                    "Unable to configure the default organization logo: " +
103                                            e.getMessage());
104                    }
105    
106                    try {
107                            InputStream is = classLoader.getResourceAsStream(
108                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
109    
110                            if (is == null) {
111                                    _log.error("Default user female portrait is not available");
112                            }
113    
114                            _defaultUserFemalePortrait = getImage(is);
115                    }
116                    catch (Exception e) {
117                            _log.error(
118                                    "Unable to configure the default user female portrait: " +
119                                            e.getMessage());
120                    }
121    
122                    try {
123                            InputStream is = classLoader.getResourceAsStream(
124                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
125    
126                            if (is == null) {
127                                    _log.error("Default user male portrait is not available");
128                            }
129    
130                            _defaultUserMalePortrait = getImage(is);
131                    }
132                    catch (Exception e) {
133                            _log.error(
134                                    "Unable to configure the default user male portrait: " +
135                                            e.getMessage());
136                    }
137            }
138    
139            @Override
140            public Image deleteImage(long imageId)
141                    throws PortalException, SystemException {
142    
143                    if (imageId <= 0) {
144                            return null;
145                    }
146    
147                    /*if (PropsValues.IMAGE_HOOK_IMPL.equals(
148                                    DatabaseHook.class.getName()) &&
149                            (imagePersistence.getListeners().length == 0)) {
150    
151                            runSQL("delete from Image where imageId = " + imageId);
152    
153                            imagePersistence.clearCache();
154                    }
155                    else {*/
156                            Image image = getImage(imageId);
157    
158                            if (image != null) {
159                                    imagePersistence.remove(image);
160    
161                                    Hook hook = HookFactory.getInstance();
162    
163                                    try {
164                                            hook.deleteImage(image);
165                                    }
166                                    catch (NoSuchImageException nsie) {
167    
168                                            // DLHook throws NoSuchImageException if the file no longer
169                                            // exists. See LPS-30430. This exception can be ignored.
170    
171                                            if (_log.isWarnEnabled()) {
172                                                    _log.warn(nsie, nsie);
173                                            }
174                                    }
175                            }
176    
177                            return image;
178                    //}
179            }
180    
181            public Image getCompanyLogo(long imageId) {
182                    Image image = getImage(imageId);
183    
184                    if (image == null) {
185                            image = getDefaultCompanyLogo();
186                    }
187    
188                    return image;
189            }
190    
191            public Image getDefaultCompanyLogo() {
192                    return _defaultCompanyLogo;
193            }
194    
195            public Image getDefaultOrganizationLogo() {
196                    return _defaultOrganizationLogo;
197            }
198    
199            public Image getDefaultSpacer() {
200                    return _defaultSpacer;
201            }
202    
203            public Image getDefaultUserFemalePortrait() {
204                    return _defaultUserFemalePortrait;
205            }
206    
207            public Image getDefaultUserMalePortrait() {
208                    return _defaultUserMalePortrait;
209            }
210    
211            public Image getImage(byte[] bytes)
212                    throws PortalException, SystemException {
213    
214                    return getImage(null, bytes);
215            }
216    
217            public Image getImage(File file) throws PortalException, SystemException {
218                    try {
219                            return getImage(new FileInputStream(file));
220                    }
221                    catch (IOException ioe) {
222                            throw new SystemException(ioe);
223                    }
224            }
225    
226            public Image getImage(InputStream is)
227                    throws PortalException, SystemException {
228    
229                    return getImage(is, null);
230            }
231    
232            public Image getImage(InputStream is, boolean cleanUpStream)
233                    throws PortalException, SystemException {
234    
235                    return getImage(is, null, cleanUpStream);
236            }
237    
238            @Override
239            public Image getImage(long imageId) {
240                    if (imageId > 0) {
241                            try {
242                                    return imagePersistence.fetchByPrimaryKey(imageId);
243                            }
244                            catch (Exception e) {
245                                    if (_log.isWarnEnabled()) {
246                                            _log.warn(
247                                                    "Unable to get image " + imageId + ": " +
248                                                            e.getMessage());
249                                    }
250                            }
251                    }
252    
253                    return null;
254            }
255    
256            public Image getImageOrDefault(long imageId) {
257                    Image image = getImage(imageId);
258    
259                    if (image == null) {
260                            image = getDefaultSpacer();
261                    }
262    
263                    return image;
264            }
265    
266            public List<Image> getImages() throws SystemException {
267                    return imagePersistence.findAll();
268            }
269    
270            public List<Image> getImagesBySize(int size) throws SystemException {
271                    return imagePersistence.findByLtSize(size);
272            }
273    
274            public boolean isNullOrDefaultSpacer(byte[] bytes) {
275                    if ((bytes == null) || (bytes.length == 0) ||
276                            (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
277    
278                            return true;
279                    }
280                    else {
281                            return false;
282                    }
283            }
284    
285            public Image updateImage(long imageId, byte[] bytes)
286                    throws PortalException, SystemException {
287    
288                    Image image = getImage(bytes);
289    
290                    return updateImage(
291                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
292                            image.getWidth(), image.getSize());
293            }
294    
295            public Image updateImage(
296                            long imageId, byte[] bytes, String type, int height, int width,
297                            int size)
298                    throws PortalException, SystemException {
299    
300                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
301    
302                    if (image == null) {
303                            image = imagePersistence.create(imageId);
304                    }
305    
306                    image.setModifiedDate(new Date());
307                    image.setType(type);
308                    image.setHeight(height);
309                    image.setWidth(width);
310                    image.setSize(size);
311    
312                    Hook hook = HookFactory.getInstance();
313    
314                    hook.updateImage(image, type, bytes);
315    
316                    imagePersistence.update(image);
317    
318                    WebServerServletTokenUtil.resetToken(imageId);
319    
320                    return image;
321            }
322    
323            public Image updateImage(long imageId, File file)
324                    throws PortalException, SystemException {
325    
326                    Image image = getImage(file);
327    
328                    return updateImage(
329                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
330                            image.getWidth(), image.getSize());
331            }
332    
333            public Image updateImage(long imageId, InputStream is)
334                    throws PortalException, SystemException {
335    
336                    Image image = getImage(is);
337    
338                    return updateImage(
339                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
340                            image.getWidth(), image.getSize());
341            }
342    
343            public Image updateImage(
344                            long imageId, InputStream is, boolean cleanUpStream)
345                    throws PortalException, SystemException {
346    
347                    Image image = getImage(is, cleanUpStream);
348    
349                    return updateImage(
350                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
351                            image.getWidth(), image.getSize());
352            }
353    
354            protected Image getImage(InputStream is, byte[] bytes)
355                    throws PortalException, SystemException {
356    
357                    return getImage(is, bytes, true);
358            }
359    
360            protected Image getImage(
361                            InputStream is, byte[] bytes, boolean cleanUpStream)
362                    throws PortalException, SystemException {
363    
364                    try {
365                            if (is != null) {
366                                    bytes = FileUtil.getBytes(is, -1, cleanUpStream);
367                            }
368    
369                            if (bytes == null) {
370                                    return null;
371                            }
372    
373                            ImageBag imageBag = ImageToolUtil.read(bytes);
374    
375                            RenderedImage renderedImage = imageBag.getRenderedImage();
376                            String type = imageBag.getType();
377    
378                            if (renderedImage == null) {
379                                    throw new ImageTypeException();
380                            }
381    
382                            int height = renderedImage.getHeight();
383                            int width = renderedImage.getWidth();
384                            int size = bytes.length;
385    
386                            Image image = new ImageImpl();
387    
388                            image.setTextObj(bytes);
389                            image.setType(type);
390                            image.setHeight(height);
391                            image.setWidth(width);
392                            image.setSize(size);
393    
394                            return image;
395                    }
396                    catch (IOException ioe) {
397                            throw new SystemException(ioe);
398                    }
399            }
400    
401            private static Log _log = LogFactoryUtil.getLog(
402                    ImageLocalServiceImpl.class);
403    
404            private Image _defaultCompanyLogo;
405            private Image _defaultOrganizationLogo;
406            private Image _defaultSpacer;
407            private Image _defaultUserFemalePortrait;
408            private Image _defaultUserMalePortrait;
409    
410    }