/// <summary>
        /// Creates a XLink to the given WorkingDirectoryFile and copies it to the clipboard.
        /// Aborts the creation if no connection to the OpenEngSB is established.
        /// </summary>
        /// <param name="file"></param>
        public string createXLink(WorkingDirectoryFile file)
        {
            if (!connected)
            {
                outputLine("Error while creating XLink. No connection to OpenEngSB.");
                return(null);
            }
            ModelDescription modelInformation = blueprint.viewToModels.ConvertMap <String, ModelDescription>()[Program.viewId];

            /*Note that only the target class SQLCreate is allowed */
            if (!modelInformation.modelClassName.Equals(classNameOfOpenEngSBModel))
            {
                outputLine("Error: Defined ModelClass '" + classNameOfOpenEngSBModel + "' for view, from OpenEngSB, is not supported by this software program.");
                return(null);
            }

            String completeUrl = blueprint.baseUrl;

            completeUrl += "&" + blueprint.keyNames.modelClassKeyName + "=" + HttpUtility.UrlEncode(modelInformation.modelClassName);
            completeUrl += "&" + blueprint.keyNames.modelVersionKeyName + "=" + HttpUtility.UrlEncode(modelInformation.versionString);
            completeUrl += "&" + blueprint.keyNames.contextIdKeyName + "=" + HttpUtility.UrlEncode(openengsbContext);

            string objectString = convertWorkingDirectoryFileToJSON(file);

            completeUrl += "&" + blueprint.keyNames.identifierKeyName + "=" + HttpUtility.UrlEncode(objectString);

            return(completeUrl);
        }
        /// <summary>
        /// Returns the FileEntry with the given Name or null
        /// </summary>
        public WorkingDirectoryFile searchForFile(String fileName)
        {
            WorkingDirectoryFile searchedFile = null;

            foreach (WorkingDirectoryFile wdf in wdFiles)
            {
                if (wdf.fileName.Equals(fileName))
                {
                    searchedFile = wdf;
                    break;
                }
            }
            if (searchedFile == null)
            {
                foreach (WorkingDirectoryFile wdf in wdFiles)
                {
                    if (wdf.fileName.Contains(fileName))
                    {
                        searchedFile = wdf;
                        break;
                    }
                }
            }
            return(searchedFile);
        }
        /// <summary>
        /// Converts a WorkingDirectoryFile instance to a OOCLass instance and serializes it to String, with JSON
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private string convertWorkingDirectoryFileToJSON(WorkingDirectoryFile file)
        {
            OOClass ooClassOfFile = LinkingUtils.convertWorkingDirectoryFileToOpenEngSBModel(file);
            string  output        = JsonConvert.SerializeObject(ooClassOfFile);

            //HACK: remove isSpecified fields from JSON String
            output = output.Replace(",\"isStaticSpecified\":true", "");
            output = output.Replace(",\"isFinalSpecified\":true", "");
            return(output);
        }
        /// <summary>
        /// Returns an int value which indicates, how many many SQLFiels are alike between the two given statements.
        /// </summary>
        private int matchValueOfClass(WorkingDirectoryFile wdFileToCheck, OOClass potentialMatch)
        {
            int matchValue = 0;

            for (int i = 0; i < potentialMatch.attributes.Length; i++)
            {
                String attributeRepresentation = potentialMatch.attributes[i].type + " " + potentialMatch.attributes[i].name;
                if (wdFileToCheck.content.ToLower().Contains(attributeRepresentation.ToLower()))
                {
                    matchValue++;
                }
            }
            return(matchValue);
        }
        /// <summary>
        /// Opens the File with the given name
        /// </summary>
        public void openFile(string fileName)
        {
            WorkingDirectoryFile searchedFile = searchForFile(fileName);

            if (searchedFile != null)
            {
                outputLine("Opening file '" + fileName + "'...");
                System.Diagnostics.Process.Start(searchedFile.wholePath);
            }
            else
            {
                outputLine("File '" + fileName + "' was not found in WorkingDirectory.");
            }
        }
        /// <summary>
        /// Creates the XLink to the given File with the given name and copies it to the Clipborad.
        /// </summary>
        public void createXLinkFromFileString(string fileName)
        {
            WorkingDirectoryFile searchedFile = searchForFile(fileName);

            if (searchedFile != null)
            {
                String xlink = Program.openengsbConnectionManager.createXLink(searchedFile);
                if (xlink != null)
                {
                    Clipboard.SetText(xlink);
                    outputLine("Xlink was copied to clipboard...");
                }
            }
            else
            {
                outputLine("File was not found.");
            }
        }
        /// <summary>
        /// Triggers the local switching functionality for the given program, using the given viewId ontop of the defined file.
        /// </summary>
        public void triggerLocalSwitch(String programname, String viewId, String filename)
        {
            OpenEngSBCore.XLinkConnector otherLocalTool = findCurrentlyInstalledToolToName(programname);
            if (otherLocalTool == null)
            {
                outputLine("Supplied programname '" + programname + "' unknown.");
                return;
            }
            OpenEngSBCore.XLinkConnectorView otherLocalView = findViewToCurrentlyInstalledTool(otherLocalTool, viewId);

            if (otherLocalView == null)
            {
                outputLine("Supplied viewId '" + viewId + "' unknown to program '" + programname + "'.");
                return;
            }

            WorkingDirectoryFile searchedFile = Program.directoryBrowser.searchForFile(filename);

            if (searchedFile == null)
            {
                outputLine("Specified file was not found.");
                return;
            }
            else
            {
                String xlink = createXLink(searchedFile);
                // TODO remove Hack (hardcoded ConnectorIdKeyname) after correct implementation at OpenEngSB
                xlink += "&" + "connectorId=" + otherLocalTool.id + "&" + blueprint.keyNames.viewIdKeyName + "=" + HttpUtility.UrlEncode(viewId);
                WebRequest      webRequest = WebRequest.Create(xlink);
                HttpWebResponse response   = (HttpWebResponse)webRequest.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    outputLine("Local switch was triggered.");
                }
                else
                {
                    outputLine("Local switch was not successfull triggered, returned status code was " + (int)response.StatusCode);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Convert the interal model "WorkingDirectoryFile" to the defined OpenEngSBModel "OOClass".
        /// Returns null if resulting instance lacks classname or packagename.
        /// </summary>
        public static OOClass convertWorkingDirectoryFileToOpenEngSBModel(WorkingDirectoryFile wdf)
        {
            //Regex to fetch Data
            Regex classRegex   = new Regex(@".*class ([a-zA-Z0-9_]+).*");
            Regex packageRegex = new Regex(@".*namespace ([a-zA-Z0-9_\.]+).*");
            Regex varRegex     = new Regex(@" *(public|protected|private) (int|double|float|string|DateTime|bool|long) ([a-zA-Z0-9_]+) {.*");

            OOClass           resultingInstance = new OOClass();
            List <OOVariable> variables         = new List <OOVariable>();

            //not used in example: set this with dummy values
            resultingInstance.methods = "";

            string[] contentLines = wdf.content.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
            for (int i = 0; i < contentLines.Length; i++)
            {
                if (resultingInstance.packageName == null)
                {
                    Match match = packageRegex.Match(contentLines[i]);
                    if (match.Success)
                    {
                        resultingInstance.packageName = match.Groups[1].Value;
                    }
                }
                else if (resultingInstance.className == null)
                {
                    Match match = classRegex.Match(contentLines[i]);
                    if (match.Success)
                    {
                        resultingInstance.className = match.Groups[1].Value;
                    }
                }
                else
                {
                    Match match = varRegex.Match(contentLines[i]);
                    if (match.Success)
                    {
                        OOVariable newVar = new OOVariable();
                        newVar.type = match.Groups[2].Value;
                        newVar.name = match.Groups[3].Value;
                        //not used in example: set this with dummy values
                        newVar.isFinal           = false;
                        newVar.isFinalSpecified  = true;
                        newVar.isStatic          = false;
                        newVar.isStaticSpecified = true;
                        variables.Add(newVar);
                    }
                }
            }
            if (resultingInstance.packageName == null || resultingInstance.className == null)
            {
                //Instance not correctly set
                return(null);
            }
            resultingInstance.attributes = new OOVariable[variables.Count];
            for (int i = 0; i < variables.Count; i++)
            {
                resultingInstance.attributes[i] = variables[i];
            }
            return(resultingInstance);
        }
        /// <summary>
        /// Searches through the Files for the most potential Match.
        /// First searches for matching filesnames.
        /// If no files where found or if more than one file was found, search for matching variables.
        /// </summary>
        public void searchForXLinkMatches(OOClass potentialMatch)
        {
            if (wdFiles.Count == 0)
            {
                outputLine("An XLink match was triggered, but the List of loaded files is empty.");
                return;
            }

            //first search for matching filesnames
            List <WorkingDirectoryFile> correspondingFiles = new List <WorkingDirectoryFile>();

            foreach (WorkingDirectoryFile wdf in wdFiles)
            {
                OOClass classModelRepresentation = LinkingUtils.convertWorkingDirectoryFileToOpenEngSBModel(wdf);
                if (classModelRepresentation.className.ToLower().Contains(potentialMatch.className.ToLower()))
                {
                    correspondingFiles.Add(wdf);
                }
            }

            WorkingDirectoryFile foundMatch = null;

            //if no files where found or if more than one file was found, compare the matching variables
            if (correspondingFiles.Count != 1)
            {
                if (correspondingFiles.Count == 0)
                {
                    correspondingFiles = wdFiles;
                }

                WorkingDirectoryFile mostPotentialLocalMatch = correspondingFiles[0];
                int maxMatchValue = matchValueOfClass(mostPotentialLocalMatch, potentialMatch);
                foreach (WorkingDirectoryFile localWDF in correspondingFiles)
                {
                    int currentMatchValue = matchValueOfClass(localWDF, potentialMatch);
                    if (currentMatchValue > maxMatchValue)
                    {
                        maxMatchValue           = currentMatchValue;
                        mostPotentialLocalMatch = localWDF;
                    }
                }
                if (matchValueOfClass(mostPotentialLocalMatch, potentialMatch) != 0)
                {
                    foundMatch = mostPotentialLocalMatch;
                }
            }
            else
            {
                foundMatch = correspondingFiles[0];
            }

            if (foundMatch != null)
            {
                outputLine("An XLink match was triggered and a local match was found.");
                System.Diagnostics.Process.Start(foundMatch.wholePath);
            }
            else
            {
                outputLine("An XLink match was triggered, but no local match was found.");
            }
        }