Пример #1
0
        public TypeModel SaveType(TypeModel model)
        {
            using (var transaction = this.tdb.BeginTransaction())
            {
                ImplementationGuideType igType = new ImplementationGuideType();

                if (model.Id != null)
                {
                    igType = this.tdb.ImplementationGuideTypes.Single(y => y.Id == model.Id);
                }
                else
                {
                    this.tdb.ImplementationGuideTypes.AddObject(igType);
                }

                if (igType.Name != model.Name)
                {
                    if (!string.IsNullOrEmpty(igType.Name))
                    {
                        string currentDirectory = Shared.Helper.GetSchemasDirectory(igType.Name);
                        string newDirectory     = Shared.Helper.GetSchemasDirectory(model.Name);
                        MoveSchemaFiles(currentDirectory, newDirectory);
                    }

                    igType.Name = model.Name;
                }

                if (igType.SchemaLocation != model.SchemaLocation)
                {
                    igType.SchemaLocation = model.SchemaLocation;
                }

                if (igType.SchemaPrefix != model.SchemaPrefix)
                {
                    igType.SchemaPrefix = model.SchemaPrefix;
                }

                if (igType.SchemaURI != model.SchemaUri)
                {
                    igType.SchemaURI = model.SchemaUri;
                }

                // Update template types
                foreach (var templateTypeModel in model.DeletedTemplateTypes)
                {
                    var templateType = igType.TemplateTypes.Single(y => y.Id == templateTypeModel.Id);
                    this.tdb.TemplateTypes.DeleteObject(templateType);
                }

                foreach (var templateTypeModel in model.TemplateTypes)
                {
                    SaveTemplateType(igType, templateTypeModel);
                }

                // Update data types
                var newDataTypes    = model.DataTypes.Where(y => igType.DataTypes.Count(x => x.DataTypeName == y) == 0).ToList();
                var deleteDataTypes = igType.DataTypes.Where(y => model.DataTypes.Count(x => x == y.DataTypeName) == 0).ToList();

                foreach (var current in newDataTypes)
                {
                    var newDataType = new ImplementationGuideTypeDataType()
                    {
                        ImplementationGuideType = igType,
                        DataTypeName            = current
                    };
                    this.tdb.ImplementationGuideTypeDataTypes.AddObject(newDataType);
                }

                foreach (var current in deleteDataTypes)
                {
                    current.green_constraint.ToList().ForEach(y => { y.ImplementationGuideTypeDataType = null; });
                    this.tdb.ImplementationGuideTypeDataTypes.DeleteObject(current);
                }

                this.tdb.SaveChanges();

                try
                {
                    // If a new schema file is attached, save it
                    if (!string.IsNullOrEmpty(model.SchemaFileContentType))
                    {
                        switch (model.SchemaFileContentType)
                        {
                        case "application/xml":
                            SaveSchemaFile(igType, model.SchemaFile);
                            break;

                        case "application/zip":
                        case "application/x-zip-compressed":
                            SaveSchemaZip(igType, model.SchemaFile);
                            break;

                        default:
                            throw new Exception("Unexpected file format " + model.SchemaFileContentType + " for new schema.");
                        }
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }

                return(GetImplementationGuideType(igType.Id));
            }
        }
        private GreenConstraint CreateGreenConstraint(GreenTemplate greenTemplate, TemplateConstraint constraint, GreenConstraint parent, string businessName = null, string elementName = null, string xpath = null, ImplementationGuideTypeDataType dataType = null)
        {
            if (string.IsNullOrEmpty(constraint.Context))
            {
                throw new ArgumentException(constraint.Context);
            }

            if (string.IsNullOrEmpty(elementName))
            {
                elementName = constraint.Context;
            }

            if (string.IsNullOrEmpty(businessName))
            {
                businessName = constraint.Context;
            }

            GreenConstraint newGreenConstraint = new GreenConstraint()
            {
                GreenTemplate        = greenTemplate,
                GreenTemplateId      = greenTemplate.Id,
                TemplateConstraint   = constraint,
                TemplateConstraintId = constraint.Id,
                Description          = businessName,
                Name      = elementName,
                RootXpath = xpath,
                ImplementationGuideTypeDataType   = dataType,
                ImplementationGuideTypeDataTypeId = dataType != null ? (int?)dataType.Id : null
            };

            if (parent != null)
            {
                newGreenConstraint.ParentGreenConstraint   = parent;
                newGreenConstraint.ParentGreenConstraintId = parent.Id;
                parent.ChildGreenConstraints.Add(newGreenConstraint);
            }

            constraint.GreenConstraints.Add(newGreenConstraint);
            greenTemplate.ChildGreenConstraints.Add(newGreenConstraint);

            this.mockRepo.GreenConstraints.AddObject(newGreenConstraint);

            return(newGreenConstraint);
        }