001    /**
002     * Copyright (c) 2000-2013 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.Group;
025    import com.liferay.portal.model.Repository;
026    import com.liferay.portal.repository.util.RepositoryFactoryUtil;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.base.RepositoryServiceBaseImpl;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
032    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
033    
034    /**
035     * @author Alexander Chow
036     * @author Mika Koivisto
037     */
038    public class RepositoryServiceImpl extends RepositoryServiceBaseImpl {
039    
040            @Override
041            public Repository addRepository(
042                            long groupId, long classNameId, long parentFolderId, String name,
043                            String description, String portletId,
044                            UnicodeProperties typeSettingsProperties,
045                            ServiceContext serviceContext)
046                    throws PortalException, SystemException {
047    
048                    DLPermission.check(
049                            getPermissionChecker(), groupId, ActionKeys.ADD_REPOSITORY);
050    
051                    return repositoryLocalService.addRepository(
052                            getUserId(), groupId, classNameId, parentFolderId, name,
053                            description, portletId, typeSettingsProperties, false,
054                            serviceContext);
055            }
056    
057            @Override
058            public void checkRepository(long repositoryId)
059                    throws PortalException, SystemException {
060    
061                    Group group = groupPersistence.fetchByPrimaryKey(repositoryId);
062    
063                    if (group != null) {
064                            return;
065                    }
066    
067                    try {
068                            Repository repository = repositoryPersistence.findByPrimaryKey(
069                                    repositoryId);
070    
071                            DLFolderPermission.check(
072                                    getPermissionChecker(), repository.getGroupId(),
073                                    repository.getDlFolderId(), ActionKeys.VIEW);
074                    }
075                    catch (NoSuchRepositoryException nsre) {
076                            throw new RepositoryException(nsre.getMessage());
077                    }
078            }
079    
080            @Override
081            public void deleteRepository(long repositoryId)
082                    throws PortalException, SystemException {
083    
084                    Repository repository = repositoryPersistence.findByPrimaryKey(
085                            repositoryId);
086    
087                    DLFolderPermission.check(
088                            getPermissionChecker(), repository.getGroupId(),
089                            repository.getDlFolderId(), ActionKeys.DELETE);
090    
091                    repositoryLocalService.deleteRepository(repository.getRepositoryId());
092            }
093    
094            @Override
095            public LocalRepository getLocalRepositoryImpl(long repositoryId)
096                    throws PortalException, SystemException {
097    
098                    checkRepository(repositoryId);
099    
100                    return repositoryLocalService.getLocalRepositoryImpl(repositoryId);
101            }
102    
103            @Override
104            public LocalRepository getLocalRepositoryImpl(
105                            long folderId, long fileEntryId, long fileVersionId)
106                    throws PortalException, SystemException {
107    
108                    LocalRepository localRepositoryImpl =
109                            repositoryLocalService.getLocalRepositoryImpl(
110                                    folderId, fileEntryId, fileVersionId);
111    
112                    checkRepository(localRepositoryImpl.getRepositoryId());
113    
114                    return localRepositoryImpl;
115            }
116    
117            @Override
118            public Repository getRepository(long repositoryId)
119                    throws PortalException, SystemException {
120    
121                    return repositoryPersistence.findByPrimaryKey(repositoryId);
122            }
123    
124            @Override
125            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
126                            long repositoryId)
127                    throws PortalException, SystemException {
128    
129                    checkRepository(repositoryId);
130    
131                    return repositoryLocalService.getRepositoryImpl(repositoryId);
132            }
133    
134            @Override
135            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
136                            long folderId, long fileEntryId, long fileVersionId)
137                    throws PortalException, SystemException {
138    
139                    com.liferay.portal.kernel.repository.Repository repositoryImpl =
140                            repositoryLocalService.getRepositoryImpl(
141                                    folderId, fileEntryId, fileVersionId);
142    
143                    checkRepository(repositoryImpl.getRepositoryId());
144    
145                    return repositoryImpl;
146            }
147    
148            @Override
149            public String[] getSupportedConfigurations(long classNameId)
150                    throws SystemException {
151    
152                    try {
153                            String repositoryImplClassName = PortalUtil.getClassName(
154                                    classNameId);
155    
156                            BaseRepository baseRepository = RepositoryFactoryUtil.getInstance(
157                                    repositoryImplClassName);
158    
159                            return baseRepository.getSupportedConfigurations();
160                    }
161                    catch (Exception e) {
162                            throw new SystemException(e);
163                    }
164            }
165    
166            @Override
167            public String[] getSupportedParameters(
168                            long classNameId, String configuration)
169                    throws SystemException {
170    
171                    try {
172                            String repositoryImplClassName = PortalUtil.getClassName(
173                                    classNameId);
174    
175                            BaseRepository baseRepository = RepositoryFactoryUtil.getInstance(
176                                    repositoryImplClassName);
177    
178                            String[] supportedConfigurations =
179                                    baseRepository.getSupportedConfigurations();
180    
181                            String[][] supportedParameters =
182                                    baseRepository.getSupportedParameters();
183    
184                            for (int i = 0; i < supportedConfigurations.length; i++) {
185                                    if (supportedConfigurations[i].equals(configuration)) {
186                                            return supportedParameters[i];
187                                    }
188                            }
189    
190                            throw new RepositoryException(
191                                    "Configuration not found for repository with class name id " +
192                                            classNameId);
193                    }
194                    catch (Exception e) {
195                            throw new SystemException(e);
196                    }
197            }
198    
199            @Override
200            public UnicodeProperties getTypeSettingsProperties(long repositoryId)
201                    throws PortalException, SystemException {
202    
203                    checkRepository(repositoryId);
204    
205                    return repositoryLocalService.getTypeSettingsProperties(repositoryId);
206            }
207    
208            @Override
209            public void updateRepository(
210                            long repositoryId, String name, String description)
211                    throws PortalException, SystemException {
212    
213                    Repository repository = repositoryPersistence.findByPrimaryKey(
214                            repositoryId);
215    
216                    DLFolderPermission.check(
217                            getPermissionChecker(), repository.getGroupId(),
218                            repository.getDlFolderId(), ActionKeys.UPDATE);
219    
220                    repositoryLocalService.updateRepository(
221                            repositoryId, name, description);
222            }
223    
224    }