001package co.codewizards.cloudstore.client;
002
003import java.net.MalformedURLException;
004import java.net.URL;
005import java.util.UUID;
006
007import org.kohsuke.args4j.Argument;
008
009import co.codewizards.cloudstore.core.repo.local.LocalRepoManager;
010import co.codewizards.cloudstore.core.repo.local.LocalRepoManagerFactory;
011import co.codewizards.cloudstore.core.repo.local.LocalRepoTransaction;
012import co.codewizards.cloudstore.local.persistence.RemoteRepository;
013import co.codewizards.cloudstore.local.persistence.RemoteRepositoryDAO;
014import co.codewizards.cloudstore.local.persistence.RemoteRepositoryRequest;
015import co.codewizards.cloudstore.local.persistence.RemoteRepositoryRequestDAO;
016
017/**
018 * {@link SubCommand} implementation for cancelling a connection with a remote repository.
019 *
020 * @author Marco หงุ่ยตระกูล-Schulze - marco at nightlabs dot de
021 */
022public class DropRepoConnectionSubCommand extends SubCommandWithExistingLocalRepo
023{
024        @Argument(metaVar="<remote>", index=1, required=true, usage="An ID or URL of a remote repository.")
025        private String remote;
026
027//      @Option(name="-localOnly", required=false, usage="Do not attempt to unregister the repo-connection on the server-side.")
028//      private boolean localOnly;
029
030        private UUID remoteRepositoryId;
031        private URL remoteRoot;
032
033        @Override
034        public String getSubCommandDescription() {
035                return "Cancel a connection to a remote repository. IMPORTANT: This does currently only operate locally. Thus, you have to cancel a connection manually on both sides.";
036        }
037
038        @Override
039        public void prepare() throws Exception {
040                super.prepare();
041
042                try {
043                        remoteRepositoryId = UUID.fromString(remote);
044                        remoteRoot = null;
045                } catch (IllegalArgumentException x) {
046                        try {
047                                remoteRoot = new URL(remote);
048                                remoteRepositoryId = null;
049                        } catch (MalformedURLException y) {
050                                throw new IllegalArgumentException(String.format("<remote> '%s' is neither a valid repositoryId nor a valid URL!", remote));
051                        }
052                }
053        }
054
055        @Override
056        public void run() throws Exception {
057                boolean foundSomethingToCancel = false;
058                UUID localRepositoryId;
059                LocalRepoManager localRepoManager = LocalRepoManagerFactory.Helper.getInstance().createLocalRepoManagerForExistingRepository(localRoot);
060                try {
061                        localRepositoryId = localRepoManager.getRepositoryId();
062                        LocalRepoTransaction transaction = localRepoManager.beginWriteTransaction();
063                        try {
064                                RemoteRepositoryDAO remoteRepositoryDAO = transaction.getDAO(RemoteRepositoryDAO.class);
065                                if (remoteRepositoryId != null) {
066                                        RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(remoteRepositoryId);
067                                        if (remoteRepository != null) {
068                                                foundSomethingToCancel = true;
069                                                remoteRoot = remoteRepository.getRemoteRoot();
070                                                remoteRepositoryDAO.deletePersistent(remoteRepository);
071                                                remoteRepositoryDAO.getPersistenceManager().flush();
072                                        }
073
074                                        RemoteRepositoryRequestDAO remoteRepositoryRequestDAO = transaction.getDAO(RemoteRepositoryRequestDAO.class);
075                                        RemoteRepositoryRequest remoteRepositoryRequest = remoteRepositoryRequestDAO.getRemoteRepositoryRequest(remoteRepositoryId);
076                                        if (remoteRepositoryRequest != null) {
077                                                foundSomethingToCancel = true;
078                                                remoteRepositoryRequestDAO.deletePersistent(remoteRepositoryRequest);
079                                                remoteRepositoryRequestDAO.getPersistenceManager().flush();
080                                        }
081                                }
082
083                                if (remoteRoot != null) {
084                                        RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(remoteRoot);
085                                        if (remoteRepository != null) {
086                                                foundSomethingToCancel = true;
087                                                remoteRepositoryId = remoteRepository.getRepositoryId();
088                                                remoteRepositoryDAO.deletePersistent(remoteRepository);
089                                                remoteRepositoryDAO.getPersistenceManager().flush();
090                                        }
091                                        // TODO automatically cancel on the remote side, too.
092                                }
093
094                                transaction.commit();
095                        } finally {
096                                transaction.rollbackIfActive();
097                        }
098                } finally {
099                        localRepoManager.close();
100                }
101
102                if (foundSomethingToCancel) {
103                        System.out.println("Successfully cancelled the connection from the local repository to the remote repository:");
104                        System.out.println();
105                        System.out.println("  localRepository.repositoryId = " + localRepositoryId);
106                        System.out.println("  localRepository.localRoot = " + localRoot);
107                        System.out.println();
108                        System.out.println("  remoteRepository.repositoryId = " + remoteRepositoryId);
109                        System.out.println("  remoteRepository.remoteRoot = " + remoteRoot);
110                        System.out.println();
111                        System.out.println("Important: This only cancelled the local side of the connection and you should cancel it on the other side, too, using this command (if you didn't do this yet):");
112                        System.out.println();
113                        System.out.println(String.format("  cloudstore dropRepoConnection %s %s", remoteRepositoryId, localRepositoryId));
114                }
115                else {
116                        System.out.println("There was nothing to be cancelled here. Maybe it was cancelled already before?!");
117                        System.out.println("Or maybe you want to instead run the following command on the other side (i.e. on the other computer - cancelling currently works only on one side):");
118                        System.out.println();
119                        System.out.println(String.format("  cloudstore dropRepoConnection %s %s", remoteRepositoryId, localRepositoryId));
120                }
121        }
122}