1
|
package eu.dnetlib.enabling.is.hib.utils;
|
2
|
|
3
|
import java.util.Map.Entry;
|
4
|
|
5
|
import org.apache.commons.lang.StringUtils;
|
6
|
import org.springframework.beans.factory.annotation.Required;
|
7
|
|
8
|
import com.google.gson.Gson;
|
9
|
|
10
|
import eu.dnetlib.enabling.is.dao.DnetSimpleDao;
|
11
|
import eu.dnetlib.enabling.is.dao.DnetSimpleDaoFactory;
|
12
|
import eu.dnetlib.enabling.is.hib.objects.HibBlackboardMessage;
|
13
|
import eu.dnetlib.enabling.is.hib.objects.HibDatastructure;
|
14
|
import eu.dnetlib.enabling.is.hib.objects.HibDnetResourceProperty;
|
15
|
import eu.dnetlib.enabling.is.hib.objects.HibDnetResourcePropertyId;
|
16
|
import eu.dnetlib.enabling.is.hib.objects.HibResourceKind;
|
17
|
import eu.dnetlib.enabling.is.hib.objects.HibResourceType;
|
18
|
import eu.dnetlib.enabling.is.hib.objects.HibService;
|
19
|
import eu.dnetlib.enabling.is.hib.objects.HibServiceProtocol;
|
20
|
import eu.dnetlib.enabling.is.hib.objects.HibServiceProtocolId;
|
21
|
import eu.dnetlib.enabling.is.hib.objects.HibSubscription;
|
22
|
import eu.dnetlib.rmi.objects.is.BlackboardMessage;
|
23
|
import eu.dnetlib.rmi.objects.is.DnetDataStructure;
|
24
|
import eu.dnetlib.rmi.objects.is.DnetResourceFormat;
|
25
|
import eu.dnetlib.rmi.objects.is.DnetResourceKind;
|
26
|
import eu.dnetlib.rmi.objects.is.DnetResourceType;
|
27
|
import eu.dnetlib.rmi.objects.is.DnetService;
|
28
|
import eu.dnetlib.rmi.objects.is.Subcription;
|
29
|
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
|
30
|
|
31
|
public class HibObjectHelper {
|
32
|
|
33
|
private DnetSimpleDaoFactory daoFactory;
|
34
|
|
35
|
public HibService createHibService(final DnetService service) throws InformationServiceException {
|
36
|
HibResourceType type = daoFactory.getDao(HibResourceType.class).find(service.getName());
|
37
|
if (type == null) {
|
38
|
type = createHibResourceType(service.getName(), DnetResourceKind.SERVICE, DnetResourceFormat.SERVICE);
|
39
|
}
|
40
|
|
41
|
final HibService hib = new HibService(service.getId(), type, service.isValid());
|
42
|
|
43
|
for (Entry<String, String> e : service.getProtocols().entrySet()) {
|
44
|
hib.getProtocols().add(new HibServiceProtocol(new HibServiceProtocolId(hib, e.getKey()), e.getValue()));
|
45
|
}
|
46
|
for (Entry<String, String> e : service.getProperties().entrySet()) {
|
47
|
hib.getProperties().add(new HibDnetResourceProperty(new HibDnetResourcePropertyId(hib, e.getKey()), e.getValue()));
|
48
|
}
|
49
|
for (BlackboardMessage b : service.getBlackboard()) {
|
50
|
hib.getBlackboard().add(new HibBlackboardMessage(hib, b));
|
51
|
}
|
52
|
daoFactory.getDao(HibService.class).save(hib);
|
53
|
|
54
|
return hib;
|
55
|
}
|
56
|
|
57
|
public HibDatastructure createHibDnetDatastructure(final DnetDataStructure ds) throws InformationServiceException {
|
58
|
final HibResourceType type = daoFactory.getDao(HibResourceType.class).find(ds.getType());
|
59
|
if (type == null) { throw new InformationServiceException("Resource type not registered: " + ds.getType()); }
|
60
|
|
61
|
final HibDatastructure hib = new HibDatastructure();
|
62
|
hib.setId(ds.getId());
|
63
|
hib.setCode(ds.getCode());
|
64
|
hib.setName(ds.getName());
|
65
|
hib.setDescription(ds.getDescription());
|
66
|
hib.setType(type);
|
67
|
hib.setContent(ds.getContent());
|
68
|
hib.setDate(ds.getDate());
|
69
|
hib.setValid(ds.isValid());
|
70
|
|
71
|
if (StringUtils.isNotBlank(ds.getServiceId())) {
|
72
|
final HibService service = daoFactory.getDao(HibService.class).find(ds.getServiceId());
|
73
|
if (service != null) {
|
74
|
hib.setService(service);
|
75
|
} else {
|
76
|
throw new InformationServiceException("Missing service: " + ds.getServiceId());
|
77
|
}
|
78
|
}
|
79
|
|
80
|
daoFactory.getDao(HibDatastructure.class).save(hib);
|
81
|
|
82
|
return hib;
|
83
|
}
|
84
|
|
85
|
public HibResourceType createHibDnetResourceType(final DnetResourceType type) throws InformationServiceException {
|
86
|
return createHibResourceType(type.getType(), type.getKind(), type.getFormat());
|
87
|
}
|
88
|
|
89
|
public void createHibBlackbooardMessage(final String serviceId, final BlackboardMessage message) throws InformationServiceException {
|
90
|
final HibService service = daoFactory.getDao(HibService.class).find(serviceId);
|
91
|
final HibBlackboardMessage hib = new HibBlackboardMessage(service, message);
|
92
|
daoFactory.getDao(HibBlackboardMessage.class).save(hib);
|
93
|
}
|
94
|
|
95
|
public void createHibSubscription(final Subcription subcription) throws InformationServiceException {
|
96
|
final HibService service = daoFactory.getDao(HibService.class).find(subcription.getServiceId());
|
97
|
final HibSubscription hib = new HibSubscription();
|
98
|
hib.setId(subcription.getId());
|
99
|
hib.setOperation(subcription.getOperation());
|
100
|
hib.setTable(subcription.getTable());
|
101
|
hib.setCondition(new Gson().toJson(subcription.getCondition()));
|
102
|
hib.setSubscriber(service);
|
103
|
daoFactory.getDao(HibSubscription.class).save(hib);
|
104
|
}
|
105
|
|
106
|
public void updateHibBlackbooardMessage(final BlackboardMessage message) throws InformationServiceException {
|
107
|
final DnetSimpleDao<HibBlackboardMessage> dao = daoFactory.getDao(HibBlackboardMessage.class);
|
108
|
final HibBlackboardMessage hib = dao.find(message.getId());
|
109
|
hib.setAction(message.getAction());
|
110
|
hib.setDate(message.getDate());
|
111
|
hib.setMessage(message.getJsonMessage());
|
112
|
hib.setStatus(message.getStatus().toString());
|
113
|
dao.save(hib);
|
114
|
}
|
115
|
|
116
|
private HibResourceType createHibResourceType(final String type,
|
117
|
final DnetResourceKind kind,
|
118
|
final DnetResourceFormat format) throws InformationServiceException {
|
119
|
final DnetSimpleDao<HibResourceKind> daoKind = daoFactory.getDao(HibResourceKind.class);
|
120
|
final HibResourceKind hibKind = daoKind.find(kind);
|
121
|
final HibResourceType hibType = new HibResourceType(type, hibKind, format);
|
122
|
daoFactory.getDao(HibResourceType.class).save(hibType);
|
123
|
return hibType;
|
124
|
}
|
125
|
|
126
|
public DnetSimpleDaoFactory getDaoFactory() {
|
127
|
return daoFactory;
|
128
|
}
|
129
|
|
130
|
@Required
|
131
|
public void setDaoFactory(final DnetSimpleDaoFactory daoFactory) {
|
132
|
this.daoFactory = daoFactory;
|
133
|
}
|
134
|
|
135
|
}
|