Project

General

Profile

1
package eu.dnetlib.enabling.is;
2

    
3
import java.util.Date;
4
import java.util.List;
5
import java.util.UUID;
6

    
7
import javax.annotation.Resource;
8

    
9
import org.apache.commons.lang.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12

    
13
import com.google.common.base.Function;
14
import com.google.common.collect.Iterables;
15
import com.google.common.collect.Lists;
16

    
17
import eu.dnetlib.enabling.is.dao.DnetSimpleDao;
18
import eu.dnetlib.enabling.is.dao.DnetSimpleDaoFactory;
19
import eu.dnetlib.enabling.is.hib.objects.HibBlackboardMessage;
20
import eu.dnetlib.enabling.is.hib.objects.HibDatastructure;
21
import eu.dnetlib.enabling.is.hib.objects.HibDnetResource;
22
import eu.dnetlib.enabling.is.hib.objects.HibResourceType;
23
import eu.dnetlib.enabling.is.hib.objects.HibService;
24
import eu.dnetlib.enabling.is.hib.objects.HibSubscription;
25
import eu.dnetlib.enabling.is.hib.utils.HibObjectHelper;
26
import eu.dnetlib.enabling.is.jdbc.DatabaseUtils;
27
import eu.dnetlib.enabling.is.rmi.InformationService;
28
import eu.dnetlib.enabling.is.rmi.InformationServiceException;
29
import eu.dnetlib.enabling.is.rmi.objects.BlackboardMessage;
30
import eu.dnetlib.enabling.is.rmi.objects.DnetDataStructure;
31
import eu.dnetlib.enabling.is.rmi.objects.DnetResourceType;
32
import eu.dnetlib.enabling.is.rmi.objects.DnetService;
33
import eu.dnetlib.enabling.is.rmi.objects.Subcription;
34
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
35
import eu.dnetlib.enabling.tools.AbstractBaseService;
36
import eu.dnetlib.miscutils.functional.hash.Hashing;
37

    
38
public class InformationServiceImpl extends AbstractBaseService implements InformationService {
39

    
40
	private static final Log log = LogFactory.getLog(InformationServiceImpl.class);
41

    
42
	@Resource
43
	private DnetSimpleDaoFactory daoFactory;
44

    
45
	@Resource
46
	private DatabaseUtils dbUtils;
47

    
48
	@Resource
49
	private UniqueServiceLocator serviceLocator;
50

    
51
	@Resource
52
	private HibObjectHelper hibObjectHelper;
53

    
54
	@Override
55
	public String addSubscription(final Subcription subscription) throws InformationServiceException {
56
		final String id = "subscr-" + Hashing.md5(subscription.getServiceId() + "-" + subscription.getOperation() + "-" + subscription.getTable());
57
		subscription.setId(id);
58
		hibObjectHelper.createHibSubscription(subscription);
59
		dbUtils.registerNotificationTrigger(subscription.getOperation(), subscription.getTable());
60
		return id;
61
	}
62

    
63
	@Override
64
	public boolean deleteSubscription(final String subscriptionId) throws InformationServiceException {
65
		daoFactory.getDao(HibSubscription.class).delete(subscriptionId);
66
		return true;
67
	}
68

    
69
	@Override
70
	public List<Subcription> listSubcriptions() throws InformationServiceException {
71
		final DnetSimpleDao<HibSubscription> dao = daoFactory.getDao(HibSubscription.class);
72
		return Lists.transform(dao.list(), new Function<HibSubscription, Subcription>() {
73

    
74
			@Override
75
			public Subcription apply(final HibSubscription hib) {
76
				return hib.asDnetSubscription();
77
			}
78
		});
79
	}
80

    
81
	@Override
82
	public List<DnetService> listServices() throws InformationServiceException {
83
		final DnetSimpleDao<HibService> dao = daoFactory.getDao(HibService.class);
84
		return Lists.transform(dao.list(), new Function<HibService, DnetService>() {
85

    
86
			@Override
87
			public DnetService apply(final HibService hib) {
88
				return hib.asDnetService();
89
			}
90
		});
91
	}
92

    
93
	@Override
94
	public String registerService(final DnetService service) throws InformationServiceException {
95
		final String id = dbUtils.findServiceIdByAddress(service.getProtocols().values());
96
		service.setId(id != null ? id : "service-" + UUID.randomUUID());
97
		hibObjectHelper.createHibService(service);
98
		log.info("Service " + service + " registered/updated");
99
		return id;
100
	}
101

    
102
	@Override
103
	public DnetService getService(final String id) throws InformationServiceException {
104
		final DnetSimpleDao<HibService> dao = daoFactory.getDao(HibService.class);
105
		final HibService hib = dao.find(id);
106
		return hib != null ? hib.asDnetService() : null;
107
	}
108

    
109
	@Override
110
	public DnetService getServiceByAddress(final String address) throws InformationServiceException {
111
		final String id = dbUtils.findServiceIdByAddress(address);
112
		if (id != null) {
113
			return getService(id);
114
		} else {
115
			throw new InformationServiceException(String.format("Too many services with address %s", address));
116
		}
117
	}
118

    
119
	@Override
120
	public boolean deleteService(final String id) throws InformationServiceException {
121
		daoFactory.getDao(HibService.class).delete(id);
122
		return true;
123
	}
124

    
125
	@Override
126
	public boolean isRegisteredService(final String address) {
127
		return dbUtils.isRegisteredService(address);
128
	}
129

    
130
	@Override
131
	public List<String> searchSql(final String sql) throws InformationServiceException {
132
		return dbUtils.searchJson(sql);
133
	}
134

    
135
	@Override
136
	public String findOneSql(final String sql) throws InformationServiceException {
137
		return Iterables.getOnlyElement(dbUtils.searchJson(sql));
138
	}
139

    
140
	@Override
141
	public int updateSql(final String sql) throws InformationServiceException {
142
		return dbUtils.update(sql);
143
	}
144

    
145
	@Override
146
	public boolean addResourceType(final DnetResourceType type) throws InformationServiceException {
147
		hibObjectHelper.createHibDnetResourceType(type);
148
		return false;
149
	}
150

    
151
	@Override
152
	public List<DnetResourceType> listResourceTypes() throws InformationServiceException {
153
		final DnetSimpleDao<HibResourceType> dao = daoFactory.getDao(HibResourceType.class);
154

    
155
		return Lists.transform(dao.list(), new Function<HibResourceType, DnetResourceType>() {
156

    
157
			@Override
158
			public DnetResourceType apply(final HibResourceType hib) {
159
				return hib.asDnetResourceType();
160
			}
161
		});
162
	}
163

    
164
	@Override
165
	public boolean addBlackBoardMessage(final String id, final BlackboardMessage message) throws InformationServiceException {
166
		hibObjectHelper.createHibBlackbooardMessage(id, message);
167
		return true;
168
	}
169

    
170
	@Override
171
	public boolean updateBlackBoardMessage(final BlackboardMessage message) throws InformationServiceException {
172
		hibObjectHelper.updateHibBlackbooardMessage(message);
173
		return true;
174
	}
175

    
176
	@Override
177
	public boolean deleteBlackBoardMessage(final BlackboardMessage message) throws InformationServiceException {
178
		daoFactory.getDao(HibBlackboardMessage.class).delete(message.getId());
179
		return true;
180
	}
181

    
182
	@Override
183
	public DnetDataStructure getDsById(final String id) throws InformationServiceException {
184
		final DnetSimpleDao<HibDatastructure> dao = daoFactory.getDao(HibDatastructure.class);
185
		final HibDatastructure hib = dao.find(id);
186
		return hib != null ? hib.asDnetDataStructure() : null;
187
	}
188

    
189
	@Override
190
	public DnetDataStructure getDsByCode(final String code, final String type) throws InformationServiceException {
191
		final String id = dbUtils.findDsIdByCode(code, type);
192
		if (id != null) {
193
			return getDsById(id);
194
		} else {
195
			throw new InformationServiceException(String.format("Missing datastructure with code %s and type %s", code, type));
196
		}
197
	}
198

    
199
	@Override
200
	public String registerDs(final DnetDataStructure ds) throws InformationServiceException {
201
		if (StringUtils.isBlank(ds.getId())) {
202
			ds.setId("ds-" + UUID.randomUUID());
203
			ds.setDate(new Date());
204
		}
205
		hibObjectHelper.createHibDnetDatastructure(ds);
206
		log.info("DS " + ds + " saved");
207
		return ds.getId();
208
	}
209

    
210
	@Override
211
	public boolean deleteDsById(final String id) throws InformationServiceException {
212
		daoFactory.getDao(HibDatastructure.class).delete(id);
213
		return true;
214
	}
215

    
216
	@Override
217
	public boolean deleteDsByCode(final String code, final String type) throws InformationServiceException {
218
		return deleteDsById(dbUtils.findDsIdByCode(code, type));
219
	}
220

    
221
	@Override
222
	public boolean isRegisteredDs(final String code, final String type) {
223
		return dbUtils.isRegisteredDs(code, type);
224
	}
225

    
226
	@Override
227
	public List<BlackboardMessage> listBlackboardMessages() {
228
		final DnetSimpleDao<HibBlackboardMessage> dao = daoFactory.getDao(HibBlackboardMessage.class);
229

    
230
		return Lists.transform(dao.list(), new Function<HibBlackboardMessage, BlackboardMessage>() {
231

    
232
			@Override
233
			public BlackboardMessage apply(final HibBlackboardMessage hib) {
234
				return hib.asDnetBlackboardMessage();
235
			}
236
		});
237
	}
238

    
239
	@Override
240
	public boolean validateResource(final String id, final boolean valid) throws InformationServiceException {
241
		final DnetSimpleDao<HibDnetResource> dao = daoFactory.getDao(HibDnetResource.class);
242
		final HibDnetResource hib = dao.find(id);
243
		hib.setValid(valid);
244
		dao.save(hib);
245
		return true;
246
	}
247

    
248
	@Override
249
	public List<DnetDataStructure> listDatastructures(final String type) {
250
		final DnetSimpleDao<HibDatastructure> dao = daoFactory.getDao(HibDatastructure.class);
251

    
252
		return Lists.transform(dao.list("type", type), new Function<HibDatastructure, DnetDataStructure>() {
253

    
254
			@Override
255
			public DnetDataStructure apply(final HibDatastructure hib) {
256
				return hib.asDnetDataStructure();
257
			}
258
		});
259
	}
260

    
261
}
    (1-1/1)