001    /**
002     * Copyright (c) 2000-2010 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.image;
016    
017    import com.liferay.documentlibrary.NoSuchFileException;
018    import com.liferay.documentlibrary.service.DLLocalServiceUtil;
019    import com.liferay.documentlibrary.service.DLServiceUtil;
020    import com.liferay.portal.NoSuchImageException;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
024    import com.liferay.portal.kernel.util.FileUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.model.Image;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortletKeys;
029    
030    import java.io.IOException;
031    import java.io.InputStream;
032    
033    import java.util.Date;
034    
035    /**
036     * @author Jorge Ferrer
037     */
038    public class DLHook extends BaseHook {
039    
040            public void deleteImage(Image image)
041                    throws PortalException, SystemException {
042    
043                    String fileName = getFileName(image.getImageId(), image.getType());
044    
045                    try {
046                            DLServiceUtil.deleteFile(
047                                    _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
048                    }
049                    catch (NoSuchFileException nsfe) {
050                            throw new NoSuchImageException(nsfe);
051                    }
052            }
053    
054            public byte[] getImageAsBytes(Image image)
055                    throws PortalException, SystemException {
056    
057                    String fileName = getFileName(image.getImageId(), image.getType());
058    
059                    InputStream is = DLLocalServiceUtil.getFileAsStream(
060                            _COMPANY_ID, _REPOSITORY_ID, fileName);
061    
062                    byte[] bytes = null;
063    
064                    try {
065                            bytes = FileUtil.getBytes(is);
066                    }
067                    catch (IOException ioe) {
068                            throw new SystemException(ioe);
069                    }
070    
071                    return bytes;
072            }
073    
074            public InputStream getImageAsStream(Image image)
075                    throws PortalException, SystemException {
076    
077                    String fileName = getFileName(image.getImageId(), image.getType());
078    
079                    return DLLocalServiceUtil.getFileAsStream(
080                            _COMPANY_ID, _REPOSITORY_ID, fileName);
081            }
082    
083            public void updateImage(Image image, String type, byte[] bytes)
084                    throws PortalException, SystemException {
085    
086                    String fileName = getFileName(image.getImageId(), image.getType());
087                    Date now = new Date();
088                    InputStream is = new UnsyncByteArrayInputStream(bytes);
089    
090                    if (DLLocalServiceUtil.hasFile(
091                            _COMPANY_ID, _REPOSITORY_ID, fileName, _VERSION_NUMBER)) {
092    
093                            DLServiceUtil.deleteFile(
094                                    _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
095                    }
096    
097                    DLLocalServiceUtil.addFile(
098                            _COMPANY_ID, _PORTLET_ID, _GROUP_ID, _REPOSITORY_ID, fileName, true,
099                            _FILE_ENTRY_ID, _PROPERTIES, now, new ServiceContext(), is);
100            }
101    
102            protected String getFileName(long imageId, String type) {
103                    return imageId + StringPool.PERIOD + type;
104            }
105    
106            private static final long _COMPANY_ID = 0;
107            private static final long _FILE_ENTRY_ID = 0;
108            private static final long _GROUP_ID = 0;
109            private static final String _PORTLET_ID = PortletKeys.PORTAL;
110            private static final String _PROPERTIES = StringPool.BLANK;
111            private static final long _REPOSITORY_ID = 0;
112            private static final String _VERSION_NUMBER = "1.0";
113    
114    }