001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.store;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portlet.documentlibrary.DuplicateFileException;
025    import com.liferay.portlet.documentlibrary.model.DLContent;
026    import com.liferay.portlet.documentlibrary.service.DLContentLocalServiceUtil;
027    
028    import java.io.ByteArrayInputStream;
029    import java.io.File;
030    import java.io.FileInputStream;
031    import java.io.FileNotFoundException;
032    import java.io.IOException;
033    import java.io.InputStream;
034    
035    import java.nio.channels.FileChannel;
036    
037    import java.sql.Blob;
038    import java.sql.SQLException;
039    
040    import java.util.List;
041    
042    /**
043     * @author Shuyang Zhou
044     * @author Tina Tian
045     */
046    public class DBStore extends BaseStore {
047    
048            @Override
049            public void addDirectory(
050                    long companyId, long repositoryId, String dirName) {
051            }
052    
053            @Override
054            public void addFile(
055                            long companyId, long repositoryId, String fileName, byte[] bytes)
056                    throws PortalException, SystemException {
057    
058                    updateFile(
059                            companyId, repositoryId, fileName, Store.VERSION_DEFAULT, bytes);
060            }
061    
062            @Override
063            public void addFile(
064                            long companyId, long repositoryId, String fileName, File file)
065                    throws PortalException, SystemException {
066    
067                    updateFile(
068                            companyId, repositoryId, fileName, Store.VERSION_DEFAULT, file);
069            }
070    
071            @Override
072            public void addFile(
073                            long companyId, long repositoryId, String fileName,
074                            InputStream inputStream)
075                    throws PortalException, SystemException {
076    
077                    updateFile(
078                            companyId, repositoryId, fileName, Store.VERSION_DEFAULT,
079                            inputStream);
080            }
081    
082            @Override
083            public void checkRoot(long companyId) {
084            }
085    
086            @Override
087            public void deleteDirectory(
088                            long companyId, long repositoryId, String dirName)
089                    throws SystemException {
090    
091                    DLContentLocalServiceUtil.deleteContentsByDirectory(
092                            companyId, repositoryId, dirName);
093            }
094    
095            @Override
096            public void deleteFile(long companyId, long repositoryId, String fileName)
097                    throws SystemException {
098    
099                    DLContentLocalServiceUtil.deleteContents(
100                            companyId, repositoryId, fileName);
101            }
102    
103            @Override
104            public void deleteFile(
105                            long companyId, long repositoryId, String fileName,
106                            String versionLabel)
107                    throws PortalException, SystemException {
108    
109                    DLContentLocalServiceUtil.deleteContent(
110                            companyId, repositoryId, fileName, versionLabel);
111            }
112    
113            @Override
114            public InputStream getFileAsStream(
115                            long companyId, long repositoryId, String fileName)
116                    throws PortalException, SystemException {
117    
118                    DLContent dlContent = DLContentLocalServiceUtil.getContent(
119                            companyId, repositoryId, fileName);
120    
121                    dlContent.resetOriginalValues();
122    
123                    Blob blobData = dlContent.getData();
124    
125                    if (blobData == null) {
126                            if (_log.isWarnEnabled()) {
127                                    StringBundler sb = new StringBundler(9);
128    
129                                    sb.append("No blob data found for file {companyId=");
130                                    sb.append(companyId);
131                                    sb.append(", repositoryId=");
132                                    sb.append(repositoryId);
133                                    sb.append(", fileName=");
134                                    sb.append(fileName);
135                                    sb.append("}");
136    
137                                    _log.warn(sb.toString());
138                            }
139    
140                            return null;
141                    }
142    
143                    try {
144                            return blobData.getBinaryStream();
145                    }
146                    catch (SQLException sqle) {
147                            StringBundler sb = new StringBundler(7);
148    
149                            sb.append("Unable to load data binary stream for file {companyId=");
150                            sb.append(companyId);
151                            sb.append(", repositoryId=");
152                            sb.append(repositoryId);
153                            sb.append(", fileName=");
154                            sb.append(fileName);
155                            sb.append("}");
156    
157                            throw new SystemException(sb.toString(), sqle);
158                    }
159            }
160    
161            @Override
162            public InputStream getFileAsStream(
163                            long companyId, long repositoryId, String fileName,
164                            String versionLabel)
165                    throws PortalException, SystemException {
166    
167                    DLContent dlContent = DLContentLocalServiceUtil.getContent(
168                            companyId, repositoryId, fileName, versionLabel);
169    
170                    Blob blobData = dlContent.getData();
171    
172                    if (blobData == null) {
173                            if (_log.isWarnEnabled()) {
174                                    StringBundler sb = new StringBundler(9);
175    
176                                    sb.append("No blob data found for file {companyId=");
177                                    sb.append(companyId);
178                                    sb.append(", repositoryId=");
179                                    sb.append(repositoryId);
180                                    sb.append(", fileName=");
181                                    sb.append(fileName);
182                                    sb.append(", versionLabel=");
183                                    sb.append(versionLabel);
184                                    sb.append("}");
185    
186                                    _log.warn(sb.toString());
187                            }
188    
189                            return null;
190                    }
191    
192                    try {
193                            return blobData.getBinaryStream();
194                    }
195                    catch (SQLException sqle) {
196                            StringBundler sb = new StringBundler(9);
197    
198                            sb.append("Unable to load data binary stream for file {companyId=");
199                            sb.append(companyId);
200                            sb.append(", repositoryId=");
201                            sb.append(repositoryId);
202                            sb.append(", fileName=");
203                            sb.append(fileName);
204                            sb.append(", versionLabel=");
205                            sb.append(versionLabel);
206                            sb.append("}");
207    
208                            throw new SystemException(sb.toString(), sqle);
209                    }
210            }
211    
212            public String[] getFileNames(long companyId, long repositoryId)
213                    throws SystemException {
214    
215                    List<DLContent> dlContents = DLContentLocalServiceUtil.getContents(
216                            companyId, repositoryId);
217    
218                    String[] fileNames = new String[dlContents.size()];
219    
220                    for (int i = 0; i < dlContents.size(); i++) {
221                            DLContent dlContent = dlContents.get(i);
222    
223                            fileNames[i] = dlContent.getPath();
224                    }
225    
226                    return fileNames;
227            }
228    
229            @Override
230            public String[] getFileNames(
231                            long companyId, long repositoryId, String dirName)
232                    throws SystemException {
233    
234                    List<DLContent> dlContents =
235                            DLContentLocalServiceUtil.getContentsByDirectory(
236                                    companyId, repositoryId, dirName);
237    
238                    String[] fileNames = new String[dlContents.size()];
239    
240                    for (int i = 0; i < dlContents.size(); i++) {
241                            DLContent dlContent = dlContents.get(i);
242    
243                            fileNames[i] = dlContent.getPath();
244                    }
245    
246                    return fileNames;
247            }
248    
249            @Override
250            public long getFileSize(long companyId, long repositoryId, String fileName)
251                    throws PortalException, SystemException {
252    
253                    DLContent dlContent = DLContentLocalServiceUtil.getContent(
254                            companyId, repositoryId, fileName);
255    
256                    return dlContent.getSize();
257            }
258    
259            @Override
260            public boolean hasDirectory(
261                    long companyId, long repositoryId, String dirName) {
262    
263                    return true;
264            }
265    
266            @Override
267            public boolean hasFile(
268                            long companyId, long repositoryId, String fileName,
269                            String versionLabel)
270                    throws SystemException {
271    
272                    return DLContentLocalServiceUtil.hasContent(
273                            companyId, repositoryId, fileName, versionLabel);
274            }
275    
276            @Override
277            public void move(String srcDir, String destDir) {
278            }
279    
280            @Override
281            public void updateFile(
282                            long companyId, long repositoryId, long newRepositoryId,
283                            String fileName)
284                    throws SystemException {
285    
286                    DLContentLocalServiceUtil.updateDLContent(
287                            companyId, repositoryId, newRepositoryId, fileName, fileName);
288            }
289    
290            public void updateFile(
291                            long companyId, long repositoryId, String fileName,
292                            String newFileName)
293                    throws SystemException {
294    
295                    DLContentLocalServiceUtil.updateDLContent(
296                            companyId, repositoryId, repositoryId, fileName, newFileName);
297            }
298    
299            @Override
300            public void updateFile(
301                            long companyId, long repositoryId, String fileName,
302                            String versionLabel, byte[] bytes)
303                    throws PortalException, SystemException {
304    
305                    if (DLContentLocalServiceUtil.hasContent(
306                                    companyId, repositoryId, fileName, versionLabel)) {
307    
308                            throw new DuplicateFileException(fileName);
309                    }
310    
311                    DLContentLocalServiceUtil.addContent(
312                            companyId, repositoryId, fileName, versionLabel, bytes);
313            }
314    
315            @Override
316            public void updateFile(
317                            long companyId, long repositoryId, String fileName,
318                            String versionLabel, File file)
319                    throws PortalException, SystemException {
320    
321                    if (DLContentLocalServiceUtil.hasContent(
322                                    companyId, repositoryId, fileName, versionLabel)) {
323    
324                            throw new DuplicateFileException(fileName);
325                    }
326    
327                    InputStream inputStream = null;
328    
329                    try {
330                             inputStream = new FileInputStream(file);
331                    }
332                    catch (FileNotFoundException fnfe) {
333                            throw new SystemException(fnfe);
334                    }
335    
336                    DLContentLocalServiceUtil.addContent(
337                            companyId, repositoryId, fileName, versionLabel, inputStream,
338                            file.length());
339            }
340    
341            @Override
342            public void updateFile(
343                            long companyId, long repositoryId, String fileName,
344                            String versionLabel, InputStream inputStream)
345                    throws PortalException, SystemException {
346    
347                    if (DLContentLocalServiceUtil.hasContent(
348                                    companyId, repositoryId, fileName, versionLabel)) {
349    
350                            throw new DuplicateFileException(fileName);
351                    }
352    
353                    long length = -1;
354    
355                    if (inputStream instanceof ByteArrayInputStream) {
356                            ByteArrayInputStream byteArrayInputStream =
357                                    (ByteArrayInputStream)inputStream;
358    
359                            length = byteArrayInputStream.available();
360                    }
361                    else if (inputStream instanceof FileInputStream) {
362                            FileInputStream fileInputStream = (FileInputStream)inputStream;
363    
364                            FileChannel fileChannel = fileInputStream.getChannel();
365    
366                            try {
367                                    length = fileChannel.size();
368                            }
369                            catch (IOException ioe) {
370                                    if (_log.isWarnEnabled()) {
371                                            _log.warn(
372                                                    "Unable to detect file size from file channel", ioe);
373                                    }
374                            }
375                    }
376                    else if (inputStream instanceof UnsyncByteArrayInputStream) {
377                            UnsyncByteArrayInputStream unsyncByteArrayInputStream =
378                                    (UnsyncByteArrayInputStream)inputStream;
379    
380                            length = unsyncByteArrayInputStream.available();
381                    }
382    
383                    if (length >= 0) {
384                            DLContentLocalServiceUtil.addContent(
385                                    companyId, repositoryId, fileName, versionLabel, inputStream,
386                                    length);
387                    }
388                    else {
389                            if (_log.isWarnEnabled()) {
390                                    _log.warn(
391                                            "Unable to detect length from input stream. Reading " +
392                                                    "entire input stream into memory as a last resort.");
393                            }
394    
395                            byte[] bytes = null;
396    
397                            try {
398                                    bytes = FileUtil.getBytes(inputStream);
399                            }
400                            catch (IOException ioe) {
401                                    throw new SystemException(ioe);
402                            }
403    
404                            DLContentLocalServiceUtil.addContent(
405                                    companyId, repositoryId, fileName, versionLabel, bytes);
406                    }
407            }
408    
409            private static Log _log = LogFactoryUtil.getLog(DBStore.class);
410    
411    }