package org.jenkinsci.plugins.p4;

import hudson.Extension;
import hudson.XmlFile;
import hudson.model.Descriptor;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.scm.SCM;
import hudson.util.LogTaskListener;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.p4.PerforceScm.DescriptorImpl;
import org.jenkinsci.plugins.p4.client.ClientHelper;
import org.jenkinsci.plugins.p4.client.ViewMapHelper;
import org.jenkinsci.plugins.p4.publish.SubmitImpl;
import org.jenkinsci.plugins.p4.workspace.ManualWorkspaceImpl;
import org.jenkinsci.plugins.p4.workspace.WorkspaceSpec;

import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

@Extension
public class ConfigurationListener extends SaveableListener {

	private static final Logger logger = Logger.getLogger(ConfigurationListener.class.getName());

	public void onChange(Saveable o, XmlFile xml) {
		Jenkins j = Jenkins.getInstance();

		@SuppressWarnings("unchecked")
		Descriptor<SCM> scm = j.getDescriptor(PerforceScm.class);
		DescriptorImpl p4scm = (DescriptorImpl) scm;

		// Exit early if disabled
		if (p4scm == null || !p4scm.isAutoSave()) {
			return;
		}

		try {
			String file = xml.getFile().getCanonicalPath();
			logger.info(">>> onUpdated: " + file);

			// create Publish object
			String desc = "Configuration change";
			boolean success = false;
			boolean delete = true;
			boolean modtime = false;
			boolean reopen = false;
			String purge = "";
			SubmitImpl publish = new SubmitImpl(desc, success, delete, modtime, reopen, purge);

			try (ClientHelper p4 = getClientHelper(p4scm)) {
				int ChangelistID = -1;

				if (!p4scm.isAutoSubmitOnChange()) {
					ChangelistID = p4.findPendingChangelistIDByDesc(desc, p4scm.getClientName());
				}
				p4.versionFile(file, publish, ChangelistID, p4scm.isAutoSubmitOnChange());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private ClientHelper getClientHelper(DescriptorImpl p4scm) throws Exception {

		LogTaskListener listener = new LogTaskListener(logger, Level.INFO);

		String credential = p4scm.getCredential();
		String clientName = p4scm.getClientName();
		String depotPath = p4scm.getDepotPath();

		// check path ends with '/...'
		if(!depotPath.endsWith("/...")) {
			depotPath = depotPath.endsWith("/") ? depotPath + "..." : depotPath + "/...";
		}

		// quote path it it has spaces
		if(depotPath.contains(" ")) {
			depotPath = "\"" + depotPath + "\"";
		}

		Jenkins j = Jenkins.getInstance();
		String rootPath = j.getRootDir().getCanonicalPath();

		String view = ViewMapHelper.getClientView(depotPath, clientName, true);

		WorkspaceSpec spec = new WorkspaceSpec(true, true, false, false, false, false, "", "LOCAL", view, null, null, null, true);

		ManualWorkspaceImpl workspace = new ManualWorkspaceImpl("utf8", false, clientName, spec, false);
		workspace.setExpand(new HashMap<>());
		workspace.setRootPath(rootPath);

		return new ClientHelper(Jenkins.getInstance(), credential, listener, workspace);
	}
}

@@ -80,40 +80,8 @@
 le;%0A
-import hudson.model.Descriptor;%0A
 impo
@@ -158,31 +158,8 @@
 er;%0A
-import hudson.scm.SCM;%0A
 impo
@@ -560,16 +560,44 @@
 eSpec;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -905,117 +905,104 @@
 %7B%0A%09%09
-Jenkins j = Jenkins.getInstance();%0A%0A%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09Descriptor%3CSCM%3E scm = j.getDescriptor(
+if (!(o instanceof PerforceScm.DescriptorImpl)) %7B%0A%09%09%09// must be a PerforceScm%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09
 Perf
@@ -1013,18 +1013,8 @@
 Scm.
-class);%0A%09%09
 Desc
@@ -1033,16 +1033,28 @@
 4scm = (
+PerforceScm.
 Descript
@@ -1061,19 +1061,17 @@
 orImpl) 
-scm
+o
 ;%0A%0A%09%09// 
@@ -1103,25 +1103,8 @@
 if (
-p4scm == null %7C%7C 
 !p4s
@@ -1855,65 +1855,46 @@
 %0A%09%09%09
-// TODO Auto-generated catch block%0A%09%09%09e.printStackTrac
+logger.severe(e.getLocalizedMessag
 e()
+)
 ;%0A%09%09
@@ -1966,16 +1966,18 @@
  throws 
+IO
 Exceptio
@@ -2223,16 +2223,17 @@
 ..'%0A%09%09if
+ 
 (!depotP
@@ -2359,17 +2359,17 @@
 e path i
-t
+f
  it has 
@@ -2379,16 +2379,17 @@
 ces%0A%09%09if
+ 
 (depotPa

Check Item is PerforceScm.DescriptorImpl onChange event. JENKINS-68378