001package co.codewizards.cloudstore.local; 002 003import static co.codewizards.cloudstore.core.oio.OioFileFactory.*; 004import static java.util.Objects.*; 005 006import java.util.ArrayList; 007import java.util.List; 008 009import co.codewizards.cloudstore.core.dto.RepoFileDto; 010import co.codewizards.cloudstore.core.oio.File; 011import co.codewizards.cloudstore.core.repo.local.LocalRepoManager; 012import co.codewizards.cloudstore.core.repo.local.LocalRepoMetaData; 013import co.codewizards.cloudstore.core.repo.local.LocalRepoTransaction; 014import co.codewizards.cloudstore.local.dto.RepoFileDtoConverter; 015import co.codewizards.cloudstore.local.persistence.RepoFile; 016import co.codewizards.cloudstore.local.persistence.RepoFileDao; 017 018public class LocalRepoMetaDataImpl implements LocalRepoMetaData { 019 020 private LocalRepoManager localRepoManager; 021 022 /** 023 * Gets the {@link LocalRepoManager}. 024 * <p> 025 * <b>Important:</b> This must not be exposed! It is the real internal single instance - not the proxy! 026 * @return the {@link LocalRepoManager}. Never <code>null</code> in normal operation. 027 */ 028 protected LocalRepoManager getLocalRepoManager() { 029 return localRepoManager; 030 } 031 protected LocalRepoManager getLocalRepoManagerOrFail() { 032 return requireNonNull(localRepoManager, "localRepoManager"); 033 } 034 protected void setLocalRepoManager(LocalRepoManager localRepoManager) { 035 this.localRepoManager = localRepoManager; 036 } 037 038 /** 039 * Begin a JDO transaction for read operations only in the underlying database. 040 * @return the transaction handle. Never <code>null</code>. 041 */ 042 protected LocalRepoTransaction beginReadTransaction() { 043 return getLocalRepoManagerOrFail().beginReadTransaction(); 044 } 045 046 /** 047 * Begin a JDO transaction for read and write operations in the underlying database. 048 * @return the transaction handle. Never <code>null</code>. 049 */ 050 protected LocalRepoTransaction beginWriteTransaction() { 051 return getLocalRepoManagerOrFail().beginWriteTransaction(); 052 } 053 054 @Override 055 public RepoFileDto getRepoFileDto(final String path, final int depth) { 056 requireNonNull(path, "path"); 057 058 final RepoFileDto result; 059 try (final LocalRepoTransaction tx = beginReadTransaction();) { 060 final RepoFileDtoConverter converter = RepoFileDtoConverter.create(tx); 061 062 final File localRoot = getLocalRepoManagerOrFail().getLocalRoot(); 063 final File file = createFile(localRoot, path); 064 final RepoFile repoFile = tx.getDao(RepoFileDao.class).getRepoFile(localRoot, file); 065 066 if (repoFile == null) 067 result = null; 068 else 069 result = converter.toRepoFileDto(repoFile, depth); 070 071 tx.commit(); 072 } 073 return result; 074 } 075 076 @Override 077 public RepoFileDto getRepoFileDto(long repoFileId, int depth) { 078 final RepoFileDto result; 079 try (final LocalRepoTransaction tx = beginReadTransaction();) { 080 final RepoFileDtoConverter converter = RepoFileDtoConverter.create(tx); 081 082 final RepoFile repoFile = tx.getDao(RepoFileDao.class).getObjectByIdOrNull(repoFileId); 083 084 if (repoFile == null) 085 result = null; 086 else 087 result = converter.toRepoFileDto(repoFile, depth); 088 089 tx.commit(); 090 } 091 return result; 092 } 093 094 @Override 095 public List<RepoFileDto> getChildRepoFileDtos(final long repoFileId, final int depth) { 096 final List<RepoFileDto> result; 097 try (final LocalRepoTransaction tx = beginReadTransaction();) { 098 final RepoFile repoFile = tx.getDao(RepoFileDao.class).getObjectByIdOrNull(repoFileId); 099 result = getChildRepoFileDtos(tx, repoFile, depth); 100 101 tx.commit(); 102 } 103 return result; 104 } 105 106 @Override 107 public List<RepoFileDto> getChildRepoFileDtos(final String path, final int depth) { 108 final List<RepoFileDto> result; 109 try (final LocalRepoTransaction tx = beginReadTransaction();) { 110 final File localRoot = getLocalRepoManagerOrFail().getLocalRoot(); 111 final File file = createFile(localRoot, path); 112 final RepoFile repoFile = tx.getDao(RepoFileDao.class).getRepoFile(localRoot, file); 113 result = getChildRepoFileDtos(tx, repoFile, depth); 114 115 tx.commit(); 116 } 117 return result; 118 } 119 120 private List<RepoFileDto> getChildRepoFileDtos(final LocalRepoTransaction tx, final RepoFile repoFile, final int depth) { 121 if (repoFile == null) 122 return null; 123 else { 124 final RepoFileDao repoFileDao = tx.getDao(RepoFileDao.class); 125 final RepoFileDtoConverter converter = RepoFileDtoConverter.create(tx); 126 127 final List<RepoFileDto> result = new ArrayList<>(); 128 for (final RepoFile childRepoFile : repoFileDao.getChildRepoFiles(repoFile)) { 129 final RepoFileDto dto = converter.toRepoFileDto(childRepoFile, depth); 130 result.add(dto); 131 } 132 return result; 133 } 134 } 135}