Book a Demo

Author Topic: Finding elements with Scripting and Addins  (Read 7171 times)

MathiasK

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Finding elements with Scripting and Addins
« on: April 22, 2014, 06:57:43 pm »
Hi
hope everybody had some nice free days ,

may someone can give me a hint if i can do the following:

search a Model for special class-names, attribute-names and values and some other stuff and collect them in a collection and show that collection in a special window or highlight the elements with colors.

With EA-Scripting?
With EA-Addins?
With from outside with the API?
With Something other?

Edit:
About scripting i found (german):
"Auf komfortable Wizards und GUIs muß bei der Verwendung von Skripten allerdings verzichtet werden." [german]

in english: "
"You have to resign compfortable wizards and guis in scripting"

http://www.sparxsystems.de/fileadmin/user_upload/Ressources/Scripting_Model_Documents/SPX_CE_-_HK_-Scripting_Model_Documents.pdf
(Page 7)
« Last Edit: April 22, 2014, 07:09:36 pm by mathaiskrill »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding elements with Scripting and Addins
« Reply #1 on: April 22, 2014, 07:39:44 pm »
You can create own windows that will display inside EA. But it's a bit more advanced stuff. To find elements by name you should use Repository.SQLQuery and supply the right query in t_object or whatever is applicable. To invoke such a script you can either write an add-in, use the internal script editor or call an external program that accesses the EA instance.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Finding elements with Scripting and Addins
« Reply #2 on: April 22, 2014, 08:53:12 pm »
Mathias,

Check out my EA Navigator. The quicksearch function probably is more or less what you are looking for.
The add-in is open source so you start from that and customize as you like.

Geert

OpenIT Solutions

  • EA User
  • **
  • Posts: 555
  • Karma: +9/-1
    • View Profile
Re: Finding elements with Scripting and Addins
« Reply #3 on: April 23, 2014, 02:20:52 am »
To find Elements by name you could also use  Repository.GetElementSet (<SQL>, 2);

i.e. Repository.GetElementSet ("SELECT * FROM t_object WHERE name LIKE '%" + name + "%'", 2);

It returns a collection of EA.Element.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Finding elements with Scripting and Addins
« Reply #4 on: April 23, 2014, 03:52:01 pm »
Quote
To find Elements by name you could also use  Repository.GetElementSet (<SQL>, 2);

i.e. Repository.GetElementSet ("SELECT * FROM t_object WHERE name LIKE '%" + name + "%'", 2);

It returns a collection of EA.Element.

... If you use a RDBMS. EAP (Mickeysoft) uses a '*' instead of '%'.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Finding elements with Scripting and Addins
« Reply #5 on: April 23, 2014, 04:31:06 pm »
If you are writing code for both, I've written a little helper function to make your modify the syntax of your xml according to the database type you are working on.
From Model.cs
Part1
Code: [Select]
/// generic query operation on the model.
    /// Returns results in an xml format
    public XmlDocument SQLQuery(string sqlQuery)
    {
          sqlQuery = this.formatSQL(sqlQuery);
          XmlDocument results = new XmlDocument();
            results.LoadXml(this.wrappedModel.SQLQuery(sqlQuery));
            return results;
    }
    
    /// <summary>
    /// sets the correct wildcards depending on the database type.
    /// changes '%' into '*' if on ms access
    /// and _ into ? on msAccess
    /// </summary>
    /// <param name="sqlQuery">the original query</param>
    /// <returns>the fixed query</returns>
    private string formatSQL(string sqlQuery)
    {
          sqlQuery = replaceSQLWildCards(sqlQuery);
          sqlQuery = formatSQLTop(sqlQuery);
          sqlQuery = formatSQLFunctions(sqlQuery);
          return sqlQuery;
    }
    
    /// <summary>
    /// Operation to translate SQL functions in there equivalents in different sql syntaxes
    /// supported functions:
    ///
    /// - lcase -> lower in T-SQL (SQLSVR and ASA)
    /// </summary>
    /// <param name="sqlQuery">the query to format</param>
    /// <returns>a query with traslated functions</returns>
    private string formatSQLFunctions(string sqlQuery)
    {
          string formattedSQL = sqlQuery;
          //lcase -> lower in T-SQL (SQLSVR and ASA and Oracle and FireBird)
          if (this.repositoryType == RepositoryType.SQLSVR ||
              this.repositoryType == RepositoryType.ASA ||
                   this.repositoryType == RepositoryType.ORACLE ||
                   this.repositoryType == RepositoryType.FIREBIRD)
          {
                formattedSQL = formattedSQL.Replace("lcase(","lower(");
          }
          return formattedSQL;
    }
    

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Finding elements with Scripting and Addins
« Reply #6 on: April 23, 2014, 04:31:48 pm »
Part2
Code: [Select]
/// <summary>
    /// limiting the number of results in an sql query is different on different platforms.
    ///
    /// "SELECT TOP N" is used on
    /// SQLSVR
    /// ADOJET
    /// ASA
    /// OPENEDGE
    /// ACCESS2007
    ///
    /// "WHERE rowcount <= N" is used on
    /// ORACLE
    ///
    /// "LIMIT N" is used on
    /// MYSQL
    /// POSTGRES
    ///
    /// This operation will replace the SELECT TOP N by the appropriate sql syntax depending on the repositorytype
    /// </summary>
    /// <param name="sqlQuery">the sql query to format</param>
    /// <returns>the formatted sql query </returns>
    private string formatSQLTop(string sqlQuery)
    {
          string formattedQuery = sqlQuery;
          string selectTop = "select top ";
          int begintop = sqlQuery.ToLower().IndexOf(selectTop);
          if (begintop >= 0)
          {
                int beginN = begintop + selectTop.Length;
                int endN = sqlQuery.ToLower().IndexOf(" ",beginN) +1;
                if (endN > beginN)
                {
                      string N = sqlQuery.ToLower().Substring(beginN, endN - beginN);
                      string selectTopN = sqlQuery.Substring(begintop, endN);
                      switch ( this.repositoryType)
                      {
                            case RepositoryType.ORACLE :
                                  // remove "top N" clause
                                  formattedQuery = formattedQuery.Replace(selectTopN, "select ");
                                  // find where clause
                                  string whereString = "where ";
                                  int beginWhere = formattedQuery.ToLower().IndexOf(whereString);
                                  string rowcountCondition = "rownum <= " + N + " and ";
                                  // add the rowcount condition
                                  formattedQuery = formattedQuery.Insert(beginWhere + whereString.Length,rowcountCondition);
                                  break;
                            case RepositoryType.MYSQL :
                            case RepositoryType.POSTGRES :
                                  // remove "top N" clause
                                  formattedQuery = formattedQuery.Replace(selectTopN, "select ");
                                  string limitString = " limit " + N ;
                                  // add limit clause
                                  formattedQuery = formattedQuery + limitString;
                                  break;
                            case RepositoryType.FIREBIRD:
                                  // in firebird top becomes first
                                  formattedQuery = formattedQuery.Replace(selectTopN,selectTopN.Replace("top","first"));
                                  break;
                      }
                }
          }
          return formattedQuery;
    }
    /// <summary>
    /// replace the wildcards in the given sql query string to match either MSAccess or ANSI syntax
    /// </summary>
    /// <param name="sqlQuery">the sql string to edit</param>
    /// <returns>the same sql query, but with its wildcards replaced according to the required syntax</returns>
    private string replaceSQLWildCards(string sqlQuery)
    {
          bool msAccess = this.repositoryType == RepositoryType.ADOJET;
          int beginLike = sqlQuery.IndexOf("like",StringComparison.InvariantCultureIgnoreCase);
          if (beginLike > 1 )
          {
                int beginString = sqlQuery.IndexOf("'",beginLike + "like".Length);
                if (beginString > 0)
                {
                      int endString = sqlQuery.IndexOf("'",beginString +1);
                      if (endString > beginString)
                      {
                            string originalLikeString = sqlQuery.Substring(beginString +1,endString - beginString );
                            string likeString = originalLikeString;
                            if (msAccess)
                            {
                                  likeString = likeString.Replace('%','*');
                                  likeString = likeString.Replace('_','?');
                                    likeString = likeString.Replace('^','!');
                            }
                            else
                            {
                                  likeString = likeString.Replace('*','%');
                                  likeString = likeString.Replace('?','_');
                                  likeString = likeString.Replace('#','_');
                                    likeString = likeString.Replace('^','!');
                            }
                            string next = string.Empty;
                            if (endString < sqlQuery.Length)
                            {
                                  next = replaceSQLWildCards(sqlQuery.Substring(endString +1));
                            }
                            sqlQuery = sqlQuery.Substring(0,beginString+1) + likeString + next;
                                                        
                      }
                }
          }
          return sqlQuery;
    }

Geert

MathiasK

  • EA User
  • **
  • Posts: 25
  • Karma: +0/-0
    • View Profile
Re: Finding elements with Scripting and Addins
« Reply #7 on: April 23, 2014, 04:51:41 pm »
Thank you, i appreciate your help . I havend figured out the context of sqlQuery here (havn't learned sql yet  :-/ , but iam on it  :) ).



thx so far ^^