1 package hu.user.mcvodsync.service.export;
4 import com.brightcove.cms.client.model.Video;
5 import hu.user.mcvodsync.brightcove.BrightCoveClient;
6 import hu.user.mcvodsync.db.Asset;
7 import hu.user.mcvodsync.db.AssetSync;
8 import hu.user.mcvodsync.db.SyncType;
9 import hu.user.mcvodsync.db.repository.AssetRepository;
10 import hu.user.mcvodsync.db.repository.AssetSyncRepository;
11 import hu.user.mcvodsync.db.repository.PlaylistSyncRepository;
12 import hu.user.mcvodsync.service.ServiceProperties;
13 import lombok.extern.log4j.Log4j2;
14 import org.apache.commons.lang3.StringUtils;
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Service;
17 import org.springframework.transaction.annotation.Transactional;
19 import javax.persistence.EntityNotFoundException;
20 import java.time.Instant;
21 import java.util.Date;
22 import java.util.List;
26 public class AssetExportService {
28 private BrightCoveClient bcClient;
31 private AssetRepository assetRepository;
34 private AssetSyncRepository assetSyncRepository;
37 private PlaylistSyncRepository playlistSyncRepository;
40 private VideoMapper videoMapper;
43 private ServiceProperties serviceProperties;
45 public void export() {
47 List<AssetSync> result = assetSyncRepository.findFirst100ByExportedSuccessIsNullOrderByCreated();
48 if (result.isEmpty()) {
52 if (serviceProperties.isTargetVideoInsertEnabled()) {
53 result.forEach(this::processAssetSync);
55 result.forEach(this::processAssetSyncWithoutInsert);
62 public void processAssetSyncWithoutInsert(AssetSync assetSync) {
64 if (SyncType.DELETE.equals(assetSync.getSyncType())) {
65 //delete not necessary, remove only from all playlists
66 assetSync.setExportedSuccess(Date.from(Instant.now()));
68 Asset asset = assetRepository.findById(assetSync.getCatalogId()).orElseThrow(EntityNotFoundException::new);
70 Video video = bcClient.getVideoByReferenceId(asset.getCatalogId());
72 Video assetVideo = videoMapper.toVideo(asset);
73 if (SyncType.INSERT.equals(assetSync.getSyncType())) {
74 Video createdVideo = bcClient.createVideo(assetVideo);
75 asset.setReferenceId(createdVideo.getId());
76 assetRepository.save(asset);
78 if (SyncType.UPDATE.equals(assetSync.getSyncType())) {
79 if (StringUtils.isBlank(asset.getReferenceId())) {
80 throw new IllegalArgumentException("Can not update Video when Asset entity reference ID is empty");
82 bcClient.updateVideo(asset.getReferenceId(), assetVideo);
84 assetSync.setExportedSuccess(Date.from(Instant.now()));
86 } catch (Exception e) {
88 assetSync.setExportedError(Date.from(Instant.now()));
90 assetSyncRepository.save(assetSync);
95 public void processAssetSync(AssetSync assetSync) {
97 if (SyncType.DELETE.equals(assetSync.getSyncType())) {
98 //delete not necessary, remove only from all playlists
99 assetSync.setExportedSuccess(Date.from(Instant.now()));
101 Asset asset = assetRepository.findById(assetSync.getCatalogId()).orElseThrow(EntityNotFoundException::new);
102 Video assetVideo = videoMapper.toVideo(asset);
103 if (SyncType.INSERT.equals(assetSync.getSyncType())) {
104 Video createdVideo = bcClient.createVideo(assetVideo);
105 asset.setReferenceId(createdVideo.getId());
106 assetRepository.save(asset);
108 if (SyncType.UPDATE.equals(assetSync.getSyncType())) {
109 if (StringUtils.isBlank(asset.getReferenceId())) {
110 throw new IllegalArgumentException("Can not update Video when Asset entity reference ID is empty");
112 bcClient.updateVideo(asset.getReferenceId(), assetVideo);
114 assetSync.setExportedSuccess(Date.from(Instant.now()));
116 } catch (Exception e) {
118 assetSync.setExportedError(Date.from(Instant.now()));
120 assetSyncRepository.save(assetSync);