public ActionResult Save(VariableEditModel model) { this.CheckPermission(UserRoleAction.EnvironmentChangeVariables); DataField dataField; if (model.VariableId == 0) { dataField = new DataField(); dataField.TypeId = 1; dataField.Mode = DataFieldMode.Global; dataField.IsDeleted = false; this.Entities.DataField.Add(dataField); } else { dataField = this.Entities.DataField .Include("DataFieldValues.Environment") .Include("DataFieldValues.Machine") .Include("BundleVersions") .First(df => df.Id == model.VariableId && !df.IsDeleted && df.Mode == DataFieldMode.Global); } Environment environment = this.Entities.Environment.Include("Machines").First(e => e.Id == model.EnvironmentId); if (model.MachineId.HasValue && environment.Machines.All(m => m.Id != model.MachineId)) { return(this.Redirect("/")); } if (!this.ModelState.IsValid) { this.ViewBag.DataField = dataField; this.ViewBag.Environment = environment; return(this.RedirectToAction("Edit", new { id = model.VariableId, environmentId = model.EnvironmentId })); } DataFieldValue value = dataField.DataFieldValues.FirstOrDefault(v => v.EnvironmentId == environment.Id && v.MachineId == model.MachineId); if (value == null) { value = new DataFieldValue(); value.DataField = dataField; value.Environment = environment; value.MachineId = model.MachineId; this.Entities.DataFieldValue.Add(value); } dataField.Key = model.Name; dataField.IsSensitive = model.IsSensitive; value.Value = model.Value; this.Entities.SaveChanges(); return(this.RedirectToAction("Details", new { id = dataField.Id })); }
private IDictionary <string, string> CreateDataFieldsDictionary(int machineId, IEnumerable <DataField> globalDataFields, IEnumerable <DataField> bundleDataFields, int environmentId) { IDictionary <string, string> dataFieldsDictionary = new Dictionary <string, string>(); foreach (DataField bundleDataField in bundleDataFields) { string keyLower = bundleDataField.Key.ToLowerInvariant(); if (dataFieldsDictionary.ContainsKey(keyLower)) { continue; } DataFieldValue dataFieldValue = bundleDataField.DataFieldValues.FirstOrDefault(); if (dataFieldValue != null) { dataFieldsDictionary.Add(keyLower, dataFieldValue.Value); } } foreach (DataField globalDataField in globalDataFields) { string keyLower = globalDataField.Key.ToLowerInvariant(); if (dataFieldsDictionary.ContainsKey(keyLower)) { continue; } DataFieldValue machineValue = globalDataField.DataFieldValues.FirstOrDefault(dfv => dfv.MachineId == machineId && dfv.EnvironmentId == environmentId); if (machineValue != null) { dataFieldsDictionary.Add(keyLower, machineValue.Value); continue; } DataFieldValue environmentValue = globalDataField.DataFieldValues.FirstOrDefault(dfv => dfv.EnvironmentId == environmentId); if (environmentValue != null) { dataFieldsDictionary.Add(keyLower, environmentValue.Value); continue; } } return(dataFieldsDictionary); }
public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (nominalType != typeof(FieldsWrapper)) { throw new ArgumentException("Cannot deserialize anything but self"); } var doc = BsonDocument.ReadFrom(bsonReader); var list = new List <DataFieldValue>(); foreach (var name in doc.Names) { var val = doc[name]; if (val.IsString) { list.Add(new DataFieldValue { LocalIdentifier = name, Values = new List <string> { val.AsString } }); } else if (val.IsBsonArray) { DataFieldValue df = new DataFieldValue { LocalIdentifier = name }; foreach (var elem in val.AsBsonArray) { df.Values.Add(elem.AsString); } list.Add(df); } } return(new FieldsWrapper { DataFieldValues = list }); }
public ActionResult CreateNewVersion(int fromBundleVersionId, string jsonData, string newVersionName, bool isHotfix = false) { this.CheckPermission(UserRoleAction.VersionCreate); if (string.IsNullOrWhiteSpace(newVersionName)) { return(this.RedirectToAction("CreateNewVersion", new { id = fromBundleVersionId, isHotfix })); } BundleVersion sourceBundleVersion = this.Entities.BundleVersion .Include("Bundle") .Include("ProjectVersions.Project") .Include("ProjectVersions.SourceControlVersion.SourceControl") .Include("Properties") .Include("DataFields.DataFieldValues.Environment") .Include("DataFields.DataFieldValues.Machine") .Include("DeploymentSteps.Properties") .Include("DeploymentSteps.MachineRoles") .First(bv => bv.Id == fromBundleVersionId); dynamic data = JsonConvert.DeserializeObject(jsonData); IEnumerable <dynamic> projects = (IEnumerable <dynamic>)data.projects; IEnumerable <dynamic> variables = (IEnumerable <dynamic>)data.variables; Dictionary <int, ProjectVersion> mapping = projects .Select(p => new { projectVersionId = (int)p.projectVersionId, sourceControlVersionId = (int)p.sourceControlVersionId }) .ToList() .Select(p => new { p.projectVersionId, p.sourceControlVersionId, projectVersion = sourceBundleVersion.ProjectVersions.First(pv => pv.Id == p.projectVersionId) }) .ToList() .Select(p => new { p.projectVersion, newProjectVersion = this.Entities.SourceControlVersion .Where(scv => scv.Id == p.sourceControlVersionId) .ToList() .SelectMany(scv => scv.ProjectVersions) .FirstOrDefault(pn => pn.Project == p.projectVersion.Project) }) .ToList() .ToDictionary(k => k.projectVersion.Id, v => v.newProjectVersion); BundleVersion newBundleVersion = new BundleVersion(); newBundleVersion.IsHead = false; newBundleVersion.Bundle = sourceBundleVersion.Bundle; newBundleVersion.Name = newVersionName.Trim(); newBundleVersion.ParentBundleVersion = sourceBundleVersion; if (sourceBundleVersion.IsHead) { sourceBundleVersion.IsHead = false; newBundleVersion.IsHead = true; } if (sourceBundleVersion.GetIntProperty("AutoDeployToEnvironment") > 0) { newBundleVersion.SetStringProperty("AutoDeployToEnvironment", sourceBundleVersion.GetIntProperty("AutoDeployToEnvironment").ToString(CultureInfo.InvariantCulture)); } if (sourceBundleVersion.GetIntProperty("HomeEnvironment") > 0) { newBundleVersion.SetStringProperty("HomeEnvironment", sourceBundleVersion.GetIntProperty("HomeEnvironment").ToString(CultureInfo.InvariantCulture)); } this.Entities.BundleVersion.Add(newBundleVersion); foreach (dynamic variable in variables) { int id = (int)variable.id; string value = (string)variable.value; DataField dataField = sourceBundleVersion.DataFields.First(df => df.Id == id && !df.IsDeleted); DataField newdataField = new DataField() { IsDeleted = dataField.IsDeleted, IsSensitive = dataField.IsSensitive, Mode = dataField.Mode, Key = dataField.Key, TypeId = dataField.TypeId }; this.Entities.DataField.Add(newdataField); newBundleVersion.DataFields.Add(newdataField); DataFieldValue dataFieldValue = new DataFieldValue(); dataFieldValue.DataField = newdataField; dataFieldValue.Value = value; this.Entities.DataFieldValue.Add(dataFieldValue); } IList <ProjectVersion> usedProjectVersions = new List <ProjectVersion>(); foreach (DeploymentStep sourceDeploymentStep in sourceBundleVersion.DeploymentSteps) { DeploymentStep newDeploymentStep = new DeploymentStep(); newDeploymentStep.BundleVersion = newBundleVersion; newDeploymentStep.OrderIndex = sourceDeploymentStep.OrderIndex; newDeploymentStep.Type = sourceDeploymentStep.Type; foreach (MachineRole machineRole in sourceDeploymentStep.MachineRoles) { newDeploymentStep.MachineRoles.Add(machineRole); } this.Entities.DeploymentStep.Add(newDeploymentStep); foreach (DeploymentStepProperty sourceDeploymentStepProperty in sourceDeploymentStep.Properties) { DeploymentStepProperty newDeploymentStepProperty = new DeploymentStepProperty(); newDeploymentStepProperty.DeploymentStep = newDeploymentStep; newDeploymentStepProperty.Key = sourceDeploymentStepProperty.Key; if (sourceDeploymentStepProperty.Key == "ProjectId") { var newProjectVersion = mapping[int.Parse(sourceDeploymentStepProperty.Value)]; newDeploymentStepProperty.Value = newProjectVersion.Id.ToString(CultureInfo.InvariantCulture); usedProjectVersions.Add(newProjectVersion); } else { newDeploymentStepProperty.Value = sourceDeploymentStepProperty.Value; } this.Entities.DeploymentStepProperty.Add(newDeploymentStepProperty); } } foreach (ProjectVersion usedProjectVersion in usedProjectVersions) { newBundleVersion.ProjectVersions.Add(usedProjectVersion); } this.Entities.SaveChanges(); if (isHotfix) { List <BundleVersion> children = this.Entities.BundleVersion.Where(bv => bv.ParentBundleVersionId == sourceBundleVersion.Id).ToList(); foreach (BundleVersion bundleVersion in children) { bundleVersion.ParentBundleVersionId = newBundleVersion.Id; } newBundleVersion.ParentBundleVersion = sourceBundleVersion; } this.Entities.SaveChanges(); return(this.RedirectToAction("Details", "Bundles", new { id = newBundleVersion.Bundle.Id })); }