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.service.impl;
016    
017    import com.liferay.portal.kernel.dao.jdbc.OutputBlob;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.StreamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portlet.documentlibrary.NoSuchContentException;
025    import com.liferay.portlet.documentlibrary.model.DLContent;
026    import com.liferay.portlet.documentlibrary.service.base.DLContentLocalServiceBaseImpl;
027    import com.liferay.portlet.documentlibrary.util.comparator.DLContentVersionComparator;
028    
029    import java.io.InputStream;
030    
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Shuyang Zhou
036     */
037    public class DLContentLocalServiceImpl extends DLContentLocalServiceBaseImpl {
038    
039            public DLContent addContent(
040                            long companyId, long repositoryId, String path, String version,
041                            byte[] bytes)
042                    throws SystemException {
043    
044                    long contentId = counterLocalService.increment();
045    
046                    DLContent dlContent = dlContentPersistence.create(contentId);
047    
048                    dlContent.setCompanyId(companyId);
049                    dlContent.setRepositoryId(repositoryId);
050                    dlContent.setPath(path);
051                    dlContent.setVersion(version);
052    
053                    UnsyncByteArrayInputStream unsyncByteArrayInputStream =
054                            new UnsyncByteArrayInputStream(bytes);
055    
056                    OutputBlob dataOutputBlob = new OutputBlob(
057                            unsyncByteArrayInputStream, bytes.length);
058    
059                    dlContent.setData(dataOutputBlob);
060    
061                    dlContent.setSize(bytes.length);
062    
063                    dlContentPersistence.update(dlContent, false);
064    
065                    return dlContent;
066            }
067    
068            public DLContent addContent(
069                            long companyId, long repositoryId, String path, String version,
070                            InputStream inputStream, long size)
071                    throws SystemException {
072    
073                    try {
074                            long contentId = counterLocalService.increment();
075    
076                            DLContent dlContent = dlContentPersistence.create(contentId);
077    
078                            dlContent.setCompanyId(companyId);
079                            dlContent.setRepositoryId(repositoryId);
080                            dlContent.setPath(path);
081                            dlContent.setVersion(version);
082    
083                            OutputBlob dataOutputBlob = new OutputBlob(inputStream, size);
084    
085                            dlContent.setData(dataOutputBlob);
086    
087                            dlContent.setSize(size);
088    
089                            dlContentPersistence.update(dlContent, false);
090    
091                            return dlContent;
092                    }
093                    finally {
094                            StreamUtil.cleanUp(inputStream);
095                    }
096            }
097    
098            public void deleteContent(
099                            long companyId, long repositoryId, String path, String version)
100                    throws PortalException, SystemException {
101    
102                    dlContentPersistence.removeByC_R_P_V(
103                            companyId, repositoryId, path, version);
104            }
105    
106            public void deleteContents(long companyId, long repositoryId, String path)
107                    throws SystemException {
108    
109                    dlContentPersistence.removeByC_R_P(companyId, repositoryId, path);
110            }
111    
112            public void deleteContentsByDirectory(
113                            long companyId, long repositoryId, String dirName)
114                    throws SystemException {
115    
116                    if (!dirName.endsWith(StringPool.SLASH)) {
117                            dirName = dirName.concat(StringPool.SLASH);
118                    }
119    
120                    dirName = dirName.concat(StringPool.PERCENT);
121    
122                    dlContentPersistence.removeByC_R_LikeP(
123                            companyId, repositoryId, dirName);
124            }
125    
126            public DLContent getContent(long companyId, long repositoryId, String path)
127                    throws NoSuchContentException, SystemException {
128    
129                    OrderByComparator orderByComparator = new DLContentVersionComparator();
130    
131                    List<DLContent> dlContents = dlContentPersistence.findByC_R_P(
132                            companyId, repositoryId, path, 0, 1, orderByComparator);
133    
134                    if (dlContents == null || dlContents.isEmpty()) {
135                            throw new NoSuchContentException(path);
136                    }
137    
138                    return dlContents.get(0);
139            }
140    
141            public DLContent getContent(
142                            long companyId, long repositoryId, String path, String version)
143                    throws NoSuchContentException, SystemException {
144    
145                    return dlContentPersistence.findByC_R_P_V(
146                            companyId, repositoryId, path, version);
147            }
148    
149            public List<DLContent> getContents(long companyId, long repositoryId)
150                    throws SystemException {
151    
152                    return dlContentPersistence.findByC_R(companyId, repositoryId);
153            }
154    
155            public List<DLContent> getContents(
156                            long companyId, long repositoryId, String path)
157                    throws SystemException {
158    
159                    return dlContentPersistence.findByC_R_P(companyId, repositoryId, path);
160            }
161    
162            public List<DLContent> getContentsByDirectory(
163                            long companyId, long repositoryId, String dirName)
164                    throws SystemException {
165    
166                    if (!dirName.endsWith(StringPool.SLASH)) {
167                            dirName = dirName.concat(StringPool.SLASH);
168                    }
169    
170                    dirName = dirName.concat(StringPool.PERCENT);
171    
172                    return dlContentPersistence.findByC_R_LikeP(
173                            companyId, repositoryId, dirName);
174            }
175    
176            public boolean hasContent(
177                            long companyId, long repositoryId, String path, String version)
178                    throws SystemException {
179    
180                    int count = dlContentPersistence.countByC_R_P_V(
181                            companyId, repositoryId, path, version);
182    
183                    if (count > 0) {
184                            return true;
185                    }
186                    else {
187                            return false;
188                    }
189            }
190    
191            public void updateDLContent(
192                            long companyId, long oldRepositoryId, long newRepositoryId,
193                            String oldPath, String newPath)
194                    throws SystemException {
195    
196                    List<DLContent> dlContents = dlContentPersistence.findByC_R_P(
197                            companyId, oldRepositoryId, oldPath);
198    
199                    for (DLContent dLContent : dlContents) {
200                            dLContent.setRepositoryId(newRepositoryId);
201                            dLContent.setPath(newPath);
202    
203                            dlContentPersistence.update(dLContent, false);
204                    }
205            }
206    
207    }