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[] tagsCategories, String[] tagsEntries,
60              byte[] bytes)
61          throws PortalException, SystemException {
62  
63          InputStream is = new ByteArrayInputStream(bytes);
64  
65          try {
66              addFile(
67                  companyId, portletId, groupId, repositoryId, fileName,
68                  fileEntryId, properties, modifiedDate, tagsCategories,
69                  tagsEntries, is);
70          }
71          finally {
72              try {
73                  is.close();
74              }
75              catch (IOException ioe) {
76                  _log.error(ioe);
77              }
78          }
79      }
80  
81      public void addFile(
82              long companyId, String portletId, long groupId, long repositoryId,
83              String fileName, long fileEntryId, String properties,
84              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
85              File file)
86          throws PortalException, SystemException {
87  
88          InputStream is = null;
89  
90          try {
91              is = new BufferedInputStream(new FileInputStream(file));
92  
93              addFile(
94                  companyId, portletId, groupId, repositoryId, fileName,
95                  fileEntryId, properties, modifiedDate, tagsCategories,
96                  tagsEntries, is);
97          }
98          catch (FileNotFoundException fnfe) {
99              throw new NoSuchFileException(fileName);
100         }
101         finally {
102             try {
103                 if (is != null) {
104                     is.close();
105                 }
106             }
107             catch (IOException ioe) {
108                 _log.error(ioe);
109             }
110         }
111     }
112 
113     public abstract void addFile(
114             long companyId, String portletId, long groupId, long repositoryId,
115             String fileName, long fileEntryId, String properties,
116             Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
117             InputStream is)
118         throws PortalException, SystemException;
119 
120     public abstract void checkRoot(long companyId) throws SystemException;
121 
122     public abstract void deleteDirectory(
123             long companyId, String portletId, long repositoryId, String dirName)
124         throws PortalException, SystemException;
125 
126     public abstract void deleteFile(
127             long companyId, String portletId, long repositoryId,
128             String fileName)
129         throws PortalException, SystemException;
130 
131     public abstract void deleteFile(
132             long companyId, String portletId, long repositoryId,
133             String fileName, double versionNumber)
134         throws PortalException, SystemException;
135 
136     public byte[] getFile(long companyId, long repositoryId, String fileName)
137         throws PortalException, SystemException {
138 
139         byte[] bytes = null;
140 
141         try {
142             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
143 
144             bytes = FileUtil.getBytes(is);
145         }
146         catch (IOException ioe) {
147             throw new SystemException(ioe);
148         }
149 
150         return bytes;
151     }
152 
153     public byte[] getFile(
154             long companyId, long repositoryId, String fileName,
155             double versionNumber)
156         throws PortalException, SystemException {
157 
158         byte[] bytes = null;
159 
160         try {
161             InputStream is = getFileAsStream(
162                 companyId, repositoryId, fileName, versionNumber);
163 
164             bytes = FileUtil.getBytes(is);
165         }
166         catch (IOException ioe) {
167             throw new SystemException(ioe);
168         }
169 
170         return bytes;
171     }
172 
173     public InputStream getFileAsStream(
174             long companyId, long repositoryId, String fileName)
175         throws PortalException, SystemException {
176 
177         return getFileAsStream(companyId, repositoryId, fileName, 0);
178     }
179 
180     public abstract InputStream getFileAsStream(
181             long companyId, long repositoryId, String fileName,
182             double versionNumber)
183         throws PortalException, SystemException;
184 
185     public abstract String[] getFileNames(
186             long companyId, long repositoryId, String dirName)
187         throws PortalException, SystemException;
188 
189     public abstract long getFileSize(
190             long companyId, long repositoryId, String fileName)
191         throws PortalException, SystemException;
192 
193     public abstract boolean hasFile(
194             long companyId, long repositoryId, String fileName,
195             double versionNumber)
196         throws PortalException, SystemException;
197 
198     public abstract void move(String srcDir, String destDir)
199         throws SystemException;
200 
201     public abstract void reIndex(String[] ids) throws SearchException;
202 
203     public void updateFile(
204             long companyId, String portletId, long groupId, long repositoryId,
205             String fileName, double versionNumber, String sourceFileName,
206             long fileEntryId, String properties, Date modifiedDate,
207             String[] tagsCategories, String[] tagsEntries, byte[] bytes)
208         throws PortalException, SystemException {
209 
210         InputStream is = new ByteArrayInputStream(bytes);
211 
212         try {
213             updateFile(
214                 companyId, portletId, groupId, repositoryId, fileName,
215                 versionNumber, sourceFileName, fileEntryId, properties,
216                 modifiedDate, tagsCategories, tagsEntries, is);
217         }
218         finally {
219             try {
220                 is.close();
221             }
222             catch (IOException ioe) {
223                 _log.error(ioe);
224             }
225         }
226     }
227 
228     public void updateFile(
229             long companyId, String portletId, long groupId, long repositoryId,
230             String fileName, double versionNumber, String sourceFileName,
231             long fileEntryId, String properties, Date modifiedDate,
232             String[] tagsCategories, String[] tagsEntries, File file)
233         throws PortalException, SystemException {
234 
235         InputStream is = null;
236 
237         try {
238             is = new BufferedInputStream(new FileInputStream(file));
239 
240             updateFile(
241                 companyId, portletId, groupId, repositoryId, fileName,
242                 versionNumber, sourceFileName, fileEntryId, properties,
243                 modifiedDate, tagsCategories, tagsEntries, is);
244         }
245         catch (FileNotFoundException fnfe) {
246             throw new NoSuchFileException(fileName);
247         }
248         finally {
249             try {
250                 if (is != null) {
251                     is.close();
252                 }
253             }
254             catch (IOException ioe) {
255                 _log.error(ioe);
256             }
257         }
258     }
259 
260     public abstract void updateFile(
261             long companyId, String portletId, long groupId, long repositoryId,
262             String fileName, double versionNumber, String sourceFileName,
263             long fileEntryId, String properties, Date modifiedDate,
264             String[] tagsCategories, String[] tagsEntries, InputStream is)
265         throws PortalException, SystemException;
266 
267     public abstract void updateFile(
268             long companyId, String portletId, long groupId, long repositoryId,
269             long newRepositoryId, String fileName, long fileEntryId)
270         throws PortalException, SystemException;
271 
272     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
273 
274 }