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

https://github.com/portalcollc/liferay-portal · Java · 325 lines · 233 code · 74 blank · 18 comment · 6 complexity · 2c612e52488181b1601960a31f4ee820 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2011 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.search.BooleanQuery;
  21. import com.liferay.portal.kernel.search.Hits;
  22. import com.liferay.portal.kernel.search.Indexer;
  23. import com.liferay.portal.kernel.search.IndexerRegistryUtil;
  24. import com.liferay.portal.kernel.search.SearchContext;
  25. import com.liferay.portal.kernel.search.SearchEngineUtil;
  26. import com.liferay.portal.kernel.search.SearchException;
  27. import com.liferay.portal.kernel.util.OrderByComparator;
  28. import com.liferay.portal.kernel.util.UnicodeProperties;
  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.model.DLFileEntryConstants;
  36. import com.liferay.portlet.documentlibrary.service.DLAppHelperLocalService;
  37. import java.io.File;
  38. import java.io.FileInputStream;
  39. import java.io.IOException;
  40. import java.io.InputStream;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. /**
  44. * Third-party repository implementations should extend from this class.
  45. *
  46. * @author Alexander Chow
  47. */
  48. public abstract class BaseRepositoryImpl implements BaseRepository {
  49. public FileEntry addFileEntry(
  50. long folderId, String sourceFileName, String mimeType, String title,
  51. String description, String changeLog, File file,
  52. ServiceContext serviceContext)
  53. throws PortalException, SystemException {
  54. InputStream is = null;
  55. long size = 0;
  56. try {
  57. is = new FileInputStream(file);
  58. size = file.length();
  59. return addFileEntry(
  60. folderId, sourceFileName, mimeType, title, description,
  61. changeLog, is, size, serviceContext);
  62. }
  63. catch (IOException ioe) {
  64. throw new SystemException(ioe);
  65. }
  66. finally {
  67. if (is != null) {
  68. try {
  69. is.close();
  70. }
  71. catch (IOException ioe) {
  72. }
  73. }
  74. }
  75. }
  76. public void deleteFileEntry(long folderId, String title)
  77. throws PortalException, SystemException {
  78. FileEntry fileEntry = getFileEntry(folderId, title);
  79. deleteFileEntry(fileEntry.getFileEntryId());
  80. }
  81. public void deleteFolder(long parentFolderId, String title)
  82. throws PortalException, SystemException {
  83. Folder folder = getFolder(parentFolderId, title);
  84. deleteFolder(folder.getFolderId());
  85. }
  86. public long getCompanyId() {
  87. return _companyId;
  88. }
  89. public List<Object> getFileEntriesAndFileShortcuts(
  90. long folderId, int status, int start, int end)
  91. throws SystemException {
  92. return new ArrayList<Object>(
  93. getFileEntries(folderId, start, end, null));
  94. }
  95. public int getFileEntriesAndFileShortcutsCount(long folderId, int status)
  96. throws SystemException {
  97. return getFileEntriesCount(folderId);
  98. }
  99. public abstract List<Object> getFoldersAndFileEntries(
  100. long folderId, int start, int end, OrderByComparator obc)
  101. throws SystemException;
  102. public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
  103. long folderId, int status, boolean includeMountFolders, int start,
  104. int end, OrderByComparator obc)
  105. throws SystemException {
  106. return getFoldersAndFileEntries(folderId, start, end, obc);
  107. }
  108. public List<Object> getFoldersAndFileEntriesAndFileShortcuts(
  109. long folderId, int status, String[] mimeTypes,
  110. boolean includeMountFolders, int start, int end,
  111. OrderByComparator obc)
  112. throws SystemException {
  113. return getFoldersAndFileEntries(folderId, start, end, obc);
  114. }
  115. public int getFoldersAndFileEntriesAndFileShortcutsCount(
  116. long folderId, int status, boolean includeMountFolders)
  117. throws SystemException {
  118. return getFoldersAndFileEntriesCount(folderId);
  119. }
  120. public int getFoldersAndFileEntriesAndFileShortcutsCount(
  121. long folderId, int status, String[] mimeTypes,
  122. boolean includeMountFolders)
  123. throws SystemException {
  124. return getFoldersAndFileEntriesCount(folderId);
  125. }
  126. public abstract int getFoldersAndFileEntriesCount(long folderId)
  127. throws SystemException;
  128. public long getGroupId() {
  129. return _groupId;
  130. }
  131. public LocalRepository getLocalRepository() {
  132. return _localRepository;
  133. }
  134. public Object[] getRepositoryEntryIds(String objectId)
  135. throws SystemException {
  136. RepositoryEntry repositoryEntry = RepositoryEntryUtil.fetchByR_M(
  137. getRepositoryId(), objectId);
  138. if (repositoryEntry == null) {
  139. long repositoryEntryId = counterLocalService.increment();
  140. repositoryEntry = RepositoryEntryUtil.create(repositoryEntryId);
  141. repositoryEntry.setGroupId(getGroupId());
  142. repositoryEntry.setRepositoryId(getRepositoryId());
  143. repositoryEntry.setMappedId(objectId);
  144. RepositoryEntryUtil.update(repositoryEntry, false);
  145. }
  146. return new Object[] {
  147. repositoryEntry.getRepositoryEntryId(),
  148. repositoryEntry.getUuid()
  149. };
  150. }
  151. public List<FileEntry> getRepositoryFileEntries(
  152. long userId, long rootFolderId, int start, int end,
  153. OrderByComparator obc)
  154. throws SystemException {
  155. return getFileEntries(rootFolderId, start, end, obc);
  156. }
  157. public int getRepositoryFileEntriesCount(long userId, long rootFolderId)
  158. throws SystemException {
  159. return getFileEntriesCount(rootFolderId);
  160. }
  161. public long getRepositoryId() {
  162. return _repositoryId;
  163. }
  164. public UnicodeProperties getTypeSettingsProperties() {
  165. return _typeSettingsProperties;
  166. }
  167. public abstract void initRepository()
  168. throws PortalException, SystemException;
  169. public Hits search(SearchContext searchContext) throws SearchException {
  170. Indexer indexer = IndexerRegistryUtil.getIndexer(
  171. DLFileEntryConstants.getClassName());
  172. searchContext.setSearchEngineId(SearchEngineUtil.GENERIC_ENGINE_ID);
  173. BooleanQuery fullQuery = indexer.getFullQuery(searchContext);
  174. return search(searchContext, fullQuery);
  175. }
  176. public void setAssetEntryLocalService(
  177. AssetEntryLocalService assetEntryLocalService) {
  178. this.assetEntryLocalService = assetEntryLocalService;
  179. }
  180. public void setCompanyId(long companyId) {
  181. _companyId = companyId;
  182. }
  183. public void setCompanyLocalService(
  184. CompanyLocalService companyLocalService) {
  185. this.companyLocalService = companyLocalService;
  186. }
  187. public void setCounterLocalService(
  188. CounterLocalService counterLocalService) {
  189. this.counterLocalService = counterLocalService;
  190. }
  191. public void setDLAppHelperLocalService(
  192. DLAppHelperLocalService dlAppHelperLocalService) {
  193. this.dlAppHelperLocalService = dlAppHelperLocalService;
  194. }
  195. public void setGroupId(long groupId) {
  196. _groupId = groupId;
  197. }
  198. public void setRepositoryId(long repositoryId) {
  199. _repositoryId = repositoryId;
  200. }
  201. public void setTypeSettingsProperties(
  202. UnicodeProperties typeSettingsProperties) {
  203. _typeSettingsProperties = typeSettingsProperties;
  204. }
  205. public void setUserLocalService(UserLocalService userLocalService) {
  206. this.userLocalService = userLocalService;
  207. }
  208. public void unlockFolder(long parentFolderId, String title, String lockUuid)
  209. throws PortalException, SystemException {
  210. Folder folder = getFolder(parentFolderId, title);
  211. unlockFolder(folder.getFolderId(), lockUuid);
  212. }
  213. public FileEntry updateFileEntry(
  214. long fileEntryId, String sourceFileName, String mimeType,
  215. String title, String description, String changeLog,
  216. boolean majorVersion, File file, ServiceContext serviceContext)
  217. throws PortalException, SystemException {
  218. InputStream is = null;
  219. long size = 0;
  220. try {
  221. is = new FileInputStream(file);
  222. size = file.length();
  223. return updateFileEntry(
  224. fileEntryId, sourceFileName, mimeType, title, description,
  225. changeLog, majorVersion, is, size, serviceContext);
  226. }
  227. catch (IOException ioe) {
  228. throw new SystemException(ioe);
  229. }
  230. finally {
  231. if (is != null) {
  232. try {
  233. is.close();
  234. }
  235. catch (IOException ioe) {
  236. }
  237. }
  238. }
  239. }
  240. protected AssetEntryLocalService assetEntryLocalService;
  241. protected CompanyLocalService companyLocalService;
  242. protected CounterLocalService counterLocalService;
  243. protected DLAppHelperLocalService dlAppHelperLocalService;
  244. protected UserLocalService userLocalService;
  245. private long _companyId;
  246. private long _groupId;
  247. private LocalRepository _localRepository = new DefaultLocalRepositoryImpl(
  248. this);
  249. private long _repositoryId;
  250. private UnicodeProperties _typeSettingsProperties;
  251. }