Project

General

Profile

« Previous | Next » 

Revision 51710

Pass datasource name for creating groups

View differences:

modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/catalogue/CatalogueRegistrator.java
52 52

  
53 53

  
54 54

  
55
	public String register(final Resource resource, final Resource type) throws IOException, ParthenosPublisherException, URISyntaxException {
55
	public String register(final Resource resource, final Resource type, final String group) throws IOException, ParthenosPublisherException, URISyntaxException {
56 56
		String resURI = resource.getURI();
57 57
		log.debug(String.format("Catalogue --> Processing resource : %s with type: %s", resURI, type.getLocalName()));
58 58
		//For the catalogue: Must be purely lowercase alphanumeric (ascii) characters and these symbols: -_
......
66 66
		}
67 67
		else {
68 68
			//resource not yet registered
69
			//ignore groups for now
70
			//ensureGroups(resource);
69
			String catGroup = ensureGroup(group);
71 70
			String json;
72 71
			switch (type.getLocalName()) {
73 72
			case "E7_Activity":
74
				json = getJsonForActivity(resource, resCatName);
73
				json = getJsonForActivity(resource, resCatName, catGroup);
75 74
				break;
76 75
			case "E29_Design_or_Procedure":
77
				json = getJsonForDesignProcedure(resource, resCatName);
76
				json = getJsonForDesignProcedure(resource, resCatName, catGroup);
78 77
				break;
79 78
			case "E39_Actor":
80
				json = getJsonForActor(resource, resCatName);
79
				json = getJsonForActor(resource, resCatName, catGroup);
81 80
				break;
82 81
			case "E70_Thing":
83
				json = getJsonForThing(resource, resCatName);
82
				json = getJsonForThing(resource, resCatName, catGroup);
84 83
				break;
85 84
			default:
86 85
				throw new IllegalArgumentException(String.format("Type " + type.getLocalName() + " not supported"));
......
117 116
		return n;
118 117
	}
119 118

  
120
	/**
121
	 * Ensure that providers referred in the Resource are available as "groups" in the registry.
122
	 * @param res Resource
123
	 */
124
	protected void ensureGroups(final Resource res) throws ParthenosPublisherException, IOException, URISyntaxException {
125
		log.debug("Ensuring groups exist");
126
		if(res.hasProperty(CRMpe.PP2_provided_by) || res.hasProperty(CRMpe.PP25_has_maintaining_RI)) {
127
			Iterator<String> providerNames = resourceReader.getProviderNames(res);
128
			while (providerNames.hasNext()) {
129
				String name = providerNames.next();
130
				if (StringUtils.isNotBlank(name)) {
131
					String groupName = getNameForCatalogue(name);
132
					if (!catalogueAPIClient.groupExist(groupName)) {
133
						String groupJson = String.format(groupTemplate, groupName, groupName, name);
134
						catalogueAPIClient.registerGroup(groupJson, groupName);
135
						log.info("NEW GROUP REGISTERED: " + groupName);
136
					}
137
				}
119
//	/**
120
//	 * Ensure that providers referred in the Resource are available as "groups" in the registry.
121
//	 * @param res Resource
122
//	 */
123
//	protected void ensureGroups(final Resource res) throws ParthenosPublisherException, IOException, URISyntaxException {
124
//		log.debug("Ensuring groups exist");
125
//		if(res.hasProperty(CRMpe.PP2_provided_by) || res.hasProperty(CRMpe.PP25_has_maintaining_RI)) {
126
//			Iterator<String> providerNames = resourceReader.getProviderNames(res);
127
//			while (providerNames.hasNext()) {
128
//				String name = providerNames.next();
129
//				if (StringUtils.isNotBlank(name)) {
130
//					String groupName = getNameForCatalogue(name);
131
//					if (!catalogueAPIClient.groupExist(groupName)) {
132
//						String groupJson = String.format(groupTemplate, groupName, groupName, name);
133
//						catalogueAPIClient.registerGroup(groupJson, groupName);
134
//						log.info("NEW GROUP REGISTERED: " + groupName);
135
//					}
136
//				}
137
//			}
138
//		}
139
//		else{
140
//			log.debug("Resource "+res.getURI()+" without PP2_provided_by: no groups to register");
141
//		}
142
//	}
143

  
144
	protected String ensureGroup(final String group) throws ParthenosPublisherException, IOException, URISyntaxException {
145
		log.debug("Ensuring group exist: "+group);
146
		if (StringUtils.isNotBlank(group)) {
147
			String groupName = getNameForCatalogue(group);
148
			if (!catalogueAPIClient.groupExist(groupName)) {
149
				String groupJson = String.format(groupTemplate, groupName, groupName, group);
150
				catalogueAPIClient.registerGroup(groupJson, groupName);
151
				log.info("NEW GROUP REGISTERED: " + groupName);
138 152
			}
153
			return groupName;
139 154
		}
140
		else{
141
			log.debug("Resource "+res.getURI()+" without PP2_provided_by: no groups to register");
142
		}
155
		return null;
143 156
	}
144 157

  
145
	protected String getJsonForActivity(final Resource res, final String resNameForCatalogue) throws IOException {
158
	protected String getJsonForActivity(final Resource res, final String resNameForCatalogue, final String catGroup) throws IOException {
146 159
		JsonFactory jsonFactory = new JsonFactory();
147 160
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
148 161
		BufferedOutputStream bos = new BufferedOutputStream(out);
149 162
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
150 163
		jg.writeStartObject();
151
		writeCommonFields(jg, res, resNameForCatalogue);
164
		writeCommonFields(jg, res, resNameForCatalogue, catGroup);
152 165
		jg.writeStringField("maintainer", Joiner.on(", ").join(resourceReader.getMaintainersForManagementSection(res)));
153 166
		//TODO: it should be better to identify email contacts rather than generic contact labels of maintainer
154 167
		//jg.writeStringField("maintainer_email", Joiner.on(", ").join(resourceReader.getMaintainerContacts(res)));
......
194 207
		return out.toString("UTF-8");
195 208
	}
196 209

  
197
	protected String getJsonForActor(final Resource res, final String resNameForCatalogue) throws IOException {
210
	protected String getJsonForActor(final Resource res, final String resNameForCatalogue, final String catGroup) throws IOException {
198 211
		JsonFactory jsonFactory = new JsonFactory();
199 212
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
200 213
		BufferedOutputStream bos = new BufferedOutputStream(out);
201 214
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
202 215
		jg.writeStartObject();
203
		writeCommonFields(jg, res, resNameForCatalogue);
216
		writeCommonFields(jg, res, resNameForCatalogue, catGroup);
204 217

  
205 218
		jg.writeArrayFieldStart("extras");
206 219
		addExtra(jg, "system:type", CRM.E39_Actor.getLocalName());
......
237 250
		return out.toString("UTF-8");
238 251
	}
239 252

  
240
	protected String getJsonForThing(final Resource res, final String resNameForCatalogue) throws IOException {
253
	protected String getJsonForThing(final Resource res, final String resNameForCatalogue, final String catGroup) throws IOException {
241 254
		JsonFactory jsonFactory = new JsonFactory();
242 255
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
243 256
		BufferedOutputStream bos = new BufferedOutputStream(out);
244 257
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
245 258
		jg.writeStartObject();
246
		writeCommonFields(jg, res, resNameForCatalogue);
259
		writeCommonFields(jg, res, resNameForCatalogue, catGroup);
247 260

  
248 261
		jg.writeArrayFieldStart("extras");
249 262
		addExtra(jg, "system:type", CRM.E70_Thing.getLocalName());
......
280 293
		return out.toString("UTF-8");
281 294
	}
282 295

  
283
	protected String getJsonForDesignProcedure(final Resource res, final String resNameForCatalogue) throws IOException {
296
	protected String getJsonForDesignProcedure(final Resource res, final String resNameForCatalogue, final String catGroup) throws IOException {
284 297
		JsonFactory jsonFactory = new JsonFactory();
285 298
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
286 299
		BufferedOutputStream bos = new BufferedOutputStream(out);
287 300
		JsonGenerator jg = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
288 301
		jg.writeStartObject();
289
		writeCommonFields(jg, res, resNameForCatalogue);
302
		writeCommonFields(jg, res, resNameForCatalogue, catGroup);
290 303

  
291 304
		jg.writeArrayFieldStart("extras");
292 305
		addExtra(jg, "system:type", CRM.E29_Design_or_Procedure.getLocalName());
......
310 323
		jg.writeEndObject();
311 324
	}
312 325

  
313
	protected void writeCommonFields(final JsonGenerator jg, final Resource res, final String resNameForCatalogue) throws IOException {
326
	protected void writeCommonFields(final JsonGenerator jg, final Resource res, final String resNameForCatalogue, final String catGroup) throws IOException {
314 327
		//end of URI
315 328
		jg.writeStringField("name", resNameForCatalogue);
316 329
		//default license
......
330 343
			jg.writeEndObject();
331 344
		}
332 345
		jg.writeEndArray();
333
		//RI from which the entity has been collected, the source from which the RI collected the entity (if available).
334
		//TODO: other Actors to add as catalogue group?
335
		//ignore groups for now
336
//		Iterator<String> providers = resourceReader.getProviderNames(res);
337
//		jg.writeArrayFieldStart("groups");
338
//		while(providers.hasNext()){
339
//			String provider = providers.next();
340
//			jg.writeStartObject();
341
//			jg.writeStringField("name", provider);
342
//			jg.writeEndObject();
343
//		}
344
//		jg.writeEndArray();
346
		jg.writeArrayFieldStart("groups");
347
		jg.writeStartObject();
348
		jg.writeStringField("name", catGroup);
349
		jg.writeEndObject();
350
		jg.writeEndArray();
345 351
	}
346 352

  
347 353

  

Also available in: Unified diff