1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.documentlibrary.util;
21  
22  import com.liferay.documentlibrary.NoSuchFileException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.SearchException;
28  import com.liferay.portal.kernel.util.FileUtil;
29  
30  import java.io.BufferedInputStream;
31  import java.io.ByteArrayInputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.FileNotFoundException;
35  import java.io.IOException;
36  import java.io.InputStream;
37  
38  import java.util.Date;
39  
40  /**
41  
42   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public abstract class BaseHook implements Hook {
48  
49      public abstract void addDirectory(
50              long companyId, long repositoryId, String dirName)
51          throws PortalException, SystemException;
52  
53      public void addFile(
54              long companyId, String portletId, long groupId, long repositoryId,
55              String fileName, String properties, Date modifiedDate,
56              String[] tagsEntries, byte[] bytes)
57          throws PortalException, SystemException {
58  
59          InputStream is = new ByteArrayInputStream(bytes);
60  
61          try {
62              addFile(
63                  companyId, portletId, groupId, repositoryId, fileName,
64                  properties, modifiedDate, tagsEntries, is);
65          }
66          finally {
67              try {
68                  is.close();
69              }
70              catch (IOException ioe) {
71                  _log.error(ioe);
72              }
73          }
74      }
75  
76      public void addFile(
77              long companyId, String portletId, long groupId, long repositoryId,
78              String fileName, String properties, Date modifiedDate,
79              String[] tagsEntries, File file)
80          throws PortalException, SystemException {
81  
82          InputStream is = null;
83  
84          try {
85              is = new BufferedInputStream(new FileInputStream(file));
86  
87              addFile(
88                  companyId, portletId, groupId, repositoryId, fileName,
89                  properties, modifiedDate, tagsEntries, is);
90          }
91          catch (FileNotFoundException fnfe) {
92              throw new NoSuchFileException(fileName);
93          }
94          finally {
95              try {
96                  if (is != null) {
97                      is.close();
98                  }
99              }
100             catch (IOException ioe) {
101                 _log.error(ioe);
102             }
103         }
104     }
105 
106     public abstract void addFile(
107             long companyId, String portletId, long groupId, long repositoryId,
108             String fileName, String properties, Date modifiedDate,
109             String[] tagsEntries, InputStream is)
110         throws PortalException, SystemException;
111 
112     public abstract void checkRoot(long companyId) throws SystemException;
113 
114     public abstract void deleteDirectory(
115             long companyId, String portletId, long repositoryId, String dirName)
116         throws PortalException, SystemException;
117 
118     public abstract void deleteFile(
119             long companyId, String portletId, long repositoryId,
120             String fileName)
121         throws PortalException, SystemException;
122 
123     public abstract void deleteFile(
124             long companyId, String portletId, long repositoryId,
125             String fileName, double versionNumber)
126         throws PortalException, SystemException;
127 
128     public byte[] getFile(long companyId, long repositoryId, String fileName)
129         throws PortalException, SystemException {
130 
131         byte[] bytes = null;
132 
133         try {
134             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
135 
136             bytes = FileUtil.getBytes(is);
137         }
138         catch (IOException ioe) {
139             throw new SystemException(ioe);
140         }
141 
142         return bytes;
143     }
144 
145     public byte[] getFile(
146             long companyId, long repositoryId, String fileName,
147             double versionNumber)
148         throws PortalException, SystemException {
149 
150         byte[] bytes = null;
151 
152         try {
153             InputStream is = getFileAsStream(
154                 companyId, repositoryId, fileName, versionNumber);
155 
156             bytes = FileUtil.getBytes(is);
157         }
158         catch (IOException ioe) {
159             throw new SystemException(ioe);
160         }
161 
162         return bytes;
163     }
164 
165     public InputStream getFileAsStream(
166             long companyId, long repositoryId, String fileName)
167         throws PortalException, SystemException {
168 
169         return getFileAsStream(companyId, repositoryId, fileName, 0);
170     }
171 
172     public abstract InputStream getFileAsStream(
173             long companyId, long repositoryId, String fileName,
174             double versionNumber)
175         throws PortalException, SystemException;
176 
177     public abstract String[] getFileNames(
178             long companyId, long repositoryId, String dirName)
179         throws PortalException, SystemException;
180 
181     public abstract long getFileSize(
182             long companyId, long repositoryId, String fileName)
183         throws PortalException, SystemException;
184 
185     public abstract boolean hasFile(
186             long companyId, long repositoryId, String fileName,
187             double versionNumber)
188         throws PortalException, SystemException;
189 
190     public abstract void move(String srcDir, String destDir)
191         throws SystemException;
192 
193     public abstract void reIndex(String[] ids) throws SearchException;
194 
195     public void updateFile(
196             long companyId, String portletId, long groupId, long repositoryId,
197             String fileName, double versionNumber, String sourceFileName,
198             String properties, Date modifiedDate, String[] tagsEntries,
199             byte[] bytes)
200         throws PortalException, SystemException {
201 
202         InputStream is = new ByteArrayInputStream(bytes);
203 
204         try {
205             updateFile(
206                 companyId, portletId, groupId, repositoryId, fileName,
207                 versionNumber, sourceFileName, properties, modifiedDate,
208                 tagsEntries, is);
209         }
210         finally {
211             try {
212                 is.close();
213             }
214             catch (IOException ioe) {
215                 _log.error(ioe);
216             }
217         }
218     }
219 
220     public void updateFile(
221             long companyId, String portletId, long groupId, long repositoryId,
222             String fileName, double versionNumber, String sourceFileName,
223             String properties, Date modifiedDate, String[] tagsEntries,
224             File file)
225         throws PortalException, SystemException {
226 
227         InputStream is = null;
228 
229         try {
230             is = new BufferedInputStream(new FileInputStream(file));
231 
232             updateFile(
233                 companyId, portletId, groupId, repositoryId, fileName,
234                 versionNumber, sourceFileName, properties, modifiedDate,
235                 tagsEntries, is);
236         }
237         catch (FileNotFoundException fnfe) {
238             throw new NoSuchFileException(fileName);
239         }
240         finally {
241             try {
242                 if (is != null) {
243                     is.close();
244                 }
245             }
246             catch (IOException ioe) {
247                 _log.error(ioe);
248             }
249         }
250     }
251 
252     public abstract void updateFile(
253             long companyId, String portletId, long groupId, long repositoryId,
254             String fileName, double versionNumber, String sourceFileName,
255             String properties, Date modifiedDate, String[] tagsEntries,
256             InputStream is)
257         throws PortalException, SystemException;
258 
259     public abstract void updateFile(
260             long companyId, String portletId, long groupId, long repositoryId,
261             long newRepositoryId, String fileName)
262         throws PortalException, SystemException;
263 
264     private static Log _log = LogFactoryUtil.getLog(BaseHook.class);
265 
266 }