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