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