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