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