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.portal.service.impl;
016    
017    import com.liferay.portal.NoSuchRepositoryException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.BaseRepository;
021    import com.liferay.portal.kernel.repository.LocalRepository;
022    import com.liferay.portal.kernel.repository.RepositoryException;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.ClassName;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Repository;
027    import com.liferay.portal.repository.util.ExternalRepositoryFactoryUtil;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.base.RepositoryServiceBaseImpl;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
033    import com.liferay.portlet.documentlibrary.model.DLFolder;
034    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
035    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
036    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
037    
038    /**
039     * @author Alexander Chow
040     * @author Mika Koivisto
041     */
042    public class RepositoryServiceImpl extends RepositoryServiceBaseImpl {
043    
044            @Override
045            public Repository addRepository(
046                            long groupId, long classNameId, long parentFolderId, String name,
047                            String description, String portletId,
048                            UnicodeProperties typeSettingsProperties,
049                            ServiceContext serviceContext)
050                    throws PortalException {
051    
052                    DLPermission.check(
053                            getPermissionChecker(), groupId, ActionKeys.ADD_REPOSITORY);
054    
055                    return repositoryLocalService.addRepository(
056                            getUserId(), groupId, classNameId, parentFolderId, name,
057                            description, portletId, typeSettingsProperties, false,
058                            serviceContext);
059            }
060    
061            @Override
062            public void checkRepository(long repositoryId) throws PortalException {
063                    checkRepository(repositoryId, 0, 0, 0);
064            }
065    
066            @Override
067            public void deleteRepository(long repositoryId) throws PortalException {
068                    Repository repository = repositoryPersistence.findByPrimaryKey(
069                            repositoryId);
070    
071                    DLFolderPermission.check(
072                            getPermissionChecker(), repository.getGroupId(),
073                            repository.getDlFolderId(), ActionKeys.DELETE);
074    
075                    repositoryLocalService.deleteRepository(repository.getRepositoryId());
076            }
077    
078            @Override
079            public LocalRepository getLocalRepositoryImpl(long repositoryId)
080                    throws PortalException {
081    
082                    checkRepository(repositoryId);
083    
084                    return repositoryLocalService.getLocalRepositoryImpl(repositoryId);
085            }
086    
087            @Override
088            public LocalRepository getLocalRepositoryImpl(
089                            long folderId, long fileEntryId, long fileVersionId)
090                    throws PortalException {
091    
092                    LocalRepository localRepositoryImpl =
093                            repositoryLocalService.getLocalRepositoryImpl(
094                                    folderId, fileEntryId, fileVersionId);
095    
096                    checkRepository(localRepositoryImpl.getRepositoryId());
097    
098                    return localRepositoryImpl;
099            }
100    
101            @Override
102            public Repository getRepository(long repositoryId) throws PortalException {
103                    return repositoryPersistence.findByPrimaryKey(repositoryId);
104            }
105    
106            @Override
107            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
108                            long repositoryId)
109                    throws PortalException {
110    
111                    checkRepository(repositoryId);
112    
113                    return repositoryLocalService.getRepositoryImpl(repositoryId);
114            }
115    
116            @Override
117            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
118                            long folderId, long fileEntryId, long fileVersionId)
119                    throws PortalException {
120    
121                    com.liferay.portal.kernel.repository.Repository repositoryImpl =
122                            repositoryLocalService.getRepositoryImpl(
123                                    folderId, fileEntryId, fileVersionId);
124    
125                    checkRepository(
126                            repositoryImpl.getRepositoryId(), folderId, fileEntryId,
127                            fileVersionId);
128    
129                    return repositoryImpl;
130            }
131    
132            @Override
133            public String[] getSupportedConfigurations(long classNameId) {
134                    try {
135                            ClassName className = classNameLocalService.getClassName(
136                                    classNameId);
137    
138                            String repositoryImplClassName = className.getValue();
139    
140                            BaseRepository baseRepository =
141                                    ExternalRepositoryFactoryUtil.getInstance(
142                                            repositoryImplClassName);
143    
144                            return baseRepository.getSupportedConfigurations();
145                    }
146                    catch (Exception e) {
147                            throw new SystemException(e);
148                    }
149            }
150    
151            @Override
152            public String[] getSupportedParameters(
153                    long classNameId, String configuration) {
154    
155                    try {
156                            ClassName className = classNameLocalService.getClassName(
157                                    classNameId);
158    
159                            String repositoryImplClassName = className.getValue();
160    
161                            BaseRepository baseRepository =
162                                    ExternalRepositoryFactoryUtil.getInstance(
163                                            repositoryImplClassName);
164    
165                            String[] supportedConfigurations =
166                                    baseRepository.getSupportedConfigurations();
167    
168                            String[][] supportedParameters =
169                                    baseRepository.getSupportedParameters();
170    
171                            for (int i = 0; i < supportedConfigurations.length; i++) {
172                                    if (supportedConfigurations[i].equals(configuration)) {
173                                            return supportedParameters[i];
174                                    }
175                            }
176    
177                            throw new RepositoryException(
178                                    "Configuration not found for repository with class name id " +
179                                            classNameId);
180                    }
181                    catch (Exception e) {
182                            throw new SystemException(e);
183                    }
184            }
185    
186            @Override
187            public UnicodeProperties getTypeSettingsProperties(long repositoryId)
188                    throws PortalException {
189    
190                    checkRepository(repositoryId);
191    
192                    return repositoryLocalService.getTypeSettingsProperties(repositoryId);
193            }
194    
195            @Override
196            public void updateRepository(
197                            long repositoryId, String name, String description)
198                    throws PortalException {
199    
200                    Repository repository = repositoryPersistence.findByPrimaryKey(
201                            repositoryId);
202    
203                    DLFolderPermission.check(
204                            getPermissionChecker(), repository.getGroupId(),
205                            repository.getDlFolderId(), ActionKeys.UPDATE);
206    
207                    repositoryLocalService.updateRepository(
208                            repositoryId, name, description);
209            }
210    
211            protected void checkModelPermissions(
212                            long folderId, long fileEntryId, long fileVersionId)
213                    throws PortalException {
214    
215                    if (folderId != 0) {
216                            DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
217    
218                            if (dlFolder != null) {
219                                    DLFolderPermission.check(
220                                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
221                            }
222                    }
223                    else if (fileEntryId != 0) {
224                            DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
225                                    fileEntryId);
226    
227                            if (dlFileEntry != null) {
228                                    DLFileEntryPermission.check(
229                                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
230                            }
231                    }
232                    else if (fileVersionId != 0) {
233                            DLFileVersion dlFileVersion =
234                                    dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
235    
236                            if (dlFileVersion != null) {
237                                    DLFileEntryPermission.check(
238                                            getPermissionChecker(), dlFileVersion.getFileEntryId(),
239                                            ActionKeys.VIEW);
240                            }
241                    }
242            }
243    
244            protected void checkRepository(
245                            long repositoryId, long folderId, long fileEntryId,
246                            long fileVersionId)
247                    throws PortalException {
248    
249                    Group group = groupPersistence.fetchByPrimaryKey(repositoryId);
250    
251                    if (group != null) {
252                            checkModelPermissions(folderId, fileEntryId, fileVersionId);
253    
254                            return;
255                    }
256    
257                    try {
258                            Repository repository = repositoryPersistence.fetchByPrimaryKey(
259                                    repositoryId);
260    
261                            if (repository != null) {
262                                    DLFolderPermission.check(
263                                            getPermissionChecker(), repository.getGroupId(),
264                                            repository.getDlFolderId(), ActionKeys.VIEW);
265    
266                                    return;
267                            }
268                    }
269                    catch (NoSuchRepositoryException nsre) {
270                            throw new RepositoryException(nsre.getMessage());
271                    }
272            }
273    
274    }