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