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