cancel
Showing results for 
Search instead for 
Did you mean: 

Intercept media creation and update

Former Member
0 Kudos

Hi,

I am trying to intercept the media creation and update in hMC. The interceptors (ValidateInterceptor and PrepareInterceptor) work fine for fields I don't need (catalog, code ..), what I am interested in is the "URL" property and when I upload a media, image in my case, and that is the only change to the media item, Interceptors are not invoked.

Can you suggest me a way of monitoring the "URL" property changes for Media type?

Regards, Leo

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi ,

I have the same problem as you. Have you already solved this problem? When yes, please share 🙂

Regards, Petar.

Former Member
0 Kudos

Thanks for the quick answer, but my problem is that the Interceptor isn't called in the first place. For example:

When changing the code of the media-object the interceptor is called and "model.getValueHistory().getDirtyAttributes()" returns a set with one String "code".

When changing the code of the media-object AND the URL (by using the wizard in the screenshot) the interceptor is called and "model.getValueHistory().getDirtyAttributes()" still returns a set with one String "code".

When only changing the URL the interceptor is not called. So I don't have the possibility to track "realFileName" or "location" at this point.

Former Member
0 Kudos

It seems that de.hybris.platform.servicelayer.media.DynamicAttributesMediaUrl is responsible for the url, it uses the MediaService to get it, which uses the MediaManager to do the following:

   public String getURLForMedia(String folderQualifier, MediaSource mediaSource)
   {
     MediaStorageConfigService.MediaFolderConfig config = this.mediaStorageConfigService.getConfigForFolder(folderQualifier);
     if (config.isSecured())
     {
       return MediaUtil.assembleSecureMediaURL(mediaSource);
     }
 
     MediaURLStrategy strategy = this.mediaStorageRegistry.getURLStrategyForFolder(config, getPreferredUrlStrategyIds());
     String url = strategy.getUrlForMedia(config, mediaSource);
 
     if (GenericValidator.isBlankOrNull(url))
     {
       url = mediaSource.getInternalUrl();
     }
 
     return url;
   }

The LocalMediaWebURLStrategy which is the default uses config data on the base url and the "realFileName" or the "location" to compose the Url.

  private String assembleLegacyURL(String folderQualifier, MediaSource mediaSource)
   {
     StringBuilder sb = new StringBuilder(MediaUtil.addTrailingFileSepIfNeeded(getMediaWebRootContext()));
     sb.append("sys_").append(getTenantId()).append("/");
     sb.append(folderQualifier).append("/");
     String realFileName = getRealFileNameForMedia(mediaSource);
     if (StringUtils.isNotBlank(realFileName))
     {
       String location = mediaSource.getLocation();
       int lastDotIndex = location.lastIndexOf('.');
       String basePath = location.substring(0, lastDotIndex);
       String fileExtension = location.substring(lastDotIndex);
       int lastDotIndexForRealFileName = realFileName.lastIndexOf('.');
       if (lastDotIndexForRealFileName != -1)
       {
         realFileName = realFileName.substring(0, lastDotIndexForRealFileName);
       }
       sb.append(basePath).append("/").append(realFileName).append(fileExtension);
     }
     else
     {
       sb.append(mediaSource.getLocation());
     }
     return sb.toString();
   }

So you could use these fields for your interceptor.

Former Member
0 Kudos

Sorry about that, i forgot to mention my Hybris-Version. I'm working on Hybris 4.8.8 and in the core-items.xml I see:

 <attribute autocreate="true" qualifier="URL" type="java.lang.String" generate="true">
     <modifiers read="true" write="true" search="false" optional="true" />
     <persistence type="property" qualifier="url">
         <columntype database="oracle">
             <value>varchar2(4000)</value>
         </columntype>
         <columntype database="mysql">
             <value>text</value>
         </columntype>
         <columntype database="sqlserver">
             <value>nvarchar(max)</value>
         </columntype>
         <columntype database="hsqldb">
             <value>longvarchar</value>
         </columntype>
     </persistence>
     <model>
         <getter name="url" deprecated="true"/>
         <setter name="url" deprecated="true"/>
     </model>
 </attribute>

Either way, what would be the right approach to track changes on that attribute? While researching I stumbled across a "AttributeValueChangeEvent", but I was not able to get the dependency to "de.hybris.platform.hmc.attribute.AttributeValueChangeListener" in my ant build-path.

Thanks in advance

Former Member
0 Kudos

Oh, ok - I was checking 5.1.1.

Former Member
0 Kudos

The Url is a dynamic attribute, obviously the interceptors don't work on these:

         <itemtype code="Media"
                      extends="AbstractMedia"
                      jaloclass="de.hybris.platform.jalo.media.Media"
                      autocreate="true"
                      generate="true">
             <deployment table="Medias" typecode="30" propertytable="MediaProps"/>
             <attributes>
                 <attribute qualifier="URL" type="java.lang.String">
                     <modifiers read="true" write="true" search="false" optional="true" />
                      <persistence type="dynamic" />
                     <model>
                         <getter name="url" deprecated="true"/>
                         <setter name="url" deprecated="true"/>
                     </model>
                 </attribute>