68 lines
2.7 KiB
Java
68 lines
2.7 KiB
Java
package org.gcube.portlets.gcubeckan.gcubeckandatacatalog.server.manage;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
import org.gcube.portlets.gcubeckan.gcubeckandatacatalog.shared.ManageProductBean;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
/**
|
|
* Thread used to patch a ckan product.
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class PatchProductThread extends Thread{
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(PatchProductThread.class);
|
|
private DataCatalogue catalogue;
|
|
private ManageProductBean bean;
|
|
private String username;
|
|
private static final String ANNOTATION_KEY = "Annotation on Status update";
|
|
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
private static final String STATUS_CUSTOM_FIELD_KEY = "Status";
|
|
|
|
/**
|
|
* @param catalogue
|
|
* @param bean
|
|
*/
|
|
public PatchProductThread(DataCatalogue catalogue, ManageProductBean bean, String username) {
|
|
super();
|
|
this.catalogue = catalogue;
|
|
this.bean = bean;
|
|
this.username = username;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
|
|
logger.info("Going to patch record in the catalogue with identifier " + bean.getCatalogueIdentifier() +
|
|
" from user " + username);
|
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
// remove the "Status" field
|
|
catalogue.removeCustomField(bean.getCatalogueIdentifier(), STATUS_CUSTOM_FIELD_KEY, bean.getCurrentStatus().toString(), apiKey);
|
|
catalogue.removeTag(bean.getCatalogueIdentifier(), apiKey, bean.getCurrentStatus().toString());
|
|
catalogue.removeDatasetFromGroup("grsf" + "-" + bean.getCurrentStatus().toString().toLowerCase(), bean.getCatalogueIdentifier(), apiKey);
|
|
|
|
Map<String, List<String>> statusMap = new HashMap<String, List<String>>();
|
|
statusMap.put(STATUS_CUSTOM_FIELD_KEY, Arrays.asList(bean.getNewStatus().toString()));
|
|
statusMap.put(ANNOTATION_KEY, Arrays.asList("date: " + DATE_FORMAT.format(new Date())
|
|
+ ", admin: " + username
|
|
+ ", annotation: " + (bean.getAnnotation() != null ? bean.getAnnotation().replaceAll("\"", "") : "none")
|
|
+ ", new status: " + bean.getNewStatus().toString()
|
|
+ ", old status: " + bean.getCurrentStatus().toString()
|
|
));
|
|
catalogue.patchProductCustomFields(bean.getCatalogueIdentifier(), apiKey, statusMap);
|
|
catalogue.addTag(bean.getCatalogueIdentifier(), apiKey, bean.getNewStatus().toString());
|
|
catalogue.assignDatasetToGroup("grsf" + "-" + bean.getCurrentStatus().toString().toLowerCase(), bean.getCatalogueIdentifier(), apiKey);
|
|
logger.info("Record patched ...");
|
|
}
|
|
|
|
}
|