1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.transcription.persistence;
22
23 import org.opencastproject.db.DBSession;
24 import org.opencastproject.db.DBSessionFactory;
25
26 import org.osgi.service.component.ComponentContext;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.util.Date;
34 import java.util.List;
35 import java.util.stream.Collectors;
36
37 import javax.persistence.EntityManagerFactory;
38
39 @Component(
40 immediate = true,
41 name = "org.opencastproject.transcription.persistence.TranscriptionDatabase",
42 service = TranscriptionDatabase.class,
43 property = {
44 "service.description=Transcription Persistence"
45 }
46 )
47 public class TranscriptionDatabaseImpl implements TranscriptionDatabase {
48
49
50
51
52 private static final Logger logger = LoggerFactory.getLogger(TranscriptionDatabaseImpl.class);
53
54
55
56
57 protected EntityManagerFactory emf;
58
59 protected DBSessionFactory dbSessionFactory;
60
61 protected DBSession db;
62
63 private final long noProviderId = -1;
64
65
66
67
68 @Activate
69 public void activate(ComponentContext cc) {
70 logger.info("Activating persistence manager for transcription service");
71 db = dbSessionFactory.createSession(emf);
72 }
73
74 @Reference(target = "(osgi.unit.name=org.opencastproject.transcription.persistence)")
75 public void setEntityManagerFactory(EntityManagerFactory emf) {
76 this.emf = emf;
77 }
78
79 @Reference
80 public void setDBSessionFactory(DBSessionFactory dbSessionFactory) {
81 this.dbSessionFactory = dbSessionFactory;
82 }
83
84 public TranscriptionJobControl storeJobControl(String mpId, String trackId, String jobId, String jobStatus,
85 long trackDuration, Date dateExpected, String provider) throws TranscriptionDatabaseException {
86 long providerId = getProviderId(provider);
87 if (providerId != noProviderId) {
88 try {
89 return db.execTx(TranscriptionJobControlDto.storeQuery(mpId, trackId, jobId, jobStatus, trackDuration,
90 dateExpected, providerId))
91 .toTranscriptionJobControl();
92 } catch (Exception e) {
93 throw new TranscriptionDatabaseException(e);
94 }
95 }
96 return null;
97 }
98
99 @Override
100 public TranscriptionProviderControl storeProviderControl(String provider) throws TranscriptionDatabaseException {
101 try {
102 TranscriptionProviderControlDto dto = db.execTx(TranscriptionProviderControlDto.storeProviderQuery(provider));
103 logger.info("Transcription provider '{}' stored", provider);
104 return dto.toTranscriptionProviderControl();
105 } catch (Exception e) {
106 throw new TranscriptionDatabaseException(e);
107 }
108 }
109
110 @Override
111 public void deleteJobControl(String jobId) throws TranscriptionDatabaseException {
112 try {
113 db.execTx(TranscriptionJobControlDto.delete(jobId));
114 } catch (Exception e) {
115 throw new TranscriptionDatabaseException(e);
116 }
117 }
118
119 @Override
120 public void updateJobControl(String jobId, String jobStatus) throws TranscriptionDatabaseException {
121 try {
122 db.execTx(TranscriptionJobControlDto.updateStatusQuery(jobId, jobStatus));
123 } catch (Exception e) {
124 throw new TranscriptionDatabaseException(e);
125 }
126 }
127
128 @Override
129 public TranscriptionJobControl findByJob(String jobId) throws TranscriptionDatabaseException {
130 try {
131 return db.exec(TranscriptionJobControlDto.findByJobQuery(jobId))
132 .map(TranscriptionJobControlDto::toTranscriptionJobControl)
133 .orElse(null);
134 } catch (Exception e) {
135 throw new TranscriptionDatabaseException(e);
136 }
137 }
138
139 @Override
140 public List<TranscriptionJobControl> findByMediaPackage(String mpId) throws TranscriptionDatabaseException {
141 try {
142 return db.exec(TranscriptionJobControlDto.findByMediaPackageQuery(mpId)).stream()
143 .map(TranscriptionJobControlDto::toTranscriptionJobControl)
144 .collect(Collectors.toList());
145 } catch (Exception e) {
146 throw new TranscriptionDatabaseException(e);
147 }
148 }
149
150 @Override
151 public List<TranscriptionJobControl> findByStatus(String... status) throws TranscriptionDatabaseException {
152 try {
153 return db.exec(TranscriptionJobControlDto.findByStatusQuery(status)).stream()
154 .map(TranscriptionJobControlDto::toTranscriptionJobControl)
155 .collect(Collectors.toList());
156 } catch (Exception e) {
157 throw new TranscriptionDatabaseException(e);
158 }
159 }
160
161 @Override
162 public List<TranscriptionJobControl> findByMediaPackageTrackAndStatus(String mpId, String trackId, String... status)
163 throws TranscriptionDatabaseException {
164 try {
165 return db.exec(TranscriptionJobControlDto.findByMediaPackageTrackAndStatusQuery(mpId, trackId, status)).stream()
166 .map(TranscriptionJobControlDto::toTranscriptionJobControl)
167 .collect(Collectors.toList());
168 } catch (Exception e) {
169 throw new TranscriptionDatabaseException(e);
170 }
171 }
172
173 @Override
174 public TranscriptionProviderControl findIdByProvider(String provider) throws TranscriptionDatabaseException {
175 try {
176 TranscriptionProviderControl tpc = db.exec(TranscriptionProviderControlDto.findIdByProviderQuery(provider))
177 .map(TranscriptionProviderControlDto::toTranscriptionProviderControl)
178 .orElse(null);
179 if (tpc != null) {
180 return tpc;
181 }
182
183
184 TranscriptionProviderControl dto = storeProviderControl(provider);
185 return db.exec(TranscriptionProviderControlDto.findIdByProviderQuery(provider))
186 .map(TranscriptionProviderControlDto::toTranscriptionProviderControl)
187 .orElse(null);
188 } catch (Exception e) {
189 throw new TranscriptionDatabaseException(e);
190 }
191 }
192
193 @Override
194 public TranscriptionProviderControl findProviderById(Long id) throws TranscriptionDatabaseException {
195 try {
196 return db.exec(TranscriptionProviderControlDto.findProviderByIdQuery(id))
197 .map(TranscriptionProviderControlDto::toTranscriptionProviderControl)
198 .orElse(null);
199 } catch (Exception e) {
200 throw new TranscriptionDatabaseException(e);
201 }
202 }
203
204 private long getProviderId(String provider) throws TranscriptionDatabaseException {
205 TranscriptionProviderControl providerInfo = findIdByProvider(provider);
206 if (providerInfo != null) {
207 return providerInfo.getId();
208 }
209 return noProviderId;
210 }
211 }