001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.DatabaseHook;
020    import com.liferay.portal.image.HookFactory;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.image.Hook;
024    import com.liferay.portal.kernel.image.ImageBag;
025    import com.liferay.portal.kernel.image.ImageProcessorUtil;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
029    import com.liferay.portal.kernel.util.FileUtil;
030    import com.liferay.portal.kernel.util.PropsKeys;
031    import com.liferay.portal.model.Image;
032    import com.liferay.portal.model.impl.ImageImpl;
033    import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
034    import com.liferay.portal.util.PropsUtil;
035    import com.liferay.portal.util.PropsValues;
036    
037    import java.awt.image.RenderedImage;
038    
039    import java.io.File;
040    import java.io.FileInputStream;
041    import java.io.IOException;
042    import java.io.InputStream;
043    
044    import java.util.Arrays;
045    import java.util.Date;
046    import java.util.List;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Julio Camarero
051     */
052    public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
053    
054            public void afterPropertiesSet() {
055                    ClassLoader classLoader = getClass().getClassLoader();
056    
057                    try {
058                            InputStream is = classLoader.getResourceAsStream(
059                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
060    
061                            if (is == null) {
062                                    _log.error("Default spacer is not available");
063                            }
064    
065                            _defaultSpacer = getImage(is);
066                    }
067                    catch (Exception ioe) {
068                            _log.error(
069                                    "Unable to configure the default spacer: " + ioe.getMessage());
070                    }
071    
072                    try {
073                            InputStream is = classLoader.getResourceAsStream(
074                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
075    
076                            if (is == null) {
077                                    _log.error("Default company logo is not available");
078                            }
079    
080                            _defaultCompanyLogo = getImage(is);
081                    }
082                    catch (Exception ioe) {
083                            _log.error(
084                                    "Unable to configure the default company logo: " +
085                                            ioe.getMessage());
086                    }
087    
088                    try {
089                            InputStream is = classLoader.getResourceAsStream(
090                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_ORGANIZATION_LOGO));
091    
092                            if (is == null) {
093                                    _log.error("Default organization logo is not available");
094                            }
095    
096                            _defaultOrganizationLogo = getImage(is);
097                    }
098                    catch (Exception ioe) {
099                            _log.error(
100                                    "Unable to configure the default organization logo: " +
101                                            ioe.getMessage());
102                    }
103    
104                    try {
105                            InputStream is = classLoader.getResourceAsStream(
106                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
107    
108                            if (is == null) {
109                                    _log.error("Default user female portrait is not available");
110                            }
111    
112                            _defaultUserFemalePortrait = getImage(is);
113                    }
114                    catch (Exception ioe) {
115                            _log.error(
116                                    "Unable to configure the default user female portrait: " +
117                                            ioe.getMessage());
118                    }
119    
120                    try {
121                            InputStream is = classLoader.getResourceAsStream(
122                                    PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
123    
124                            if (is == null) {
125                                    _log.error("Default user male portrait is not available");
126                            }
127    
128                            _defaultUserMalePortrait = getImage(is);
129                    }
130                    catch (Exception ioe) {
131                            _log.error(
132                                    "Unable to configure the default user male portrait: " +
133                                            ioe.getMessage());
134                    }
135            }
136    
137            public void deleteImage(long imageId)
138                    throws PortalException, SystemException {
139    
140                    if (imageId <= 0) {
141                            return;
142                    }
143    
144                    if (PropsValues.IMAGE_HOOK_IMPL.equals(DatabaseHook.class.getName()) &&
145                            (imagePersistence.getListeners().length == 0)) {
146    
147                            runSQL("delete from Image where imageId = " + imageId);
148    
149                            imagePersistence.clearCache();
150                    }
151                    else {
152                            try {
153                                    Image image = getImage(imageId);
154    
155                                    imagePersistence.remove(imageId);
156    
157                                    Hook hook = HookFactory.getInstance();
158    
159                                    hook.deleteImage(image);
160                            }
161                            catch (NoSuchImageException nsie) {
162                            }
163                    }
164            }
165    
166            public Image getCompanyLogo(long imageId) {
167                    Image image = getImage(imageId);
168    
169                    if (image == null) {
170                            image = getDefaultCompanyLogo();
171                    }
172    
173                    return image;
174            }
175    
176            public Image getDefaultCompanyLogo() {
177                    return _defaultCompanyLogo;
178            }
179    
180            public Image getDefaultOrganizationLogo() {
181                    return _defaultOrganizationLogo;
182            }
183    
184            public Image getDefaultSpacer() {
185                    return _defaultSpacer;
186            }
187    
188            public Image getDefaultUserFemalePortrait() {
189                    return _defaultUserFemalePortrait;
190            }
191    
192            public Image getDefaultUserMalePortrait() {
193                    return _defaultUserMalePortrait;
194            }
195    
196            public Image getImage(long imageId) {
197                    try {
198                            if (imageId > 0) {
199                                    return imagePersistence.findByPrimaryKey(imageId);
200                            }
201                    }
202                    catch (Exception e) {
203                            if (_log.isWarnEnabled()) {
204                                    _log.warn(
205                                            "Unable to get image " + imageId + ": " + e.getMessage());
206                            }
207                    }
208    
209                    return null;
210            }
211    
212            public Image getImage(byte[] bytes)
213                    throws PortalException, SystemException {
214    
215                    return getImage(null, bytes);
216            }
217    
218            public Image getImage(File file) throws PortalException, SystemException {
219                    try {
220                            return getImage(new FileInputStream(file));
221                    }
222                    catch (IOException ioe) {
223                            throw new SystemException(ioe);
224                    }
225            }
226    
227            public Image getImage(InputStream is)
228                    throws PortalException, SystemException {
229    
230                    return getImage(is, null);
231            }
232    
233            public Image getImageOrDefault(long imageId) {
234                    Image image = getImage(imageId);
235    
236                    if (image == null) {
237                            image = getDefaultSpacer();
238                    }
239    
240                    return image;
241            }
242    
243            public List<Image> getImages() throws SystemException {
244                    return imagePersistence.findAll();
245            }
246    
247            public List<Image> getImages(int start, int end) throws SystemException {
248                    return imagePersistence.findAll(start, end);
249            }
250    
251            public List<Image> getImagesBySize(int size) throws SystemException {
252                    return imagePersistence.findByLtSize(size);
253            }
254    
255            public boolean isNullOrDefaultSpacer(byte[] bytes) {
256                    if ((bytes == null) || (bytes.length == 0) ||
257                            (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
258    
259                            return true;
260                    }
261                    else {
262                            return false;
263                    }
264            }
265    
266            public Image updateImage(long imageId, byte[] bytes)
267                    throws PortalException, SystemException {
268    
269                    Image image = getImage(bytes);
270    
271                    return updateImage(
272                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
273                            image.getWidth(), image.getSize());
274            }
275    
276            public Image updateImage(long imageId, File file)
277                    throws PortalException, SystemException {
278    
279                    Image image = getImage(file);
280    
281                    return updateImage(
282                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
283                            image.getWidth(), image.getSize());
284            }
285    
286            public Image updateImage(long imageId, InputStream is)
287                    throws PortalException, SystemException {
288    
289                    Image image = getImage(is);
290    
291                    return updateImage(
292                            imageId, image.getTextObj(), image.getType(), image.getHeight(),
293                            image.getWidth(), image.getSize());
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)
325                    throws PortalException, SystemException {
326    
327                    try {
328                            if (is != null) {
329                                    bytes = FileUtil.getBytes(is);
330                            }
331    
332                            ImageBag imageBag = ImageProcessorUtil.read(bytes);
333    
334                            RenderedImage renderedImage = imageBag.getRenderedImage();
335                            String type = imageBag.getType();
336    
337                            if (renderedImage == null) {
338                                    throw new ImageTypeException();
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                    catch (IOException ioe) {
356                            throw new SystemException(ioe);
357                    }
358                    finally {
359                            if (is != null) {
360                                    try {
361                                            is.close();
362                                    }
363                                    catch (IOException ioe) {
364                                            if (_log.isWarnEnabled()) {
365                                                    _log.warn(ioe);
366                                            }
367                                    }
368                            }
369                    }
370            }
371    
372            private static Log _log = LogFactoryUtil.getLog(
373                    ImageLocalServiceImpl.class);
374    
375            private Image _defaultSpacer;
376            private Image _defaultCompanyLogo;
377            private Image _defaultOrganizationLogo;
378            private Image _defaultUserFemalePortrait;
379            private Image _defaultUserMalePortrait;
380    
381    }