public static void Apply(PatternInstance instance, Transaction transaction)
        {
            HPatternInstance wwInstance = HPatternInstance.Load(instance);
            HPatternSettings wwSettings = wwInstance.Settings;

            // Update rules and events.
            bool gera = wwInstance.Transaction.WebBC;
            /*
            bool gera = wwSettings.Template.DataEntryWebPanelBC;
            if (wwInstance.Transaction.DataEntryWebPanelBC.ToLower() == "true")
                gera = true;
            if (wwInstance.Transaction.DataEntryWebPanelBC.ToLower() == "false")
                gera = false;
            */

            bool overwrite = (wwInstance.Transaction.UpdateObject == TransactionElement.UpdateObjectValue.Overwrite);

            if (gera)
            {
                transaction.SetPropertyValue(Properties.TRN.BusinessComponent, true);
            }

            if (wwSettings.Template.TabFunction == SettingsTemplateElement.TabFunctionValue.TreeViewAnchor)
            {
                transaction.SetPropertyValue(Properties.TRN.Type, Properties.TRN.Type_Values.Component);
                transaction.SetPropertyValue(Properties.TRN.UrlAccess, Properties.TRN.UrlAccess_Values.Yes);
            }

            if (wwInstance.Transaction.UpdateObject != TransactionElement.UpdateObjectValue.DoNotUpdate)
            {

                UpdateProperties(wwInstance, wwSettings, transaction);
                UpdateVariables(wwInstance,wwSettings, transaction);
                ParserFactory.MergeRules(transaction.Rules, UpdateRules(wwInstance, wwSettings, transaction),overwrite);
                ParserFactory.MergeEvents(transaction.Events, UpdateEvents(wwInstance, wwSettings, transaction), overwrite);
                UpdateAttributes(wwInstance, wwSettings, transaction);

                if (wwInstance.Transaction.UpdateObject != TransactionElement.UpdateObjectValue.OnlyRulesEventsAndConditions)
                {
                    ParserFactory.MergeWebForm(transaction.WebForm, Heurys.Patterns.HPattern.Helpers.Template.TrnWebForm(transaction, wwSettings, wwInstance, false, "", false, wwInstance.Transaction));
                }

                if (wwInstance.DocumentationEnabled)
                {
                    TemplateInternal.DocumentationSave(transaction.Documentation, transaction, wwInstance, wwSettings);
                }

            }
        }
        private static void UpdateProperties(HPatternInstance instance, HPatternSettings settings, Transaction transaction)
        {
            WebPanel mTransaction = settings.MasterPages.Transaction;
            string AMP = SettingsThemeElement.SetObjectThemeValue.True;
            string ATema = settings.Theme.SetObjectTheme;
            Theme tTema = null;
            if (ATema != SettingsThemeElement.SetObjectThemeValue.False)
                tTema = settings.Theme.Theme;

            foreach (SettingsThemeMasterPageElement sis in settings.CustomThemeMasterPage)
            {
                if (transaction.Name.IndexOf(sis.Name, StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    if (sis.SetMasterPage != SettingsThemeMasterPageElement.SetMasterPageValue.False)
                    {
                        mTransaction = sis.Transaction;
                        AMP = sis.SetMasterPage;
                    }
                    if (sis.SetTheme != SettingsThemeMasterPageElement.SetThemeValue.False)
                    {
                        tTema = sis.Theme;
                        ATema = sis.SetTheme;
                    }
                    break;
                }
            }

            if (transaction.IsPropertyDefault(Properties.TRN.MasterPage) || AMP == SettingsThemeElement.SetObjectThemeValue.Force)
            {
                if (mTransaction == null)
                {
                    transaction.SetPropertyValue(Properties.TRN.MasterPage, new WebPanelReference());
                }
                else
                {
                    transaction.SetPropertyValue(Properties.TRN.MasterPage, new WebPanelReference(mTransaction));
                }
            }

            if (tTema != null)
            {
                if (transaction.IsPropertyDefault(Properties.TRN.Theme) || ATema == SettingsThemeElement.SetObjectThemeValue.Force)
                {
                    transaction.SetPropertyValue(Properties.TRN.Theme, new KBObjectReference(tTema));
                }
            }

            if (instance.Levels.Count > 0 && instance.Levels[0].View != null)
            {
                if (instance.Levels[0].View.UseAsSearchViewer)
                {
                    try
                    {

                        PropertyManager.PropertySpecDescriptor searchViewerProperty = transaction.GetPropertyDescriptor(Properties.TRN.SearchViewer);
                        if (searchViewerProperty.IsApplicable && searchViewerProperty.IsBrowsable && searchViewerProperty.IsDefaultValue)
                        {
                            WebPanel viewObject = WebPanel.Get(transaction.Model, instance.Levels[0].View.ObjectName);
                            if (viewObject != null)
                                transaction.SetPropertyValue(Properties.TRN.SearchViewer, new KBObjectReference(viewObject));
                        }

                    }
                    catch (Exception e) {
                        LogLicense.LogEmpty(e.Message + e.StackTrace);
                    }
                }
            }
        }