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.NoSuchImageException;
018    import com.liferay.portal.image.HookFactory;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.image.Hook;
022    import com.liferay.portal.kernel.image.ImageToolUtil;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.model.Image;
026    import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
027    import com.liferay.portal.webserver.WebServerServletTokenUtil;
028    
029    import java.io.File;
030    import java.io.IOException;
031    import java.io.InputStream;
032    
033    import java.util.Date;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Julio Camarero
039     * @author Shuyang Zhou
040     */
041    public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
042    
043            @Override
044            public Image deleteImage(long imageId)
045                    throws PortalException, SystemException {
046    
047                    if (imageId <= 0) {
048                            return null;
049                    }
050    
051                    /*if (PropsValues.IMAGE_HOOK_IMPL.equals(
052                                    DatabaseHook.class.getName()) &&
053                            (imagePersistence.getListeners().length == 0)) {
054    
055                            runSQL("delete from Image where imageId = " + imageId);
056    
057                            imagePersistence.clearCache();
058                    }
059                    else {*/
060                            Image image = getImage(imageId);
061    
062                            if (image != null) {
063                                    imagePersistence.remove(image);
064    
065                                    Hook hook = HookFactory.getInstance();
066    
067                                    try {
068                                            hook.deleteImage(image);
069                                    }
070                                    catch (NoSuchImageException nsie) {
071    
072                                            // DLHook throws NoSuchImageException if the file no longer
073                                            // exists. See LPS-30430. This exception can be ignored.
074    
075                                            if (_log.isWarnEnabled()) {
076                                                    _log.warn(nsie, nsie);
077                                            }
078                                    }
079                            }
080    
081                            return image;
082                    //}
083            }
084    
085            public Image getCompanyLogo(long imageId) {
086                    Image image = getImage(imageId);
087    
088                    if (image == null) {
089                            image = ImageToolUtil.getDefaultCompanyLogo();
090                    }
091    
092                    return image;
093            }
094    
095            @Override
096            public Image getImage(long imageId) {
097                    if (imageId > 0) {
098                            try {
099                                    return imagePersistence.fetchByPrimaryKey(imageId);
100                            }
101                            catch (Exception e) {
102                                    if (_log.isWarnEnabled()) {
103                                            _log.warn(
104                                                    "Unable to get image " + imageId + ": " +
105                                                            e.getMessage());
106                                    }
107                            }
108                    }
109    
110                    return null;
111            }
112    
113            public Image getImageOrDefault(long imageId) {
114                    Image image = getImage(imageId);
115    
116                    if (image == null) {
117                            image = ImageToolUtil.getDefaultSpacer();
118                    }
119    
120                    return image;
121            }
122    
123            public List<Image> getImages() throws SystemException {
124                    return imagePersistence.findAll();
125            }
126    
127            public List<Image> getImagesBySize(int size) throws SystemException {
128                    return imagePersistence.findByLtSize(size);
129            }
130    
131            public Image updateImage(long imageId, byte[] bytes)
132                    throws PortalException, SystemException {
133    
134                    Image image = null;
135    
136                    try {
137                            image = ImageToolUtil.getImage(bytes);
138                    }
139                    catch (IOException ioe) {
140                            throw new SystemException(ioe);
141                    }
142    
143                    return updateImage(
144                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
145                            image.getWidth(), image.getSize());
146            }
147    
148            public Image updateImage(
149                            long imageId, byte[] bytes, String type, int height, int width,
150                            int size)
151                    throws PortalException, SystemException {
152    
153                    Image image = imagePersistence.fetchByPrimaryKey(imageId);
154    
155                    if (image == null) {
156                            image = imagePersistence.create(imageId);
157                    }
158    
159                    image.setModifiedDate(new Date());
160                    image.setType(type);
161                    image.setHeight(height);
162                    image.setWidth(width);
163                    image.setSize(size);
164    
165                    Hook hook = HookFactory.getInstance();
166    
167                    hook.updateImage(image, type, bytes);
168    
169                    imagePersistence.update(image);
170    
171                    WebServerServletTokenUtil.resetToken(imageId);
172    
173                    return image;
174            }
175    
176            public Image updateImage(long imageId, File file)
177                    throws PortalException, SystemException {
178    
179                    Image image = null;
180    
181                    try {
182                            image = ImageToolUtil.getImage(file);
183                    }
184                    catch (IOException ioe) {
185                            throw new SystemException(ioe);
186                    }
187    
188                    return updateImage(
189                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
190                            image.getWidth(), image.getSize());
191            }
192    
193            public Image updateImage(long imageId, InputStream is)
194                    throws PortalException, SystemException {
195    
196                    Image image = null;
197    
198                    try {
199                            image = ImageToolUtil.getImage(is);
200                    }
201                    catch (IOException ioe) {
202                            throw new SystemException(ioe);
203                    }
204    
205                    return updateImage(
206                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
207                            image.getWidth(), image.getSize());
208            }
209    
210            public Image updateImage(
211                            long imageId, InputStream is, boolean cleanUpStream)
212                    throws PortalException, SystemException {
213    
214                    Image image = null;
215    
216                    try {
217                            image = ImageToolUtil.getImage(is, cleanUpStream);
218                    }
219                    catch (IOException ioe) {
220                            throw new SystemException(ioe);
221                    }
222    
223                    return updateImage(
224                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
225                            image.getWidth(), image.getSize());
226            }
227    
228            private static Log _log = LogFactoryUtil.getLog(
229                    ImageLocalServiceImpl.class);
230    
231    }