c34dd3660723e6a6e82df613a7e4410f296c409e
[mediacube.git] /
1 package hu.user.mcvodsync.service.xls;
2
3
4 import hu.user.mcvodsync.db.Asset;
5 import hu.user.mcvodsync.db.AssetSync;
6 import hu.user.mcvodsync.db.PlaylistSync;
7 import hu.user.mcvodsync.db.SyncType;
8 import hu.user.mcvodsync.db.repository.AssetRepository;
9 import hu.user.mcvodsync.db.repository.AssetSyncRepository;
10 import hu.user.mcvodsync.db.repository.PlaylistSyncRepository;
11 import hu.user.mcvodsync.service.data.EntityDataService;
12 import lombok.extern.log4j.Log4j2;
13 import org.apache.commons.collections4.CollectionUtils;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Service;
16
17 import java.time.Instant;
18 import java.time.LocalDate;
19 import java.util.*;
20 import java.util.stream.Collectors;
21
22 @Log4j2
23 @Service
24 public class AssetImportService {
25     @Autowired
26     private AssetRepository assetRepository;
27
28     @Autowired
29     private AssetSyncRepository assetSyncRepository;
30
31     @Autowired
32     private PlaylistSyncRepository playlistSyncRepository;
33
34     @Autowired
35     private EntityDataService<Asset> entityDataService;
36
37     private Map<String, List<String>> currentPlaylists;
38
39     private hu.user.mcvodsync.service.xls.ImportSummary summary;
40
41     public void prepare(hu.user.mcvodsync.service.xls.ImportSummary summary) {
42         this.summary = summary;
43         currentPlaylists = getPlayLists();
44     }
45
46     private Map<String, List<String>> getPlayLists() {
47         Map<String, List<String>> result = new HashMap<>();
48         List<String> playlistNames = assetRepository.queryDistinctPlaylists();
49         playlistNames.forEach(playlist -> {
50             List<String> playlistsAssets = assetRepository.queryPlaylistsAssets(playlist);
51             result.put(playlist, playlistsAssets);
52         });
53         return result;
54     }
55
56     public void processAsset(Asset asset) {
57         Optional<Asset> optionalEntity = assetRepository.findById(asset.getCatalogId());
58
59         AssetSync assetSync = AssetSync.builder()
60                 .catalogId(asset.getCatalogId())
61                 .build();
62
63         if (optionalEntity.isPresent()) {
64             boolean areDifferent = entityDataService.areDifferent(asset, optionalEntity.get());
65             if (areDifferent) {
66                 assetSync.setSyncType(SyncType.UPDATE);
67             }
68         } else {
69             assetSync.setSyncType(SyncType.INSERT);
70         }
71
72         if (Objects.nonNull(assetSync.getSyncType())) {
73             assetRepository.save(asset);
74             assetSyncRepository.save(assetSync);
75         }
76
77         if (assetSync.getSyncType() == SyncType.UPDATE) {
78             summary.incUpdated();
79         }
80         if (assetSync.getSyncType() == SyncType.INSERT) {
81             summary.incInserted();
82         }
83     }
84
85     public void removeExpired(LocalDate currentDate) {
86         List<String> ids = assetRepository.queryExpired(java.sql.Date.valueOf(currentDate));
87         List<AssetSync> syncs = ids.stream().map(id -> AssetSync.builder()
88                 .catalogId(id)
89                 .syncType(SyncType.DELETE)
90                 .build()).collect(Collectors.toList());
91         assetSyncRepository.saveAll(syncs);
92         assetRepository.deleteExpired(java.sql.Date.valueOf(currentDate));
93         summary.setDeleted(summary.getDeleted() + ids.size());
94     }
95
96     public void finish() {
97         removeExpired(LocalDate.now());
98         processPlaylists();
99     }
100
101     public void processPlaylists() {
102         Map<String, List<String>> newPlaylists = getPlayLists();
103         Set<String> currentPlaylistNames = currentPlaylists.keySet();
104         Set<String> newPlaylistNames = newPlaylists.keySet();
105
106         Collection<String> playlistNamesDifference = CollectionUtils.subtract(currentPlaylistNames, newPlaylistNames);
107         if (!playlistNamesDifference.isEmpty()) {
108             playlistNamesDifference.forEach(playlist -> {
109                 PlaylistSync sync = PlaylistSync.builder()
110                         .playlist(playlist)
111                         .syncType(SyncType.DELETE)
112                         .created(Date.from(Instant.now()))
113                         .build();
114                 playlistSyncRepository.save(sync);
115                 summary.incDeletedPlaylist();
116             });
117         }
118
119         playlistNamesDifference = CollectionUtils.subtract(newPlaylistNames, currentPlaylistNames);
120         if (!playlistNamesDifference.isEmpty()) {
121             playlistNamesDifference.forEach(playlist -> {
122                 PlaylistSync sync = PlaylistSync.builder()
123                         .playlist(playlist)
124                         .syncType(SyncType.INSERT)
125                         .created(Date.from(Instant.now()))
126                         .build();
127                 playlistSyncRepository.save(sync);
128                 summary.incInsertedPlaylist();
129             });
130         }
131
132         currentPlaylistNames.forEach(playlist -> {
133             List<String> currentContent = currentPlaylists.get(playlist);
134             List<String> newContent = newPlaylists.get(playlist);
135             if (isContentChanged(currentContent, newContent)) {
136                 PlaylistSync sync = PlaylistSync.builder()
137                         .playlist(playlist)
138                         .syncType(SyncType.UPDATE)
139                         .created(Date.from(Instant.now()))
140                         .build();
141                 playlistSyncRepository.save(sync);
142                 summary.incUpdatedPlaylist();
143             }
144         });
145     }
146
147     private boolean isContentChanged(List<String> currentContent, List<String> newContent) {
148         if (currentContent.size() != newContent.size()) {
149             return true;
150         }
151         for (int i = 0; i < currentContent.size(); i++) {
152             if (!currentContent.get(i).equals(newContent.get(i))) {
153                 return true;
154             }
155         }
156         return false;
157     }
158 }