1

I'm implementing service, that should store csv file in the Hybris backoffice, one file for one day. (and add one line when the specific process is done)

Is there any way how to rewrite that file, because only thing I'm still getting is new versions of the file. Is there any doc for this? I'm afraid there isn't any at help.hybris.com

File fileForCatalog = ... // I'm able to get that file from backoffice and add a line
Media mediaForFile = MediaManager.getInstance().createMedia(fileForCatalog.getName());
// creates new file 
mediaForFile.setFile(fileForCatalog);
mediaForFile.writeReplace(); // creates duplicate
Stepan Tuhacek
  • 219
  • 5
  • 18

2 Answers2

3

I would suggest using medias with the MediaService.

// Get media, f.e. by code
MediaModel media = mediaService.getMedia("myMediaCode");
InputStream oldInputStream = mediaService.getStreamFromMedia(media);
// add your logic to add a line
InputStream newInputStream = ...;
mediaService.setStreamForMedia(media, newInputStream);

Lookup Adding characters to beginning and end of InputStream in Java for examples how to add your line to the input stream.

Johannes von Zmuda
  • 1,814
  • 8
  • 14
0

I think you need using Media Service and unaware media because you don't need to sync this media.

Saving unaware media:

final CatalogUnawareMediaModel media = modelService.create(CatalogUnawareMediaModel.class);
media.setCode(name);
media.setMime(MediaType.CSV_UTF_8.type());
media.setRealFileName(name + "_" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date()) + ".csv");
modelService.save(media);
mediaService.setStreamForMedia(media, IOUtils.toInputStream(buffer));

You can use same setStreamForMedia method for updating data.

mkysoft
  • 5,392
  • 1
  • 21
  • 30