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

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