1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.documentlibrary.util;
21  
22  import com.liferay.documentlibrary.NoSuchFileException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.FileUtil;
29  
30  import java.io.BufferedInputStream;
31  import java.io.ByteArrayInputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.FileNotFoundException;
35  import java.io.IOException;
36  import java.io.InputStream;
37  
38  import java.util.Date;
39  
40  /**
41  
42   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public abstract class BaseHook implements Hook {
48  
49      public abstract void addDirectory(
50              long companyId, long repositoryId, String dirName)
51          throws PortalException, SystemException;
52  
53      public void addFile(
54              long companyId, String portletId, long groupId, long repositoryId,
55              String fileName, long fileEntryId, String properties,
56              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
57              byte[] bytes)
58          throws PortalException, SystemException {
59  
60          InputStream is = new ByteArrayInputStream(bytes);
61  
62          try {
63              addFile(
64                  companyId, portletId, groupId, repositoryId, fileName,
65                  fileEntryId, properties, modifiedDate, tagsCategories,
66                  tagsEntries, is);
67          }
68          finally {
69              try {
70                  is.close();
71              }
72              catch (IOException ioe) {
73                  _log.error(ioe);
74              }
75          }
76      }
77  
78      public void addFile(
79              long companyId, String portletId, long groupId, long repositoryId,
80              String fileName, long fileEntryId, String properties,
81              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
82              File file)
83          throws PortalException, SystemException {
84  
85          InputStream is = null;
86  
87          try {
88              is = new BufferedInputStream(new FileInputStream(file));
89  
90              addFile(
91                  companyId, portletId, groupId, repositoryId, fileName,
92                  fileEntryId, properties, modifiedDate, tagsCategories,
93                  tagsEntries, is);
94          }
95          catch (FileNotFoundException fnfe) {
96              throw new NoSuchFileException(fileName);
97          }
98          finally {
99              try {
100                 if (is != null) {
101                     is.close();
102                 }
103             }
104             catch (IOException ioe) {
105                 _log.error(ioe);
106             }
107         }
108     }
109 
110     public abstract void addFile(
111             long companyId, String portletId, long groupId, long repositoryId,
112             String fileName, long fileEntryId, String properties,
113             Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
114             InputStream is)
115         throws PortalException, SystemException;
116 
117     public abstract void checkRoot(long companyId) throws SystemException;
118 
119     public abstract void deleteDirectory(
120             long companyId, String portletId, long repositoryId, String dirName)
121         throws PortalException, SystemException;
122 
123     public abstract void deleteFile(
124             long companyId, String portletId, long repositoryId,
125             String fileName)
126         throws PortalException, SystemException;
127 
128     public abstract void deleteFile(
129             long companyId, String portletId, long repositoryId,
130             String fileName, double versionNumber)
131         throws PortalException, SystemException;
132 
133     public byte[] getFile(long companyId, long repositoryId, String fileName)
134         throws PortalException, SystemException {
135 
136         byte[] bytes = null;
137 
138         try {
139             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
140 
141             bytes = FileUtil.getBytes(is);
142         }
143         catch (IOException ioe) {
144             throw new SystemException(ioe);
145         }
146 
147         return bytes;
148     }
149 
150     public byte[] getFile(
151             long companyId, long repositoryId, String fileName,
152             double versionNumber)
153         throws PortalException, SystemException {
154 
155         byte[] bytes = null;
156 
157         try {
158             InputStream is = getFileAsStream(
159                 companyId, repositoryId, fileName, versionNumber);
160 
161             bytes = FileUtil.getBytes(is);
162         }
163         catch (IOException ioe) {
164             throw new SystemException(ioe);
165         }
166 
167         return bytes;
168     }
169 
170     public InputStream getFileAsStream(
171             long companyId, long repositoryId, String fileName)
172         throws PortalException, SystemException {
173 
174         return getFileAsStream(companyId, repositoryId, fileName, 0);
175     }
176 
177     public abstract InputStream getFileAsStream(
178             long companyId, long repositoryId, String fileName,
179             double versionNumber)
180         throws PortalException, SystemException;
181 
182     public abstract String[] getFileNames(
183             long companyId, long repositoryId, String dirName)
184         throws PortalException, SystemException;
185 
186     public abstract long getFileSize(
187             long companyId, long repositoryId, String fileName)
188         throws PortalException, SystemException;
189 
190     public abstract boolean hasFile(
191             long companyId, long repositoryId, String fileName,
192             double versionNumber)
193         throws PortalException, SystemException;
194 
195     public abstract void move(String srcDir, String destDir)
196         throws SystemException;
197 
198     public abstract void reIndex(String[] ids) throws SearchException;
199 
200     public void updateFile(
201             long companyId, String portletId, long groupId, long repositoryId,
202             String fileName, double versionNumber, String sourceFileName,
203             long fileEntryId, String properties, Date modifiedDate,
204             String[] tagsCategories, String[] tagsEntries, byte[] bytes)
205         throws PortalException, SystemException {
206 
207         InputStream is = new ByteArrayInputStream(bytes);
208 
209         try {
210             updateFile(
211                 companyId, portletId, groupId, repositoryId, fileName,
212                 versionNumber, sourceFileName, fileEntryId, properties,
213                 modifiedDate, tagsCategories, tagsEntries, is);
214         }
215         finally {
216             try {
217                 is.close();
218             }
219             catch (IOException ioe) {
220                 _log.error(ioe);
221             }
222         }
223     }
224 
225     public void updateFile(
226             long companyId, String portletId, long groupId, long repositoryId,
227             String fileName, double versionNumber, String sourceFileName,
228             long fileEntryId, String properties, Date modifiedDate,
229             String[] tagsCategories, String[] tagsEntries, File file)
230         throws PortalException, SystemException {
231 
232         InputStream is = null;
233 
234         try {
235             is = new BufferedInputStream(new FileInputStream(file));
236 
237             updateFile(
238                 companyId, portletId, groupId, repositoryId, fileName,
239                 versionNumber, sourceFileName, fileEntryId, properties,
240                 modifiedDate, tagsCategories, tagsEntries, is);
241         }
242         catch (FileNotFoundException fnfe) {
243             throw new NoSuchFileException(fileName);
244         }
245         finally {
246             try {
247                 if (is != null) {
248                     is.close();
249                 }
250             }
251             catch (IOException ioe) {
252                 _log.error(ioe);
253             }
254         }
255     }
256 
257     public abstract void updateFile(
258             long companyId, String portletId, long groupId, long repositoryId,
259             String fileName, double versionNumber, String sourceFileName,
260             long fileEntryId, String properties, Date modifiedDate,
261             String[] tagsCategories, String[] tagsEntries, InputStream is)
262         throws PortalException, SystemException;
263 
264     public abstract void updateFile(
265             long companyId, String portletId, long groupId, long repositoryId,
266             long newRepositoryId, String fileName, long fileEntryId)
267         throws PortalException, SystemException;
268 
269     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
270 
271 }