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