[21921] | 1 | package org.greenstone.admin.guiext.mat;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 |
|
---|
| 5 | import javax.swing.JFrame;
|
---|
| 6 | import javax.swing.JOptionPane;
|
---|
| 7 | import javax.swing.JTextArea;
|
---|
| 8 |
|
---|
| 9 | import org.tmatesoft.svn.core.SVNException;
|
---|
| 10 | import org.tmatesoft.svn.core.SVNURL;
|
---|
| 11 | import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
|
---|
| 12 | import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
|
---|
| 13 | import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
|
---|
| 14 | import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
|
---|
| 15 | import org.tmatesoft.svn.core.wc.SVNClientManager;
|
---|
| 16 | import org.tmatesoft.svn.core.wc.SVNRevision;
|
---|
| 17 | import org.tmatesoft.svn.core.wc.SVNUpdateClient;
|
---|
| 18 | import org.tmatesoft.svn.core.wc.SVNWCUtil;
|
---|
| 19 | import org.tmatesoft.svn.core.wc.ISVNEventHandler;
|
---|
| 20 |
|
---|
| 21 | import org.greenstone.admin.gui.*;
|
---|
| 22 |
|
---|
| 23 | public class MatWorkingCopy {
|
---|
| 24 |
|
---|
| 25 | private static SVNClientManager ourClientManager;
|
---|
| 26 | private static ISVNEventHandler myMatUpdateEventHandler;
|
---|
| 27 | public static boolean suc = true;
|
---|
| 28 | JTextArea messageTextArea;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | public void Download(JTextArea messageArea,String svnURL, String destination, String extensionName) throws SVNException {
|
---|
| 32 |
|
---|
| 33 | messageTextArea = messageArea;
|
---|
| 34 | setupLibrary();
|
---|
| 35 | SVNURL repositoryURL = null;
|
---|
| 36 | try {
|
---|
| 37 | repositoryURL = SVNURL.parseURIEncoded(svnURL);
|
---|
| 38 | } catch (SVNException e) {
|
---|
| 39 | e.printStackTrace();
|
---|
| 40 | }
|
---|
| 41 | String name = "";
|
---|
| 42 | String password = "";
|
---|
| 43 | String myMatWorkingCopyPath = destination;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | SVNURL url = repositoryURL;
|
---|
| 47 |
|
---|
| 48 | myMatUpdateEventHandler = new MatUpdateEventHandler(messageTextArea, destination, extensionName);
|
---|
| 49 |
|
---|
| 50 | DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
|
---|
| 51 |
|
---|
| 52 | ourClientManager = SVNClientManager.newInstance(options, name, password);
|
---|
| 53 |
|
---|
| 54 | ourClientManager.getUpdateClient().setEventHandler(myMatUpdateEventHandler);
|
---|
| 55 |
|
---|
| 56 | File wcDir = new File(myMatWorkingCopyPath);
|
---|
| 57 | if (wcDir.exists()) {
|
---|
| 58 |
|
---|
| 59 | Object[] option = {"Remove it!","Keep it!"};
|
---|
| 60 | int n = JOptionPane.showOptionDialog(new JFrame(),"The folder '"+ wcDir.getAbsolutePath() + "' already exists!","Attention",
|
---|
| 61 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
| 62 | JOptionPane.QUESTION_MESSAGE,
|
---|
| 63 | null,
|
---|
| 64 | option,
|
---|
| 65 | option[0]);
|
---|
| 66 |
|
---|
| 67 | if(n == 0){
|
---|
| 68 | deleteDir(wcDir);
|
---|
| 69 | }
|
---|
| 70 | else{return;}
|
---|
| 71 | }
|
---|
| 72 | wcDir.mkdirs();
|
---|
| 73 |
|
---|
| 74 | messageTextArea.append("Checking out a working copy from '" + url + "'...\n");
|
---|
| 75 |
|
---|
| 76 | try {
|
---|
| 77 | checkout(url, SVNRevision.HEAD, wcDir, true);
|
---|
| 78 | } catch (SVNException svne) {
|
---|
| 79 | error("error while checking out a working copy for the location '"
|
---|
| 80 | + url + "'", svne);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private static boolean deleteDir(File dir) {
|
---|
| 86 |
|
---|
| 87 | if (dir.isDirectory()) {
|
---|
| 88 | String[] children = dir.list();
|
---|
| 89 | for (int i=0; i<children.length; i++) {
|
---|
| 90 | boolean success = deleteDir(new File(dir, children[i]));
|
---|
| 91 | if (!success) {
|
---|
| 92 | return false;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | return dir.delete();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private static void setupLibrary() {
|
---|
| 100 |
|
---|
| 101 | DAVRepositoryFactory.setup();
|
---|
| 102 |
|
---|
| 103 | SVNRepositoryFactoryImpl.setup();
|
---|
| 104 |
|
---|
| 105 | FSRepositoryFactory.setup();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private static void checkout(SVNURL url,
|
---|
| 109 | SVNRevision revision, File destPath, boolean isRecursive)
|
---|
| 110 | throws SVNException {
|
---|
| 111 |
|
---|
| 112 | SVNUpdateClient updateClient = ourClientManager.getUpdateClient();
|
---|
| 113 |
|
---|
| 114 | updateClient.setIgnoreExternals(false);
|
---|
| 115 |
|
---|
| 116 | long x = updateClient.doCheckout(url, destPath, revision, revision, isRecursive);
|
---|
| 117 |
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public boolean getStatus(){
|
---|
| 121 | return suc;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | private static void error(String message, Exception e){
|
---|
| 125 | System.err.println(message+(e!=null ? ": "+e.getMessage() : ""));
|
---|
| 126 | JOptionPane.showMessageDialog(new JFrame(),Mat.DownloadErrorMsg);
|
---|
| 127 | suc = false;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|