gt: 					", packageId=" + resource.getPackageId() + 
context: // ALIADA - Automatic publication under Linked Data paradigm
  //          of library and museum data
  //
  // Component: aliada-ckan-datahub-page-creation
  // Responsible: ALIADA Consortium
  package eu.aliada.ckancreation.impl;
  
  import java.io.*;
  import java.nio.file.Files;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.Map;
  
  import javax.ws.rs.client.ClientBuilder;
  import javax.ws.rs.client.Client;
  import javax.ws.rs.client.WebTarget;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.client.Entity;
  import javax.ws.rs.core.Response;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  import eu.aliada.ckancreation.model.Organization;
  import eu.aliada.ckancreation.model.CKANOrgResponse;
  import eu.aliada.ckancreation.model.Dataset;
  import eu.aliada.ckancreation.model.CKANDatasetResponse;
  import eu.aliada.ckancreation.model.Resource;
  import eu.aliada.ckancreation.model.CKANResourceResponse;
  import eu.aliada.ckancreation.model.Job;
  import eu.aliada.ckancreation.model.JobConfiguration;
  import eu.aliada.ckancreation.model.DumpFileInfo;
  import eu.aliada.ckancreation.model.Subset;
  import eu.aliada.ckancreation.log.MessageCatalog;
  import eu.aliada.ckancreation.rdbms.DBConnectionManager;
  import eu.aliada.ckancreation.impl.DataDump;
  import eu.aliada.ckancreation.impl.DatasetDescFile;
  import eu.aliada.shared.log.Log;
  import eu.aliada.shared.rdfstore.RDFStoreDAO;
  
  /**
   * CKAN Creation implementation. 
   * It creates a new Dataset in CKAN Datahub. 
   * Removes it first if it already exists.
   * It also updates the organization information in CKAN Datahub.   
   * 
   * @author Idoia Murua
   * @since 2.0
   */
  public class CKANCreation {
  	/** For logging. */
  	private static final Log LOGGER  = new Log(CKANCreation.class);
  	/** Folder where to store the dataset sump files **/
  	static final String DUMPS_FOLDER = "datadumps"; 
  	/** Job Configuration which contains required parameters. **/
  	private final JobConfiguration jobConf;
  	/** Database connection **/
  	private final DBConnectionManager dbConn;
  	/** The folder name where the dataset dumps and the 
  	 * description file will reside **/
  	private String dataFolderName;
  	/** The folder URL where the dataset dumps and the description file will reside **/
  	private final String dataFolderURL; 
  	/** the URL of the organization in CKAN. **/
  	private String ckanOrgUrl;	
  	/** the URL of the dataset in CKAN. **/
  	private String ckanDatasetUrl;
  	/** the info of the dataset dump files. */
  	private final ArrayList<DumpFileInfo> datasetDumpFileInfoList = new ArrayList<DumpFileInfo>(); 
  
  	/**
  	 * Constructor. 
  	 * Initializes the class variables.
  	 *
  	 * @param jobConf		the {@link eu.aliada.ckancreation.model.JobConfiguration}
  	 *						that contains information to create the dataset in CKAN Datahub.  
  	 * @param db			The DDBB connection. 
  	 * @since 2.0
  	 */
  	public CKANCreation(final JobConfiguration jobConf, final DBConnectionManager dbConn) {
  		this.jobConf = jobConf;
  		this.dbConn = dbConn;
  		//Create the folder where the data dumps 
  		//and the dataset description file will reside
  		dataFolderName = jobConf.getDomainName().replace("http", ""); 
  		dataFolderName = dataFolderName.replace(":", "");
  		dataFolderName = dataFolderName.replace("/", "");
  		dataFolderName = dataFolderName.replace(".", "");
  		dataFolderName = jobConf.getVirtHttpServRoot() + File.separator + dataFolderName +
  				File.separator + DUMPS_FOLDER;
  		final File fFolder = new File(dataFolderName);
  		if (!fFolder.exists()) {
  			fFolder.mkdir();
  		}
  		dataFolderURL = "http://" + jobConf.getDomainName() + "/" + DUMPS_FOLDER;
  	}
  	
  	/**
  	 * Gets the organization information from CKAN Datahub.
  	 *
  	 * @param orgName	the organization name.
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANOrgResponse}
  	 * 			which contains the information of the organization in CKAN.
  	 * @since 2.0
  	 */
      public CKANOrgResponse getOrganizationCKAN(final String orgName){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//GET 
  		final WebTarget resourceWebTarget = webTarget.path("/organization_show");
  		final Response getResponse = resourceWebTarget
  				.queryParam("id", orgName)
  				.queryParam("include_datasets", "True")
  				.request().header("Authorization", jobConf.getCkanApiKey()).get();
  		final String orgShowResp = getResponse.readEntity(String.class);
  		//Convert JSON representation to Java Object with JACKSON libraries
  		CKANOrgResponse cResponse = new CKANOrgResponse();
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			cResponse = mapper.readValue(orgShowResp, CKANOrgResponse.class);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  		return cResponse;
  	}
  
  	/**
  	 * Update the organization information in CKAN Datahub.
  	 *
  	 * @param orgId     		the organization id.
  	 * @param orgName			the organization name.
  	 * @param orgTitle			the organization title.
  	 * @param orgDescription	the organization description.
  	 * @param orgImageURL		the organization image URL.
  	 * @param orgHomePage		the organization home page.
  	 * @param lUsers			the organization's users and their capacity.
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANOrgResponse}
  	 * 			which contains the information of the organization in CKAN.
  	 * @since 2.0
  	 */
  	public CKANOrgResponse updateOrganizationCKAN(final String orgId, final String orgName, 
  			final String orgTitle, final String orgDescription, final String orgImageURL, 
  			final String orgHomePage, final ArrayList<Map<String, String>> lUsers){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//Create a Java Object with the organization data
  		final Organization org = new Organization(orgName, orgId, orgTitle, 
  											orgDescription, orgImageURL, orgHomePage);
  		/////////////////////////******************///////////////////
  		///VERY IMPORTANT!!!!!
  		//Update also the users dictionary(including "capacity" attribute), 
  		//otherwise we lose rights for updating the organization
  		for (final Iterator<Map<String, String>> iterUsers = lUsers.iterator(); iterUsers.hasNext();  ) {
  			final Map<String, String> user = iterUsers.next();
  			org.setUser(user.get("name"), user.get("capacity"));
  		}
  
  		//Convert Java Object to JSON representation with JACKSON libraries
  		String orgJSON = "";
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			orgJSON = mapper.writeValueAsString(org);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  
  		//POST 
  		final WebTarget resourceWebTarget = webTarget.path("/organization_update");
  		final Response postResponse = resourceWebTarget
  				.request(MediaType.APPLICATION_XML_TYPE)
  				.header("Authorization", jobConf.getCkanApiKey()).post(Entity.json(orgJSON));
  		final String orgUpdateResp = postResponse.readEntity(String.class);
  		//Convert JSON representation to Java Object with JACKSON libraries
  		CKANOrgResponse cResponse = new CKANOrgResponse();
  		try {
  			cResponse = mapper.readValue(orgUpdateResp, CKANOrgResponse.class);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  		return cResponse;
  	}
  	
  	/**
  	 * Gets the organization information from CKAN Datahub.
  	 *
  	 * @param datasetName	the dataset name.
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANDatasetResponse}
  	 * 			which contains the information of the dataset in CKAN.
  	 * @since 2.0
  	 */
      public CKANDatasetResponse getDatasetCKAN(final String datasetName){
      	final Client client = ClientBuilder.newClient();
      	final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//GET 
      	final WebTarget resourceWebTarget = webTarget.path("/package_show");
      	final Response getResponse = resourceWebTarget
  				.queryParam("id", datasetName)
  				.request().header("Authorization", jobConf.getCkanApiKey()).get();
  		final String datasetShowResp = getResponse.readEntity(String.class);
  		//Convert JSON representation to Java Object with JACKSON libraries
  		CKANDatasetResponse cResponse = new CKANDatasetResponse();
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			cResponse = mapper.readValue(datasetShowResp, CKANDatasetResponse.class);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  		return cResponse;
  	}
  
  	/**
  	 * Delete the dataset from CKAN Datahub.
  	 * The package_delete method of CKAN only sets the "state" field 
  	 * of the dataset to the "deleted" value.
  	 * It does not remove it permanently.
  	 *
  	 * @param packId  the package/dataset id to delete.
  	 * @return	the response of CKAN.
  	 * @since 2.0
  	 */
  	public String deleteDatasetCKAN(final String packId){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//Create a Java Object with the dataset data
  		final Dataset dataset = new Dataset();
  		dataset.setState("deleted");
  		dataset.setId(packId);
  
  		//Convert Java Object to JSON representation with JACKSON libraries
  		String datasetJSON = "";
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			datasetJSON = mapper.writeValueAsString(dataset);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  
  		//POST
  		//The package_delete method of CKAN only sets the "state" field of the dataset 
  		//to the "deleted" value. It does not remove it permanently.
  		final WebTarget resourceWebTarget = webTarget.path("/package_delete");
  		final Response getResponse = resourceWebTarget
  				.queryParam("id", packId)
  				.request().header("Authorization", jobConf.getCkanApiKey()).post(Entity.json(datasetJSON));
  		final String orgDeleteResp = getResponse.readEntity(String.class);
  		return orgDeleteResp;	
  	}
  
  
  	/**
  	 * Create the dataset in CKAN Datahub.
  	 *
  	 * @param dataset	the {@link eu.aliada.ckancreation.model.Dataset} object to create.
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANDatasetResponse}
  	 * 			which contains the information of the dataset in CKAN.
  	 * @since 2.0
  	 */
  	public CKANDatasetResponse createDatasetCKAN(final Dataset dataset){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//Convert Java Object to JSON representation with JACKSON libraries
  		String datasetJSON = "";
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			datasetJSON = mapper.writeValueAsString(dataset);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  
  		//POST 
  		final WebTarget resourceWebTarget = webTarget.path("/package_create");
  		final Response postResponse = resourceWebTarget
  				.request(MediaType.APPLICATION_XML_TYPE)
  				.header("Authorization", jobConf.getCkanApiKey()).post(Entity.json(datasetJSON));
  		final String datasetCreateResp = postResponse.readEntity(String.class);
  		//Convert JSON representation to Java Object with JACKSON libraries
  		CKANDatasetResponse cResponse = new CKANDatasetResponse();
  		try {
  			cResponse = mapper.readValue(datasetCreateResp, CKANDatasetResponse.class);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  		return cResponse;
  	}
  	
  	/**
  	 * Update the dataset in CKAN Datahub.
  	 *
  	 * @param dataset	the {@link eu.aliada.ckancreation.model.Dataset} object to update.
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANDatasetResponse}
  	 * 			which contains the information of the dataset in CKAN.
  	 * @since 2.0
  	 */
  	public CKANDatasetResponse updateDatasetCKAN(final Dataset dataset){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//Convert Java Object to JSON representation with JACKSON libraries
  		String datasetJSON = "";
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			datasetJSON = mapper.writeValueAsString(dataset);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  
  		//POST 
  		final WebTarget resourceWebTarget = webTarget.path("/package_update");
  		final Response postResponse = resourceWebTarget
  				.request(MediaType.APPLICATION_XML_TYPE)
  				.header("Authorization", jobConf.getCkanApiKey()).post(Entity.json(datasetJSON));
  		final String datasetCreateResp = postResponse.readEntity(String.class);
  		//Convert JSON representation to Java Object with JACKSON libraries
  		CKANDatasetResponse cResponse = new CKANDatasetResponse();
  		try {
  			cResponse = mapper.readValue(datasetCreateResp, CKANDatasetResponse.class);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  		}
  		return cResponse;
  	}
  	
  	/**
  	 * Add the resource to a dataset in CKAN Datahub.
  	 *
  	 * @param datasetId  		the dataset/package id.
  	 * @param resName			the resource name.
  	 * @param resDescription	the resource description.
  	 * @param resFormat			the resource format.
  	 * @param resUrl			the resource URL.
  	 * @param resType			the resource type (file, api, example, etc.)
  	 * @return	the {@link eu.aliada.ckancreation.model.CKANResourceResponse}
  	 * 			which contains the information of the resource in CKAN.
  	 * @since 2.0
  	 */
  	public CKANResourceResponse createResourceCKAN(final String datasetId, final String resName, 
  			final String resDescription, final String resFormat, final String resUrl, final String resType){
  		final Client client = ClientBuilder.newClient();
  		final WebTarget webTarget = client.target(jobConf.getCkanApiURL());
  		
  		//Create a Java Object with the resource data
  		final Resource resource = new Resource(datasetId, resName, 
  				resDescription, resFormat, resUrl, resType);
  
  		//Convert Java Object to JSON representation with JACKSON libraries
  		String resourceJSON = "";
  		final ObjectMapper mapper = new ObjectMapper();
  		try {
  			resourceJSON = mapper.writeValueAsString(resource);
      	} catch (Exception exception) {
  			LOGGER.error(MessageCatalog._00032_OBJECT_CONVERSION_FAILURE, exception);
  			LOGGER.debug("name=" + resource.getName() + 
  