3629b82240a2289365dda75129c74a23e991e608
[mediacube.git] /
1 package hu.user.mcvodsync.service.export;
2
3
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;
18
19 import javax.persistence.EntityNotFoundException;
20 import java.time.Instant;
21 import java.util.Date;
22 import java.util.List;
23
24 @Log4j2
25 @Service
26 public class AssetExportService {
27     @Autowired
28     private BrightCoveClient bcClient;
29
30     @Autowired
31     private AssetRepository assetRepository;
32
33     @Autowired
34     private AssetSyncRepository assetSyncRepository;
35
36     @Autowired
37     private PlaylistSyncRepository playlistSyncRepository;
38
39     @Autowired
40     private VideoMapper videoMapper;
41
42     @Autowired
43     private ServiceProperties serviceProperties;
44
45     public void export() {
46         while (true) {
47             List<AssetSync> result = assetSyncRepository.findFirst100ByExportedSuccessIsNullOrderByCreated();
48             if (result.isEmpty()) {
49                 break;
50             }
51
52             if (serviceProperties.isTargetVideoInsertEnabled()) {
53                 result.forEach(this::processAssetSync);
54             } else {
55                 result.forEach(this::processAssetSyncWithoutInsert);
56             }
57         }
58
59     }
60
61     @Transactional
62     public void processAssetSyncWithoutInsert(AssetSync assetSync) {
63         try {
64             if (SyncType.DELETE.equals(assetSync.getSyncType())) {
65                 //delete not necessary, remove only from all playlists
66                 assetSync.setExportedSuccess(Date.from(Instant.now()));
67             } else {
68                 Asset asset = assetRepository.findById(assetSync.getCatalogId()).orElseThrow(EntityNotFoundException::new);
69
70                 Video video = bcClient.getVideoByReferenceId(asset.getCatalogId());
71
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);
77                 }
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");
81                     }
82                     bcClient.updateVideo(asset.getReferenceId(), assetVideo);
83                 }
84                 assetSync.setExportedSuccess(Date.from(Instant.now()));
85             }
86         } catch (Exception e) {
87             log.error(e);
88             assetSync.setExportedError(Date.from(Instant.now()));
89         } finally {
90             assetSyncRepository.save(assetSync);
91         }
92     }
93
94     @Transactional
95     public void processAssetSync(AssetSync assetSync) {
96         try {
97             if (SyncType.DELETE.equals(assetSync.getSyncType())) {
98                 //delete not necessary, remove only from all playlists
99                 assetSync.setExportedSuccess(Date.from(Instant.now()));
100             } else {
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);
107                 }
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");
111                     }
112                     bcClient.updateVideo(asset.getReferenceId(), assetVideo);
113                 }
114                 assetSync.setExportedSuccess(Date.from(Instant.now()));
115             }
116         } catch (Exception e) {
117             log.error(e);
118             assetSync.setExportedError(Date.from(Instant.now()));
119         } finally {
120             assetSyncRepository.save(assetSync);
121         }
122     }
123
124 }