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 InvalidRepositoryIdException(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 finally {
185 SystemEventHierarchyEntryThreadLocal.pop(Repository.class);
186 }
187
188 systemEventLocalService.addSystemEvent(
189 0, repository.getGroupId(), Repository.class.getName(),
190 repositoryId, repository.getUuid(), null,
191 SystemEventConstants.TYPE_DELETE, StringPool.BLANK);
192 }
193
194 _localRepositoriesByRepositoryId.remove(repositoryId);
195
196 _repositoriesByRepositoryId.remove(repositoryId);
197
198 return repository;
199 }
200
201 @Override
202 public Repository fetchRepository(long groupId, String portletId)
203 throws SystemException {
204
205 return fetchRepository(groupId, portletId, portletId);
206 }
207
208 @Override
209 public Repository fetchRepository(
210 long groupId, String name, String portletId)
211 throws SystemException {
212
213 return repositoryPersistence.fetchByG_N_P(groupId, name, portletId);
214 }
215
216 @Override
217 public LocalRepository getLocalRepositoryImpl(long repositoryId)
218 throws PortalException, SystemException {
219
220 LocalRepository localRepositoryImpl =
221 _localRepositoriesByRepositoryId.get(repositoryId);
222
223 if (localRepositoryImpl != null) {
224 return localRepositoryImpl;
225 }
226
227 long classNameId = getRepositoryClassNameId(repositoryId);
228
229 if (classNameId == getDefaultClassNameId()) {
230 localRepositoryImpl = new LiferayLocalRepository(
231 repositoryLocalService, repositoryService,
232 dlAppHelperLocalService, dlFileEntryLocalService,
233 dlFileEntryService, dlFileEntryTypeLocalService,
234 dlFileVersionLocalService, dlFileVersionService,
235 dlFolderLocalService, dlFolderService, resourceLocalService,
236 repositoryId);
237 }
238 else {
239 BaseRepository baseRepository = createRepositoryImpl(
240 repositoryId, classNameId);
241
242 localRepositoryImpl = baseRepository.getLocalRepository();
243 }
244
245 checkRepository(repositoryId);
246
247 _localRepositoriesByRepositoryId.put(repositoryId, localRepositoryImpl);
248
249 return localRepositoryImpl;
250 }
251
252 @Override
253 public LocalRepository getLocalRepositoryImpl(
254 long folderId, long fileEntryId, long fileVersionId)
255 throws PortalException, SystemException {
256
257 long repositoryEntryId = getRepositoryEntryId(
258 folderId, fileEntryId, fileVersionId);
259
260 LocalRepository localRepositoryImpl =
261 _localRepositoriesByRepositoryEntryId.get(repositoryEntryId);
262
263 if (localRepositoryImpl != null) {
264 return localRepositoryImpl;
265 }
266
267 localRepositoryImpl = new LiferayLocalRepository(
268 repositoryLocalService, repositoryService, dlAppHelperLocalService,
269 dlFileEntryLocalService, dlFileEntryService,
270 dlFileEntryTypeLocalService, dlFileVersionLocalService,
271 dlFileVersionService, dlFolderLocalService, dlFolderService,
272 resourceLocalService, folderId, fileEntryId, fileVersionId);
273
274 if (localRepositoryImpl.getRepositoryId() == 0) {
275 localRepositoryImpl = null;
276 }
277
278 if (localRepositoryImpl == null) {
279 BaseRepository baseRepository = createRepositoryImpl(
280 repositoryEntryId);
281
282 localRepositoryImpl = baseRepository.getLocalRepository();
283 }
284 else {
285 checkRepository(localRepositoryImpl.getRepositoryId());
286 }
287
288 _localRepositoriesByRepositoryEntryId.put(
289 repositoryEntryId, localRepositoryImpl);
290
291 return localRepositoryImpl;
292 }
293
294 @Override
295 public Repository getRepository(long groupId, String portletId)
296 throws PortalException, SystemException {
297
298 return getRepository(groupId, portletId, portletId);
299 }
300
301 @Override
302 public Repository getRepository(long groupId, String name, String portletId)
303 throws PortalException, SystemException {
304
305 return repositoryPersistence.findByG_N_P(groupId, name, portletId);
306 }
307
308 @Override
309 public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
310 long repositoryId)
311 throws PortalException, SystemException {
312
313 com.liferay.portal.kernel.repository.Repository repositoryImpl =
314 _repositoriesByRepositoryId.get(repositoryId);
315
316 if (repositoryImpl != null) {
317 return repositoryImpl;
318 }
319
320 long classNameId = getRepositoryClassNameId(repositoryId);
321
322 if (classNameId ==
323 PortalUtil.getClassNameId(LiferayRepository.class.getName())) {
324
325 repositoryImpl = new LiferayRepository(
326 repositoryLocalService, repositoryService,
327 dlAppHelperLocalService, dlFileEntryLocalService,
328 dlFileEntryService, dlFileEntryTypeLocalService,
329 dlFileVersionLocalService, dlFileVersionService,
330 dlFolderLocalService, dlFolderService, resourceLocalService,
331 repositoryId);
332 }
333 else {
334 repositoryImpl = createRepositoryImpl(repositoryId, classNameId);
335 }
336
337 checkRepository(repositoryId);
338
339 _repositoriesByRepositoryId.put(repositoryId, repositoryImpl);
340
341 return repositoryImpl;
342 }
343
344 @Override
345 public com.liferay.portal.kernel.repository.Repository getRepositoryImpl(
346 long folderId, long fileEntryId, long fileVersionId)
347 throws PortalException, SystemException {
348
349 long repositoryEntryId = getRepositoryEntryId(
350 folderId, fileEntryId, fileVersionId);
351
352 com.liferay.portal.kernel.repository.Repository repositoryImpl =
353 _repositoriesByEntryId.get(repositoryEntryId);
354
355 if (repositoryImpl != null) {
356 return repositoryImpl;
357 }
358
359 repositoryImpl = new LiferayRepository(
360 repositoryLocalService, repositoryService, dlAppHelperLocalService,
361 dlFileEntryLocalService, dlFileEntryService,
362 dlFileEntryTypeLocalService, dlFileVersionLocalService,
363 dlFileVersionService, dlFolderLocalService, dlFolderService,
364 resourceLocalService, folderId, fileEntryId, fileVersionId);
365
366 if (repositoryImpl.getRepositoryId() == 0) {
367 repositoryImpl = null;
368 }
369
370 if (repositoryImpl == null) {
371 repositoryImpl = createRepositoryImpl(repositoryEntryId);
372 }
373 else {
374 checkRepository(repositoryImpl.getRepositoryId());
375 }
376
377 _repositoriesByEntryId.put(repositoryEntryId, repositoryImpl);
378
379 return repositoryImpl;
380 }
381
382 @Override
383 public UnicodeProperties getTypeSettingsProperties(long repositoryId)
384 throws PortalException, SystemException {
385
386 Repository repository = repositoryPersistence.findByPrimaryKey(
387 repositoryId);
388
389 return repository.getTypeSettingsProperties();
390 }
391
392 @Override
393 public void updateRepository(
394 long repositoryId, String name, String description)
395 throws PortalException, SystemException {
396
397 Date now = new Date();
398
399 Repository repository = repositoryPersistence.findByPrimaryKey(
400 repositoryId);
401
402 repository.setModifiedDate(now);
403 repository.setName(name);
404 repository.setDescription(description);
405
406 repositoryPersistence.update(repository);
407
408 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
409 repository.getDlFolderId());
410
411 dlFolder.setModifiedDate(now);
412 dlFolder.setName(name);
413 dlFolder.setDescription(description);
414
415 dlFolderPersistence.update(dlFolder);
416 }
417
418 protected BaseRepository createRepositoryImpl(long repositoryEntryId)
419 throws PortalException, SystemException {
420
421 RepositoryEntry repositoryEntry =
422 repositoryEntryPersistence.findByPrimaryKey(repositoryEntryId);
423
424 long repositoryId = repositoryEntry.getRepositoryId();
425
426 return (BaseRepository)getRepositoryImpl(repositoryId);
427 }
428
429 protected BaseRepository createRepositoryImpl(
430 long repositoryId, long classNameId)
431 throws PortalException, SystemException {
432
433 BaseRepository baseRepository = null;
434
435 Repository repository = null;
436
437 try {
438 repository = getRepository(repositoryId);
439
440 String repositoryImplClassName = PortalUtil.getClassName(
441 classNameId);
442
443 baseRepository = RepositoryFactoryUtil.getInstance(
444 repositoryImplClassName);
445 }
446 catch (Exception e) {
447 throw new RepositoryException(
448 "There is no valid repository class with class name id " +
449 classNameId,
450 e);
451 }
452
453 CMISRepositoryHandler cmisRepositoryHandler = null;
454
455 if (baseRepository instanceof CMISRepositoryHandler) {
456 cmisRepositoryHandler = (CMISRepositoryHandler)baseRepository;
457 }
458 else if (baseRepository instanceof BaseRepositoryProxyBean) {
459 BaseRepositoryProxyBean baseRepositoryProxyBean =
460 (BaseRepositoryProxyBean)baseRepository;
461
462 ClassLoaderBeanHandler classLoaderBeanHandler =
463 (ClassLoaderBeanHandler)ProxyUtil.getInvocationHandler(
464 baseRepositoryProxyBean.getProxyBean());
465
466 Object bean = classLoaderBeanHandler.getBean();
467
468 if (bean instanceof CMISRepositoryHandler) {
469 cmisRepositoryHandler = (CMISRepositoryHandler)bean;
470 }
471 }
472
473 if (cmisRepositoryHandler != null) {
474 CMISRepository cmisRepository = new CMISRepository(
475 cmisRepositoryHandler);
476
477 cmisRepositoryHandler.setCmisRepository(cmisRepository);
478
479 setupRepository(repositoryId, repository, cmisRepository);
480 }
481
482 setupRepository(repositoryId, repository, baseRepository);
483
484 if (!ExportImportThreadLocal.isImportInProcess()) {
485 baseRepository.initRepository();
486 }
487
488 return baseRepository;
489 }
490
491 protected long getDefaultClassNameId() {
492 if (_defaultClassNameId == 0) {
493 _defaultClassNameId = PortalUtil.getClassNameId(
494 LiferayRepository.class.getName());
495 }
496
497 return _defaultClassNameId;
498 }
499
500 protected long getDLFolderId(
501 User user, long groupId, long repositoryId, long parentFolderId,
502 String name, String description, boolean hidden,
503 ServiceContext serviceContext)
504 throws PortalException, SystemException {
505
506 if (Validator.isNull(name)) {
507 throw new RepositoryNameException();
508 }
509
510 DLFolder dlFolder = dlFolderLocalService.addFolder(
511 user.getUserId(), groupId, repositoryId, true, parentFolderId, name,
512 description, hidden, serviceContext);
513
514 return dlFolder.getFolderId();
515 }
516
517 protected long getRepositoryClassNameId(long repositoryId)
518 throws SystemException {
519
520 Repository repository = repositoryPersistence.fetchByPrimaryKey(
521 repositoryId);
522
523 if (repository != null) {
524 return repository.getClassNameId();
525 }
526
527 return PortalUtil.getClassNameId(LiferayRepository.class.getName());
528 }
529
530 protected long getRepositoryEntryId(
531 long folderId, long fileEntryId, long fileVersionId)
532 throws RepositoryException {
533
534 long repositoryEntryId = 0;
535
536 if (folderId != 0) {
537 repositoryEntryId = folderId;
538 }
539 else if (fileEntryId != 0) {
540 repositoryEntryId = fileEntryId;
541 }
542 else if (fileVersionId != 0) {
543 repositoryEntryId = fileVersionId;
544 }
545
546 if (repositoryEntryId == 0) {
547 throw new InvalidRepositoryIdException(
548 "Missing a valid ID for folder, file entry or file version");
549 }
550
551 return repositoryEntryId;
552 }
553
554 protected void setupRepository(
555 long repositoryId, Repository repository,
556 BaseRepository baseRepository) {
557
558 baseRepository.setAssetEntryLocalService(assetEntryLocalService);
559 baseRepository.setCompanyId(repository.getCompanyId());
560 baseRepository.setCompanyLocalService(companyLocalService);
561 baseRepository.setCounterLocalService(counterLocalService);
562 baseRepository.setDLAppHelperLocalService(dlAppHelperLocalService);
563 baseRepository.setGroupId(repository.getGroupId());
564 baseRepository.setRepositoryId(repositoryId);
565 baseRepository.setTypeSettingsProperties(
566 repository.getTypeSettingsProperties());
567 baseRepository.setUserLocalService(userLocalService);
568 }
569
570 private static Log _log = LogFactoryUtil.getLog(
571 RepositoryLocalServiceImpl.class);
572
573 private long _defaultClassNameId;
574 private Map<Long, LocalRepository> _localRepositoriesByRepositoryEntryId =
575 new ConcurrentHashMap<Long, LocalRepository>();
576 private Map<Long, LocalRepository> _localRepositoriesByRepositoryId =
577 new ConcurrentHashMap<Long, LocalRepository>();
578 private Map<Long, com.liferay.portal.kernel.repository.Repository>
579 _repositoriesByEntryId = new ConcurrentHashMap
580 <Long, com.liferay.portal.kernel.repository.Repository>();
581 private Map<Long, com.liferay.portal.kernel.repository.Repository>
582 _repositoriesByRepositoryId = new ConcurrentHashMap
583 <Long, com.liferay.portal.kernel.repository.Repository>();
584
585 }