/// <summary>
        /// Сохранение шаблона
        /// </summary>
        /// <param name="curTemplate">Сохраняемый шаблон</param>
        /// <param name="items">Новый список элементов в сохраняемом шаблоне</param>
        /// <param name="m_Log">Объект логгера</param>
        /// <returns></returns>
        public Cl_Template f_SaveTemplate(Cl_Template curTemplate, I_Element[] elements, Cl_EntityLog m_Log = null)
        {
            using (var transaction = m_DataContextMegaTemplate.Database.BeginTransaction())
            {
                try
                {
                    Cl_Template newTemplate = null;
                    if (curTemplate.p_Version == 0)
                    {
                        newTemplate           = curTemplate;
                        newTemplate.p_Version = 1;
                    }
                    else
                    {
                        newTemplate = new Cl_Template();
                        newTemplate.p_TemplateID       = curTemplate.p_TemplateID;
                        newTemplate.p_Title            = curTemplate.p_Title;
                        newTemplate.p_CategoryTotalID  = curTemplate.p_CategoryTotalID;
                        newTemplate.p_CategoryTotal    = curTemplate.p_CategoryTotal;
                        newTemplate.p_CategoryClinicID = curTemplate.p_CategoryClinicID;
                        newTemplate.p_CategoryClinic   = curTemplate.p_CategoryClinic;
                        newTemplate.p_Type             = curTemplate.p_Type;
                        newTemplate.p_Name             = curTemplate.p_Name;
                        newTemplate.p_Version          = curTemplate.p_Version + 1;
                        newTemplate.p_ParentGroupID    = curTemplate.p_ParentGroupID;
                        newTemplate.p_ParentGroup      = curTemplate.p_ParentGroup;
                        newTemplate.p_Description      = curTemplate.p_Description;

                        m_DataContextMegaTemplate.p_Templates.Add(newTemplate);
                    }

                    m_DataContextMegaTemplate.SaveChanges();

                    foreach (I_Element item in elements)
                    {
                        Cl_TemplateElement tplEl = new Cl_TemplateElement();
                        tplEl.p_TemplateID = newTemplate.p_ID;
                        tplEl.p_Template   = newTemplate;
                        if (item is Ctrl_Element)
                        {
                            Ctrl_Element block = (Ctrl_Element)item;
                            tplEl.p_ChildElementID = block.p_ID;
                            tplEl.p_ChildElement   = block.p_Element;
                        }
                        else if (item is Ctrl_Template)
                        {
                            Ctrl_Template block = (Ctrl_Template)item;
                            tplEl.p_ChildTemplateID = block.p_ID;
                            tplEl.p_ChildTemplate   = block.p_Template;
                        }

                        tplEl.p_Index = Array.IndexOf(elements, item) + 1;

                        m_DataContextMegaTemplate.p_TemplatesElements.Add(tplEl);
                    }

                    m_DataContextMegaTemplate.SaveChanges();

                    if (m_Log != null && m_Log.f_IsChanged(newTemplate) == false)
                    {
                        if (newTemplate.Equals(curTemplate) && newTemplate.p_Version == 1)
                        {
                            newTemplate.p_Version = 0;
                        }

                        MonitoringStub.Message("Шаблон не изменялся!");
                        transaction.Rollback();
                    }
                    else
                    {
                        m_Log.f_SaveEntity(newTemplate);
                        transaction.Commit();

                        return(newTemplate);
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    MonitoringStub.Error("Error_Editor", "При сохранении изменений произошла ошибка", ex, null, null);
                }

                return(curTemplate);
            }
        }
        private Cl_Template f_UpdateTemplate(Cl_Template template)
        {
            Cl_Template defNewTemplate = this.f_GetActualTemplate(template);

            if (defNewTemplate.p_Version > template.p_Version)
            {
                return(defNewTemplate);
            }

            if (template.p_TemplateElements == null || template.p_TemplateElements.Count == 0)
            {
                return(template);
            }

            Cl_Template newTemplate = new Cl_Template();

            newTemplate.p_TemplateID       = template.p_TemplateID;
            newTemplate.p_Title            = template.p_Title;
            newTemplate.p_CategoryTotalID  = template.p_CategoryTotalID;
            newTemplate.p_CategoryTotal    = template.p_CategoryTotal;
            newTemplate.p_CategoryClinicID = template.p_CategoryClinicID;
            newTemplate.p_CategoryClinic   = template.p_CategoryClinic;
            newTemplate.p_Type             = template.p_Type;
            newTemplate.p_Name             = template.p_Name;
            newTemplate.p_Version          = template.p_Version + 1;
            newTemplate.p_ParentGroupID    = template.p_ParentGroupID;
            newTemplate.p_ParentGroup      = template.p_ParentGroup;
            newTemplate.p_Description      = template.p_Description;

            m_DataContextMegaTemplate.p_Templates.Add(newTemplate);
            m_DataContextMegaTemplate.SaveChanges();

            int i = 0;

            foreach (Cl_TemplateElement item in template.p_TemplateElements)
            {
                Cl_TemplateElement tplEl = new Cl_TemplateElement();
                tplEl.p_Template = newTemplate;

                if (item.p_ChildElement != null)
                {
                    tplEl.p_ChildElement = this.f_GetActualElement(item.p_ChildElement);
                }
                else if (item.p_ChildTemplate != null)
                {
                    tplEl.p_ChildTemplate = this.f_UpdateTemplate(item.p_ChildTemplate);

                    if (this.f_IsActualElementsOnTemplate(item.p_ChildTemplate) == false)
                    {
                        tplEl.p_ChildTemplate = this.f_UpdateTemplate(tplEl.p_ChildTemplate);
                    }
                }

                tplEl.p_Index = i = ++i;
                m_DataContextMegaTemplate.p_TemplatesElements.Add(tplEl);
            }

            m_DataContextMegaTemplate.SaveChanges();

            return(newTemplate);
        }
Exemple #3
0
        private static string f_GetTemplateElementsValue(PropertyInfo pInfo, object value)
        {
            StringBuilder sBuilder = new StringBuilder();
            ICollection <Cl_TemplateElement> last    = (ICollection <Cl_TemplateElement>)Cl_EntityValue.lastValue;
            ICollection <Cl_TemplateElement> current = (ICollection <Cl_TemplateElement>)value;
            bool flag = false;

            if (last == null)
            {
                last = new List <Cl_TemplateElement>();
            }
            if (current == null)
            {
                current = new List <Cl_TemplateElement>();
            }

            // new element
            foreach (Cl_TemplateElement cur in current)
            {
                flag = false;
                foreach (Cl_TemplateElement l in last)
                {
                    if (cur.p_ChildElement != null)
                    {
                        if (cur.p_ChildElementID == l.p_ChildElementID)
                        {
                            flag = true;
                            break;
                        }
                    }
                    else
                    {
                        if (cur.p_ChildTemplateID == l.p_ChildTemplateID)
                        {
                            flag = true;
                            break;
                        }
                    }
                }

                if (flag == false)
                {
                    if (cur.p_ChildElement != null)
                    {
                        sBuilder.AppendLine("Добавлен новый элемент \"" + cur.p_ChildElement.p_Name + "\" (" + cur.p_ChildElement.p_GetElementName + ")");
                    }
                    else
                    {
                        sBuilder.AppendLine("Добавлен новый блок \"" + cur.p_ChildTemplate.p_Name + "\" (" + cur.p_ChildTemplate.p_GetTypeName + ")");
                    }
                }
            }

            // new position
            for (int x = 0; x < current.Count(); x++)
            {
                Cl_TemplateElement cur = current.ElementAt(x);
                flag = false;
                for (int y = 0; y < last.Count(); y++)
                {
                    Cl_TemplateElement l = last.ElementAt(y);

                    if (cur.p_ChildElement != null)
                    {
                        if (cur.p_ChildElementID == l.p_ChildElementID)
                        {
                            flag = (x != y);
                            break;
                        }
                    }
                    else
                    {
                        if (cur.p_ChildTemplateID == l.p_ChildTemplateID)
                        {
                            flag = (x != y);
                            break;
                        }
                    }
                }

                if (flag)
                {
                    if (cur.p_ChildElement != null)
                    {
                        sBuilder.AppendLine("Изменилась позиция элемента \"" + cur.p_ChildElement.p_Name + "\" (" + cur.p_ChildElement.p_GetElementName + ")");
                    }
                    else
                    {
                        sBuilder.AppendLine("Изменилась позиция блока \"" + cur.p_ChildTemplate.p_Name + "\" (" + cur.p_ChildTemplate.p_GetTypeName + ")");
                    }
                }
            }

            // delete element
            foreach (Cl_TemplateElement l in last)
            {
                flag = true;
                foreach (Cl_TemplateElement cur in current)
                {
                    if (cur.p_ChildElement != null)
                    {
                        if (cur.p_ChildElementID == l.p_ChildElementID)
                        {
                            flag = false;
                            break;
                        }
                    }
                    else
                    {
                        if (cur.p_ChildTemplateID == l.p_ChildTemplateID)
                        {
                            flag = false;
                            break;
                        }
                    }
                }

                if (flag)
                {
                    if (l.p_ChildElement != null)
                    {
                        sBuilder.AppendLine("Удален элемент \"" + l.p_ChildElement.p_Name + "\" (" + l.p_ChildElement.p_GetElementName + ")");
                    }
                    else
                    {
                        sBuilder.AppendLine("Удален элемент \"" + l.p_ChildTemplate.p_Name + "\" (" + l.p_ChildTemplate.p_GetTypeName + ")");
                    }
                }
            }

            return(sBuilder.ToString().Trim());
        }