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.documentlibrary.util;
016    
017    import com.liferay.documentlibrary.NoSuchFileException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
021    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.search.SearchException;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.service.ServiceContext;
028    
029    import java.io.File;
030    import java.io.FileInputStream;
031    import java.io.FileNotFoundException;
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    import java.util.Date;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public abstract class BaseHook implements Hook {
041    
042            public abstract void addDirectory(
043                            long companyId, long repositoryId, String dirName)
044                    throws PortalException, SystemException;
045    
046            public void addFile(
047                            long companyId, String portletId, long groupId, long repositoryId,
048                            String fileName, long fileEntryId, String properties,
049                            Date modifiedDate, ServiceContext serviceContext, byte[] bytes)
050                    throws PortalException, SystemException {
051    
052                    InputStream is = new UnsyncByteArrayInputStream(bytes);
053    
054                    try {
055                            addFile(
056                                    companyId, portletId, groupId, repositoryId, fileName,
057                                    fileEntryId, properties, modifiedDate, serviceContext, is);
058                    }
059                    finally {
060                            try {
061                                    is.close();
062                            }
063                            catch (IOException ioe) {
064                                    _log.error(ioe);
065                            }
066                    }
067            }
068    
069            public void addFile(
070                            long companyId, String portletId, long groupId, long repositoryId,
071                            String fileName, long fileEntryId, String properties,
072                            Date modifiedDate, ServiceContext serviceContext, File file)
073                    throws PortalException, SystemException {
074    
075                    InputStream is = null;
076    
077                    try {
078                            is = new UnsyncBufferedInputStream(new FileInputStream(file));
079    
080                            addFile(
081                                    companyId, portletId, groupId, repositoryId, fileName,
082                                    fileEntryId, properties, modifiedDate, serviceContext, is);
083                    }
084                    catch (FileNotFoundException fnfe) {
085                            throw new NoSuchFileException(fileName);
086                    }
087                    finally {
088                            try {
089                                    if (is != null) {
090                                            is.close();
091                                    }
092                            }
093                            catch (IOException ioe) {
094                                    _log.error(ioe);
095                            }
096                    }
097            }
098    
099            public abstract void addFile(
100                            long companyId, String portletId, long groupId, long repositoryId,
101                            String fileName, long fileEntryId, String properties,
102                            Date modifiedDate, ServiceContext serviceContext, InputStream is)
103                    throws PortalException, SystemException;
104    
105            public abstract void checkRoot(long companyId) throws SystemException;
106    
107            public abstract void deleteDirectory(
108                            long companyId, String portletId, long repositoryId, String dirName)
109                    throws PortalException, SystemException;
110    
111            public abstract void deleteFile(
112                            long companyId, String portletId, long repositoryId,
113                            String fileName)
114                    throws PortalException, SystemException;
115    
116            public abstract void deleteFile(
117                            long companyId, String portletId, long repositoryId,
118                            String fileName, String versionNumber)
119                    throws PortalException, SystemException;
120    
121            public byte[] getFile(long companyId, long repositoryId, String fileName)
122                    throws PortalException, SystemException {
123    
124                    byte[] bytes = null;
125    
126                    try {
127                            InputStream is = getFileAsStream(companyId, repositoryId, fileName);
128    
129                            bytes = FileUtil.getBytes(is);
130                    }
131                    catch (IOException ioe) {
132                            throw new SystemException(ioe);
133                    }
134    
135                    return bytes;
136            }
137    
138            public byte[] getFile(
139                            long companyId, long repositoryId, String fileName,
140                            String versionNumber)
141                    throws PortalException, SystemException {
142    
143                    byte[] bytes = null;
144    
145                    try {
146                            InputStream is = getFileAsStream(
147                                    companyId, repositoryId, fileName, versionNumber);
148    
149                            bytes = FileUtil.getBytes(is);
150                    }
151                    catch (IOException ioe) {
152                            throw new SystemException(ioe);
153                    }
154    
155                    return bytes;
156            }
157    
158            public InputStream getFileAsStream(
159                            long companyId, long repositoryId, String fileName)
160                    throws PortalException, SystemException {
161    
162                    return getFileAsStream(companyId, repositoryId, fileName,
163                            StringPool.BLANK);
164            }
165    
166            public abstract InputStream getFileAsStream(
167                            long companyId, long repositoryId, String fileName,
168                            String versionNumber)
169                    throws PortalException, SystemException;
170    
171            public abstract String[] getFileNames(
172                            long companyId, long repositoryId, String dirName)
173                    throws PortalException, SystemException;
174    
175            public abstract long getFileSize(
176                            long companyId, long repositoryId, String fileName)
177                    throws PortalException, SystemException;
178    
179            public abstract boolean hasFile(
180                            long companyId, long repositoryId, String fileName,
181                            String versionNumber)
182                    throws PortalException, SystemException;
183    
184            public abstract void move(String srcDir, String destDir)
185                    throws SystemException;
186    
187            public abstract void reindex(String[] ids) throws SearchException;
188    
189            public abstract void updateFile(
190                            long companyId, String portletId, long groupId, long repositoryId,
191                            long newRepositoryId, String fileName, long fileEntryId)
192                    throws PortalException, SystemException;
193    
194            public void updateFile(
195                            long companyId, String portletId, long groupId, long repositoryId,
196                            String fileName, String versionNumber, String sourceFileName,
197                            long fileEntryId, String properties, Date modifiedDate,
198                            ServiceContext serviceContext, byte[] bytes)
199                    throws PortalException, SystemException {
200    
201                    InputStream is = new UnsyncByteArrayInputStream(bytes);
202    
203                    try {
204                            updateFile(
205                                    companyId, portletId, groupId, repositoryId, fileName,
206                                    versionNumber, sourceFileName, fileEntryId, properties,
207                                    modifiedDate, serviceContext, is);
208                    }
209                    finally {
210                            try {
211                                    is.close();
212                            }
213                            catch (IOException ioe) {
214                                    _log.error(ioe);
215                            }
216                    }
217            }
218    
219            public void updateFile(
220                            long companyId, String portletId, long groupId, long repositoryId,
221                            String fileName, String versionNumber, String sourceFileName,
222                            long fileEntryId, String properties, Date modifiedDate,
223                            ServiceContext serviceContext, File file)
224                    throws PortalException, SystemException {
225    
226                    InputStream is = null;
227    
228                    try {
229                            is = new UnsyncBufferedInputStream(new FileInputStream(file));
230    
231                            updateFile(
232                                    companyId, portletId, groupId, repositoryId, fileName,
233                                    versionNumber, sourceFileName, fileEntryId, properties,
234                                    modifiedDate, serviceContext, is);
235                    }
236                    catch (FileNotFoundException fnfe) {
237                            throw new NoSuchFileException(fileName);
238                    }
239                    finally {
240                            try {
241                                    if (is != null) {
242                                            is.close();
243                                    }
244                            }
245                            catch (IOException ioe) {
246                                    _log.error(ioe);
247                            }
248                    }
249            }
250    
251            public abstract void updateFile(
252                            long companyId, String portletId, long groupId, long repositoryId,
253                            String fileName, String versionNumber, String sourceFileName,
254                            long fileEntryId, String properties, Date modifiedDate,
255                            ServiceContext serviceContext, InputStream is)
256                    throws PortalException, SystemException;
257    
258            private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
259    
260    }