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.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   * <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 ByteArrayInputStream(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 BufferedInputStream(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 void updateFile(
202             long companyId, String portletId, long groupId, long repositoryId,
203             String fileName, double versionNumber, String sourceFileName,
204             long fileEntryId, String properties, Date modifiedDate,
205             String[] tagsCategories, String[] tagsEntries, byte[] bytes)
206         throws PortalException, SystemException {
207 
208         InputStream is = new ByteArrayInputStream(bytes);
209 
210         try {
211             updateFile(
212                 companyId, portletId, groupId, repositoryId, fileName,
213                 versionNumber, sourceFileName, fileEntryId, properties,
214                 modifiedDate, tagsCategories, tagsEntries, is);
215         }
216         finally {
217             try {
218                 is.close();
219             }
220             catch (IOException ioe) {
221                 _log.error(ioe);
222             }
223         }
224     }
225 
226     public void updateFile(
227             long companyId, String portletId, long groupId, long repositoryId,
228             String fileName, double versionNumber, String sourceFileName,
229             long fileEntryId, String properties, Date modifiedDate,
230             String[] tagsCategories, String[] tagsEntries, File file)
231         throws PortalException, SystemException {
232 
233         InputStream is = null;
234 
235         try {
236             is = new BufferedInputStream(new FileInputStream(file));
237 
238             updateFile(
239                 companyId, portletId, groupId, repositoryId, fileName,
240                 versionNumber, sourceFileName, fileEntryId, properties,
241                 modifiedDate, tagsCategories, tagsEntries, is);
242         }
243         catch (FileNotFoundException fnfe) {
244             throw new NoSuchFileException(fileName);
245         }
246         finally {
247             try {
248                 if (is != null) {
249                     is.close();
250                 }
251             }
252             catch (IOException ioe) {
253                 _log.error(ioe);
254             }
255         }
256     }
257 
258     public abstract void updateFile(
259             long companyId, String portletId, long groupId, long repositoryId,
260             String fileName, double versionNumber, String sourceFileName,
261             long fileEntryId, String properties, Date modifiedDate,
262             String[] tagsCategories, String[] tagsEntries, InputStream is)
263         throws PortalException, SystemException;
264 
265     public abstract void updateFile(
266             long companyId, String portletId, long groupId, long repositoryId,
267             long newRepositoryId, String fileName, long fileEntryId)
268         throws PortalException, SystemException;
269 
270     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
271 
272 }