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