/portal-service/src/com/liferay/portal/kernel/repository/BaseRepositoryImpl.java

https://github.com/monicali/liferay-portal · Java · 375 lines · 270 code · 87 blank · 18 comment · 6 complexity · b3439c1f3dc383b62d60243e36a6bd5a MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.kernel.repository;
  15. import com.liferay.counter.service.CounterLocalService;
  16. import com.liferay.portal.kernel.exception.PortalException;
  17. import com.liferay.portal.kernel.exception.SystemException;
  18. import com.liferay.portal.kernel.repository.model.FileEntry;
  19. import com.liferay.portal.kernel.repository.model.Folder;
  20. import com.liferay.portal.kernel.repository.search.RepositorySearchQueryBuilderUtil;
  21. import com.liferay.portal.kernel.search.BooleanQuery;
  22. import com.liferay.portal.kernel.search.Hits;
  23. import com.liferay.portal.kernel.search.SearchContext;
  24. import com.liferay.portal.kernel.search.SearchEngineUtil;
  25. import com.liferay.portal.kernel.search.SearchException;
  26. import com.liferay.portal.kernel.util.OrderByComparator;
  27. import com.liferay.portal.kernel.util.UnicodeProperties;
  28. import com.liferay.portal.model.Lock;
  29. import com.liferay.portal.model.RepositoryEntry;
  30. import com.liferay.portal.service.CompanyLocalService;
  31. import com.liferay.portal.service.ServiceContext;
  32. import com.liferay.portal.service.UserLocalService;
  33. import com.liferay.portal.service.persistence.RepositoryEntryUtil;
  34. import com.liferay.portlet.asset.service.AssetEntryLocalService;
  35. import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
  36. import java.io.File;
  37. import java.io.FileInputStream;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.util.ArrayList;
  41. import java.util.List;
  42. /**
  43. * Third-party repository implementations should extend from this class.
  44. *
  45. * @author Alexander Chow
  46. */
  47. public abstract class BaseRepositoryImpl implements BaseRepository {
  48. public FileEntry addFileEntry(
  49. long folderId, String sourceFileName, String mimeType, String title,
  50. String description, String changeLog, File file,
  51. ServiceContext serviceContext)
  52. throws PortalException, SystemException {
  53. InputStream is = null;
  54. long size = 0;
  55. try {
  56. is = new FileInputStream(file);
  57. size = file.length();
  58. return addFileEntry(
  59. folderId, sourceFileName, mimeType, title, description,
  60. changeLog, is, size, serviceContext);
  61. }
  62. catch (IOException ioe) {
  63. throw new SystemException(ioe);
  64. }
  65. finally {
  66. if (is != null) {
  67. try {
  68. is.close();
  69. }
  70. catch (IOException ioe) {
  71. }
  72. }
  73. }
  74. }
  75. public void deleteFileEntry(long folderId, String title)
  76. throws PortalException, SystemException {
  77. FileEntry fileEntry = getFileEntry(folderId, title);
  78. deleteFileEntry(fileEntry.getFileEntryId());
  79. }
  80. public void deleteFolder(long parentFolderId, String title)
  81. throws PortalException, SystemException {
  82. Folder folder = getFolder(parentFolderId, title);
  83. deleteFolder(folder.getFolderId());
  84. }
  85. public long getCompanyId() {
  86. return _companyId;
  87. }
  88. public List<Object> getFileEntriesAndFileShortcuts(
  89. long folderId, int status, int start, int end)
  90. throws SystemException {
  91. return new ArrayList<Object>(
  92. getFileEntries(folderId, start, end, null));
  93. }
  94. public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
  95. throws SystemException {
  96. return getFileEntriesCount(folderId);
  97. }
  98. public int getFileEntriesAndFileShortcutsCount(
  99. long folderId, int status, String[] mimeTypes)
  100. throws PortalException, SystemException {
  101. return getFileEntriesCount(folderId, mimeTypes);
  102. }
  103. public abstract List<Object> getFoldersAndFileEntries(
  104. long folderId, int start, int end, OrderByComparator obc)
  105. throws SystemException;
  106. public abstract List<Object> getFoldersAndFileEntries(
  107. long folderId, String[] mimeTypes, int start, int end,
  108. OrderByComparator obc)
  109. throws PortalException, SystemException;
  110. public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
  111. long folderId, int status, boolean includeMountFolders, int start,
  112. int end, OrderByComparator obc)
  113. throws SystemException {
  114. return getFoldersAndFileEntries(folderId, start, end, obc);
  115. }
  116. public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
  117. long folderId, int status, String[] mimeTypes,
  118. boolean includeMountFolders, int start, int end,
  119. OrderByComparator obc)
  120. throws PortalException, SystemException {
  121. return getFoldersAndFileEntries(folderId, mimeTypes, start, end, obc);
  122. }
  123. public int getFoldersAndFileEntriesAndFileShortcutsCount(
  124. long folderId, int status, boolean includeMountFolders)
  125. throws SystemException {
  126. return getFoldersAndFileEntriesCount(folderId);
  127. }
  128. public int getFoldersAndFileEntriesAndFileShortcutsCount(
  129. long folderId, int status, String[] mimeTypes,
  130. boolean includeMountFolders)
  131. throws PortalException, SystemException {
  132. return getFoldersAndFileEntriesCount(folderId, mimeTypes);
  133. }
  134. public abstract int getFoldersAndFileEntriesCount(long folderId)
  135. throws SystemException;
  136. public abstract int getFoldersAndFileEntriesCount(
  137. long folderId, String[] mimeTypes)
  138. throws PortalException, SystemException;
  139. public long getGroupId() {
  140. return _groupId;
  141. }
  142. public LocalRepository getLocalRepository() {
  143. return _localRepository;
  144. }
  145. public Object[] getRepositoryEntryIds(String objectId)
  146. throws SystemException {
  147. RepositoryEntry repositoryEntry = RepositoryEntryUtil.fetchByR_M(
  148. getRepositoryId(), objectId);
  149. if (repositoryEntry == null) {
  150. long repositoryEntryId = counterLocalService.increment();
  151. repositoryEntry = RepositoryEntryUtil.create(repositoryEntryId);
  152. repositoryEntry.setGroupId(getGroupId());
  153. repositoryEntry.setRepositoryId(getRepositoryId());
  154. repositoryEntry.setMappedId(objectId);
  155. RepositoryEntryUtil.update(repositoryEntry, false);
  156. }
  157. return new Object[] {
  158. repositoryEntry.getRepositoryEntryId(),
  159. repositoryEntry.getUuid()
  160. };
  161. }
  162. public List<FileEntry> getRepositoryFileEntries(
  163. long userId, long rootFolderId, int start, int end,
  164. OrderByComparator obc)
  165. throws SystemException {
  166. return getFileEntries(rootFolderId, start, end, obc);
  167. }
  168. public List<FileEntry> getRepositoryFileEntries(
  169. long userId, long rootFolderId, String[] mimeTypes, int status,
  170. int start, int end, OrderByComparator obc)
  171. throws PortalException, SystemException {
  172. return getFileEntries(rootFolderId, mimeTypes, start, end, obc);
  173. }
  174. public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
  175. throws SystemException {
  176. return getFileEntriesCount(rootFolderId);
  177. }
  178. public int getRepositoryFileEntriesCount(
  179. long userId, long rootFolderId, String[] mimeTypes, int status)
  180. throws PortalException, SystemException {
  181. return getFileEntriesCount(rootFolderId, mimeTypes);
  182. }
  183. public long getRepositoryId() {
  184. return _repositoryId;
  185. }
  186. public UnicodeProperties getTypeSettingsProperties() {
  187. return _typeSettingsProperties;
  188. }
  189. public abstract void initRepository()
  190. throws PortalException, SystemException;
  191. public Lock lockFileEntry(long fileEntryId) {
  192. throw new UnsupportedOperationException();
  193. }
  194. public Lock lockFileEntry(
  195. long fileEntryId, String owner, long expirationTime) {
  196. throw new UnsupportedOperationException();
  197. }
  198. public Hits search(SearchContext searchContext) throws SearchException {
  199. searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
  200. BooleanQuery fullQuery = RepositorySearchQueryBuilderUtil.getFullQuery(
  201. searchContext);
  202. return search(searchContext, fullQuery);
  203. }
  204. public void setAssetEntryLocalService(
  205. AssetEntryLocalService assetEntryLocalService) {
  206. this.assetEntryLocalService = assetEntryLocalService;
  207. }
  208. public void setCompanyId(long companyId) {
  209. _companyId = companyId;
  210. }
  211. public void setCompanyLocalService(
  212. CompanyLocalService companyLocalService) {
  213. this.companyLocalService = companyLocalService;
  214. }
  215. public void setCounterLocalService(
  216. CounterLocalService counterLocalService) {
  217. this.counterLocalService = counterLocalService;
  218. }
  219. public void setDLAppHelperLocalService(
  220. DLAppHelperLocalService dlAppHelperLocalService) {
  221. this.dlAppHelperLocalService = dlAppHelperLocalService;
  222. }
  223. public void setGroupId(long groupId) {
  224. _groupId = groupId;
  225. }
  226. public void setRepositoryId(long repositoryId) {
  227. _repositoryId = repositoryId;
  228. }
  229. public void setTypeSettingsProperties(
  230. UnicodeProperties typeSettingsProperties) {
  231. _typeSettingsProperties = typeSettingsProperties;
  232. }
  233. public void setUserLocalService(UserLocalService userLocalService) {
  234. this.userLocalService = userLocalService;
  235. }
  236. public void unlockFileEntry(long fileEntryId) {
  237. throw new UnsupportedOperationException();
  238. }
  239. public void unlockFileEntry(long fileEntryId, String lockUuid) {
  240. throw new UnsupportedOperationException();
  241. }
  242. public void unlockFolder(long parentFolderId, String title, String lockUuid)
  243. throws PortalException, SystemException {
  244. Folder folder = getFolder(parentFolderId, title);
  245. unlockFolder(folder.getFolderId(), lockUuid);
  246. }
  247. public FileEntry updateFileEntry(
  248. long fileEntryId, String sourceFileName, String mimeType,
  249. String title, String description, String changeLog,
  250. boolean majorVersion, File file, ServiceContext serviceContext)
  251. throws PortalException, SystemException {
  252. InputStream is = null;
  253. long size = 0;
  254. try {
  255. is = new FileInputStream(file);
  256. size = file.length();
  257. return updateFileEntry(
  258. fileEntryId, sourceFileName, mimeType, title, description,
  259. changeLog, majorVersion, is, size, serviceContext);
  260. }
  261. catch (IOException ioe) {
  262. throw new SystemException(ioe);
  263. }
  264. finally {
  265. if (is != null) {
  266. try {
  267. is.close();
  268. }
  269. catch (IOException ioe) {
  270. }
  271. }
  272. }
  273. }
  274. public boolean verifyFileEntryLock(long fileEntryId, String lockUuid) {
  275. throw new UnsupportedOperationException();
  276. }
  277. protected AssetEntryLocalService assetEntryLocalService;
  278. protected CompanyLocalService companyLocalService;
  279. protected CounterLocalService counterLocalService;
  280. protected DLAppHelperLocalService dlAppHelperLocalService;
  281. protected UserLocalService userLocalService;
  282. private long _companyId;
  283. private long _groupId;
  284. private LocalRepository _localRepository = new DefaultLocalRepositoryImpl(
  285. this);
  286. private long _repositoryId;
  287. private UnicodeProperties _typeSettingsProperties;
  288. }