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