001    /**
002     * Copyright (c) 2000-2013 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            @Override
040            public DLContent addContent(
041                            long companyId, long repositoryId, String path, String version,
042                            byte[] bytes)
043                    throws SystemException {
044    
045                    long contentId = counterLocalService.increment();
046    
047                    DLContent dlContent = dlContentPersistence.create(contentId);
048    
049                    dlContent.setCompanyId(companyId);
050                    dlContent.setRepositoryId(repositoryId);
051                    dlContent.setPath(path);
052                    dlContent.setVersion(version);
053    
054                    UnsyncByteArrayInputStream unsyncByteArrayInputStream =
055                            new UnsyncByteArrayInputStream(bytes);
056    
057                    OutputBlob dataOutputBlob = new OutputBlob(
058                            unsyncByteArrayInputStream, bytes.length);
059    
060                    dlContent.setData(dataOutputBlob);
061    
062                    dlContent.setSize(bytes.length);
063    
064                    dlContentPersistence.update(dlContent, false);
065    
066                    return dlContent;
067            }
068    
069            @Override
070            public DLContent addContent(
071                            long companyId, long repositoryId, String path, String version,
072                            InputStream inputStream, long size)
073                    throws SystemException {
074    
075                    try {
076                            long contentId = counterLocalService.increment();
077    
078                            DLContent dlContent = dlContentPersistence.create(contentId);
079    
080                            dlContent.setCompanyId(companyId);
081                            dlContent.setRepositoryId(repositoryId);
082                            dlContent.setPath(path);
083                            dlContent.setVersion(version);
084    
085                            OutputBlob dataOutputBlob = new OutputBlob(inputStream, size);
086    
087                            dlContent.setData(dataOutputBlob);
088    
089                            dlContent.setSize(size);
090    
091                            dlContentPersistence.update(dlContent, false);
092    
093                            return dlContent;
094                    }
095                    finally {
096                            StreamUtil.cleanUp(inputStream);
097                    }
098            }
099    
100            @Override
101            public void deleteContent(
102                            long companyId, long repositoryId, String path, String version)
103                    throws PortalException, SystemException {
104    
105                    dlContentPersistence.removeByC_R_P_V(
106                            companyId, repositoryId, path, version);
107            }
108    
109            @Override
110            public void deleteContents(long companyId, long repositoryId, String path)
111                    throws SystemException {
112    
113                    dlContentPersistence.removeByC_R_P(companyId, repositoryId, path);
114            }
115    
116            @Override
117            public void deleteContentsByDirectory(
118                            long companyId, long repositoryId, String dirName)
119                    throws SystemException {
120    
121                    if (!dirName.endsWith(StringPool.SLASH)) {
122                            dirName = dirName.concat(StringPool.SLASH);
123                    }
124    
125                    dirName = dirName.concat(StringPool.PERCENT);
126    
127                    dlContentPersistence.removeByC_R_LikeP(
128                            companyId, repositoryId, dirName);
129            }
130    
131            @Override
132            public DLContent getContent(long companyId, long repositoryId, String path)
133                    throws NoSuchContentException, SystemException {
134    
135                    OrderByComparator orderByComparator = new DLContentVersionComparator();
136    
137                    List<DLContent> dlContents = dlContentPersistence.findByC_R_P(
138                            companyId, repositoryId, path, 0, 1, orderByComparator);
139    
140                    if ((dlContents == null) || dlContents.isEmpty()) {
141                            throw new NoSuchContentException(path);
142                    }
143    
144                    return dlContents.get(0);
145            }
146    
147            @Override
148            public DLContent getContent(
149                            long companyId, long repositoryId, String path, String version)
150                    throws NoSuchContentException, SystemException {
151    
152                    return dlContentPersistence.findByC_R_P_V(
153                            companyId, repositoryId, path, version);
154            }
155    
156            @Override
157            public List<DLContent> getContents(long companyId, long repositoryId)
158                    throws SystemException {
159    
160                    return dlContentPersistence.findByC_R(companyId, repositoryId);
161            }
162    
163            @Override
164            public List<DLContent> getContents(
165                            long companyId, long repositoryId, String path)
166                    throws SystemException {
167    
168                    return dlContentPersistence.findByC_R_P(companyId, repositoryId, path);
169            }
170    
171            @Override
172            public List<DLContent> getContentsByDirectory(
173                            long companyId, long repositoryId, String dirName)
174                    throws SystemException {
175    
176                    if (!dirName.endsWith(StringPool.SLASH)) {
177                            dirName = dirName.concat(StringPool.SLASH);
178                    }
179    
180                    dirName = dirName.concat(StringPool.PERCENT);
181    
182                    return dlContentPersistence.findByC_R_LikeP(
183                            companyId, repositoryId, dirName);
184            }
185    
186            @Override
187            public boolean hasContent(
188                            long companyId, long repositoryId, String path, String version)
189                    throws SystemException {
190    
191                    int count = dlContentPersistence.countByC_R_P_V(
192                            companyId, repositoryId, path, version);
193    
194                    if (count > 0) {
195                            return true;
196                    }
197                    else {
198                            return false;
199                    }
200            }
201    
202            @Override
203            public void updateDLContent(
204                            long companyId, long oldRepositoryId, long newRepositoryId,
205                            String oldPath, String newPath)
206                    throws SystemException {
207    
208                    List<DLContent> dlContents = dlContentPersistence.findByC_R_P(
209                            companyId, oldRepositoryId, oldPath);
210    
211                    for (DLContent dLContent : dlContents) {
212                            dLContent.setRepositoryId(newRepositoryId);
213                            dLContent.setPath(newPath);
214    
215                            dlContentPersistence.update(dLContent, false);
216                    }
217            }
218    
219    }