1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.documentlibrary.NoSuchFileException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
21  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.SearchException;
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.service.ServiceContext;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  
35  import java.util.Date;
36  
37  /**
38   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public abstract class BaseHook implements Hook {
43  
44      public abstract void addDirectory(
45              long companyId, long repositoryId, String dirName)
46          throws PortalException, SystemException;
47  
48      public void addFile(
49              long companyId, String portletId, long groupId, long repositoryId,
50              String fileName, long fileEntryId, String properties,
51              Date modifiedDate, ServiceContext serviceContext, byte[] bytes)
52          throws PortalException, SystemException {
53  
54          InputStream is = new UnsyncByteArrayInputStream(bytes);
55  
56          try {
57              addFile(
58                  companyId, portletId, groupId, repositoryId, fileName,
59                  fileEntryId, properties, modifiedDate, serviceContext, is);
60          }
61          finally {
62              try {
63                  is.close();
64              }
65              catch (IOException ioe) {
66                  _log.error(ioe);
67              }
68          }
69      }
70  
71      public void addFile(
72              long companyId, String portletId, long groupId, long repositoryId,
73              String fileName, long fileEntryId, String properties,
74              Date modifiedDate, ServiceContext serviceContext, File file)
75          throws PortalException, SystemException {
76  
77          InputStream is = null;
78  
79          try {
80              is = new UnsyncBufferedInputStream(new FileInputStream(file));
81  
82              addFile(
83                  companyId, portletId, groupId, repositoryId, fileName,
84                  fileEntryId, properties, modifiedDate, serviceContext, is);
85          }
86          catch (FileNotFoundException fnfe) {
87              throw new NoSuchFileException(fileName);
88          }
89          finally {
90              try {
91                  if (is != null) {
92                      is.close();
93                  }
94              }
95              catch (IOException ioe) {
96                  _log.error(ioe);
97              }
98          }
99      }
100 
101     public abstract void addFile(
102             long companyId, String portletId, long groupId, long repositoryId,
103             String fileName, long fileEntryId, String properties,
104             Date modifiedDate, ServiceContext serviceContext, InputStream is)
105         throws PortalException, SystemException;
106 
107     public abstract void checkRoot(long companyId) throws SystemException;
108 
109     public abstract void deleteDirectory(
110             long companyId, String portletId, long repositoryId, String dirName)
111         throws PortalException, SystemException;
112 
113     public abstract void deleteFile(
114             long companyId, String portletId, long repositoryId,
115             String fileName)
116         throws PortalException, SystemException;
117 
118     public abstract void deleteFile(
119             long companyId, String portletId, long repositoryId,
120             String fileName, String versionNumber)
121         throws PortalException, SystemException;
122 
123     public byte[] getFile(long companyId, long repositoryId, String fileName)
124         throws PortalException, SystemException {
125 
126         byte[] bytes = null;
127 
128         try {
129             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
130 
131             bytes = FileUtil.getBytes(is);
132         }
133         catch (IOException ioe) {
134             throw new SystemException(ioe);
135         }
136 
137         return bytes;
138     }
139 
140     public byte[] getFile(
141             long companyId, long repositoryId, String fileName,
142             String versionNumber)
143         throws PortalException, SystemException {
144 
145         byte[] bytes = null;
146 
147         try {
148             InputStream is = getFileAsStream(
149                 companyId, repositoryId, fileName, versionNumber);
150 
151             bytes = FileUtil.getBytes(is);
152         }
153         catch (IOException ioe) {
154             throw new SystemException(ioe);
155         }
156 
157         return bytes;
158     }
159 
160     public InputStream getFileAsStream(
161             long companyId, long repositoryId, String fileName)
162         throws PortalException, SystemException {
163 
164         return getFileAsStream(companyId, repositoryId, fileName,
165             StringPool.BLANK);
166     }
167 
168     public abstract InputStream getFileAsStream(
169             long companyId, long repositoryId, String fileName,
170             String versionNumber)
171         throws PortalException, SystemException;
172 
173     public abstract String[] getFileNames(
174             long companyId, long repositoryId, String dirName)
175         throws PortalException, SystemException;
176 
177     public abstract long getFileSize(
178             long companyId, long repositoryId, String fileName)
179         throws PortalException, SystemException;
180 
181     public abstract boolean hasFile(
182             long companyId, long repositoryId, String fileName,
183             String versionNumber)
184         throws PortalException, SystemException;
185 
186     public abstract void move(String srcDir, String destDir)
187         throws SystemException;
188 
189     public abstract void reindex(String[] ids) throws SearchException;
190 
191     public abstract void updateFile(
192             long companyId, String portletId, long groupId, long repositoryId,
193             long newRepositoryId, String fileName, long fileEntryId)
194         throws PortalException, SystemException;
195 
196     public void updateFile(
197             long companyId, String portletId, long groupId, long repositoryId,
198             String fileName, String versionNumber, String sourceFileName,
199             long fileEntryId, String properties, Date modifiedDate,
200             ServiceContext serviceContext, byte[] bytes)
201         throws PortalException, SystemException {
202 
203         InputStream is = new UnsyncByteArrayInputStream(bytes);
204 
205         try {
206             updateFile(
207                 companyId, portletId, groupId, repositoryId, fileName,
208                 versionNumber, sourceFileName, fileEntryId, properties,
209                 modifiedDate, serviceContext, is);
210         }
211         finally {
212             try {
213                 is.close();
214             }
215             catch (IOException ioe) {
216                 _log.error(ioe);
217             }
218         }
219     }
220 
221     public void updateFile(
222             long companyId, String portletId, long groupId, long repositoryId,
223             String fileName, String versionNumber, String sourceFileName,
224             long fileEntryId, String properties, Date modifiedDate,
225             ServiceContext serviceContext, File file)
226         throws PortalException, SystemException {
227 
228         InputStream is = null;
229 
230         try {
231             is = new UnsyncBufferedInputStream(new FileInputStream(file));
232 
233             updateFile(
234                 companyId, portletId, groupId, repositoryId, fileName,
235                 versionNumber, sourceFileName, fileEntryId, properties,
236                 modifiedDate, serviceContext, is);
237         }
238         catch (FileNotFoundException fnfe) {
239             throw new NoSuchFileException(fileName);
240         }
241         finally {
242             try {
243                 if (is != null) {
244                     is.close();
245                 }
246             }
247             catch (IOException ioe) {
248                 _log.error(ioe);
249             }
250         }
251     }
252 
253     public abstract void updateFile(
254             long companyId, String portletId, long groupId, long repositoryId,
255             String fileName, String versionNumber, String sourceFileName,
256             long fileEntryId, String properties, Date modifiedDate,
257             ServiceContext serviceContext, InputStream is)
258         throws PortalException, SystemException;
259 
260     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
261 
262 }