001package co.codewizards.cloudstore.client;
002
003import co.codewizards.cloudstore.core.dto.DateTime;
004import co.codewizards.cloudstore.core.oio.File;
005import co.codewizards.cloudstore.core.util.AssertUtil;
006import co.codewizards.cloudstore.core.util.HashUtil;
007import co.codewizards.cloudstore.ls.core.dto.RemoteRepositoryDto;
008import co.codewizards.cloudstore.ls.core.dto.RemoteRepositoryRequestDto;
009import co.codewizards.cloudstore.ls.core.dto.RepoInfoRequestDto;
010import co.codewizards.cloudstore.ls.core.dto.RepoInfoResponseDto;
011import co.codewizards.cloudstore.ls.rest.client.request.RepoInfoRequest;
012
013/**
014 * {@link SubCommand} implementation for showing information about a repository in the local file system.
015 *
016 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
017 */
018public class RepoInfoSubCommand extends SubCommandWithExistingLocalRepo
019{
020        public RepoInfoSubCommand() { }
021
022        protected RepoInfoSubCommand(final File localRoot) {
023                this.localRoot = AssertUtil.assertNotNull(localRoot, "localRoot");
024                this.localFile = this.localRoot;
025                this.local = localRoot.getPath();
026        }
027
028        @Override
029        public String getSubCommandDescription() {
030                return "Show information about an existing repository.";
031        }
032
033        @Override
034        public void run() throws Exception {
035                final RepoInfoRequestDto repoInfoRequestDto = new RepoInfoRequestDto();
036                repoInfoRequestDto.setLocalRoot(localRoot.getAbsolutePath());
037                final RepoInfoResponseDto repoInfoResponseDto = getLocalServerRestClient().execute(new RepoInfoRequest(repoInfoRequestDto));
038
039                showMainProperties(repoInfoResponseDto);
040                showRemoteRepositories(repoInfoResponseDto);
041                showRemoteRepositoryRequests(repoInfoResponseDto);
042                showRepositoryStats(repoInfoResponseDto);
043        }
044
045        private void showMainProperties(final RepoInfoResponseDto repoInfoResponseDto) {
046                System.out.println("Local repository:");
047                System.out.println("  repository.repositoryId = " + repoInfoResponseDto.getRepositoryId());
048                System.out.println("  repository.localRoot = " + repoInfoResponseDto.getLocalRoot());
049                System.out.println("  repository.aliases = " + repoInfoResponseDto);
050                System.out.println("  repository.publicKeySha1 = " + HashUtil.sha1ForHuman(repoInfoResponseDto.getPublicKey()));
051                System.out.println();
052        }
053
054        private void showRemoteRepositories(final RepoInfoResponseDto repoInfoResponseDto) {
055                if (repoInfoResponseDto.getRemoteRepositoryDtos().isEmpty()) {
056                        System.out.println("Remote repositories connected: {NONE}");
057                        System.out.println();
058                }
059                else {
060                        System.out.println("Remote repositories connected:");
061                        for (final RemoteRepositoryDto remoteRepositoryDto : repoInfoResponseDto.getRemoteRepositoryDtos()) {
062                                System.out.println("  * remoteRepository.repositoryId = " + remoteRepositoryDto.getRepositoryId());
063                                if (remoteRepositoryDto.getRemoteRoot() != null)
064                                        System.out.println("    remoteRepository.remoteRoot = " + remoteRepositoryDto.getRemoteRoot());
065
066                                System.out.println("    remoteRepository.publicKeySha1 = " + HashUtil.sha1ForHuman(remoteRepositoryDto.getPublicKey()));
067                                System.out.println();
068                        }
069                }
070        }
071
072        private void showRemoteRepositoryRequests(final RepoInfoResponseDto repoInfoResponseDto) {
073                if (repoInfoResponseDto.getRemoteRepositoryRequestDtos().isEmpty()) {
074                        System.out.println("Remote repositories requesting connection: {NONE}");
075                        System.out.println();
076                }
077                else {
078                        System.out.println("Remote repositories requesting connection:");
079                        for (final RemoteRepositoryRequestDto remoteRepositoryRequestDto : repoInfoResponseDto.getRemoteRepositoryRequestDtos()) {
080                                System.out.println("  * remoteRepositoryRequest.repositoryId = " + remoteRepositoryRequestDto.getRepositoryId());
081                                System.out.println("    remoteRepositoryRequest.publicKeySha1 = " + HashUtil.sha1ForHuman(remoteRepositoryRequestDto.getPublicKey()));
082                                System.out.println("    remoteRepositoryRequest.created = " + new DateTime(remoteRepositoryRequestDto.getCreated()));
083                                System.out.println("    remoteRepositoryRequest.changed = " + new DateTime(remoteRepositoryRequestDto.getChanged()));
084                                System.out.println();
085                        }
086                }
087        }
088
089        private void showRepositoryStats(final RepoInfoResponseDto repoInfoResponseDto) {
090                System.out.println("Statistics:");
091                System.out.println("  * Count(NormalFile): " + repoInfoResponseDto.getNormalFileCount());
092                System.out.println("  * Count(Directory): " + repoInfoResponseDto.getDirectoryCount());
093                System.out.println("  * Count(CopyModification): " + repoInfoResponseDto.getCopyModificationCount());
094                System.out.println("  * Count(DeleteModification): " + repoInfoResponseDto.getDeleteModificationCount());
095                System.out.println();
096        }
097}