1   /**
2    * Copyright (c) 2000-2008 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.search.SearchException;
29  import com.liferay.util.FileUtil;
30  
31  import java.io.BufferedInputStream;
32  import java.io.ByteArrayInputStream;
33  import java.io.File;
34  import java.io.FileInputStream;
35  import java.io.FileNotFoundException;
36  import java.io.IOException;
37  import java.io.InputStream;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  /**
43  
44   * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public abstract class BaseHook implements Hook {
50  
51      public abstract void addDirectory(
52              long companyId, long repositoryId, String dirName)
53          throws PortalException, SystemException;
54  
55      public void addFile(
56              long companyId, String portletId, long groupId, long repositoryId,
57              String fileName, String properties, String[] tagsEntries,
58              File file)
59          throws PortalException, SystemException {
60  
61          InputStream is = null;
62  
63          try {
64              is = new BufferedInputStream(new FileInputStream(file));
65  
66              addFile(
67                  companyId, portletId, groupId, repositoryId, fileName,
68                  properties, tagsEntries, is);
69          }
70          catch (FileNotFoundException fnfe) {
71              throw new NoSuchFileException(fileName);
72          }
73          finally {
74              try {
75                  if (is != null) {
76                      is.close();
77                  }
78              }
79              catch (IOException ioe) {
80                  _log.error(ioe);
81              }
82          }
83      }
84  
85      public void addFile(
86              long companyId, String portletId, long groupId, long repositoryId,
87              String fileName, String properties, String[] tagsEntries,
88              byte[] byteArray)
89          throws PortalException, SystemException {
90  
91          InputStream is = new ByteArrayInputStream(byteArray);
92  
93          try {
94              addFile(
95                  companyId, portletId, groupId, repositoryId, fileName,
96                  properties, tagsEntries, is);
97          }
98          finally {
99              try {
100                 is.close();
101             }
102             catch (IOException ioe) {
103                 _log.error(ioe);
104             }
105         }
106     }
107 
108     public abstract void addFile(
109             long companyId, String portletId, long groupId, long repositoryId,
110             String fileName, String properties, String[] tagsEntries,
111             InputStream is)
112         throws PortalException, SystemException;
113 
114     public abstract void checkRoot(long companyId) throws SystemException;
115 
116     public abstract void deleteDirectory(
117             long companyId, String portletId, long repositoryId, String dirName)
118         throws PortalException, SystemException;
119 
120     public abstract void deleteFile(
121             long companyId, String portletId, long repositoryId,
122             String fileName)
123         throws PortalException, SystemException;
124 
125     public abstract void deleteFile(
126             long companyId, String portletId, long repositoryId,
127             String fileName, double versionNumber)
128         throws PortalException, SystemException;
129 
130     public byte[] getFile(long companyId, long repositoryId, String fileName)
131         throws PortalException, SystemException {
132 
133         byte[] bytes = null;
134 
135         try {
136             InputStream is = getFileAsStream(companyId, repositoryId, fileName);
137 
138             bytes = FileUtil.getBytes(is);
139         }
140         catch (IOException ioe) {
141             throw new SystemException(ioe);
142         }
143 
144         return bytes;
145     }
146 
147     public byte[] getFile(
148             long companyId, long repositoryId, String fileName,
149             double versionNumber)
150         throws PortalException, SystemException {
151 
152         byte[] bytes = null;
153 
154         try {
155             InputStream is = getFileAsStream(
156                 companyId, repositoryId, fileName, versionNumber);
157 
158             bytes = FileUtil.getBytes(is);
159         }
160         catch (IOException ioe) {
161             throw new SystemException(ioe);
162         }
163 
164         return bytes;
165     }
166 
167     public InputStream getFileAsStream(
168             long companyId, long repositoryId, String fileName)
169         throws PortalException, SystemException {
170 
171         return getFileAsStream(companyId, repositoryId, fileName, 0);
172     }
173 
174     public abstract InputStream getFileAsStream(
175             long companyId, long repositoryId, String fileName,
176             double versionNumber)
177         throws PortalException, SystemException;
178 
179     public abstract String[] getFileNames(
180             long companyId, long repositoryId, String dirName)
181         throws PortalException, SystemException;
182 
183     public abstract long getFileSize(
184             long companyId, long repositoryId, String fileName)
185         throws PortalException, SystemException;
186 
187     public abstract boolean hasFile(
188             long companyId, long repositoryId, String fileName,
189             double versionNumber)
190         throws PortalException, SystemException;
191 
192     public abstract void move(String srcDir, String destDir)
193         throws SystemException;
194 
195     public abstract void reIndex(String[] ids) throws SearchException;
196 
197     public void updateFile(
198             long companyId, String portletId, long groupId, long repositoryId,
199             String fileName, double versionNumber, String sourceFileName,
200             String properties, String[] tagsEntries, File file)
201         throws PortalException, SystemException {
202 
203         InputStream is = null;
204 
205         try {
206             is = new BufferedInputStream(new FileInputStream(file));
207 
208             updateFile(
209                 companyId, portletId, groupId, repositoryId, fileName,
210                 versionNumber, sourceFileName, properties, tagsEntries, is);
211         }
212         catch (FileNotFoundException fnfe) {
213             throw new NoSuchFileException(fileName);
214         }
215         finally {
216             try {
217                 if (is != null) {
218                     is.close();
219                 }
220             }
221             catch (IOException ioe) {
222                 _log.error(ioe);
223             }
224         }
225     }
226 
227     public void updateFile(
228             long companyId, String portletId, long groupId, long repositoryId,
229             String fileName, double versionNumber, String sourceFileName,
230             String properties, String[] tagsEntries, byte[] byteArray)
231         throws PortalException, SystemException {
232 
233         InputStream is = new ByteArrayInputStream(byteArray);
234 
235         try {
236             updateFile(
237                 companyId, portletId, groupId, repositoryId, fileName,
238                 versionNumber, sourceFileName, properties, tagsEntries, is);
239         }
240         finally {
241             try {
242                 is.close();
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, String[] tagsEntries, InputStream is)
254         throws PortalException, SystemException;
255 
256     public abstract void updateFile(
257             long companyId, String portletId, long groupId, long repositoryId,
258             long newRepositoryId, String fileName)
259         throws PortalException, SystemException;
260 
261     private static Log _log = LogFactory.getLog(BaseHook.class);
262 
263 }