minor fixes: when a product change status also its current groups and tags need to be updated
git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/gcube-ckan-datacatalog@135117 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
bedbe09eaa
commit
79229daa5e
|
@ -4,6 +4,9 @@
|
|||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
|
||||
<dependent-module archiveName="ckan-util-library-2.1.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ckan-util-library/ckan-util-library">
|
||||
<dependency-type>uses</dependency-type>
|
||||
</dependent-module>
|
||||
<property name="context-root" value="gcube-ckan-datacatalog"/>
|
||||
<property name="java-output-path" value="/gcube-ckan-datacatalog/target/gcube-ckan-datacatalog-1.0.0-SNAPSHOT/WEB-INF/classes"/>
|
||||
</wb-module>
|
||||
|
|
|
@ -26,11 +26,11 @@ import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpCl
|
|||
* Endpoint for sending update records information to GRSF KB
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
public class GRSFNotificationServices {
|
||||
public class GRSFNotificationService {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(GRSFNotificationServices.class);
|
||||
private static Logger logger = LoggerFactory.getLogger(GRSFNotificationService.class);
|
||||
private static final String SERVICE_POST_METHOD= "/service/updater/post";
|
||||
|
||||
|
||||
// request post fields
|
||||
private static final String CATALOGUE_ID = "catalog_id";
|
||||
private static final String KB_ID = "record_id";
|
||||
|
@ -39,7 +39,7 @@ public class GRSFNotificationServices {
|
|||
private static final String ANNOTATION = "annotation_msg";
|
||||
private static final String ERROR = "error";
|
||||
private static final int STATUS_SUCCESS = 200;
|
||||
|
||||
|
||||
/**
|
||||
* Discover the service endpoint and return its url
|
||||
* @param context
|
||||
|
@ -49,7 +49,7 @@ public class GRSFNotificationServices {
|
|||
// TODO discover the instance in this scope
|
||||
return "http://62.217.127.124:8080/grsf-services-updater";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send an update for this bean
|
||||
* @param baseUrl
|
||||
|
@ -60,60 +60,63 @@ public class GRSFNotificationServices {
|
|||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String updateCatalogueRecord(String serviceUrl, ManageProductBean bean, DataCatalogue catalogue, String username){
|
||||
|
||||
|
||||
if(serviceUrl == null)
|
||||
throw new IllegalArgumentException("Service url cannot be null");
|
||||
|
||||
|
||||
if(bean == null)
|
||||
throw new IllegalArgumentException("Product bean to manage cannot be null");
|
||||
|
||||
|
||||
try(CloseableHttpClient httpClient = HttpClientBuilder.create().build();){
|
||||
|
||||
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put(CATALOGUE_ID, bean.getCatalogueIdentifier());
|
||||
obj.put(KB_ID, bean.getKnowledgeBaseIdentifier());
|
||||
obj.put(PRODUCT_TYPE, bean.getProductType().toLowerCase());
|
||||
obj.put(STATUS, bean.getNewStatus().toLowerCase());
|
||||
|
||||
|
||||
String annotation = bean.getAnnotation();
|
||||
if(annotation != null)
|
||||
obj.put(ANNOTATION, annotation);
|
||||
|
||||
|
||||
logger.debug("Update request looks like " + obj.toJSONString());
|
||||
|
||||
|
||||
HttpPost request = new HttpPost(serviceUrl + SERVICE_POST_METHOD);
|
||||
request.setHeader("Accept", "application/json");
|
||||
request.setHeader("Content-type", "application/json");
|
||||
StringEntity params = new StringEntity(obj.toJSONString());
|
||||
request.setEntity(params);
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
|
||||
|
||||
logger.debug("Response code is " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
||||
|
||||
String result = convertStreamToString(response.getEntity().getContent());
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject parsedJSON = (JSONObject)parser.parse(result);
|
||||
|
||||
|
||||
if(response.getStatusLine().getStatusCode() > STATUS_SUCCESS)
|
||||
throw new IllegalArgumentException(
|
||||
"Error while performing the update request: " + response.getStatusLine().getReasonPhrase() +
|
||||
"and error in the result bean is " + parsedJSON.get(ERROR));
|
||||
|
||||
|
||||
// patch the catalogue product
|
||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||
Map<String, List<String>> statusMap = new HashMap<String, List<String>>();
|
||||
statusMap.put("Status", Arrays.asList(bean.getNewStatus()));
|
||||
boolean updated = catalogue.patchProductCustomFields(bean.getCatalogueIdentifier(), catalogue.getApiKeyFromUsername(username), statusMap);
|
||||
|
||||
logger.info("Was the catalogue product updated too? " + updated);
|
||||
|
||||
catalogue.patchProductCustomFields(bean.getCatalogueIdentifier(), apiKey, statusMap);
|
||||
catalogue.removeTag(bean.getCatalogueIdentifier(), apiKey, bean.getCurrentStatus());
|
||||
catalogue.addTag(bean.getCatalogueIdentifier(), apiKey, bean.getNewStatus());
|
||||
catalogue.removeDatasetFromGroup(bean.getCurrentStatus().toLowerCase(), bean.getCatalogueIdentifier(), apiKey);
|
||||
catalogue.assignDatasetToGroup(bean.getCurrentStatus().toLowerCase(), bean.getCatalogueIdentifier(), apiKey);
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Unable to update this record", e);
|
||||
return e.getMessage();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert an input stream to a string
|
||||
* @param is
|
||||
|
@ -121,24 +124,24 @@ public class GRSFNotificationServices {
|
|||
*/
|
||||
private static String convertStreamToString(InputStream is) {
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String line = null;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line + "\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
String line = null;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line + "\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -528,8 +528,8 @@ public class GcubeCkanDataCatalogServiceImpl extends RemoteServiceServlet implem
|
|||
|
||||
String context = SessionUtil.getScopeFromClientUrl(getThreadLocalRequest());
|
||||
DataCatalogue catalogue = getCatalogue(context);
|
||||
String baseUrl = GRSFNotificationServices.discoverEndPoint(context);
|
||||
String result = GRSFNotificationServices.updateCatalogueRecord(baseUrl, bean, catalogue, SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername());
|
||||
String baseUrl = GRSFNotificationService.discoverEndPoint(context);
|
||||
String result = GRSFNotificationService.updateCatalogueRecord(baseUrl, bean, catalogue, SessionUtil.getCurrentUser(getThreadLocalRequest()).getUsername());
|
||||
|
||||
if(result == null)
|
||||
return true;
|
||||
|
|
Loading…
Reference in New Issue