Project

General

Profile

1
package eu.dnetlib.enabling.is;
2

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

    
8
import javax.annotation.Resource;
9

    
10
import org.apache.commons.lang.StringUtils;
11
import org.apache.commons.lang.math.NumberUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.annotation.Required;
15

    
16
import com.google.common.base.Function;
17
import com.google.common.collect.Iterables;
18
import com.google.common.collect.Lists;
19

    
20
import eu.dnetlib.common.services.AbstractBaseService;
21
import eu.dnetlib.common.services.locators.DnetServiceLocator;
22
import eu.dnetlib.enabling.is.dao.DnetSimpleDao;
23
import eu.dnetlib.enabling.is.dao.DnetSimpleDaoFactory;
24
import eu.dnetlib.enabling.is.hib.objects.HibBlackboardMessage;
25
import eu.dnetlib.enabling.is.hib.objects.HibDatastructure;
26
import eu.dnetlib.enabling.is.hib.objects.HibDnetResource;
27
import eu.dnetlib.enabling.is.hib.objects.HibService;
28
import eu.dnetlib.enabling.is.hib.objects.HibSubscription;
29
import eu.dnetlib.enabling.is.hib.utils.HibObjectHelper;
30
import eu.dnetlib.enabling.is.jdbc.DatabaseUtils;
31
import eu.dnetlib.rmi.objects.is.BlackboardMessage;
32
import eu.dnetlib.rmi.objects.is.DnetDataStructure;
33
import eu.dnetlib.rmi.objects.is.DnetResourceFormat;
34
import eu.dnetlib.rmi.objects.is.DnetResourceKind;
35
import eu.dnetlib.rmi.objects.is.DnetResourceType;
36
import eu.dnetlib.rmi.objects.is.DnetService;
37
import eu.dnetlib.rmi.objects.is.Subscription;
38
import eu.dnetlib.rmi.soap.InformationService;
39
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
40

    
41
public class InformationServiceImpl extends AbstractBaseService implements InformationService {
42

    
43
	@Resource
44
	private DnetServiceLocator serviceLocator;
45

    
46
	private DnetSimpleDaoFactory daoFactory;
47
	private DatabaseUtils dbUtils;
48
	private HibObjectHelper hibObjectHelper;
49

    
50
	private static final Log log = LogFactory.getLog(InformationServiceImpl.class);
51

    
52
	@Override
53
	public String addSubscription(final Subscription subscription) throws InformationServiceException {
54
		if (StringUtils.isBlank(subscription.getId())) {
55
			subscription.setId("subscr-" + UUID.randomUUID());
56
		}
57
		hibObjectHelper.createHibSubscription(subscription);
58
		dbUtils.registerNotificationTrigger(subscription.getOperation(), subscription.getTable());
59
		return subscription.getId();
60
	}
61

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

    
68
	@Override
69
	public boolean deleteAllServiceSubscriptions(final String serviceId) {
70
		dbUtils.update("delete from subscriptions where subscriber = ?", serviceId);
71
		return true;
72
	}
73

    
74
	@Override
75
	public List<Subscription> listSubcriptions() throws InformationServiceException {
76
		final DnetSimpleDao<HibSubscription> dao = daoFactory.getDao(HibSubscription.class);
77
		return Lists.transform(dao.list(), new Function<HibSubscription, Subscription>() {
78

    
79
			@Override
80
			public Subscription apply(final HibSubscription hib) {
81
				return hib.asDnetSubscription();
82
			}
83
		});
84
	}
85

    
86
	@Override
87
	public List<DnetService> listServices() throws InformationServiceException {
88
		final DnetSimpleDao<HibService> dao = daoFactory.getDao(HibService.class);
89
		return Lists.transform(dao.list(), new Function<HibService, DnetService>() {
90

    
91
			@Override
92
			public DnetService apply(final HibService hib) {
93
				return hib.asDnetService();
94
			}
95
		});
96
	}
97

    
98
	@Override
99
	public String registerService(final DnetService service) throws InformationServiceException {
100
		final String id = dbUtils.findServiceIdByAddress(service.getProtocols().values());
101
		service.setId(id != null ? id : "service-" + UUID.randomUUID());
102
		hibObjectHelper.createHibService(service);
103
		return service.getId();
104
	}
105

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

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

    
123
	@Override
124
	public boolean deleteService(final String id) throws InformationServiceException {
125
		daoFactory.getDao(HibService.class).delete(id);
126
		return true;
127
	}
128

    
129
	@Override
130
	public boolean isRegisteredService(final String address) {
131
		return dbUtils.isRegisteredService(address);
132
	}
133

    
134
	@Override
135
	public List<String> searchSql(final String sql) throws InformationServiceException {
136
		return dbUtils.searchJson(sql);
137
	}
138

    
139
	@Override
140
	public String findOneSql(final String sql) throws InformationServiceException {
141
		return Iterables.getOnlyElement(dbUtils.searchJson(sql));
142
	}
143

    
144
	@Override
145
	public int updateSql(final String sql) throws InformationServiceException {
146
		return dbUtils.update(sql);
147
	}
148

    
149
	@Override
150
	public boolean addResourceType(final DnetResourceType type) throws InformationServiceException {
151
		hibObjectHelper.createHibDnetResourceType(type);
152
		return false;
153
	}
154

    
155
	@Override
156
	public List<DnetResourceType> listResourceTypes(final String kind) throws InformationServiceException {
157

    
158
		final String query = "select t.id as type, t.format as format, count(ds.id) as total "
159
				+ "from resource_types t left outer join datastructures ds on (t.id = ds.type) "
160
				+ "where t.kind = ?"
161
				+ "group by t.id, t.format";
162

    
163
		return Lists.transform(dbUtils.searchSimple(query, kind), new Function<Map<String, Object>, DnetResourceType>() {
164

    
165
			@Override
166
			public DnetResourceType apply(final Map<String, Object> map) {
167
				return new DnetResourceType(
168
						map.get("type").toString(),
169
						DnetResourceKind.valueOf(kind),
170
						DnetResourceFormat.valueOf(map.get("format").toString()),
171
						NumberUtils.toLong(map.get("total").toString(), 0)
172
				);
173
			}
174

    
175
		});
176
	}
177

    
178
	@Override
179
	public boolean addBlackBoardMessage(final String id, final BlackboardMessage message) throws InformationServiceException {
180
		hibObjectHelper.createHibBlackbooardMessage(id, message);
181
		return true;
182
	}
183

    
184
	@Override
185
	public boolean updateBlackBoardMessage(final BlackboardMessage message) throws InformationServiceException {
186
		hibObjectHelper.updateHibBlackbooardMessage(message);
187
		return true;
188
	}
189

    
190
	@Override
191
	public boolean deleteBlackBoardMessage(final BlackboardMessage message) throws InformationServiceException {
192
		daoFactory.getDao(HibBlackboardMessage.class).delete(message.getId());
193
		return true;
194
	}
195

    
196
	@Override
197
	public DnetDataStructure getDsById(final String id) throws InformationServiceException {
198
		final DnetSimpleDao<HibDatastructure> dao = daoFactory.getDao(HibDatastructure.class);
199
		final HibDatastructure hib = dao.find(id);
200
		return hib != null ? hib.asDnetDataStructure() : null;
201
	}
202

    
203
	@Override
204
	public DnetDataStructure getDsByCode(final String code, final String type) throws InformationServiceException {
205
		final String id = dbUtils.findDsIdByCode(code, type);
206
		if (id != null) {
207
			return getDsById(id);
208
		} else {
209
			throw new InformationServiceException(String.format("Missing datastructure with code %s and type %s", code, type));
210
		}
211
	}
212

    
213
	@Override
214
	public String registerDs(final DnetDataStructure ds) throws InformationServiceException {
215
		if (StringUtils.isBlank(ds.getId())) {
216
			ds.setId("ds-" + UUID.randomUUID());
217
			ds.setDate(new Date());
218
		}
219
		hibObjectHelper.createHibDnetDatastructure(ds);
220
		log.info("DS " + ds + " saved");
221
		return ds.getId();
222
	}
223

    
224
	@Override
225
	public boolean deleteDsById(final String id) throws InformationServiceException {
226
		daoFactory.getDao(HibDatastructure.class).delete(id);
227
		return true;
228
	}
229

    
230
	@Override
231
	public boolean deleteDsByCode(final String code, final String type) throws InformationServiceException {
232
		return deleteDsById(dbUtils.findDsIdByCode(code, type));
233
	}
234

    
235
	@Override
236
	public boolean isRegisteredDs(final String code, final String type) {
237
		return dbUtils.isRegisteredDs(code, type);
238
	}
239

    
240
	@Override
241
	public List<BlackboardMessage> listBlackboardMessages() throws InformationServiceException {
242
		final DnetSimpleDao<HibBlackboardMessage> dao = daoFactory.getDao(HibBlackboardMessage.class);
243

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

    
246
			@Override
247
			public BlackboardMessage apply(final HibBlackboardMessage hib) {
248
				return hib.asDnetBlackboardMessage();
249
			}
250
		});
251
	}
252

    
253
	@Override
254
	public boolean validateResource(final String id, final boolean valid) throws InformationServiceException {
255
		final DnetSimpleDao<HibDnetResource> dao = daoFactory.getDao(HibDnetResource.class);
256
		final HibDnetResource hib = dao.find(id);
257
		hib.setValid(valid);
258
		dao.save(hib);
259
		return true;
260
	}
261

    
262
	@Override
263
	public List<DnetDataStructure> listDatastructures(final String type) throws InformationServiceException {
264
		final DnetSimpleDao<HibDatastructure> dao = daoFactory.getDao(HibDatastructure.class);
265

    
266
		return Lists.transform(dao.list("type.id", type), new Function<HibDatastructure, DnetDataStructure>() {
267

    
268
			@Override
269
			public DnetDataStructure apply(final HibDatastructure hib) {
270
				return hib.asDnetDataStructure();
271
			}
272
		});
273
	}
274

    
275
	public DnetSimpleDaoFactory getDaoFactory() {
276
		return daoFactory;
277
	}
278

    
279
	@Required
280
	public void setDaoFactory(final DnetSimpleDaoFactory daoFactory) {
281
		this.daoFactory = daoFactory;
282
	}
283

    
284
	public DatabaseUtils getDbUtils() {
285
		return dbUtils;
286
	}
287

    
288
	@Required
289
	public void setDbUtils(final DatabaseUtils dbUtils) {
290
		this.dbUtils = dbUtils;
291
	}
292

    
293
	public HibObjectHelper getHibObjectHelper() {
294
		return hibObjectHelper;
295
	}
296

    
297
	@Required
298
	public void setHibObjectHelper(final HibObjectHelper hibObjectHelper) {
299
		this.hibObjectHelper = hibObjectHelper;
300
	}
301

    
302
}
    (1-1/1)