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