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