Project

General

Profile

1
package eu.dnetlib.enabling.is.client;
2

    
3
import java.util.List;
4
import java.util.Map;
5

    
6
import javax.annotation.Resource;
7

    
8
import com.google.common.base.Function;
9
import com.google.common.collect.Iterables;
10
import com.google.common.collect.Lists;
11
import com.google.gson.Gson;
12
import com.google.gson.reflect.TypeToken;
13

    
14
import eu.dnetlib.enabling.annotations.DnetResource;
15
import eu.dnetlib.rmi.objects.is.DnetDataStructure;
16
import eu.dnetlib.rmi.objects.is.DnetResourceFormat;
17
import eu.dnetlib.rmi.soap.InformationService;
18
import eu.dnetlib.rmi.soap.exceptions.InformationServiceException;
19

    
20
public class InformationServiceClient {
21

    
22
	@Resource
23
	private InformationService informationService;
24

    
25
	public <T> T getResourceByCode(final String code, final Class<T> clazz) throws InformationServiceException {
26

    
27
		if (clazz != null && clazz.isAnnotationPresent(DnetResource.class)) {
28
			final DnetResource annotation = clazz.getAnnotation(DnetResource.class);
29
			if (annotation.format() == DnetResourceFormat.JSON) {
30
				final DnetDataStructure ds = informationService.getDsByCode(code, annotation.type());
31
				return ds.getContentAsObject(clazz);
32
			} else {
33
				throw new InformationServiceException("Not JSON resource, class: " + clazz);
34
			}
35
		} else {
36
			throw new InformationServiceException("Invalid class " + clazz);
37
		}
38

    
39
	}
40

    
41
	public String getXmlResourceByCode(final String code, final Class<?> clazz) throws InformationServiceException {
42
		if (clazz != null && clazz.isAnnotationPresent(DnetResource.class)) {
43
			final DnetResource annotation = clazz.getAnnotation(DnetResource.class);
44
			if (annotation.format() == DnetResourceFormat.XML) {
45
				final DnetDataStructure ds = informationService.getDsByCode(code, annotation.type());
46
				return ds.getContent();
47
			} else {
48
				throw new InformationServiceException("Not XML resource, class: " + clazz);
49
			}
50
		} else {
51
			throw new InformationServiceException("Invalid class " + clazz);
52
		}
53
	}
54

    
55
	public String getTextResourceByCode(final String code, final Class<?> clazz) throws InformationServiceException {
56
		if (clazz != null && clazz.isAnnotationPresent(DnetResource.class)) {
57
			final DnetResource annotation = clazz.getAnnotation(DnetResource.class);
58
			if (annotation.format() == DnetResourceFormat.TEXT) {
59
				final DnetDataStructure ds = informationService.getDsByCode(code, annotation.type());
60
				return ds.getContent();
61
			} else {
62
				throw new InformationServiceException("Not TEXT resource, class: " + clazz);
63
			}
64
		} else {
65
			throw new InformationServiceException("Invalid class " + clazz);
66
		}
67
	}
68

    
69
	public void updateResourceByCode(final String code, final Object obj) throws InformationServiceException {
70
		if (obj != null && obj.getClass().isAnnotationPresent(DnetResource.class)) {
71
			final DnetResource annotation = obj.getClass().getAnnotation(DnetResource.class);
72
			if (annotation.format() == DnetResourceFormat.JSON) {
73
				final DnetDataStructure ds = informationService.getDsByCode(code, annotation.type());
74
				ds.setContentFromObject(obj);
75
				informationService.registerDs(ds);
76
			} else {
77
				throw new InformationServiceException("Not JSON resource, obj class: " + obj.getClass());
78
			}
79
		} else {
80
			throw new InformationServiceException("Invalid Object: " + obj);
81
		}
82
	}
83

    
84
	public void deleteResourceByCode(final String code, final Class<?> clazz) throws InformationServiceException {
85
		if (clazz != null && clazz.isAnnotationPresent(DnetResource.class)) {
86
			informationService.deleteDsByCode(code, clazz.getAnnotation(DnetResource.class).type());
87
		} else {
88
			throw new InformationServiceException("Invalid class " + clazz);
89
		}
90
	}
91

    
92
	public <T> List<T> searchByQuery(final String sql, final Class<T> clazz) throws InformationServiceException {
93
		if (clazz == null) { throw new InformationServiceException("Class is NULL"); }
94

    
95
		final Gson gson = new Gson();
96

    
97
		return Lists.transform(informationService.searchSql(sql), new Function<String, T>() {
98

    
99
			@Override
100
			public T apply(final String s) {
101
				if (clazz == String.class) {
102
					final Map<String, T> map = gson.fromJson(s, new TypeToken<Map<String, T>>() {}.getType());
103
					return Iterables.getOnlyElement(map.values());
104
				} else if (clazz == Map.class) {
105
					return gson.fromJson(s, new TypeToken<Map<String, Object>>() {}.getType());
106
				} else {
107
					return gson.fromJson(s, clazz);
108
				}
109
			}
110
		});
111
	}
112

    
113
	public <T> List<T> searchResourceByQuery(final String sql, final Class<T> clazz) throws InformationServiceException {
114
		if (clazz == null) { throw new InformationServiceException("Class is NULL"); }
115

    
116
		final Gson gson = new Gson();
117

    
118
		return Lists.transform(informationService.searchSql(sql), new Function<String, T>() {
119

    
120
			@SuppressWarnings("unchecked")
121
			@Override
122
			public T apply(final String s) {
123
				if (clazz == String.class) {
124
					final Map<String, T> map = gson.fromJson(s, new TypeToken<Map<String, T>>() {}.getType());
125
					return Iterables.getOnlyElement(map.values());
126
				}
127
				final Map<String, Object> map = gson.fromJson(s, new TypeToken<Map<String, Object>>() {}.getType());
128

    
129
				if (clazz == Map.class) {
130
					return (T) map;
131
				} else {
132
					return gson.fromJson((String) Iterables.getOnlyElement(map.values()), clazz);
133
				}
134
			}
135
		});
136
	}
137
}
    (1-1/1)