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