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.document.library.kernel.model.DLFileEntry;
018    import com.liferay.document.library.kernel.model.DLFileVersion;
019    import com.liferay.document.library.kernel.model.DLFolder;
020    import com.liferay.portal.kernel.bean.BeanReference;
021    import com.liferay.portal.kernel.exception.NoSuchRepositoryException;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.model.ClassName;
025    import com.liferay.portal.kernel.model.Group;
026    import com.liferay.portal.kernel.model.Repository;
027    import com.liferay.portal.kernel.repository.InvalidRepositoryIdException;
028    import com.liferay.portal.kernel.repository.RepositoryConfiguration;
029    import com.liferay.portal.kernel.security.permission.ActionKeys;
030    import com.liferay.portal.kernel.service.ServiceContext;
031    import com.liferay.portal.kernel.util.UnicodeProperties;
032    import com.liferay.portal.repository.registry.RepositoryClassDefinition;
033    import com.liferay.portal.repository.registry.RepositoryClassDefinitionCatalog;
034    import com.liferay.portal.service.base.RepositoryServiceBaseImpl;
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                    Repository repository = repositoryPersistence.findByPrimaryKey(
085                            repositoryId);
086    
087                    DLFolderPermission.check(
088                            getPermissionChecker(), repository.getGroupId(),
089                            repository.getDlFolderId(), ActionKeys.VIEW);
090    
091                    return repository;
092            }
093    
094            /**
095             * @deprecated As of 7.0.0, with no direct replacement
096             */
097            @Deprecated
098            @Override
099            public String[] getSupportedConfigurations(long classNameId) {
100                    return _SUPPORTED_CONFIGURATIONS;
101            }
102    
103            /**
104             * @deprecated As of 7.0.0, with no direct replacement
105             */
106            @Deprecated
107            @Override
108            public String[] getSupportedParameters(
109                    long classNameId, String configuration) {
110    
111                    try {
112                            ClassName className = classNameLocalService.getClassName(
113                                    classNameId);
114    
115                            String repositoryImplClassName = className.getValue();
116    
117                            return getSupportedParameters(
118                                    repositoryImplClassName, configuration);
119                    }
120                    catch (PortalException pe) {
121                            throw new SystemException(pe);
122                    }
123            }
124    
125            /**
126             * @deprecated As of 7.0.0, with no direct replacement
127             */
128            @Deprecated
129            @Override
130            public String[] getSupportedParameters(
131                    String className, String configuration) {
132    
133                    try {
134                            if (!configuration.equals(_CONFIGURATION)) {
135                                    throw new IllegalArgumentException(
136                                            "Specified " + configuration + " does not match " +
137                                                    "supported configuration " + _CONFIGURATION);
138                            }
139    
140                            Collection<String> supportedParameters = new ArrayList<>();
141    
142                            RepositoryClassDefinition repositoryClassDefinition =
143                                    _repositoryClassDefinitionCatalog.getRepositoryClassDefinition(
144                                            className);
145    
146                            RepositoryConfiguration repositoryConfiguration =
147                                    repositoryClassDefinition.getRepositoryConfiguration();
148    
149                            Collection<RepositoryConfiguration.Parameter>
150                                    repositoryConfigurationParameters =
151                                            repositoryConfiguration.getParameters();
152    
153                            for (RepositoryConfiguration.Parameter
154                                            repositoryConfigurationParameter :
155                                                    repositoryConfigurationParameters) {
156    
157                                    supportedParameters.add(
158                                            repositoryConfigurationParameter.getName());
159                            }
160    
161                            return supportedParameters.toArray(
162                                    new String[repositoryConfigurationParameters.size()]);
163                    }
164                    catch (Exception e) {
165                            throw new SystemException(e);
166                    }
167            }
168    
169            @Override
170            public UnicodeProperties getTypeSettingsProperties(long repositoryId)
171                    throws PortalException {
172    
173                    checkRepository(repositoryId);
174    
175                    return repositoryLocalService.getTypeSettingsProperties(repositoryId);
176            }
177    
178            @Override
179            public void updateRepository(
180                            long repositoryId, String name, String description)
181                    throws PortalException {
182    
183                    Repository repository = repositoryPersistence.findByPrimaryKey(
184                            repositoryId);
185    
186                    DLFolderPermission.check(
187                            getPermissionChecker(), repository.getGroupId(),
188                            repository.getDlFolderId(), ActionKeys.UPDATE);
189    
190                    repositoryLocalService.updateRepository(
191                            repositoryId, name, description);
192            }
193    
194            protected void checkModelPermissions(
195                            long folderId, long fileEntryId, long fileVersionId)
196                    throws PortalException {
197    
198                    if (folderId != 0) {
199                            DLFolder dlFolder = dlFolderLocalService.fetchDLFolder(folderId);
200    
201                            if (dlFolder != null) {
202                                    DLFolderPermission.check(
203                                            getPermissionChecker(), dlFolder, ActionKeys.VIEW);
204                            }
205                    }
206                    else if (fileEntryId != 0) {
207                            DLFileEntry dlFileEntry = dlFileEntryLocalService.fetchDLFileEntry(
208                                    fileEntryId);
209    
210                            if (dlFileEntry != null) {
211                                    DLFileEntryPermission.check(
212                                            getPermissionChecker(), fileEntryId, ActionKeys.VIEW);
213                            }
214                    }
215                    else if (fileVersionId != 0) {
216                            DLFileVersion dlFileVersion =
217                                    dlFileVersionLocalService.fetchDLFileVersion(fileVersionId);
218    
219                            if (dlFileVersion != null) {
220                                    DLFileEntryPermission.check(
221                                            getPermissionChecker(), dlFileVersion.getFileEntryId(),
222                                            ActionKeys.VIEW);
223                            }
224                    }
225            }
226    
227            protected void checkRepository(
228                            long repositoryId, long folderId, long fileEntryId,
229                            long fileVersionId)
230                    throws PortalException {
231    
232                    Group group = groupPersistence.fetchByPrimaryKey(repositoryId);
233    
234                    if (group != null) {
235                            checkModelPermissions(folderId, fileEntryId, fileVersionId);
236    
237                            return;
238                    }
239    
240                    try {
241                            Repository repository = repositoryPersistence.fetchByPrimaryKey(
242                                    repositoryId);
243    
244                            if (repository != null) {
245                                    DLFolderPermission.check(
246                                            getPermissionChecker(), repository.getGroupId(),
247                                            repository.getDlFolderId(), ActionKeys.VIEW);
248    
249                                    return;
250                            }
251                    }
252                    catch (NoSuchRepositoryException nsre) {
253                            throw new InvalidRepositoryIdException(nsre.getMessage());
254                    }
255            }
256    
257            private static final String _CONFIGURATION = "DEFAULT";
258    
259            private static final String[] _SUPPORTED_CONFIGURATIONS =
260                    new String[] {_CONFIGURATION};
261    
262            @BeanReference(type = RepositoryClassDefinitionCatalog.class)
263            private RepositoryClassDefinitionCatalog _repositoryClassDefinitionCatalog;
264    
265    }