Cancel
Showing results for 
Search instead for 
Did you mean: 

C# - ExcecuteSavedQueries obsolete and return ModelObject causes loops of arrays

Genius
Genius

 

We had a note in our C# code that Teamcenter.Services.Strong.Query._2007_06.SavedQuery.SavedQueryInput[] is obsolete.

We have used Query.ExecuteSavedQueries to get different ModelObjects through query search.

 

Now we updated the code so it points to Teamcenter.Services.Strong.Query._2008_06.SavedQuery instead as advised. (We needed to hardcode the namespace pointer even if we declared it by using)

-This lead to that we didn't get an array of ModelObject[] as return and some additional code modifications was needed. The biggest change is that the QueryResult class was changed inbeteen ._2007_06.SavedQuery and ._2007_09.SavedQuery so no ModelObject[] are returned.

-What we now get as return in the array is the Uid on the different object.

- From this Uid array we need to use the Teamcentner.Services.Strong.Core.DataManagementService and LoadObject to get the different objects.

-After that we need to scan through this array and insert them into a List<ModelObject[]> using GetPlainObject. (It's this section when we need to scan through the list and wait until we get a catch that is a little bit akward. We could findout how to see the Lenght of the array created by LoadObject)

- Finaly we send the object from the List to ModelObject[].

...puuh!

 

Our question is if there are some other function that we can use to shorten the code and get a ModelObject[] array as return without using LoadObject and the different array scans?

For us it seems as the obsolete method was alot better!

 

 

This code we had:

 if (imanQuery != null)
            {
                SavedQueryInput[] queryInput = new SavedQueryInput[1];
                queryInput[0] = new SavedQueryInput();
                queryInput[0].Entries = AttributeNames;
                queryInput[0].Values = AttributeValues;
                queryInput[0].Query = imanQuery;
                queryInput[0].MaxNumToReturn = MaxHits;

                ExecuteSavedQueriesResponse Response2 = savedQuery.ExecuteSavedQueries(queryInput);
                if (Response2.ArrayOfResults.Length > 0)
                {
                    if (Response2.ArrayOfResults[0].NumOfObjects > 0)
                    {
                        SavedQueryResults[] results = Response2.ArrayOfResults;
                        return results[0].Objects;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
            else
            {
                throw new Exception("Could not find specified Teamcenter query");
            }
        

 

Now we needed to change to this code:

List<ModelObject> mObj = new List<ModelObject>();

if (imanQuery != null)
            {                
                Teamcenter.Services.Strong.Query._2008_06.SavedQuery.QueryInput[] queryInput = new Teamcenter.Services.Strong.Query._2008_06.SavedQuery.QueryInput[1];
                queryInput[0] = new Teamcenter.Services.Strong.Query._2008_06.SavedQuery.QueryInput();
                queryInput[0].Entries = AttributeNames;
                queryInput[0].Values = AttributeValues;
                queryInput[0].Query = imanQuery;
                queryInput[0].MaxNumToReturn = MaxHits;

Teamcenter.Services.Strong.Query._2007_09.SavedQuery.SavedQueriesResponse Response2 = savedQuery.ExecuteSavedQueries(queryInput);
                
                if (Response2.ArrayOfResults.Length > 0)
                {
                    if (Response2.ArrayOfResults[0].ObjectUIDS.Length > 0)
                    {
                        
                        String[] results = Response2.ArrayOfResults[0].ObjectUIDS; 
                        String result2= results[0];
                        Teamcenter.Services.Strong.Core.DataManagementService dmService = Teamcenter.Services.Strong.Core.DataManagementService.getService(connection);
                        ServiceData sData = dmService.LoadObjects(results);

                        for (int i = 0; ; i++)
                        {
                            ModelObject obj = null;
                            try
                            {
                                obj = sData.GetPlainObject(i);
                            } catch(Exception)
                            {
                                obj = null;
                            }

                            if (obj != null)
                            {
                                log.Debug("Now it checks the PlainObject " + i);
                                mObj.Add(obj);
                            }
                            else
                            {
                                break;
                            }
                            
                        }
                        log.Debug("Now will create mObj2 with these number of mObj " + mObj.Count);
                        ModelObject[] mObj2 = new ModelObject[mObj.Count];
                        
                        for (int i=0; i < mObj.Count; i++)
                        {
                            mObj2[i]=mObj[i];
                        }
                        return mObj2;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
            else
            {
                throw new Exception("Could not find specified Teamcenter query");
            }
        }

 

 

1 REPLY

Re: C# - ExcecuteSavedQueries obsolete and return ModelObject causes loops of arrays

Genius
Genius

.. so I answer on my own! Checked avalible methods again and now we saw the 'sizeOfPlainObjects' and this gave the info we needed. Don't know why we missed it during the previous days, could now remove the noob code and got it more clean!Smiley Very Happy

 

 

if (Response2.ArrayOfResults.Length > 0)
                {
                    if (Response2.ArrayOfResults[0].ObjectUIDS.Length > 0)
                    {
                        
                        String[] results = Response2.ArrayOfResults[0].ObjectUIDS; 
                        String result2= results[0];
                        Teamcenter.Services.Strong.Core.DataManagementService dmService = Teamcenter.Services.Strong.Core.DataManagementService.getService(connection);
                        ServiceData sData = dmService.LoadObjects(results);
                        int size = sData.sizeOfPlainObjects();
                        ModelObject[] mObj = new ModelObject[size];
                        for (int i = 0; i < size; i++)
                        {
                            ModelObject obj = sData.GetPlainObject(i);
                            mObj[i]=obj;
                        }
                        return mObj;
                    }
                    else
                        return null;
                }
                else
                    return null;