MonkeyWrench.DB.CloneLane C# (CSharp) Method

CloneLane() public method

public CloneLane ( int lane_id, string new_name, bool copy_files ) : DBLane
lane_id int
new_name string
copy_files bool
return MonkeyWrench.DataClasses.DBLane
		public DBLane CloneLane (int lane_id, string new_name, bool copy_files)
		{
			DBLane result = null;
			DBLane master = DBLane_Extensions.Create (this, lane_id);

			if (this.LookupLane (new_name, false) != null)
				throw new Exception (string.Format ("The lane '{0}' already exists.", new_name));

			try {
				using (IDbTransaction transaction = BeginTransaction ()) {
					result = new DBLane ();
					result.lane = new_name;
					result.max_revision = master.max_revision;
					result.min_revision = master.min_revision;
					result.repository = master.repository;
					result.source_control = master.source_control;
					result.parent_lane_id = master.parent_lane_id;
					result.additional_roles = master.additional_roles;
					result.enabled = master.enabled;
					result.priority = 1;
					result.Save (this);

					foreach (DBLanefile filemaster in master.GetFiles (this, null)) {
						int fid;

						if (copy_files) {
							DBLanefile clone = new DBLanefile ();
							clone.contents = filemaster.contents;
							clone.mime = filemaster.mime;
							clone.name = filemaster.name;
							clone.additional_roles = filemaster.additional_roles;
							clone.Save (this);
							fid = clone.id;
						} else {
							fid = filemaster.id;
						}

						DBLanefiles lane_files = new DBLanefiles ();
						lane_files.lane_id = result.id;
						lane_files.lanefile_id = fid;
						lane_files.Save (this);
					}

					foreach (DBCommand cmdmaster in GetCommands (master.id)) {
						DBCommand clone = new DBCommand ();
						clone.lane_id = result.id;
						clone.alwaysexecute = cmdmaster.alwaysexecute;
						clone.arguments = cmdmaster.arguments;
						clone.command = cmdmaster.command;
						clone.filename = cmdmaster.filename;
						clone.nonfatal = cmdmaster.nonfatal;
						clone.sequence = cmdmaster.sequence;
						clone.timeout = cmdmaster.timeout;
						clone.working_directory = cmdmaster.working_directory;
						clone.upload_files = cmdmaster.upload_files;
						clone.Save (this);
					}

					foreach (DBHostLaneView hostlanemaster in master.GetHosts (this)) {
						DBHostLane clone = new DBHostLane ();
						clone.enabled = false;
						clone.lane_id = result.id;
						clone.host_id = hostlanemaster.host_id;
						clone.Save (this);
					}

					foreach (DBEnvironmentVariable env in master.GetEnvironmentVariables (this)) {
						DBEnvironmentVariable clone = new DBEnvironmentVariable ();
						clone.host_id = env.host_id;
						clone.lane_id = result.id;
						clone.name = env.name;
						clone.value = env.value;
						clone.Save (this);
					}

					foreach (DBLaneNotification notification in master.GetNotifications (this)) {
						DBLaneNotification clone = new DBLaneNotification ();
						clone.lane_id = result.id;
						clone.notification_id = notification.notification_id;
						clone.Save (this);
					}

					foreach (var tag in master.GetTags (this)) {
						var clone = new DBLaneTag ();
						clone.lane_id = result.id;
						clone.tag = tag.tag;
						clone.Save (this);
					}

					transaction.Commit ();
				}
			} catch {
				result = null;
				throw;
			}

			return result;
		}