cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

How to obtain Choices from Filters?

SD_11338516
11-Garnet

How to obtain Choices from Filters?

Hi there,

 

I'm trying to obtain the options from a saved filter. "Choices" 

SD_11338516_2-1730285502396.png

 

 

try {
    java.lang.String username = "testuser";
    wt.session.SessionHelper.manager.setPrincipal(username);
    wt.filter.NavigationCriteria navigationCriteria = wt.filter.NavigationCriteriaHelper.service.getNavigationCriteria("MyFilter");

    if (navigationCriteria == null) {
        java.lang.System.out.println("Filter 'MyFilter' not found or user does not have access.");
    } else {
        java.lang.System.out.println("Navigation Criteria Name: " + navigationCriteria.toString());
   java.util.List<wt.filter.NavigationFilter2> filters =  (java.util.List<wt.filter.NavigationFilter2>)navigationCriteria.getFilters();

        if (filters == null || filters.isEmpty()) {
            java.lang.System.out.println("No filters found for the Navigation Criteria.");
        } else {
            for (wt.filter.NavigationFilter2 filter : filters) {
                java.lang.System.out.println("Filter Name: " + filter.toString());
                java.lang.System.out.println("Filter Type: " + filter.getType());
            }
        }
    }

} catch (java.lang.Exception e) {
    java.lang.System.out.println("Error switching session: " + e.getMessage());
} finally {
    java.lang.System.out.println("Process completed.");
}

Here my logs:

Navigation Criteria Name: wt.filter.NavigationCriteria:xxxxxx
Filter Name: com.ptc.windchill.option.model.ATONavigationFilter:xxxxxx
Filter Type: ATONavigation Filter
Process completed.

I think I'm sucessfully obtain the filter but i can't reach the Choices from this type. I need to parse the Choices. 

Also i'm trying to get it from NavigationFilter2, i've found a javadoc for this i think i can parse it from the json if i call "getJSONFromFilter(NavigationFilter2 navFilter)"  getJSONFromFilter function. 

SD_11338516_0-1730286325468.png

 

 

 

So i've used code like that; 

java.util.List<wt.filter.NavigationFilter2> filters = 
    (java.util.List<wt.filter.NavigationFilter2>) navigationCriteria.getFilters();
for (wt.filter.NavigationFilter2 navFilter : filters) {
String jsonRepresentation = wt.filter.NavigationFilterDelegate2.getJSONFromFilter(navFilter);
    System.out.println(jsonRepresentation);
}

But i couldn't compile this code because NavigationFilterDelegate2 is an interface so i can't call it directly. 

@HelesicPetr  i wonder your comment for second part too 🙂 

Regards,

ACCEPTED SOLUTION

Accepted Solutions

Hi @SD_11338516 

If the Filter contains the Options then it should be an ATONavigationFilter type.

So retype the filter to the mentioned type and use the get methods for the options set and choice map 

 

It works. btw if you are not sure if the user has an access to the saved filter, then change the active user to an Administrator for the time.

HelesicPetr_0-1730970681119.png

 

 

NavigationCriteria crit = NavigationCriteriaHelper.service.getNavigationCriteria("MYVARIANT", true);
System.out.println(crit.toString());
Collection<NavigationFilter2> filters = crit.getFilters();

for (NavigationFilter2 navigationFilter2 : filters)
{
	if (navigationFilter2 instanceof ATONavigationFilter)
	{
		ATONavigationFilter atonFilter = (ATONavigationFilter) navigationFilter2;
		System.out.println(atonFilter.getOptionSet().toString());
		System.out.println(atonFilter.getFilterType());
		System.out.println(atonFilter.getChoiceMap().toString());
	}
}

 

 

my output

HelesicPetr_1-1730970846793.png

PetrH

 

View solution in original post

3 REPLIES 3

Hi @SD_11338516

Thank you for your question!

Your post appears well documented but has not yet received any response. I am replying to raise awareness. Hopefully, another community member will be able to help.

Also, feel free to add any additional information you think might be relevant.

 

Best regards,

Catalina
PTC Community Moderator

Hi @SD_11338516 

If the Filter contains the Options then it should be an ATONavigationFilter type.

So retype the filter to the mentioned type and use the get methods for the options set and choice map 

 

It works. btw if you are not sure if the user has an access to the saved filter, then change the active user to an Administrator for the time.

HelesicPetr_0-1730970681119.png

 

 

NavigationCriteria crit = NavigationCriteriaHelper.service.getNavigationCriteria("MYVARIANT", true);
System.out.println(crit.toString());
Collection<NavigationFilter2> filters = crit.getFilters();

for (NavigationFilter2 navigationFilter2 : filters)
{
	if (navigationFilter2 instanceof ATONavigationFilter)
	{
		ATONavigationFilter atonFilter = (ATONavigationFilter) navigationFilter2;
		System.out.println(atonFilter.getOptionSet().toString());
		System.out.println(atonFilter.getFilterType());
		System.out.println(atonFilter.getChoiceMap().toString());
	}
}

 

 

my output

HelesicPetr_1-1730970846793.png

PetrH

 

Thank you @HelesicPetr , i've finally succesfully done it. I'm able to obtain all choice names by this code 🙂 

try {
    // Set Active user
    java.lang.String username = "testuser";
    wt.session.SessionHelper.manager.setPrincipal(username);
    // Get Filter
    wt.filter.NavigationCriteria navigationCriteria = wt.filter.NavigationCriteriaHelper.service.getNavigationCriteria("MyFilter");

    if (navigationCriteria == null) {
        java.lang.System.out.println("Filter 'MyFilter' not found or user does not have access.");
    } else {
        java.lang.System.out.println("Navigation Criteria Name: " + navigationCriteria.toString());
    }

    java.util.List<wt.filter.NavigationFilter2> filters = (java.util.List<wt.filter.NavigationFilter2>) navigationCriteria.getFilters();
    StringBuilder choiceNames = new StringBuilder();
    for (wt.filter.NavigationFilter2 navigationFilter2 : filters) {
        if (navigationFilter2 instanceof com.ptc.windchill.option.model.ATONavigationFilter) {
            com.ptc.windchill.option.model.ATONavigationFilter atonFilter = (com.ptc.windchill.option.model.ATONavigationFilter) navigationFilter2;
            System.out.println("OptionSet: " + atonFilter.getOptionSet().toString());
            System.out.println("FilterType: " + atonFilter.getFilterType());
            java.util.Map<?, ?> choiceMap = atonFilter.getChoiceMap();
            for (Object optionObj : choiceMap.keySet()) {
                Object choicesObj = choiceMap.get(optionObj);
                if (choicesObj instanceof java.util.Set) {
                    java.util.Set<?> choicesSet = (java.util.Set<?>) choicesObj;
                    for (Object choiceObj : choicesSet) {
                        long choiceId = Long.parseLong(choiceObj.toString().split(":")[1]);
                        wt.fc.ReferenceFactory rf = new wt.fc.ReferenceFactory();
                        com.ptc.windchill.option.model.Choice choiceFromRef = (com.ptc.windchill.option.model.Choice) rf.getReference("com.ptc.windchill.option.model.Choice:" + choiceId).getObject();
                        if (choiceFromRef != null) {
                            if (choiceNames.length() > 0) {
                                choiceNames.append(", "); 
                            }
                            choiceNames.append(choiceFromRef.getName());
                        } else {
                            System.out.println("Choice with ID " + choiceId + " not found.");
                        }
                    }
                }
            }
        }
    }
    if (choiceNames.length() > 0) {
        System.out.println("Choice Names: " + choiceNames.toString());
    } else {
        System.out.println("No choices found.");
    }
} catch (java.lang.Exception e) {
    java.lang.System.out.println("Error switching session: " + e.getMessage());
} finally {
    java.lang.System.out.println("Process completed.");
}

 

Announcements

Top Tags