001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.documentlibrary.util;
022:
023: import com.liferay.documentlibrary.NoSuchFileException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.search.SearchException;
027: import com.liferay.util.FileUtil;
028:
029: import java.io.BufferedInputStream;
030: import java.io.ByteArrayInputStream;
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.FileNotFoundException;
034: import java.io.IOException;
035: import java.io.InputStream;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041:
042: * <a href="BaseHook.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public abstract class BaseHook implements Hook {
048:
049: public abstract void addDirectory(long companyId,
050: long repositoryId, String dirName) throws PortalException,
051: SystemException;
052:
053: public void addFile(long companyId, String portletId, long groupId,
054: long repositoryId, String fileName, String properties,
055: String[] tagsEntries, File file) throws PortalException,
056: SystemException {
057:
058: InputStream is = null;
059:
060: try {
061: is = new BufferedInputStream(new FileInputStream(file));
062:
063: addFile(companyId, portletId, groupId, repositoryId,
064: fileName, properties, tagsEntries, is);
065: } catch (FileNotFoundException fnfe) {
066: throw new NoSuchFileException(fileName);
067: } finally {
068: try {
069: if (is != null) {
070: is.close();
071: }
072: } catch (IOException ioe) {
073: _log.error(ioe);
074: }
075: }
076: }
077:
078: public void addFile(long companyId, String portletId, long groupId,
079: long repositoryId, String fileName, String properties,
080: String[] tagsEntries, byte[] byteArray)
081: throws PortalException, SystemException {
082:
083: InputStream is = new ByteArrayInputStream(byteArray);
084:
085: try {
086: addFile(companyId, portletId, groupId, repositoryId,
087: fileName, properties, tagsEntries, is);
088: } finally {
089: try {
090: is.close();
091: } catch (IOException ioe) {
092: _log.error(ioe);
093: }
094: }
095: }
096:
097: public abstract void addFile(long companyId, String portletId,
098: long groupId, long repositoryId, String fileName,
099: String properties, String[] tagsEntries, InputStream is)
100: throws PortalException, SystemException;
101:
102: public abstract void checkRoot(long companyId)
103: throws SystemException;
104:
105: public abstract void deleteDirectory(long companyId,
106: String portletId, long repositoryId, String dirName)
107: throws PortalException, SystemException;
108:
109: public abstract void deleteFile(long companyId, String portletId,
110: long repositoryId, String fileName) throws PortalException,
111: SystemException;
112:
113: public abstract void deleteFile(long companyId, String portletId,
114: long repositoryId, String fileName, double versionNumber)
115: throws PortalException, SystemException;
116:
117: public byte[] getFile(long companyId, long repositoryId,
118: String fileName) throws PortalException, SystemException {
119:
120: byte[] bytes = null;
121:
122: try {
123: InputStream is = getFileAsStream(companyId, repositoryId,
124: fileName);
125:
126: bytes = FileUtil.getBytes(is);
127: } catch (IOException ioe) {
128: throw new SystemException(ioe);
129: }
130:
131: return bytes;
132: }
133:
134: public byte[] getFile(long companyId, long repositoryId,
135: String fileName, double versionNumber)
136: throws PortalException, SystemException {
137:
138: byte[] bytes = null;
139:
140: try {
141: InputStream is = getFileAsStream(companyId, repositoryId,
142: fileName, versionNumber);
143:
144: bytes = FileUtil.getBytes(is);
145: } catch (IOException ioe) {
146: throw new SystemException(ioe);
147: }
148:
149: return bytes;
150: }
151:
152: public InputStream getFileAsStream(long companyId,
153: long repositoryId, String fileName) throws PortalException,
154: SystemException {
155:
156: return getFileAsStream(companyId, repositoryId, fileName, 0);
157: }
158:
159: public abstract InputStream getFileAsStream(long companyId,
160: long repositoryId, String fileName, double versionNumber)
161: throws PortalException, SystemException;
162:
163: public abstract String[] getFileNames(long companyId,
164: long repositoryId, String dirName) throws PortalException,
165: SystemException;
166:
167: public abstract long getFileSize(long companyId, long repositoryId,
168: String fileName) throws PortalException, SystemException;
169:
170: public abstract boolean hasFile(long companyId, long repositoryId,
171: String fileName, double versionNumber)
172: throws PortalException, SystemException;
173:
174: public abstract void move(String srcDir, String destDir)
175: throws SystemException;
176:
177: public abstract void reIndex(String[] ids) throws SearchException;
178:
179: public void updateFile(long companyId, String portletId,
180: long groupId, long repositoryId, String fileName,
181: double versionNumber, String sourceFileName,
182: String properties, String[] tagsEntries, File file)
183: throws PortalException, SystemException {
184:
185: InputStream is = null;
186:
187: try {
188: is = new BufferedInputStream(new FileInputStream(file));
189:
190: updateFile(companyId, portletId, groupId, repositoryId,
191: fileName, versionNumber, sourceFileName,
192: properties, tagsEntries, is);
193: } catch (FileNotFoundException fnfe) {
194: throw new NoSuchFileException(fileName);
195: } finally {
196: try {
197: if (is != null) {
198: is.close();
199: }
200: } catch (IOException ioe) {
201: _log.error(ioe);
202: }
203: }
204: }
205:
206: public void updateFile(long companyId, String portletId,
207: long groupId, long repositoryId, String fileName,
208: double versionNumber, String sourceFileName,
209: String properties, String[] tagsEntries, byte[] byteArray)
210: throws PortalException, SystemException {
211:
212: InputStream is = new ByteArrayInputStream(byteArray);
213:
214: try {
215: updateFile(companyId, portletId, groupId, repositoryId,
216: fileName, versionNumber, sourceFileName,
217: properties, tagsEntries, is);
218: } finally {
219: try {
220: is.close();
221: } catch (IOException ioe) {
222: _log.error(ioe);
223: }
224: }
225: }
226:
227: public abstract void updateFile(long companyId, String portletId,
228: long groupId, long repositoryId, String fileName,
229: double versionNumber, String sourceFileName,
230: String properties, String[] tagsEntries, InputStream is)
231: throws PortalException, SystemException;
232:
233: public abstract void updateFile(long companyId, String portletId,
234: long groupId, long repositoryId, long newRepositoryId,
235: String fileName) throws PortalException, SystemException;
236:
237: private static Log _log = LogFactory.getLog(BaseHook.class);
238:
239: }
|