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