View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.dist.commands;
17  
18  import javax.jcr.Node;
19  import javax.jcr.Repository;
20  import javax.jcr.RepositoryException;
21  import javax.jcr.Session;
22  import javax.jcr.security.Privilege;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.argeo.cms.ui.workbench.util.PrivilegedJob;
27  import org.argeo.eclipse.ui.EclipseJcrMonitor;
28  import org.argeo.jcr.JcrMonitor;
29  import org.argeo.jcr.JcrUtils;
30  import org.argeo.slc.SlcConstants;
31  import org.argeo.slc.SlcException;
32  import org.argeo.slc.client.ui.dist.DistPlugin;
33  import org.argeo.slc.client.ui.dist.utils.CommandHelpers;
34  import org.argeo.slc.repo.JavaRepoManager;
35  import org.argeo.slc.repo.RepoUtils;
36  import org.eclipse.core.commands.AbstractHandler;
37  import org.eclipse.core.commands.ExecutionEvent;
38  import org.eclipse.core.commands.ExecutionException;
39  import org.eclipse.core.runtime.IProgressMonitor;
40  import org.eclipse.core.runtime.IStatus;
41  import org.eclipse.core.runtime.Status;
42  import org.eclipse.core.runtime.jobs.Job;
43  import org.eclipse.jface.dialogs.ErrorDialog;
44  import org.eclipse.jface.dialogs.InputDialog;
45  import org.eclipse.jface.resource.ImageDescriptor;
46  import org.eclipse.jface.window.Window;
47  import org.eclipse.swt.widgets.Display;
48  import org.eclipse.ui.handlers.HandlerUtil;
49  
50  /**
51   * Create a copy of the chosen workspace in the local Java repository using a
52   * JavaRepoManager.
53   */
54  public class CopyLocalJavaWorkspace extends AbstractHandler {
55  	private static final Log log = LogFactory
56  			.getLog(CopyLocalJavaWorkspace.class);
57  
58  	public final static String ID = DistPlugin.PLUGIN_ID + ".copyLocalJavaWorkspace";
59  	public final static String DEFAULT_LABEL = "Copy Java Workspace...";
60  	public final static ImageDescriptor DEFAULT_ICON = DistPlugin
61  			.getImageDescriptor("icons/addItem.gif");
62  
63  	public final static String PARAM_SOURCE_WORKSPACE_NAME = "srcWkspName";
64  
65  	// DEPENDENCY INJECTION
66  	private Repository javaRepository;
67  	private JavaRepoManager javaRepoManager;
68  
69  	public Object execute(ExecutionEvent event) throws ExecutionException {
70  		String wkspName = event.getParameter(PARAM_SOURCE_WORKSPACE_NAME);
71  
72  		InputDialog inputDialog = new InputDialog(HandlerUtil
73  				.getActiveWorkbenchWindow(event).getShell(),
74  				"New copy of workspace " + wkspName,
75  				"Choose a name for the workspace to create", "", null);
76  		int result = inputDialog.open();
77  		if (result == Window.OK) {
78  			String newWorkspaceName = inputDialog.getValue();
79  
80  			if (newWorkspaceName == null || newWorkspaceName.trim().equals("")
81  					|| newWorkspaceName.trim().equals(wkspName.trim())) {
82  				ErrorDialog
83  						.openError(HandlerUtil.getActiveShell(event),
84  								"Non valid workspace name", newWorkspaceName
85  										+ " is not a valid workspace name.",
86  								new Status(IStatus.ERROR, "not valid", 0,
87  										"Error", null));
88  				return null;
89  			}
90  			Job copyWkspJob = new CopyWkspJob(javaRepoManager, javaRepository,
91  					wkspName, newWorkspaceName, HandlerUtil
92  							.getActiveWorkbenchWindow(event).getShell()
93  							.getDisplay());
94  			copyWkspJob.setUser(true);
95  			copyWkspJob.schedule();
96  		}
97  		return null;
98  	}
99  
100 	private static class CopyWkspJob extends PrivilegedJob {
101 
102 		private JavaRepoManager javaRepoManager;
103 		private Repository javaRepository;
104 		private String srcWkspName;
105 		private String targetWkspName;
106 		private Display display;
107 
108 		public CopyWkspJob(JavaRepoManager javaRepoManager,
109 				Repository javaRepository, String srcWkspName,
110 				String targetWkspName, Display display) {
111 			super("Duplicate workspace");
112 			this.javaRepoManager = javaRepoManager;
113 			this.javaRepository = javaRepository;
114 			this.srcWkspName = srcWkspName;
115 			this.targetWkspName = targetWkspName;
116 			this.display = display;
117 		}
118 
119 		@Override
120 		protected IStatus doRun(IProgressMonitor progressMonitor) {
121 			long begin = System.currentTimeMillis();
122 
123 			JcrMonitor monitor = new EclipseJcrMonitor(progressMonitor);
124 			monitor.beginTask("Copy workspace", -1);
125 			monitor.subTask("Copying nodes");
126 
127 			Session srcSession = null;
128 			Session targetSession = null;
129 			try {
130 				// Initialize source
131 				srcSession = javaRepository.login(srcWkspName);
132 				Node srcRootNode = srcSession.getRootNode();
133 
134 				// Create the workspace -
135 				// FIXME will throw an error if workspace already exists
136 				javaRepoManager.createWorkspace(targetWkspName);
137 				targetSession = javaRepository.login(targetWkspName);
138 				Node newRootNode = targetSession.getRootNode();
139 
140 				RepoUtils.copy(srcRootNode, newRootNode, monitor);
141 				targetSession.save();
142 				JcrUtils.addPrivilege(targetSession, "/",
143 						SlcConstants.ROLE_SLC, Privilege.JCR_ALL);
144 				monitor.worked(1);
145 
146 				display.asyncExec(new Runnable() {
147 					public void run() {
148 						CommandHelpers.callCommand(RefreshDistributionsView.ID);
149 					}
150 				});
151 
152 			} catch (RepositoryException re) {
153 				throw new SlcException(
154 						"Unexpected error while creating the new workspace.",
155 						re);
156 			} finally {
157 				JcrUtils.logoutQuietly(srcSession);
158 				JcrUtils.logoutQuietly(targetSession);
159 			}
160 
161 			monitor.done();
162 			long duration = (System.currentTimeMillis() - begin) / 1000;// in
163 																		// s
164 			if (log.isDebugEnabled())
165 				log.debug("Duplicated local java workspace " + srcWkspName
166 						+ " to workspace " + targetWkspName + " in "
167 						+ (duration / 60) + "min " + (duration % 60) + "s");
168 			return Status.OK_STATUS;
169 		}
170 	}
171 
172 	/* DEPENDENCY INJECTION */
173 	public void setJavaRepository(Repository javaRepository) {
174 		this.javaRepository = javaRepository;
175 	}
176 
177 	public void setJavaRepoManager(JavaRepoManager javaRepoManager) {
178 		this.javaRepoManager = javaRepoManager;
179 	}
180 }