PublisherServiceDefault.java
7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.legrog.web.publisher;
import org.legrog.entities.*;
import org.legrog.web.xyz.SharedService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
import java.util.*;
/**
* Implémentation de l'interface PublisherService
*/
@Stateless
public class PublisherServiceDefault implements PublisherService {
Logger logger = LoggerFactory.getLogger(getClass());
PublisherRepository publisherRepository;
PublisherVersionRepository publisherVersionRepository;
PublisherActionRepository publisherActionRepository;
PublisherSearchRepository publisherSearchRepository;
SharedService sharedService;
/**
* Le service s'appuie concrètement sur un ensemble de dépôts et sur le service auxiliaire SharedService.
*
* @param publisherRepository
* @param publisherVersionRepository
* @param publisherActionRepository
* @param publisherSearchRepository
* @param sharedService
*/
@Inject
public PublisherServiceDefault(PublisherRepository publisherRepository,
PublisherVersionRepository publisherVersionRepository,
PublisherActionRepository publisherActionRepository,
PublisherSearchRepository publisherSearchRepository,
SharedService sharedService) {
this.publisherRepository = publisherRepository;
this.publisherVersionRepository = publisherVersionRepository;
this.publisherActionRepository = publisherActionRepository;
this.publisherSearchRepository = publisherSearchRepository;
this.sharedService = sharedService;
}
PublisherServiceDefault() {
//no args constructor to make it proxyable
}
@Override
public PublisherVersion addNewPublisher(PublisherVersion publisherVersion) {
Publisher publisher = new Publisher();
Set<PublisherVersion> publisherVersions = new HashSet<>();
publisherVersions.add(publisherVersion);
publisher.setVersions(publisherVersions);
publisherVersion.setPublisher(publisher);
publisherVersion.setPublisherVersionCreator(sharedService.getCurrentUser());
publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
this.savePublisherVersion(publisherVersion);
this.savePublisher(publisher);
return publisherVersion;
}
@Override
public PublisherVersion addVersionToPublisher(Publisher publisher, PublisherVersion publisherVersion) {
logger.trace("debut addVersionToPublisher, publisherVersion = {}", publisherVersion);
Set<PublisherVersion> publisherVersions = publisher.getVersions();
publisherVersions.add(publisherVersion);
publisher.setVersions(publisherVersions);
publisherVersion.setPublisher(publisher);
publisherVersion.setPublisherVersionCreator(sharedService.getCurrentUser());
publisherVersion.setPublisherVersionDatetime(new Timestamp(new Date().getTime()));
this.savePublisherVersion(publisherVersion);
this.savePublisher(publisher);
logger.trace("fin addVersionToPublisher, publisherVersion = {}", publisherVersion);
return publisherVersion;
}
@Override
public PublisherAction validatePublisherVersion(PublisherVersion publisherVersion) {
Publisher publisher = publisherVersion.getPublisher();
publisher.setValidatedVersion(publisherVersion);
PublisherAction publisherAction = new PublisherAction();
publisherAction.setPublisherActionActor(sharedService.getCurrentUser());
publisherAction.setPublisherActionDatetime(new Timestamp(new Date().getTime()));
publisherAction.setActionType(ActionType.VALIDATE);
publisherAction.setPublisherVersion(publisherVersion);
publisherAction.setPublisher(publisher);
this.savePublisher(publisher);
this.index(publisher);
publisherActionRepository.save(publisherAction);
return publisherAction;
}
protected void index(Publisher publisher) {
IndexedPublisher indexedPublisher = new IndexedPublisher(publisher);
try {
publisherSearchRepository.save(indexedPublisher);
} catch (IndexingException e) {
logger.error("IndexingException {}", e);
// TODO Ajouter l'information de manière plus accessible que dans les logs
}
}
protected void savePublisherVersion(PublisherVersion publisherVersion) {
publisherVersionRepository.save(publisherVersion);
}
protected void savePublisher(Publisher publisher) {
publisherRepository.save(publisher);
}
// get
@Override
public List<PublisherVersion> getAllPublisherVersions() {
return publisherVersionRepository.findAll();
}
@Override
public PublisherVersion getPublisherVersion(Integer publisherVersionId) {
return publisherVersionRepository.findOne(publisherVersionId);
}
@Override
public PublisherAction getLastValidate(Publisher publisher) {
return publisherActionRepository.findFirstByActionTypeAndPublisherOrderByPublisherActionDatetimeDesc(ActionType.VALIDATE, publisher);
}
@Override
public List<PublisherAction> getAllPublisherActions() {
return publisherActionRepository.findAll();
}
@Override
public List<PublisherAction> getAllPublisherVersionActions(PublisherVersion publisherVersion) {
return publisherActionRepository.findByPublisherVersion(publisherVersion);
}
@Override
public List<PublisherVersion> search(@NotNull String string) throws SearchingException {
return convertIndexedPublishersIntoPublisherVersions(publisherSearchRepository.search(string));
}
@Override
public List<PublisherVersion> convertIndexedPublishersIntoPublisherVersions(List<IndexedPublisher> indexedPublishers) {
List<Integer> integers = new ArrayList<>(indexedPublishers.size());
indexedPublishers.forEach(indexedPublisher -> integers.add(indexedPublisher.getPublisherId()));
if (!integers.isEmpty()) {
return publisherVersionRepository.findByPublisherVersionIdIn(integers);
}
return new ArrayList<>();
}
protected List<IndexedPublisher> convertPublishersIntoIndexedPublishers(List<Publisher> publishers) {
List<IndexedPublisher> indexedPublishers = new ArrayList<>(publishers.size());
publishers.forEach(publisher -> indexedPublishers.add(new IndexedPublisher(publisher)));
return indexedPublishers;
}
@Override
public int reindexAllPublishers() throws IndexingException {
List<Publisher> publishers = publisherRepository.findByValidatedVersionIsNotNull();
List<IndexedPublisher> indexedPublishers = convertPublishersIntoIndexedPublishers(publishers);
publisherSearchRepository.reindex(indexedPublishers);
return indexedPublishers.size();
}
}