Esempio n. 1
0
        /// <summary>
        /// Check for existing label, if none exists create ie
        /// </summary>
        /// <param name="labelText"><c>String</c> Value for the label</param>
        /// <param name="elementName"><c>String</c> value thats is the prefix and main portion of the labelID</param>
        /// <param name="propertyName"><c>String</c> value that is the PropertyName which becomes the Suffix</param>
        /// <param name="labelFile">Instance of <c>AXLabelFile</c> to insert into</param>
        /// <returns></returns>
        private String FindOrCreateLabel(String labelText, String elementName, String propertyName, bool delaySave = false)
        {
            string newLabelId = String.Empty;

            if (labelController == null)
            {
                labelController = factory.GetOrCreateLabelController(currentLabelFile, LabelHelper.Context);
            }
            // Don't bother if the string is empty
            if (String.IsNullOrEmpty(labelText) == false)
            {
                // Construct a label id that will be unique
                string labelId = $"{elementName}_{propertyName}";

                // Make sure the label doesn't exist.
                // What will you do if it does?
                if (labelController.Exists(labelId) == false)
                {
                    labelController.Insert(labelId, labelText, String.Empty);
                    if (!delaySave)
                    {
                        labelController.Save();
                    }
                    // Construct a label reference to go into the label property
                    newLabelId = $"@{currentLabelFile.LabelFileId}:{labelId}";
                }
                else if (promptOnDuplicate)
                {
                    CoreUtility.DisplayWarning($"Label: {labelId} already exists");
                }
            }

            return(newLabelId);
        }
Esempio n. 2
0
        public static string Convert(this string name)
        {
            Project = LocalUtils.GetActiveProjectNode();
            ProjectParameters.Contruct();
            MetaModelProviders = ServiceLocator.GetService(typeof(IMetaModelProviders)) as IMetaModelProviders;
            MetaModelService   = MetaModelProviders.CurrentMetaModelService;

            var extension = ProjectParameters.Instance.Extension;

            var englishLabelFile =
                MetaModelService.GetLabelFileNames()
                .FirstOrDefault(
                    lableFileName => lableFileName.StartsWith(extension) && lableFileName.Contains("en-US"));

            var lableFile = MetaModelService.GetLabelFile(englishLabelFile);
            var labelKey  = name.Replace(extension, "");

            var lableTxt = Regex.Replace(labelKey, "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1").Trim().ToLower().UppercaseFirst();

            LabelControllerFactory factory = new LabelControllerFactory();
            LabelEditorController  labelEditorController = factory.GetOrCreateLabelController(lableFile, LocalUtils.Context);

            //CoreUtility.DisplayInfo(labelKey);

            if (!labelEditorController.Exists(labelKey))
            {
                labelEditorController.Insert(labelKey, lableTxt, string.Empty);
                labelEditorController.Save();
            }

            return($"@{extension}Labels:{labelKey}");
        }
Esempio n. 3
0
        /// <summary>
        /// If property text is prefixed, then create a new label.
        /// Otherwise, return the very same property text value
        /// </summary>
        /// <param name="propertyText">Label text from element property (label, help text, caption, etc)</param>
        /// <returns>The new label id created</returns>
        public string createLabel(string propertyText)
        {
            string ret = propertyText;

            if (this.isPrefixed(propertyText))
            {
                string labelId = LabelManager.getLabelId(propertyText);
                string label   = LabelManager.getLabel(propertyText);

                foreach (AxLabelFile labelfile in this.labelFiles)
                {
                    LabelControllerFactory factory = new LabelControllerFactory();
                    LabelEditorController  labelEditorController = factory.GetOrCreateLabelController(labelfile, context);

                    if (!labelEditorController.Exists(labelId))
                    {
                        labelEditorController.Insert(labelId, label, string.Empty);
                        labelEditorController.Save();

                        this.log(labelId, label, labelfile.Name);
                    }
                }

                ret = $"@{LABELFILEID}:{labelId}";
            }

            return(ret);
        }
Esempio n. 4
0
        public static LabelContent FindLabelGlobally_OLD(string labelIdText)
        {
            //string labelId = String.Empty;
            LabelContent labelContent = new LabelContent()
            {
                LabelIdForProperty = labelIdText
            };

            if (labelIdText.StartsWith("@") == false)
            {
                return(labelContent);
            }

            //var labelFileId
            labelContent.LabelFileId = LabelHelper.GetLabelFileId(labelIdText);
            labelContent.LabelId     = labelContent.LabelIdForProperty.Substring(labelContent.LabelIdForProperty.IndexOf(":") + 1);

            // Get the label factory
            LabelControllerFactory factory = new LabelControllerFactory();

            // Get the label edit controller
            AxLabelFile labelFile = null;

            if (String.IsNullOrEmpty(labelContent.LabelFileId) == false)
            {
                // Issue with finding label file (as it seems its searching for the label file in the current model)
                labelFile = Common.CommonUtil.GetModelSaveService().GetLabelFile(labelContent.LabelFileId);
                if (labelFile == null)
                {
                    var fileName = Common.CommonUtil.GetModelSaveService().GetLabelFileNames()
                                   .Where(l => l.StartsWith(labelContent.LabelFileId) && l.Contains("en-")).FirstOrDefault();
                    labelFile = Common.CommonUtil.GetModelSaveService().GetLabelFile(fileName);
                }
            }
            else
            {
                var labelFiles = LabelHelper.GetLabelFiles();

                labelFile = labelFiles
                            .Where(l => l.LabelFileId.Equals(labelContent.LabelFileId, StringComparison.InvariantCultureIgnoreCase))
                            .First();
            }
            if (labelFile != null)
            {
                LabelEditorController labelController = factory.GetOrCreateLabelController(labelFile, Common.CommonUtil.GetVSApplicationContext());
                labelController.LabelSearchOption = SearchOption.MatchExactly;
                labelController.IsMatchExactly    = true;

                var test  = labelController.Labels.ToList();
                var label = labelController.Labels.Where(l => l.ID.Equals(labelContent.LabelId, StringComparison.InvariantCultureIgnoreCase))
                            .FirstOrDefault();
                if (label != null)
                {
                    labelContent.LabelDescription = label.Description;
                    labelContent.LabelText        = label.Text;
                }
            }
            return(labelContent);
        }
Esempio n. 5
0
        private static void CreateLabel(AxLabelFile labelFile, string labelId, string labelText)
        {
            // Get the label factory
            LabelControllerFactory factory = new LabelControllerFactory();

            LabelEditorController labelControllerToAdd = factory.GetOrCreateLabelController(labelFile, Common.CommonUtil.GetVSApplicationContext());

            labelControllerToAdd.Insert(labelId, labelText, null);
            labelControllerToAdd.Save();
        }
Esempio n. 6
0
        public static string Convert(this string name, string alternative = null)
        {
            Project = GetActiveProjectNode();
            ProjectParameters.Contruct();

            MetaModelProviders = CoreUtility.ServiceProvider.GetService(typeof(IMetaModelProviders)) as IMetaModelProviders;
            if (MetaModelProviders != null)
            {
                MetaModelService = MetaModelProviders.CurrentMetaModelService;
            }

            var extension             = ProjectParameters.Instance.Extension;
            var defaultLablesFileName = ProjectParameters.Instance.DefaultLabelsFileName;

            if (string.IsNullOrEmpty(defaultLablesFileName))
            {
                throw new System.Exception(
                          "Label file name not specified in the Settings: Dynamics 365 > Addins > Ninja DevTools Settings");
            }

            var lableFile = MetaModelService.GetLabelFile(defaultLablesFileName);

            if (lableFile == null)
            {
                throw new Exception("Labels file not found");
            }

            var    labelKey = name.Replace(extension, "");
            string lableTxt;

            if (alternative != null && !alternative.StartsWith("@"))
            {
                lableTxt = alternative;
            }
            else
            {
                lableTxt = Regex.Replace(labelKey, "((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", " $1").Trim().ToLower().UppercaseFirst();
            }

            LabelControllerFactory factory = new LabelControllerFactory();
            LabelEditorController  labelEditorController = factory.GetOrCreateLabelController(lableFile, LocalUtils.Context);

            if (!labelEditorController.Exists(labelKey))
            {
                labelEditorController.Insert(labelKey, lableTxt, string.Empty);
                labelEditorController.Save();
            }

            return($"@{extension}Labels:{labelKey}");
        }
Esempio n. 7
0
        public static LabelContent FindLabel(string labelIdText)
        {
            //string labelId = String.Empty;
            LabelContent labelContent = new LabelContent()
            {
                LabelIdForProperty = labelIdText
            };

            if (labelIdText.StartsWith("@") == false)
            {
                return(labelContent);
            }

            //var labelFileId
            labelContent.LabelFileId = LabelHelper.GetLabelFileId(labelIdText);
            labelContent.LabelId     = labelContent.LabelIdForProperty.Substring(labelContent.LabelIdForProperty.IndexOf(":") + 1);

            // Get the label factory
            LabelControllerFactory factory = new LabelControllerFactory();

            // Get the label edit controller
            var labelFiles = LabelHelper.GetLabelFiles();

            var labelFile = labelFiles
                            .Where(l => l.LabelFileId.Equals(labelContent.LabelFileId, StringComparison.InvariantCultureIgnoreCase))
                            .First();

            if (labelFile != null)
            {
                LabelEditorController labelController = factory.GetOrCreateLabelController(labelFile, Common.CommonUtil.GetVSApplicationContext());
                labelController.LabelSearchOption = SearchOption.MatchExactly;
                labelController.IsMatchExactly    = true;


                var label = labelController.Labels.Where(l => l.ID.Equals(labelContent.LabelId, StringComparison.InvariantCultureIgnoreCase))
                            .First();
                //if (label != null)
                {
                    labelContent.LabelDescription = label.Description;
                    labelContent.LabelText        = label.Text;
                }
            }
            return(labelContent);
        }
Esempio n. 8
0
        public static string FindOrCreateLabel(string labelText)
        {
            string newLabelId = String.Empty;

            //Is the labeltext Already a label?
            if (labelText.StartsWith("@"))
            {
                return(labelText);
            }

            // Don't bother if the string is empty
            if (String.IsNullOrEmpty(labelText) == false)
            {
                // Construct a label id that will be unique
                //string labelId = $"{elementName}{propertyName}";

                string labelId = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(labelText)
                                 .Replace(" ", "")                 // remove spaces
                                 .Replace("%", "P")                // Replace %1 with P1 or %2 with P2 etc
                                 .Replace("_", "");
                if (Regex.IsMatch(labelId.Substring(0, 1), @"\d")) // label id cannot start with a number
                {
                    labelId = "Label" + labelId;
                }
                labelId = Regex.Replace(labelId, "[^a-zA-Z0-9]", ""); // no special chars in the label id
                // Get the label factory
                LabelControllerFactory factory = new LabelControllerFactory();

                // Get the label edit controller
                var labelFiles = LabelHelper.GetLabelFiles();

                var labelFile = labelFiles.First();

                LabelEditorController labelController = factory.GetOrCreateLabelController(labelFile, Common.CommonUtil.GetVSApplicationContext());
                labelController.LabelSearchOption = SearchOption.MatchExactly;
                labelController.IsMatchExactly    = true;

                // Make sure the label doesn't exist.
                // What will you do if it does?
                //var labelsx = labelController.Labels.ToList();
                if (labelController.Exists(labelId) == false &&
                    labelController.Exists(labelText) == false)    // the label text may be an Id by itself
                {
                    labelController.Insert(labelId, labelText, null);
                    labelController.Save();
                    Common.CommonUtil.AddElementToProject(labelFile);

                    // Construct a label reference to go into the label property
                    // newLabelId = $"@{labelFile.LabelFileId}:{labelId}";

                    //Create this label in the other languages as well.
                    foreach (var labelFileToAdd in labelFiles)
                    {
                        if (labelFileToAdd.LabelFileId == labelFile.LabelFileId &&
                            !labelFileToAdd.Language.Equals(labelFile.Language))
                        {
                            LabelEditorController labelControllerToAdd = factory.GetOrCreateLabelController(labelFileToAdd, Common.CommonUtil.GetVSApplicationContext());
                            labelControllerToAdd.Insert(labelId, labelText, null);
                            labelControllerToAdd.Save();
                            Common.CommonUtil.AddElementToProject(labelFileToAdd);
                        }
                    }
                }
                newLabelId = $"@{labelFile.LabelFileId}:{labelId}";
            }

            return(newLabelId);
        }
Esempio n. 9
0
        /// <summary>
        /// Check all properties and create labels for them if needed
        /// </summary>
        /// <param name="ob">Instance of an <c>Object</c> to check for preoprties</param>
        /// <param name="labelPrefix"><c>String</c> Prefix to add to the label</param>
        /// <param name="labelFile">Instance of <c>AXLabelFile</c> to insert into</param>
        public void createPropertyLabels(Object ob, String labelPrefix)
        {
            if (currentLabelFile == null)
            {
                CoreUtility.DisplayError("Labelfile not found");
                return;
            }

            labelController = factory.GetOrCreateLabelController(currentLabelFile, LabelHelper.Context);

            var type = ob.GetType();

            var LabelProperty = getLabelProperty(type);

            if (LabelProperty != null)
            {
                var label = LabelProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.LabelTag, true);
                    LabelProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            var helpTextProperty = getHelpTextProperty(type);

            if (helpTextProperty != null)
            {
                var label = helpTextProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.HelpTextTag, true);
                    helpTextProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            var captionProperty = getCaptionProperty(type);

            if (captionProperty != null)
            {
                var label = captionProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.CaptionTag, true);
                    captionProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            var textProperty = getTextProperty(type);

            if (textProperty != null)
            {
                var label = textProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.TextTag, true);
                    textProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            var DescriptionProperty = getDescriptionProperty(type);

            if (DescriptionProperty != null)
            {
                var label = DescriptionProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.DescriptionTag, true);
                    DescriptionProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            var developerDocumentationProperty = getDeveloperDocumentationProperty(type);

            if (developerDocumentationProperty != null)
            {
                var label = developerDocumentationProperty.GetValue(ob).ToString();

                if (this.IsValidLabelId(label) == false)
                {
                    var labelid = this.FindOrCreateLabel(label, labelPrefix, Tags.DeveloperDocumentationTag, true);
                    developerDocumentationProperty.SetValue(ob, labelid != string.Empty ? labelid : label);
                }
            }

            labelController.Save();
        }