1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.io.unsync.UnsyncBufferedInputStream;
29  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.search.SearchException;
33  import com.liferay.portal.kernel.util.FileUtil;
34  
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   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public abstract class BaseHook implements Hook {
49  
50      public abstract void addDirectory(
51              long companyId, long repositoryId, String dirName)
52          throws PortalException, SystemException;
53  
54      public void addFile(
55              long companyId, String portletId, long groupId, long repositoryId,
56              String fileName, long fileEntryId, String properties,
57              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
58              byte[] bytes)
59          throws PortalException, SystemException {
60  
61          InputStream is = new UnsyncByteArrayInputStream(bytes);
62  
63          try {
64              addFile(
65                  companyId, portletId, groupId, repositoryId, fileName,
66                  fileEntryId, properties, modifiedDate, tagsCategories,
67                  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[] tagsCategories, String[] tagsEntries,
83              File file)
84          throws PortalException, SystemException {
85  
86          InputStream is = null;
87  
88          try {
89              is = new UnsyncBufferedInputStream(new FileInputStream(file));
90  
91              addFile(
92                  companyId, portletId, groupId, repositoryId, fileName,
93                  fileEntryId, properties, modifiedDate, tagsCategories,
94                  tagsEntries, is);
95          }
96          catch (FileNotFoundException fnfe) {
97              throw new NoSuchFileException(fileName);
98          }
99          finally {
100             try {
101                 if (is != null) {
102                     is.close();
103                 }
104             }
105             catch (IOException ioe) {
106                 _log.error(ioe);
107             }
108         }
109     }
110 
111     public abstract void addFile(
112             long companyId, String portletId, long groupId, long repositoryId,
113             String fileName, long fileEntryId, String properties,
114             Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
115             InputStream is)
116         throws PortalException, SystemException;
117 
118     public abstract void checkRoot(long companyId) throws SystemException;
119 
120     public abstract void deleteDirectory(
121             long companyId, String portletId, long repositoryId, String dirName)
122         throws PortalException, SystemException;
123 
124     public abstract void deleteFile(
125             long companyId, String portletId, long repositoryId,
126             String fileName)
127         throws PortalException, SystemException;
128 
129     public abstract void deleteFile(
130             long companyId, String portletId, long repositoryId,
131             String fileName, double versionNumber)
132         throws PortalException, SystemException;
133 
134     public byte[] getFile(long companyId, long repositoryId, String fileName)
135         throws PortalException, SystemException {
136 
137         byte[] bytes = null;
138 
139         try {
140             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
141 
142             bytes = FileUtil.getBytes(is);
143         }
144         catch (IOException ioe) {
145             throw new SystemException(ioe);
146         }
147 
148         return bytes;
149     }
150 
151     public byte[] getFile(
152             long companyId, long repositoryId, String fileName,
153             double versionNumber)
154         throws PortalException, SystemException {
155 
156         byte[] bytes = null;
157 
158         try {
159             InputStream is = getFileAsStream(
160                 companyId, repositoryId, fileName, versionNumber);
161 
162             bytes = FileUtil.getBytes(is);
163         }
164         catch (IOException ioe) {
165             throw new SystemException(ioe);
166         }
167 
168         return bytes;
169     }
170 
171     public InputStream getFileAsStream(
172             long companyId, long repositoryId, String fileName)
173         throws PortalException, SystemException {
174 
175         return getFileAsStream(companyId, repositoryId, fileName, 0);
176     }
177 
178     public abstract InputStream getFileAsStream(
179             long companyId, long repositoryId, String fileName,
180             double versionNumber)
181         throws PortalException, SystemException;
182 
183     public abstract String[] getFileNames(
184             long companyId, long repositoryId, String dirName)
185         throws PortalException, SystemException;
186 
187     public abstract long getFileSize(
188             long companyId, long repositoryId, String fileName)
189         throws PortalException, SystemException;
190 
191     public abstract boolean hasFile(
192             long companyId, long repositoryId, String fileName,
193             double versionNumber)
194         throws PortalException, SystemException;
195 
196     public abstract void move(String srcDir, String destDir)
197         throws SystemException;
198 
199     public abstract void reIndex(String[] ids) throws SearchException;
200 
201     public abstract void updateFile(
202             long companyId, String portletId, long groupId, long repositoryId,
203             long newRepositoryId, String fileName, long fileEntryId)
204         throws PortalException, SystemException;
205 
206     public void updateFile(
207             long companyId, String portletId, long groupId, long repositoryId,
208             String fileName, double versionNumber, String sourceFileName,
209             long fileEntryId, String properties, Date modifiedDate,
210             String[] tagsCategories, String[] tagsEntries, byte[] bytes)
211         throws PortalException, SystemException {
212 
213         InputStream is = new UnsyncByteArrayInputStream(bytes);
214 
215         try {
216             updateFile(
217                 companyId, portletId, groupId, repositoryId, fileName,
218                 versionNumber, sourceFileName, fileEntryId, properties,
219                 modifiedDate, tagsCategories, tagsEntries, is);
220         }
221         finally {
222             try {
223                 is.close();
224             }
225             catch (IOException ioe) {
226                 _log.error(ioe);
227             }
228         }
229     }
230 
231     public void updateFile(
232             long companyId, String portletId, long groupId, long repositoryId,
233             String fileName, double versionNumber, String sourceFileName,
234             long fileEntryId, String properties, Date modifiedDate,
235             String[] tagsCategories, String[] tagsEntries, File file)
236         throws PortalException, SystemException {
237 
238         InputStream is = null;
239 
240         try {
241             is = new UnsyncBufferedInputStream(new FileInputStream(file));
242 
243             updateFile(
244                 companyId, portletId, groupId, repositoryId, fileName,
245                 versionNumber, sourceFileName, fileEntryId, properties,
246                 modifiedDate, tagsCategories, tagsEntries, is);
247         }
248         catch (FileNotFoundException fnfe) {
249             throw new NoSuchFileException(fileName);
250         }
251         finally {
252             try {
253                 if (is != null) {
254                     is.close();
255                 }
256             }
257             catch (IOException ioe) {
258                 _log.error(ioe);
259             }
260         }
261     }
262 
263     public abstract void updateFile(
264             long companyId, String portletId, long groupId, long repositoryId,
265             String fileName, double versionNumber, String sourceFileName,
266             long fileEntryId, String properties, Date modifiedDate,
267             String[] tagsCategories, String[] tagsEntries, InputStream is)
268         throws PortalException, SystemException;
269 
270     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
271 
272 }