1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.documentlibrary.NoSuchFileException;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.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  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import java.util.Date;
34  
35  /**
36   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public abstract class BaseHook implements Hook {
41  
42      public abstract void addDirectory(
43              long companyId, long repositoryId, String dirName)
44          throws PortalException, SystemException;
45  
46      public void addFile(
47              long companyId, String portletId, long groupId, long repositoryId,
48              String fileName, long fileEntryId, String properties,
49              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
50              byte[] bytes)
51          throws PortalException, SystemException {
52  
53          InputStream is = new UnsyncByteArrayInputStream(bytes);
54  
55          try {
56              addFile(
57                  companyId, portletId, groupId, repositoryId, fileName,
58                  fileEntryId, properties, modifiedDate, tagsCategories,
59                  tagsEntries, 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, String[] tagsCategories, String[] tagsEntries,
75              File file)
76          throws PortalException, SystemException {
77  
78          InputStream is = null;
79  
80          try {
81              is = new UnsyncBufferedInputStream(new FileInputStream(file));
82  
83              addFile(
84                  companyId, portletId, groupId, repositoryId, fileName,
85                  fileEntryId, properties, modifiedDate, tagsCategories,
86                  tagsEntries, is);
87          }
88          catch (FileNotFoundException fnfe) {
89              throw new NoSuchFileException(fileName);
90          }
91          finally {
92              try {
93                  if (is != null) {
94                      is.close();
95                  }
96              }
97              catch (IOException ioe) {
98                  _log.error(ioe);
99              }
100         }
101     }
102 
103     public abstract void addFile(
104             long companyId, String portletId, long groupId, long repositoryId,
105             String fileName, long fileEntryId, String properties,
106             Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
107             InputStream is)
108         throws PortalException, SystemException;
109 
110     public abstract void checkRoot(long companyId) throws SystemException;
111 
112     public abstract void deleteDirectory(
113             long companyId, String portletId, long repositoryId, String dirName)
114         throws PortalException, SystemException;
115 
116     public abstract void deleteFile(
117             long companyId, String portletId, long repositoryId,
118             String fileName)
119         throws PortalException, SystemException;
120 
121     public abstract void deleteFile(
122             long companyId, String portletId, long repositoryId,
123             String fileName, double versionNumber)
124         throws PortalException, SystemException;
125 
126     public byte[] getFile(long companyId, long repositoryId, String fileName)
127         throws PortalException, SystemException {
128 
129         byte[] bytes = null;
130 
131         try {
132             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
133 
134             bytes = FileUtil.getBytes(is);
135         }
136         catch (IOException ioe) {
137             throw new SystemException(ioe);
138         }
139 
140         return bytes;
141     }
142 
143     public byte[] getFile(
144             long companyId, long repositoryId, String fileName,
145             double versionNumber)
146         throws PortalException, SystemException {
147 
148         byte[] bytes = null;
149 
150         try {
151             InputStream is = getFileAsStream(
152                 companyId, repositoryId, fileName, versionNumber);
153 
154             bytes = FileUtil.getBytes(is);
155         }
156         catch (IOException ioe) {
157             throw new SystemException(ioe);
158         }
159 
160         return bytes;
161     }
162 
163     public InputStream getFileAsStream(
164             long companyId, long repositoryId, String fileName)
165         throws PortalException, SystemException {
166 
167         return getFileAsStream(companyId, repositoryId, fileName, 0);
168     }
169 
170     public abstract InputStream getFileAsStream(
171             long companyId, long repositoryId, String fileName,
172             double versionNumber)
173         throws PortalException, SystemException;
174 
175     public abstract String[] getFileNames(
176             long companyId, long repositoryId, String dirName)
177         throws PortalException, SystemException;
178 
179     public abstract long getFileSize(
180             long companyId, long repositoryId, String fileName)
181         throws PortalException, SystemException;
182 
183     public abstract boolean hasFile(
184             long companyId, long repositoryId, String fileName,
185             double versionNumber)
186         throws PortalException, SystemException;
187 
188     public abstract void move(String srcDir, String destDir)
189         throws SystemException;
190 
191     public abstract void reIndex(String[] ids) throws SearchException;
192 
193     public abstract void updateFile(
194             long companyId, String portletId, long groupId, long repositoryId,
195             long newRepositoryId, String fileName, long fileEntryId)
196         throws PortalException, SystemException;
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[] tagsCategories, String[] tagsEntries, byte[] bytes)
203         throws PortalException, SystemException {
204 
205         InputStream is = new UnsyncByteArrayInputStream(bytes);
206 
207         try {
208             updateFile(
209                 companyId, portletId, groupId, repositoryId, fileName,
210                 versionNumber, sourceFileName, fileEntryId, properties,
211                 modifiedDate, tagsCategories, 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[] tagsCategories, String[] tagsEntries, File file)
228         throws PortalException, SystemException {
229 
230         InputStream is = null;
231 
232         try {
233             is = new UnsyncBufferedInputStream(new FileInputStream(file));
234 
235             updateFile(
236                 companyId, portletId, groupId, repositoryId, fileName,
237                 versionNumber, sourceFileName, fileEntryId, properties,
238                 modifiedDate, tagsCategories, 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[] tagsCategories, String[] tagsEntries, InputStream is)
260         throws PortalException, SystemException;
261 
262     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
263 
264 }