Skip to Content
author's profile photo Former Member
Former Member

What is the purpose of executeInLocalView?

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?

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Best Answer
    Posted on Aug 23, 2016 at 05:30 PM

    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)

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2016 at 05:42 PM

    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/DefaultSessionService.html

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2016 at 06:50 PM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.