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