cancel
Showing results for 
Search instead for 
Did you mean: 

What is the purpose of executeInLocalView?

Former Member

What does local view mean? Is there any "global" view? Let's see this method (from de/hybris/platform/catalog/impl/DefaultCatalogVersionService.java:181):

     @Override
     public Collection<CatalogVersionModel> getAllReadableCatalogVersions(final PrincipalModel principal)
     {
         ServicesUtil.validateParameterNotNull(principal, "Parameter 'principal' must not be null");
         final Collection<CatalogVersionModel> readableCatalogVersions = (Collection<CatalogVersionModel>) getSessionService()
                 .executeInLocalView(new SessionExecutionBody()
                 {
 
                     @Override
                     public Object execute()
                     {
                         try
                         {
                             searchRestrictionService.disableSearchRestrictions();
                             return catalogVersionDao.findReadableCatalogVersions(principal);
                         }
                         finally
                         {
                             searchRestrictionService.enableSearchRestrictions();
                         }
                     }
 
                 });
         return readableCatalogVersions;
     }

What changes if I rewrite it this way:

     @Override
     public Collection<CatalogVersionModel> getAllReadableCatalogVersions(final PrincipalModel principal)
     {
         ServicesUtil.validateParameterNotNull(principal, "Parameter 'principal' must not be null");
         final Collection<CatalogVersionModel> readableCatalogVersions = (Collection<CatalogVersionModel>) getSessionService()
 
         try
         {
             searchRestrictionService.disableSearchRestrictions();
             return catalogVersionDao.findReadableCatalogVersions(principal);
         }
         finally
         {
             searchRestrictionService.enableSearchRestrictions();
         }
 
         return readableCatalogVersions;
     }


As far as I know whenever the code is being executed JaloSession and consequently SessionContext exists and it will be used when needed. I suppose that this method can be used to do something on behalf of another user but it does not make any sense for me to specify that the code has to be executed in a "local view". What does that mean? Why we need this?

Accepted Solutions (1)

Accepted Solutions (1)

andyfletcher
Active Contributor

I believe it makes a copy of your current session context.

The session context could be used by multiple threads at the same time and you don't want your changes to unexpectedly change things running in another thread. This way you make the change just for the running thread and roll it back again afterwards (even if there was an exception)

Answers (2)

Answers (2)

Former Member

executeInLocalView is useful if you need to execute some code, for example run a flexible search query, in the context of a different user, than the logged in user. Or with disabled search restrictions. in other words: you bypass restrictions for the logged in user for specific purposes.

See also:

https://download.hybris.com/api/5.7.0/commercesuite/de/hybris/platform/servicelayer/session/impl/Def...

https://wiki.hybris.com/display/forum/Difference+between+normal+flexisearch+and+executeInLocalView

https://answers.sap.com/questions/12757402/execute-flexible-query-in-admin-context.html

Former Member
0 Kudos

When a web request is being executed, the controller, facades, services, and DAOs are executing in the context of the logged-in user (or the anonymous user). When a cron job executes, this is not true. Sometimes you need to execute something in a cron job in the context of a user, such as recalculate the cart when there is user-specific pricing.