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