001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.InvalidRepositoryException;
018    import com.liferay.portal.NoSuchRepositoryException;
019    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.exception.SystemException;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.repository.BaseRepository;
025    import com.liferay.portal.kernel.repository.LocalRepository;
026    import com.liferay.portal.kernel.repository.RepositoryException;
027    import com.liferay.portal.kernel.repository.cmis.CMISRepositoryHandler;
028    import com.liferay.portal.kernel.util.ProxyUtil;
029    import com.liferay.portal.kernel.util.UnicodeProperties;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.Repository;
033    import com.liferay.portal.model.RepositoryEntry;
034    import com.liferay.portal.model.User;
035    import com.liferay.portal.repository.cmis.CMISRepository;
036    import com.liferay.portal.repository.liferayrepository.LiferayLocalRepository;
037    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
038    import com.liferay.portal.repository.proxy.BaseRepositoryProxyBean;
039    import com.liferay.portal.repository.util.RepositoryFactoryUtil;
040    import com.liferay.portal.service.ServiceContext;
041    import com.liferay.portal.service.base.RepositoryServiceBaseImpl;
042    import com.liferay.portal.util.PortalUtil;
043    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
044    import com.liferay.portlet.documentlibrary.RepositoryNameException;
045    import com.liferay.portlet.documentlibrary.model.DLFolder;
046    
047    import java.util.Date;
048    import java.util.List;
049    import java.util.Map;
050    import java.util.concurrent.ConcurrentHashMap;
051    
052    /**
053     * @author Alexander Chow
054     * @author Mika Koivisto
055     */
056    public class RepositoryServiceImpl extends RepositoryServiceBaseImpl {
057    
058            public long addRepository(
059                            long groupId, long classNameId, long parentFolderId, String name,
060                            String description, String portletId,
061                            UnicodeProperties typeSettingsProperties,
062                            ServiceContext serviceContext)
063                    throws PortalException, SystemException {
064    
065                    User user = getUser();
066                    Date now = new Date();
067    
068                    long repositoryId = counterLocalService.increment();
069    
070                    Repository repository = repositoryPersistence.create(repositoryId);
071    
072                    repository.setGroupId(groupId);
073                    repository.setCompanyId(user.getCompanyId());
074                    repository.setCreateDate(now);
075                    repository.setModifiedDate(now);
076                    repository.setClassNameId(classNameId);
077                    repository.setName(name);
078                    repository.setDescription(description);
079                    repository.setPortletId(portletId);
080                    repository.setTypeSettingsProperties(typeSettingsProperties);
081                    repository.setDlFolderId(
082                            getDLFolderId(
083                                    user, groupId, repositoryId, parentFolderId, name, description,
084                                    serviceContext));
085    
086                    repositoryPersistence.update(repository, false);
087    
088                    if (classNameId != getDefaultClassNameId()) {
089                            try {
090                                    createRepositoryImpl(repositoryId, classNameId);
091                            }
092                            catch (Exception e) {
093                                    if (_log.isWarnEnabled()) {
094                                            _log.warn(e.getMessage());
095                                    }
096    
097                                    throw new InvalidRepositoryException(e);
098                            }
099                    }
100    
101                    return repositoryId;
102            }
103    
104            public void checkRepository(long repositoryId) throws SystemException {
105                    Group group = groupPersistence.fetchByPrimaryKey(repositoryId);
106    
107                    if (group != null) {
108                            return;
109                    }
110    
111                    try {
112                            repositoryPersistence.findByPrimaryKey(repositoryId);
113                    }
114                    catch (NoSuchRepositoryException nsre) {
115                            throw new RepositoryException(nsre.getMessage());
116                    }
117            }
118    
119            public LocalRepository getLocalRepositoryImpl(long repositoryId)
120                    throws PortalException, SystemException {
121    
122                    LocalRepository localRepositoryImpl =
123                            _localRepositoriesByRepositoryId.get(repositoryId);
124    
125                    if (localRepositoryImpl != null) {
126                            return localRepositoryImpl;
127                    }
128    
129                    long classNameId = getRepositoryClassNameId(repositoryId);
130    
131                    if (classNameId == getDefaultClassNameId()) {
132                            localRepositoryImpl = new LiferayLocalRepository(
133                                    repositoryService, dlAppHelperLocalService,
134                                    dlFileEntryLocalService, dlFileEntryService,
135                                    dlFileVersionLocalService, dlFileVersionService,
136                                    dlFolderLocalService, dlFolderService, repositoryId);
137                    }
138                    else {
139                            BaseRepository baseRepository = createRepositoryImpl(
140                                    repositoryId, classNameId);
141    
142                            localRepositoryImpl = baseRepository.getLocalRepository();
143                    }
144    
145                    checkRepository(repositoryId);
146    
147                    _localRepositoriesByRepositoryId.put(repositoryId, localRepositoryImpl);
148    
149                    return localRepositoryImpl;
150            }
151    
152            public LocalRepository getLocalRepositoryImpl(
153                            long folderId, long fileEntryId, long fileVersionId)
154                    throws PortalException, SystemException {
155    
156                    long repositoryEntryId = getRepositoryEntryId(
157                            folderId, fileEntryId, fileVersionId);
158    
159                    LocalRepository localRepositoryImpl =
160                            _localRepositoriesByRepositoryEntryId.get(repositoryEntryId);
161    
162                    if (localRepositoryImpl != null) {
163                            return localRepositoryImpl;
164                    }
165    
166                    localRepositoryImpl = new LiferayLocalRepository(
167                            repositoryService, dlAppHelperLocalService, dlFileEntryLocalService,
168                            dlFileEntryService, dlFileVersionLocalService, dlFileVersionService,
169                            dlFolderLocalService, dlFolderService, folderId, fileEntryId,
170                            fileVersionId);
171    
172                    if (localRepositoryImpl.getRepositoryId() == 0) {
173                            localRepositoryImpl = null;
174                    }
175    
176                    if (localRepositoryImpl == null) {
177                            BaseRepository baseRepository = createRepositoryImpl(
178                                    repositoryEntryId);
179    
180                            localRepositoryImpl = baseRepository.getLocalRepository();
181                    }
182                    else {
183                            checkRepository(localRepositoryImpl.getRepositoryId());
184                    }
185    
186                    _localRepositoriesByRepositoryEntryId.put(
187                            repositoryEntryId, localRepositoryImpl);
188    
189                    return localRepositoryImpl;
190            }
191    
192            public Repository getRepository(long repositoryId)
193                    throws PortalException, SystemException {
194    
195                    return repositoryPersistence.findByPrimaryKey(repositoryId);
196            }
197    
198            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
199                            long repositoryId)
200                    throws PortalException, SystemException {
201    
202                    com.liferay.portal.kernel.repository.Repository repositoryImpl =
203                            _repositoriesByRepositoryId.get(repositoryId);
204    
205                    if (repositoryImpl != null) {
206                            return repositoryImpl;
207                    }
208    
209                    long classNameId = getRepositoryClassNameId(repositoryId);
210    
211                    if (classNameId ==
212                                    PortalUtil.getClassNameId(LiferayRepository.class.getName())) {
213    
214                            repositoryImpl = new LiferayRepository(
215                                    repositoryService, dlAppHelperLocalService,
216                                    dlFileEntryLocalService, dlFileEntryService,
217                                    dlFileVersionLocalService, dlFileVersionService,
218                                    dlFolderLocalService, dlFolderService, repositoryId);
219                    }
220                    else {
221                            repositoryImpl = createRepositoryImpl(repositoryId, classNameId);
222                    }
223    
224                    checkRepository(repositoryId);
225    
226                    _repositoriesByRepositoryId.put(repositoryId, repositoryImpl);
227    
228                    return repositoryImpl;
229            }
230    
231            public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
232                            long folderId, long fileEntryId, long fileVersionId)
233                    throws PortalException, SystemException {
234    
235                    long repositoryEntryId = getRepositoryEntryId(
236                            folderId, fileEntryId, fileVersionId);
237    
238                    com.liferay.portal.kernel.repository.Repository repositoryImpl =
239                            _repositoriesByEntryId.get(repositoryEntryId);
240    
241                    if (repositoryImpl != null) {
242                            return repositoryImpl;
243                    }
244    
245                    repositoryImpl = new LiferayRepository(
246                            repositoryService, dlAppHelperLocalService, dlFileEntryLocalService,
247                            dlFileEntryService, dlFileVersionLocalService, dlFileVersionService,
248                            dlFolderLocalService, dlFolderService, folderId, fileEntryId,
249                            fileVersionId);
250    
251                    if (repositoryImpl.getRepositoryId() == 0) {
252                            repositoryImpl = null;
253                    }
254    
255                    if (repositoryImpl == null) {
256                            repositoryImpl = createRepositoryImpl(repositoryEntryId);
257                    }
258                    else {
259                            checkRepository(repositoryImpl.getRepositoryId());
260                    }
261    
262                    _repositoriesByEntryId.put(repositoryEntryId, repositoryImpl);
263    
264                    return repositoryImpl;
265            }
266    
267            public String[] getSupportedConfigurations(long classNameId)
268                    throws SystemException {
269    
270                    try {
271                            String repositoryImplClassName = PortalUtil.getClassName(
272                                    classNameId);
273    
274                            BaseRepository baseRepository = RepositoryFactoryUtil.getInstance(
275                                    repositoryImplClassName);
276    
277                            return baseRepository.getSupportedConfigurations();
278                    }
279                    catch (Exception e) {
280                            throw new SystemException(e);
281                    }
282            }
283    
284            public String[] getSupportedParameters(
285                            long classNameId, String configuration)
286                    throws SystemException {
287    
288                    try {
289                            String repositoryImplClassName = PortalUtil.getClassName(
290                                    classNameId);
291    
292                            BaseRepository baseRepository = RepositoryFactoryUtil.getInstance(
293                                    repositoryImplClassName);
294    
295                            String[] supportedConfigurations =
296                                    baseRepository.getSupportedConfigurations();
297    
298                            String[][] supportedParameters =
299                                    baseRepository.getSupportedParameters();
300    
301                            for (int i = 0; i < supportedConfigurations.length; i++) {
302                                    if (supportedConfigurations[i].equals(configuration)) {
303                                            return supportedParameters[i];
304                                    }
305                            }
306    
307                            throw new RepositoryException(
308                                    "Configuration not found for repository with class name id " +
309                                            classNameId);
310                    }
311                    catch (Exception e) {
312                            throw new SystemException(e);
313                    }
314            }
315    
316            public UnicodeProperties getTypeSettingsProperties(long repositoryId)
317                    throws PortalException, SystemException {
318    
319                    Repository repository = repositoryPersistence.findByPrimaryKey(
320                            repositoryId);
321    
322                    return repository.getTypeSettingsProperties();
323            }
324    
325            public void unmountRepositories(long groupId)
326                    throws PortalException, SystemException {
327    
328                    List<Repository> repositories = repositoryPersistence.findByGroupId(
329                            groupId);
330    
331                    for (Repository repository : repositories) {
332                            long repositoryId = repository.getRepositoryId();
333    
334                            unmountRepository(repositoryId);
335                    }
336    
337                    dlFolderLocalService.deleteAll(groupId);
338            }
339    
340            public void unmountRepository(long repositoryId)
341                    throws PortalException, SystemException {
342    
343                    Repository repository = repositoryPersistence.fetchByPrimaryKey(
344                            repositoryId);
345    
346                    if (repository != null) {
347                            expandoValueLocalService.deleteValues(
348                                    Repository.class.getName(), repositoryId);
349    
350                            try {
351                                    dlFolderLocalService.deleteFolder(
352                                            repository.getDlFolderId());
353                            }
354                            catch (NoSuchFolderException nsfe) {
355                            }
356    
357                            repositoryPersistence.remove(repository);
358    
359                            repositoryEntryPersistence.removeByRepositoryId(repositoryId);
360                    }
361            }
362    
363            public void updateRepository(
364                            long repositoryId, String name, String description)
365                    throws PortalException, SystemException {
366    
367                    Repository repository = repositoryPersistence.findByPrimaryKey(
368                            repositoryId);
369    
370                    repository.setModifiedDate(new Date());
371                    repository.setName(name);
372                    repository.setDescription(description);
373    
374                    repositoryPersistence.update(repository, false);
375    
376                    DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
377                            repository.getDlFolderId());
378    
379                    dlFolder.setModifiedDate(new Date());
380                    dlFolder.setName(name);
381                    dlFolder.setDescription(description);
382    
383                    dlFolderPersistence.update(dlFolder, false);
384            }
385    
386            protected BaseRepository createRepositoryImpl(long repositoryEntryId)
387                    throws PortalException, SystemException {
388    
389                    RepositoryEntry repositoryEntry =
390                            repositoryEntryPersistence.findByPrimaryKey(repositoryEntryId);
391    
392                    long repositoryId = repositoryEntry.getRepositoryId();
393    
394                    return (BaseRepository)getRepositoryImpl(repositoryId);
395            }
396    
397            protected BaseRepository createRepositoryImpl(
398                            long repositoryId, long classNameId)
399                    throws PortalException, SystemException {
400    
401                    BaseRepository baseRepository = null;
402    
403                    Repository repository = null;
404    
405                    try {
406                            repository = getRepository(repositoryId);
407    
408                            String repositoryImplClassName = PortalUtil.getClassName(
409                                    classNameId);
410    
411                            baseRepository = RepositoryFactoryUtil.getInstance(
412                                    repositoryImplClassName);
413                    }
414                    catch (Exception e) {
415                            throw new RepositoryException(
416                                    "There is no valid repository class with class name id " +
417                                            classNameId,
418                                    e);
419                    }
420    
421                    CMISRepositoryHandler cmisRepositoryHandler = null;
422    
423                    if (baseRepository instanceof CMISRepositoryHandler) {
424                            cmisRepositoryHandler = (CMISRepositoryHandler)baseRepository;
425                    }
426                    else if (baseRepository instanceof BaseRepositoryProxyBean) {
427                            BaseRepositoryProxyBean baseRepositoryProxyBean =
428                                    (BaseRepositoryProxyBean) baseRepository;
429    
430                            ClassLoaderBeanHandler classLoaderBeanHandler =
431                                    (ClassLoaderBeanHandler)ProxyUtil.getInvocationHandler(
432                                            baseRepositoryProxyBean.getProxyBean());
433    
434                            Object bean = classLoaderBeanHandler.getBean();
435    
436                            if (bean instanceof CMISRepositoryHandler) {
437                                    cmisRepositoryHandler = (CMISRepositoryHandler)bean;
438                            }
439                    }
440    
441                    if (cmisRepositoryHandler != null) {
442                            CMISRepository cmisRepository = new CMISRepository(
443                                    cmisRepositoryHandler);
444    
445                            cmisRepositoryHandler.setCmisRepository(cmisRepository);
446    
447                            setupRepository(repositoryId, repository, cmisRepository);
448                    }
449    
450                    setupRepository(repositoryId, repository, baseRepository);
451    
452                    baseRepository.initRepository();
453    
454                    return baseRepository;
455            }
456    
457            protected long getDefaultClassNameId() {
458                    if (_defaultClassNameId == 0) {
459                            _defaultClassNameId = PortalUtil.getClassNameId(
460                                    LiferayRepository.class.getName());
461                    }
462    
463                    return _defaultClassNameId;
464            }
465    
466            protected long getDLFolderId(
467                            User user, long groupId, long repositoryId, long parentFolderId,
468                            String name, String description, ServiceContext serviceContext)
469                    throws PortalException, SystemException {
470    
471                    if (Validator.isNull(name)) {
472                            throw new RepositoryNameException();
473                    }
474    
475                    DLFolder dlFolder = dlFolderLocalService.addFolder(
476                            user.getUserId(), groupId, repositoryId, true, parentFolderId, name,
477                            description, serviceContext);
478    
479                    return dlFolder.getFolderId();
480            }
481    
482            protected long getRepositoryClassNameId(long repositoryId)
483                    throws SystemException {
484    
485                    Repository repository = repositoryPersistence.fetchByPrimaryKey(
486                            repositoryId);
487    
488                    if (repository != null) {
489                            return repository.getClassNameId();
490                    }
491    
492                    return PortalUtil.getClassNameId(LiferayRepository.class.getName());
493            }
494    
495            protected long getRepositoryEntryId(
496                            long folderId, long fileEntryId, long fileVersionId)
497                    throws RepositoryException {
498    
499                    long repositoryEntryId = 0;
500    
501                    if (folderId != 0) {
502                            repositoryEntryId = folderId;
503                    }
504                    else if (fileEntryId != 0) {
505                            repositoryEntryId = fileEntryId;
506                    }
507                    else if (fileVersionId != 0) {
508                            repositoryEntryId = fileVersionId;
509                    }
510    
511                    if (repositoryEntryId == 0) {
512                            throw new RepositoryException(
513                                    "Missing a valid ID for folder, fileEntry or fileVersion");
514                    }
515    
516                    return repositoryEntryId;
517            }
518    
519            protected void setupRepository(
520                    long repositoryId, Repository repository,
521                    BaseRepository baseRepository) {
522    
523                    baseRepository.setAssetEntryLocalService(assetEntryLocalService);
524                    baseRepository.setCompanyId(repository.getCompanyId());
525                    baseRepository.setCompanyLocalService(companyLocalService);
526                    baseRepository.setCounterLocalService(counterLocalService);
527                    baseRepository.setDLAppHelperLocalService(dlAppHelperLocalService);
528                    baseRepository.setGroupId(repository.getGroupId());
529                    baseRepository.setRepositoryId(repositoryId);
530                    baseRepository.setTypeSettingsProperties(
531                            repository.getTypeSettingsProperties());
532                    baseRepository.setUserLocalService(userLocalService);
533            }
534    
535            private static Log _log = LogFactoryUtil.getLog(
536                    RepositoryServiceImpl.class);
537    
538            private long _defaultClassNameId;
539            private Map<Long, LocalRepository> _localRepositoriesByRepositoryEntryId =
540                    new ConcurrentHashMap<Long, LocalRepository>();
541            private Map<Long, LocalRepository> _localRepositoriesByRepositoryId =
542                    new ConcurrentHashMap<Long, LocalRepository>();
543            private Map<Long, com.liferay.portal.kernel.repository.Repository>
544                    _repositoriesByEntryId = new ConcurrentHashMap
545                            <Long, com.liferay.portal.kernel.repository.Repository>();
546            private Map<Long, com.liferay.portal.kernel.repository.Repository>
547                    _repositoriesByRepositoryId = new ConcurrentHashMap
548                            <Long, com.liferay.portal.kernel.repository.Repository>();
549    
550    }