private void AlterDatabase(List <SSAS.TabularTranslatedItem> translatedItems)
        {
#if DENALI || SQL2014
            Microsoft.AnalysisServices.BackEnd.DataModelingSandbox.AMOCode code;
#else
            Microsoft.AnalysisServices.BackEnd.AMOCode code;
#endif
            code = delegate
            {
                using (Microsoft.AnalysisServices.BackEnd.SandboxTransaction tran = sandboxWrapper.GetSandbox().CreateTransaction())
                {
                    SSAS.TabularTranslationsAnnotation             annotation      = new SSAS.TabularTranslationsAnnotation();
                    List <SSAS.TabularTranslationObjectAnnotation> annotationsList = new List <SSAS.TabularTranslationObjectAnnotation>();
                    foreach (SSAS.TabularTranslatedItem item in translatedItems)
                    {
                        item.Save(annotationsList);
                    }
                    annotation.TabularTranslations = annotationsList.ToArray();
                    TabularHelpers.SaveXmlAnnotation(cube.Parent, TRANSLATIONS_ANNOTATION, annotation);

                    TabularHelpers.EnsureDataSourceCredentials(sandboxWrapper.GetSandbox());
                    cube.Parent.Update(UpdateOptions.ExpandFull);

                    tran.GetType().InvokeMember("Commit", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, tran, null); //The .Commit() function used to return a list of strings, but in the latest set of code it is a void method which leads to "method not found" errors
                    //tran.Commit();
                }
            };
#if DENALI || SQL2014
            sandboxWrapper.GetSandbox().ExecuteAMOCode(Microsoft.AnalysisServices.BackEnd.DataModelingSandbox.OperationType.Update, Microsoft.AnalysisServices.BackEnd.DataModelingSandbox.OperationCancellability.AlwaysExecute, code, true);
#else
            sandboxWrapper.GetSandbox().ExecuteEngineCode(Microsoft.AnalysisServices.BackEnd.DataModelingSandbox.OperationType.Update, Microsoft.AnalysisServices.BackEnd.DataModelingSandbox.OperationCancellability.AlwaysExecute, code, true);
#endif
        }
        private SSAS.TabularTranslationsAnnotation GetAnnotation(Microsoft.AnalysisServices.BackEnd.DataModelingSandbox sandbox)
        {
            SSAS.TabularTranslationsAnnotation annotation = new SSAS.TabularTranslationsAnnotation();
#if DENALI || SQL2014
            var db = sandbox.Database;
#else
            var db = ((Microsoft.AnalysisServices.BackEnd.DataModelingSandboxAmo)sandbox.Impl).Database;
#endif
            if (db.Annotations.Contains(TRANSLATIONS_ANNOTATION))
            {
                string xml = TabularHelpers.GetAnnotationXml(db, TRANSLATIONS_ANNOTATION);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(SSAS.TabularTranslationsAnnotation));
                annotation = (SSAS.TabularTranslationsAnnotation)serializer.Deserialize(new System.IO.StringReader(xml));
            }
            return(annotation);
        }
Ejemplo n.º 3
0
        public TabularTranslatedItem(string table, IModelComponent obj, TabularTranslatedItemProperty property, TabularTranslatedItem caption, SSAS.TabularTranslationsAnnotation annotations)
        {
            _Table    = table;
            _object   = obj;
            _Property = property;

            TranslationCollection translations;
            string sCaption;
            string sDescription;
            string sDisplayFolder = null;

            if (obj is DimensionAttribute)
            {
                _ObjectType = TabularTranslatedItemType.Column;
                DimensionAttribute typedobj = (DimensionAttribute)obj;
                translations   = typedobj.Translations;
                sDisplayFolder = typedobj.AttributeHierarchyDisplayFolder;
                sCaption       = typedobj.Name;
                sDescription   = typedobj.Description;
            }
            else if (obj is Hierarchy)
            {
                _ObjectType = TabularTranslatedItemType.Hierarchy;
                Hierarchy typedobj = (Hierarchy)obj;
                translations   = typedobj.Translations;
                sDisplayFolder = typedobj.DisplayFolder;
                sCaption       = typedobj.Name;
                sDescription   = typedobj.Description;
            }
            else if (obj is Level)
            {
                _ObjectType = TabularTranslatedItemType.Level;
                Level typedobj = (Level)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is CalculationProperty)
            {
                _ObjectType = TabularTranslatedItemType.Measure;
                CalculationProperty typedobj = (CalculationProperty)obj;
                translations   = typedobj.Translations;
                sDisplayFolder = typedobj.DisplayFolder;
                sCaption       = typedobj.CalculationReference;
                sDescription   = typedobj.Description;

                if (sCaption.StartsWith("[") && sCaption.EndsWith("]"))
                {
                    sCaption = sCaption.Substring(1, sCaption.Length - 2);
                }
            }
            else if (obj is Database)
            {
                _ObjectType = TabularTranslatedItemType.Database;
                Database typedobj = (Database)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Cube)
            {
                _ObjectType = TabularTranslatedItemType.Cube;
                Cube typedobj = (Cube)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Perspective)
            {
                _ObjectType = TabularTranslatedItemType.Perspective;
                Perspective typedobj = (Perspective)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Dimension)
            {
                _ObjectType = TabularTranslatedItemType.Table;
                Dimension typedobj = (Dimension)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else if (obj is Microsoft.AnalysisServices.Action)
            {
                _ObjectType = TabularTranslatedItemType.Action;
                Microsoft.AnalysisServices.Action typedobj = (Microsoft.AnalysisServices.Action)obj;
                translations = typedobj.Translations;
                sCaption     = typedobj.Name;
                sDescription = typedobj.Description;
            }
            else
            {
                throw new Exception("Unexpected object type: " + obj.GetType().Name);
            }

            _ObjectName = sCaption;

            if (property == TabularTranslatedItemProperty.Caption)
            {
                _DefaultLanguage = sCaption;

                SSAS.TabularTranslationObjectAnnotation annotation = annotations.Find(obj);
                if (annotation != null && translations.Count == 0)
                {
                    foreach (SSAS.TabularTranslationAnnotation tranAnnotation in annotation.TabularTranslations)
                    {
                        Translation t = new Translation(tranAnnotation.Language);
                        if (obj is DimensionAttribute)
                        {
                            t = new AttributeTranslation(tranAnnotation.Language);
                        }
                        t.Caption       = tranAnnotation.Caption;
                        t.Description   = tranAnnotation.Description;
                        t.DisplayFolder = tranAnnotation.DisplayFolder;
                        translations.Add(t);
                    }
                    _restoredTranslations = true;
                }
            }
            else if (property == TabularTranslatedItemProperty.Description)
            {
                _DefaultLanguage = sDescription;
            }
            else if (property == TabularTranslatedItemProperty.DisplayFolder)
            {
                _DefaultLanguage = sDisplayFolder;
            }

            Languages = new Core.DirtyMonitoredDictionary <int, string>();
            foreach (Translation t in translations)
            {
                if (property == TabularTranslatedItemProperty.Caption)
                {
                    Languages.Add(t.Language, t.Caption);
                }
                else if (property == TabularTranslatedItemProperty.Description)
                {
                    Languages.Add(t.Language, t.Description);
                }
                else if (property == TabularTranslatedItemProperty.DisplayFolder)
                {
                    Languages.Add(t.Language, t.DisplayFolder);
                }
            }

            if (caption != null)
            {
                caption.DependentProperties.Add(this);
            }
        }
        private void ExecInternal(bool bPreBuildCheckOnly)
        {
            try
            {
                UIHierarchy     solExplorer = this.ApplicationObject.ToolWindows.SolutionExplorer;
                UIHierarchyItem hierItem    = ((UIHierarchyItem)((System.Array)solExplorer.SelectedItems).GetValue(0));
#if !(YUKON || KATMAI) //TODO
                cube = sandboxWrapper.GetSandboxAmo().Cube;
#else
                cube = sandboxWrapper.GetSandboxAmo().Cube;
#endif
                if (cube == null)
                {
                    throw new Exception("The workspace database cube doesn't exist.");
                }

                SSAS.TabularDisplayFoldersAnnotation annotationSavedFolders = TabularDisplayFolderPlugin.GetAnnotation(sandboxWrapper.GetSandbox());
                SSAS.TabularTranslationsAnnotation   annotationSaved        = GetAnnotation(sandboxWrapper.GetSandbox());

                string sTargetDatabase = null;
                string sTargetCubeName = null;
                try
                {
                    //get the deployment settings to display in the translations dialog (rather than the workspace database and cube name)
                    Microsoft.VisualStudio.Project.Automation.OAFileItem fileitem = hierItem.Object as Microsoft.VisualStudio.Project.Automation.OAFileItem;
                    DeploymentSettings deploySet = new DeploymentSettings(fileitem.ContainingProject);
                    sTargetDatabase = deploySet.TargetDatabase;
                    sTargetCubeName = deploySet.TargetCubeName;
                }
                catch { } //this seems to blow up in debug mode on my laptop... oh well


                bool bRestoredDisplayFolders = false;
                List <SSAS.TabularTranslatedItem> translationRows = new List <BIDSHelper.SSAS.TabularTranslatedItem>();

                var restrictions = new Dictionary <string, string>();
                restrictions.Add("CUBE_NAME", cube.Name);
                DataSet           datasetMeasures = sandboxWrapper.GetSchemaDataSet("MDSCHEMA_MEASURES", restrictions);
                DataRowCollection rowsMeasures    = datasetMeasures.Tables[0].Rows;

                Database db = cube.Parent;
                SSAS.TabularTranslatedItem captionTranslationDatabase = new SSAS.TabularTranslatedItem(null, db, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                if (!string.IsNullOrEmpty(sTargetDatabase))
                {
                    captionTranslationDatabase.OverrideCaption(sTargetDatabase);
                }
                translationRows.Add(captionTranslationDatabase);
                if (!string.IsNullOrEmpty(db.Description))
                {
                    translationRows.Add(new SSAS.TabularTranslatedItem(null, db, SSAS.TabularTranslatedItemProperty.Description, captionTranslationDatabase, null));
                }

                List <CalculationProperty> calcProperties = new List <CalculationProperty>();
                foreach (Cube c in db.Cubes)
                {
                    SSAS.TabularTranslatedItem captionTranslationCube = new SSAS.TabularTranslatedItem(null, c, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                    if (!string.IsNullOrEmpty(sTargetCubeName))
                    {
                        captionTranslationCube.OverrideCaption(sTargetCubeName);
                    }
                    translationRows.Add(captionTranslationCube);
                    if (!string.IsNullOrEmpty(c.Description))
                    {
                        translationRows.Add(new SSAS.TabularTranslatedItem(null, c, SSAS.TabularTranslatedItemProperty.Description, captionTranslationCube, null));
                    }

                    foreach (Perspective p in c.Perspectives)
                    {
                        SSAS.TabularTranslatedItem captionTranslationPerspective = new SSAS.TabularTranslatedItem(null, p, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                        translationRows.Add(captionTranslationPerspective);
                        if (!string.IsNullOrEmpty(p.Description))
                        {
                            translationRows.Add(new SSAS.TabularTranslatedItem(null, p, SSAS.TabularTranslatedItemProperty.Description, captionTranslationPerspective, null));
                        }
                    }

                    foreach (MdxScript mdx in c.MdxScripts)
                    {
                        foreach (CalculationProperty calc in mdx.CalculationProperties)
                        {
                            if (calc.Visible && !calc.CalculationReference.StartsWith("KPIs."))
                            {
                                calcProperties.Add(calc);
                            }
                        }
                    }
                }

                foreach (Dimension d in db.Dimensions)
                {
                    SSAS.TabularTranslatedItem captionTranslationDimension = new SSAS.TabularTranslatedItem(d.Name, d, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                    translationRows.Add(captionTranslationDimension);
                    if (!string.IsNullOrEmpty(d.Description))
                    {
                        translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, d, SSAS.TabularTranslatedItemProperty.Description, captionTranslationDimension, null));
                    }

                    foreach (DimensionAttribute da in d.Attributes)
                    {
                        if (da.Type != AttributeType.RowNumber && da.AttributeHierarchyVisible)
                        {
                            if (string.IsNullOrEmpty(da.AttributeHierarchyDisplayFolder))
                            {
                                SSAS.TabularDisplayFolderAnnotation a = annotationSavedFolders.Find(da);
                                if (a != null)
                                {
                                    da.AttributeHierarchyDisplayFolder = a.DisplayFolder;
                                    bRestoredDisplayFolders            = true; //we have to restore the display folder or the translations on the display folder won't even be visible in the translations dialog
                                }
                            }
                            SSAS.TabularTranslatedItem captionTranslation = new SSAS.TabularTranslatedItem(d.Name, da, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                            translationRows.Add(captionTranslation);
                            if (!string.IsNullOrEmpty(da.AttributeHierarchyDisplayFolder))
                            {
                                translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, da, SSAS.TabularTranslatedItemProperty.DisplayFolder, captionTranslation, null));
                            }
                            if (!string.IsNullOrEmpty(da.Description))
                            {
                                translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, da, SSAS.TabularTranslatedItemProperty.Description, captionTranslation, null));
                            }
                        }
                    }
                    foreach (Hierarchy h in d.Hierarchies)
                    {
                        if (string.IsNullOrEmpty(h.DisplayFolder))
                        {
                            SSAS.TabularDisplayFolderAnnotation a = annotationSavedFolders.Find(h);
                            if (a != null)
                            {
                                h.DisplayFolder         = a.DisplayFolder;
                                bRestoredDisplayFolders = true; //we have to restore the display folder or the translations on the display folder won't even be visible in the translations dialog
                            }
                        }

                        SSAS.TabularTranslatedItem captionTranslation = new SSAS.TabularTranslatedItem(d.Name, h, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                        translationRows.Add(captionTranslation);

                        if (!string.IsNullOrEmpty(h.DisplayFolder))
                        {
                            translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, h, SSAS.TabularTranslatedItemProperty.DisplayFolder, captionTranslation, null));
                        }
                        if (!string.IsNullOrEmpty(h.Description))
                        {
                            translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, h, SSAS.TabularTranslatedItemProperty.Description, captionTranslation, null));
                        }

                        foreach (Level level in h.Levels)
                        {
                            SSAS.TabularTranslatedItem captionTranslationLevel = new SSAS.TabularTranslatedItem(d.Name, level, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                            translationRows.Add(captionTranslationLevel);

                            if (!string.IsNullOrEmpty(level.Description))
                            {
                                translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, level, SSAS.TabularTranslatedItemProperty.Description, captionTranslationLevel, null));
                            }
                        }
                    }

                    for (int i = 0; i < rowsMeasures.Count; i++)
                    {
                        DataRow r = rowsMeasures[i];
                        if (Convert.ToString(r["MEASUREGROUP_NAME"]) == d.Name)
                        {
                            foreach (CalculationProperty calc in calcProperties)
                            {
                                if (Convert.ToString(r["MEASURE_UNIQUE_NAME"]) == "[Measures]." + calc.CalculationReference)
                                {
                                    if (string.IsNullOrEmpty(calc.DisplayFolder))
                                    {
                                        SSAS.TabularDisplayFolderAnnotation a = annotationSavedFolders.Find(calc);
                                        if (a != null)
                                        {
                                            calc.DisplayFolder      = a.DisplayFolder;
                                            bRestoredDisplayFolders = true; //we have to restore the display folder or the translations on the display folder won't even be visible in the translations dialog
                                        }
                                    }

                                    SSAS.TabularTranslatedItem captionTranslation = new SSAS.TabularTranslatedItem(d.Name, calc, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                                    translationRows.Add(captionTranslation);
                                    if (!string.IsNullOrEmpty(calc.DisplayFolder))
                                    {
                                        translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, calc, SSAS.TabularTranslatedItemProperty.DisplayFolder, captionTranslation, null));
                                    }
                                    if (!string.IsNullOrEmpty(calc.Description))
                                    {
                                        translationRows.Add(new SSAS.TabularTranslatedItem(d.Name, calc, SSAS.TabularTranslatedItemProperty.Description, captionTranslation, null));
                                    }

                                    rowsMeasures.Remove(r);
                                    i--;
                                    break;
                                }
                            }
                        }
                    }
                }

                foreach (Cube c in db.Cubes)
                {
                    SSAS.TabularActionsAnnotation actionAnnotations = TabularActionsEditorPlugin.GetAnnotation(c);
                    foreach (Microsoft.AnalysisServices.Action a in c.Actions)
                    {
                        SSAS.TabularAction actionAnnotation = actionAnnotations.Find(a.ID);
                        if (actionAnnotation == null)
                        {
                            actionAnnotation = new SSAS.TabularAction();
                        }

                        if (!string.IsNullOrEmpty(actionAnnotation.OriginalTarget) &&
                            !actionAnnotation.IsMasterClone)
                        {
                            continue;
                        }

                        SSAS.TabularTranslatedItem captionTranslationAction = new SSAS.TabularTranslatedItem(null, a, SSAS.TabularTranslatedItemProperty.Caption, null, annotationSaved);
                        translationRows.Add(captionTranslationAction);
                        if (!string.IsNullOrEmpty(a.Description))
                        {
                            translationRows.Add(new SSAS.TabularTranslatedItem(null, a, SSAS.TabularTranslatedItemProperty.Description, captionTranslationAction, null));
                        }
                    }
                }

                bool bRestoredTranslations = false;

                //get a list of all the distinct languages
                List <int> listLanguages = new List <int>();
                foreach (SSAS.TabularTranslatedItem item in translationRows)
                {
                    foreach (int iLang in item.Languages.Keys)
                    {
                        if (!listLanguages.Contains(iLang))
                        {
                            listLanguages.Add(iLang);
                        }
                    }
                    if (item.RestoredTranslations)
                    {
                        bRestoredTranslations = true;
                    }
                }
                listLanguages.Sort();

                if (bRestoredTranslations)
                {
                    MessageBox.Show("Some translations have been wiped out by other editing operations. Restoring translations may be possible except when an object like a measure or a hierarchy has been renamed.\r\n\r\nBIDS Helper will attempt to restore the translations now. Be sure to click OK after you finish your edits in the Tabular Translations window.", "BIDS Helper Tabular Translations");
                }
                else if (bRestoredDisplayFolders)
                {
                    MessageBox.Show("Some display folders have been wiped out by other editing operations. Restoring display folders may be possible except when an object like a measure or a hierarchy has been renamed.\r\n\r\nBIDS Helper will attempt to restore the display folders now. Properly restored display folders are necessary for this Tabular Translations dialog to function properly. Be sure to click OK after you finish your edits in the Tabular Translations window.", "BIDS Helper Tabular Translations");
                }
                else if (bPreBuildCheckOnly)
                {
                    return; //if this is just a pre-build check and if there's nothing that needs to be restored, then just return without popping up the dialog
                }

                SSAS.TabularTranslationsEditorWindow form = new SSAS.TabularTranslationsEditorWindow(db.Language, listLanguages);
                form.WindowState           = System.Windows.WindowState.Maximized;
                form.dataGrid1.ItemsSource = translationRows;


                if (form.ShowDialog() == true)
                {
                    //count dirty changes and create annotation
                    int iNumberOfChanges = 0;
                    foreach (SSAS.TabularTranslatedItem item in translationRows)
                    {
                        if (item.Dirty)
                        {
                            iNumberOfChanges++;
                        }
                    }

                    if (iNumberOfChanges > 0 || bRestoredDisplayFolders || bRestoredTranslations)
                    {
                        AlterDatabase(translationRows);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, "BIDS Helper - Error");
            }
        }