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