Search code examples
cronmediasap-commerce-cloud

SAP HYBRIS : how to use media to convert csv file to media hmc


I am a beginner in the Sap Hybris. I created a CronJob that works perfectly. which returns all the products with status approved and generated CSV file in local C://...

But I want to create or convert my CSV file to a media in HMC MEDIA? can someone help me?

I already have gone through Hybris wiki but I didn't understand.

Thank u for all !!


Solution

  • To achieve this, you only need to create your Media object, and attach your file to the created object, something like :

    private MediaModel createMedia(final File file) throws MediaIOException, IllegalArgumentException, FileNotFoundException
    {
    
        final CatalogVersionModel catalogVersion = catalogVersionService.getCatalogVersion("MY_MEDIA_CATALOG", "VERSION");
    
        MediaModel mediaModel;
    
        try
        {
            mediaModel = mediaService.getMedia(catalogVersion, file.getName());
        }
        catch (final UnknownIdentifierException e)
        {
            mediaModel = modelService.create(MediaModel.class);
        }
    
        mediaModel.setCode(file.getName());
        mediaModel.setCatalogVersion(catalogVersion);
        mediaModel.setMime("text/csv");
        mediaModel.setRealFileName(file.getName());
        modelService.save(mediaModel);
        mediaService.setStreamForMedia(mediaModel, new FileInputStream(file));
    
        //Remove file
        FileUtils.removeFile(file);
    
        return mediaModel;
    }