Almost all logic for dataset creation as well as resource creation/deletion has been moved into the ckan-util-library
git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@129108 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
0ded532dde
commit
053daea14a
6
pom.xml
6
pom.xml
|
@ -170,12 +170,6 @@
|
|||
<artifactId>gcube-widgets</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.htmlparser.jericho</groupId>
|
||||
<artifactId>jericho-html</artifactId>
|
||||
<version>3.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.gcube.portlets.widgets.ckandatapublisherwidget.client;
|
|||
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.google.gwt.user.client.rpc.RemoteService;
|
||||
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
|
||||
|
@ -43,7 +43,7 @@ public interface CKanPublisherService extends RemoteService {
|
|||
* @param datasetId
|
||||
* @param owner of the dataset
|
||||
*/
|
||||
ResourceBean addResourceToDataset(ResourceBean resource, String datasetId, String owner);
|
||||
ResourceBeanWrapper addResourceToDataset(ResourceBeanWrapper resource, String datasetId, String owner);
|
||||
|
||||
/**
|
||||
* Delete this resource from the dataset with id datasetId
|
||||
|
@ -52,6 +52,6 @@ public interface CKanPublisherService extends RemoteService {
|
|||
* @param owner of the dataset
|
||||
* @return <b>true</b> on success, false otherwise
|
||||
*/
|
||||
boolean deleteResourceFromDataset(ResourceBean resource, String owner);
|
||||
boolean deleteResourceFromDataset(ResourceBeanWrapper resource, String owner);
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.gcube.portlets.widgets.ckandatapublisherwidget.client;
|
|||
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.google.gwt.user.client.rpc.AsyncCallback;
|
||||
|
||||
|
@ -44,8 +44,8 @@ public interface CKanPublisherServiceAsync {
|
|||
* @param owner of the dataset
|
||||
* @param callback
|
||||
*/
|
||||
void addResourceToDataset(ResourceBean resource, String datasetId, String owner,
|
||||
AsyncCallback<ResourceBean> callback);
|
||||
void addResourceToDataset(ResourceBeanWrapper resource, String datasetId, String owner,
|
||||
AsyncCallback<ResourceBeanWrapper> callback);
|
||||
|
||||
/**
|
||||
* Delete this resource from the dataset with id datasetId
|
||||
|
@ -53,7 +53,7 @@ public interface CKanPublisherServiceAsync {
|
|||
* @param owner of the dataset
|
||||
* @return <b>true</b> on success, false otherwise
|
||||
*/
|
||||
void deleteResourceFromDataset(ResourceBean resource,
|
||||
void deleteResourceFromDataset(ResourceBeanWrapper resource,
|
||||
String owner, AsyncCallback<Boolean> callback);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.events;
|
||||
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.google.gwt.event.shared.GwtEvent;
|
||||
|
||||
|
@ -11,13 +11,13 @@ import com.google.gwt.event.shared.GwtEvent;
|
|||
public class AddResourceEvent extends GwtEvent<AddResourceEventHandler> {
|
||||
public static Type<AddResourceEventHandler> TYPE = new Type<AddResourceEventHandler>();
|
||||
|
||||
private ResourceBean resource;
|
||||
private ResourceBeanWrapper resource;
|
||||
|
||||
public AddResourceEvent(ResourceBean resource) {
|
||||
public AddResourceEvent(ResourceBeanWrapper resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public ResourceBean getResource() {
|
||||
public ResourceBeanWrapper getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.events;
|
||||
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.google.gwt.event.shared.GwtEvent;
|
||||
|
||||
|
@ -11,13 +11,13 @@ import com.google.gwt.event.shared.GwtEvent;
|
|||
public class DeleteResourceEvent extends GwtEvent<DeleteResourceEventHandler> {
|
||||
public static Type<DeleteResourceEventHandler> TYPE = new Type<DeleteResourceEventHandler>();
|
||||
|
||||
private ResourceBean resource;
|
||||
private ResourceBeanWrapper resource;
|
||||
|
||||
public DeleteResourceEvent(ResourceBean resource) {
|
||||
public DeleteResourceEvent(ResourceBeanWrapper resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public ResourceBean getResource() {
|
||||
public ResourceBeanWrapper getResource() {
|
||||
return resource;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherSe
|
|||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherServiceAsync;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.Encoder;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEvent;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.github.gwtbootstrap.client.ui.AlertBlock;
|
||||
import com.github.gwtbootstrap.client.ui.Button;
|
||||
|
@ -98,16 +98,16 @@ public class AddResourceToDataset extends Composite{
|
|||
}
|
||||
|
||||
// collect data and build up the bean
|
||||
final ResourceBean resource = new ResourceBean(resourceUrlTextBox.getText(), resourceNameTextBox.getText(), resourceDescriptionTextArea.getText());
|
||||
final ResourceBeanWrapper resource = new ResourceBeanWrapper(resourceUrlTextBox.getText(), resourceNameTextBox.getText(), resourceDescriptionTextArea.getText());
|
||||
|
||||
// disable add button
|
||||
addResourceButton.setEnabled(false);
|
||||
|
||||
// try to create
|
||||
ckanServices.addResourceToDataset(resource, datasetId, owner, new AsyncCallback<ResourceBean>() {
|
||||
ckanServices.addResourceToDataset(resource, datasetId, owner, new AsyncCallback<ResourceBeanWrapper>() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(ResourceBean result) {
|
||||
public void onSuccess(ResourceBeanWrapper result) {
|
||||
|
||||
if(result != null){
|
||||
showAlert("Resource created correctly", AlertType.SUCCESS);
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResou
|
|||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.AddResourceEventHandler;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.DeleteResourceEvent;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.events.DeleteResourceEventHandler;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
|
||||
import com.github.gwtbootstrap.client.ui.Accordion;
|
||||
import com.github.gwtbootstrap.client.ui.AccordionGroup;
|
||||
|
@ -46,7 +46,7 @@ public class AddedResourcesSummary extends Composite{
|
|||
private HandlerManager eventBus;
|
||||
|
||||
// list of added resources (beans)
|
||||
List<ResourceBean> addedResources;
|
||||
List<ResourceBeanWrapper> addedResources;
|
||||
|
||||
// the owner
|
||||
private String owner;
|
||||
|
@ -66,7 +66,7 @@ public class AddedResourcesSummary extends Composite{
|
|||
bind();
|
||||
|
||||
// init list
|
||||
addedResources = new ArrayList<ResourceBean>();
|
||||
addedResources = new ArrayList<ResourceBeanWrapper>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,7 +81,7 @@ public class AddedResourcesSummary extends Composite{
|
|||
public void onAddedResource(AddResourceEvent addResourceEvent) {
|
||||
|
||||
// get the resource
|
||||
final ResourceBean justAddedResource = addResourceEvent.getResource();
|
||||
final ResourceBeanWrapper justAddedResource = addResourceEvent.getResource();
|
||||
|
||||
// Build an accordion to show resource info
|
||||
Accordion accordion = new Accordion();
|
||||
|
@ -128,7 +128,7 @@ public class AddedResourcesSummary extends Composite{
|
|||
public void onDeletedResource(DeleteResourceEvent deleteResourceEvent) {
|
||||
|
||||
// to delete
|
||||
ResourceBean toDelete = deleteResourceEvent.getResource();
|
||||
ResourceBeanWrapper toDelete = deleteResourceEvent.getResource();
|
||||
|
||||
// find it
|
||||
for(int i = 0; i < addedResources.size(); i++){
|
||||
|
|
|
@ -729,6 +729,17 @@ public class CreateDatasetForm extends Composite{
|
|||
return errorMessage;
|
||||
}
|
||||
|
||||
// better check for the title
|
||||
String regexTitleSubWord = "^[a-zA-Z0-9_]+$";
|
||||
String[] splittedTitle = titleTextBox.getText().split(" ");
|
||||
|
||||
for (String word : splittedTitle) {
|
||||
|
||||
if(!word.matches(regexTitleSubWord))
|
||||
return "Please note that only alphanumeric characters are allowed for the title";
|
||||
|
||||
}
|
||||
|
||||
// name reg expression
|
||||
String regexName = "^[a-zA-Z\\s]+";
|
||||
if(!validateByRegExpression(maintainerTextbox.getText(), regexName)){
|
||||
|
|
|
@ -1,18 +1,10 @@
|
|||
package org.gcube.portlets.widgets.ckandatapublisherwidget.server;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import net.htmlparser.jericho.Renderer;
|
||||
import net.htmlparser.jericho.Segment;
|
||||
import net.htmlparser.jericho.Source;
|
||||
|
||||
import org.gcube.common.homelibrary.home.HomeLibrary;
|
||||
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
||||
|
@ -22,6 +14,8 @@ import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
|||
import org.gcube.common.homelibrary.home.workspace.folder.items.GCubeItem;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsFactory;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.CKanUtilsImpl;
|
||||
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType;
|
||||
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
|
||||
|
@ -34,7 +28,7 @@ import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
|
|||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataTypeWrapper;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetadataFieldWrapper;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBean;
|
||||
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
||||
import org.gcube.vomanagement.usermanagement.UserManager;
|
||||
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager;
|
||||
import org.gcube.vomanagement.usermanagement.model.UserModel;
|
||||
|
@ -43,13 +37,6 @@ import org.slf4j.LoggerFactory;
|
|||
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
||||
import com.liferay.portal.service.UserLocalServiceUtil;
|
||||
|
||||
import eu.trentorise.opendata.jackan.CheckedCkanClient;
|
||||
import eu.trentorise.opendata.jackan.model.CkanDataset;
|
||||
import eu.trentorise.opendata.jackan.model.CkanOrganization;
|
||||
import eu.trentorise.opendata.jackan.model.CkanPair;
|
||||
import eu.trentorise.opendata.jackan.model.CkanResource;
|
||||
import eu.trentorise.opendata.jackan.model.CkanTag;
|
||||
|
||||
/**
|
||||
* Server side of the data publisher.
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
|
@ -60,43 +47,6 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
// Logger
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CKANPublisherServicesImpl.class);
|
||||
|
||||
/**
|
||||
* Return the ckan catalogue url for this scope.
|
||||
* @return
|
||||
*/
|
||||
private String getCatalogueUrl(){
|
||||
|
||||
logger.debug("Request for catalogue url");
|
||||
|
||||
try{
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
return CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope).getCatalogueUrl();
|
||||
}catch(Exception e){
|
||||
logger.error("Failed to retrieve catalogue url information", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the api key for this user
|
||||
* @param owner
|
||||
* @return
|
||||
*/
|
||||
private String getCKANApikeyFromUser(String owner) {
|
||||
|
||||
logger.debug("Request for user api key");
|
||||
|
||||
try{
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
return CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope).getApiKeyFromUser(owner);
|
||||
}catch(Exception e){
|
||||
logger.error("Failed to retrieve api key for user " + owner, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the list of organizations in which the user can publish
|
||||
* @param owner
|
||||
|
@ -230,7 +180,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
|
||||
DatasetMetadataBean bean = null;
|
||||
|
||||
logger.info("Bean request for " + folderId + " and " + owner);
|
||||
logger.info("DatasetBean request for " + folderId + " and " + owner);
|
||||
|
||||
if(isWithinPortal()){
|
||||
try{
|
||||
|
@ -372,82 +322,37 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
@Override
|
||||
public DatasetMetadataBean createCKanDataset(DatasetMetadataBean toCreate, boolean isWorkspaceRequest) {
|
||||
|
||||
// retrieve ckan's catalog url
|
||||
String ckanPortalUrl = getCatalogueUrl();
|
||||
logger.debug("Request for creating a dataset with these information " + toCreate);
|
||||
|
||||
// retrieve the owner identifier (i.e. costantino.perciante)
|
||||
String owner = toCreate.getOwnerIdentifier();
|
||||
try{
|
||||
|
||||
// retrieve the api key for this user
|
||||
String apiKey = getCKANApikeyFromUser(fromOwnerToCKanOwner(owner));
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
|
||||
logger.info("Trying to create the dataset described by this bean " + toCreate + " into CKAN catalog at url " + ckanPortalUrl);
|
||||
// get the owner
|
||||
String username = fromOwnerToCKanOwner(toCreate.getOwnerIdentifier());
|
||||
CKanUtilsImpl utilityInstance = CKanUtilsFactory.getInstance().getCkanUtilsForScope(currentScope);
|
||||
|
||||
// Checked client
|
||||
CheckedCkanClient client = new CheckedCkanClient(ckanPortalUrl, apiKey);
|
||||
// set dataset info (same id as the folder, if the request comes from the workspace)
|
||||
String withId = null;
|
||||
if(isWorkspaceRequest)
|
||||
withId = toCreate.getId();
|
||||
|
||||
// create the base dataset and fill it
|
||||
CkanDataset dataset = new CkanDataset();
|
||||
|
||||
// set dataset info (same id as the folder)
|
||||
dataset.setId(toCreate.getId());
|
||||
|
||||
// get the name from the title
|
||||
dataset.setName(nameFromTitle(toCreate.getTitle()));
|
||||
dataset.setTitle(toCreate.getTitle());
|
||||
|
||||
CkanOrganization orgOwner = client.getOrganization(toCreate.getSelectedOrganization());
|
||||
dataset.setOwnerOrg(orgOwner.getId());
|
||||
dataset.setAuthor(toCreate.getAuthor());
|
||||
dataset.setAuthorEmail(toCreate.getAuthorEmail());
|
||||
dataset.setMaintainer(toCreate.getMaintainer());
|
||||
dataset.setMaintainerEmail(toCreate.getMaintainerEmail());
|
||||
dataset.setVersion(String.valueOf(toCreate.getVersion()));
|
||||
|
||||
// description must be escaped
|
||||
Source description = new Source(toCreate.getDescription());
|
||||
Segment htmlSeg = new Segment(description, 0, description.length());
|
||||
Renderer htmlRend = new Renderer(htmlSeg);
|
||||
dataset.setNotes(htmlRend.toString());
|
||||
|
||||
logger.debug("Description (escaped is ) " + htmlRend.toString());
|
||||
|
||||
// iterate over the licenses to find the id of the chosen one
|
||||
String title = toCreate.getTitle();
|
||||
String organizationNameOrId = toCreate.getSelectedOrganization();
|
||||
String author = toCreate.getAuthor();
|
||||
String authorMail = toCreate.getAuthorEmail();
|
||||
String maintainer = toCreate.getMaintainer();
|
||||
String maintainerMail = toCreate.getMaintainerEmail();
|
||||
long version = toCreate.getVersion();
|
||||
String description = toCreate.getDescription();
|
||||
String chosenLicense = toCreate.getLicense();
|
||||
String licenseId = findLicenseIdByLicense(chosenLicense);
|
||||
dataset.setLicenseId(licenseId);
|
||||
List<String> listOfTags = toCreate.getTags();
|
||||
Map<String, String> customFields = toCreate.getCustomFields();
|
||||
boolean setPublic = toCreate.getVisibility();
|
||||
List<ResourceBean> resources = new ArrayList<ResourceBean>();
|
||||
|
||||
// set the tags, if any
|
||||
if(toCreate.getTags() != null && !toCreate.getTags().isEmpty()){
|
||||
|
||||
// convert to ckan tags
|
||||
List<CkanTag> ckanTags = new ArrayList<CkanTag>(toCreate.getTags().size());
|
||||
for (String stringTag : toCreate.getTags()) {
|
||||
ckanTags.add(new CkanTag(stringTag));
|
||||
}
|
||||
|
||||
dataset.setTags(ckanTags);
|
||||
}
|
||||
|
||||
// set the custom fields, if any
|
||||
if(toCreate.getCustomFields() != null && !toCreate.getCustomFields().isEmpty()){
|
||||
|
||||
// iterate and create
|
||||
Iterator<Entry<String, String>> iterator = toCreate.getCustomFields().entrySet().iterator();
|
||||
|
||||
List<CkanPair> extras = new ArrayList<CkanPair>(toCreate.getCustomFields().entrySet().size());
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
|
||||
Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
|
||||
extras.add(new CkanPair(entry.getKey(), entry.getValue()));
|
||||
|
||||
}
|
||||
|
||||
dataset.setExtras(extras);
|
||||
}
|
||||
|
||||
// check if we need to add the resources
|
||||
// build the resource (if it is a workspaceRequest type)
|
||||
if(toCreate.isAddResources() && isWorkspaceRequest){
|
||||
|
||||
logger.debug("We need to add resources to the dataset");
|
||||
|
@ -459,64 +364,56 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
.getHomeManager()
|
||||
.getHome(toCreate.getOwnerIdentifier()).getWorkspace();
|
||||
|
||||
List<CkanResource> resources = new ArrayList<CkanResource>();
|
||||
|
||||
|
||||
for(String resourceId: toCreate.getResourcesIds()){
|
||||
|
||||
FolderItem item = (FolderItem)ws.getItem(resourceId);
|
||||
CkanResource newResource = new CkanResource();
|
||||
ResourceBean newResource = new ResourceBean();
|
||||
newResource.setDescription(item.getDescription());
|
||||
newResource.setId(item.getId());
|
||||
newResource.setUrl(item.getPublicLink(false));
|
||||
newResource.setName(item.getName());
|
||||
newResource.setMimetype(item.getMimeType());
|
||||
newResource.setOwner(fromOwnerToCKanOwner(owner));
|
||||
newResource.setMimeType(item.getMimeType());
|
||||
newResource.setOwner(username);
|
||||
resources.add(newResource);
|
||||
}
|
||||
|
||||
// add to the dataset
|
||||
dataset.setResources(resources);
|
||||
}
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Unable to add those resources to the dataset", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// try to create
|
||||
CkanDataset res = null;
|
||||
try{
|
||||
|
||||
res = client.createDataset(dataset);
|
||||
String datasetId = utilityInstance.createCKanDataset(username, withId, title, organizationNameOrId, author,
|
||||
authorMail, maintainer, maintainerMail, version, description, licenseId,
|
||||
listOfTags, customFields, resources, setPublic);
|
||||
|
||||
// add source and id to the incoming bean
|
||||
toCreate.setId(res.getId());
|
||||
toCreate.setSource(getCatalogueUrl() + "/dataset/" + res.getName());
|
||||
if(datasetId != null){
|
||||
|
||||
// set visibility
|
||||
setVisibility(toCreate, orgOwner.getId());
|
||||
logger.debug("Dataset created!");
|
||||
toCreate.setId(datasetId);
|
||||
|
||||
// retrieve the url
|
||||
String datasetUrl = utilityInstance.getUrlFromDatasetIdOrName(username, datasetId);
|
||||
|
||||
toCreate.setSource(datasetUrl);
|
||||
return toCreate;
|
||||
|
||||
}else{
|
||||
|
||||
logger.error("Failed to create the dataset");
|
||||
|
||||
}
|
||||
|
||||
}catch(Exception e){
|
||||
|
||||
// try to update
|
||||
logger.error("Error while creating the dataset, probably it already exists.", e);
|
||||
|
||||
logger.error("Unable to create the dataset", e);
|
||||
}
|
||||
|
||||
if(res != null)
|
||||
logger.debug("Dataset created/updated " + res.getId());
|
||||
else{
|
||||
logger.error("Dataset described by " + toCreate + " not created!");
|
||||
return null;
|
||||
}
|
||||
|
||||
return toCreate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ckan username has _ instead of . (that is, costantino_perciante)
|
||||
* Ckan username has _ instead of . (that is, costantino.perciante -> costantino_perciante)
|
||||
* @param owner
|
||||
* @return
|
||||
*/
|
||||
|
@ -524,105 +421,42 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
return owner.replaceAll("\\.", "_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the dataset visibility.
|
||||
* @param created
|
||||
* @param orgOwnerId
|
||||
* @throws Exception
|
||||
*/
|
||||
private void setVisibility(DatasetMetadataBean created, String orgOwnerId) throws Exception{
|
||||
|
||||
logger.debug("Setting visibility of the dataset");
|
||||
// set its visibility (true means public, false means private)
|
||||
boolean mustbePrivate = !created.getVisibility();
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
|
||||
CKanUtilsFactory.getInstance().
|
||||
getCkanUtilsForScope(currentScope).
|
||||
setDatasetPrivate(mustbePrivate, orgOwnerId, created.getId(), fromOwnerToCKanOwner(created.getOwnerIdentifier()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the catalogue's dataset name from its title
|
||||
* @param title
|
||||
* @return
|
||||
*/
|
||||
private String nameFromTitle(String title) {
|
||||
String convertedName = title.replaceAll(" ", "_");
|
||||
convertedName = convertedName.replaceAll("\\.", "_");
|
||||
convertedName = convertedName.toLowerCase();
|
||||
if(convertedName.endsWith("_"))
|
||||
convertedName = convertedName.substring(0, convertedName.length() - 2);
|
||||
|
||||
return convertedName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility methods
|
||||
* @param URLName
|
||||
* @return
|
||||
*/
|
||||
private static boolean exists(String URLName){
|
||||
try {
|
||||
HttpURLConnection.setFollowRedirects(true);
|
||||
HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
|
||||
con.setRequestMethod("HEAD");
|
||||
logger.debug("Return code is " + con.getResponseCode());
|
||||
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Exception while checking url", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceBean addResourceToDataset(ResourceBean resourceBean, String datasetId, String owner) {
|
||||
public ResourceBeanWrapper addResourceToDataset(ResourceBeanWrapper resource, String datasetId, String owner) {
|
||||
|
||||
logger.debug("Incoming request for creating new resource for dataset with id " + datasetId);
|
||||
logger.debug("Owner is " + owner + " and resource is " + resourceBean);
|
||||
|
||||
// of course, if it exists
|
||||
if(exists(resourceBean.getUrl())){
|
||||
logger.debug("Owner is " + owner + " and resource is " + resource);
|
||||
|
||||
try{
|
||||
// retrieve ckan's catalog url
|
||||
String ckanPortalUrl = getCatalogueUrl();
|
||||
|
||||
// retrieve the api key for this user
|
||||
String apiKey = getCKANApikeyFromUser(fromOwnerToCKanOwner(owner));
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
|
||||
CkanResource resource = new CkanResource(ckanPortalUrl, datasetId);
|
||||
resource.setName(resourceBean.getName());
|
||||
ResourceBean resourceBean = new ResourceBean(
|
||||
resource.getUrl(),
|
||||
resource.getName(),
|
||||
resource.getDescription(),
|
||||
null,
|
||||
fromOwnerToCKanOwner(owner),
|
||||
datasetId,
|
||||
null);
|
||||
|
||||
// escape description
|
||||
Source description = new Source(resourceBean.getDescription());
|
||||
Segment htmlSeg = new Segment(description, 0, description.length());
|
||||
Renderer htmlRend = new Renderer(htmlSeg);
|
||||
String resourceId = CKanUtilsFactory.getInstance().
|
||||
getCkanUtilsForScope(currentScope).
|
||||
addResourceToDataset(resourceBean);
|
||||
|
||||
resource.setDescription(htmlRend.toString());
|
||||
resource.setUrl(resourceBean.getUrl());
|
||||
resource.setOwner(fromOwnerToCKanOwner(owner));
|
||||
if(resourceId != null){
|
||||
|
||||
// Checked client
|
||||
CheckedCkanClient client = new CheckedCkanClient(ckanPortalUrl, apiKey);
|
||||
CkanResource createdRes = client.createResource(resource);
|
||||
|
||||
if(createdRes != null){
|
||||
|
||||
logger.debug("Resource " + createdRes.getName() + " is now available");
|
||||
logger.debug("Resource " + resource.getName() + " is now available");
|
||||
|
||||
// set its id and turn it to the client
|
||||
resourceBean.setId(createdRes.getId());
|
||||
return resourceBean;
|
||||
resource.setId(resourceId);
|
||||
return resource;
|
||||
|
||||
}
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Unable to create new resource", e);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("No resource created");
|
||||
|
||||
|
@ -630,29 +464,29 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteResourceFromDataset(ResourceBean resource,
|
||||
public boolean deleteResourceFromDataset(ResourceBeanWrapper resource,
|
||||
String owner) {
|
||||
logger.debug("Request for deleting resource " + resource);
|
||||
|
||||
boolean deleted = false;
|
||||
|
||||
try{
|
||||
String currentScope = ScopeProvider.instance.get();
|
||||
|
||||
// retrieve ckan's catalog url
|
||||
String ckanPortalUrl = getCatalogueUrl();
|
||||
deleted = CKanUtilsFactory.getInstance().
|
||||
getCkanUtilsForScope(currentScope).
|
||||
deleteResourceFromDataset(fromOwnerToCKanOwner(owner), resource.getId());
|
||||
|
||||
// retrieve the api key for this user
|
||||
String apiKey = getCKANApikeyFromUser(fromOwnerToCKanOwner(owner));
|
||||
|
||||
// Checked client
|
||||
CheckedCkanClient client = new CheckedCkanClient(ckanPortalUrl, apiKey);
|
||||
client.deleteResource(resource.getId());
|
||||
|
||||
return true;
|
||||
if(deleted){
|
||||
logger.debug("Resource described by " + resource + " deleted");
|
||||
}else
|
||||
logger.error("Resource described by " + resource + " NOT deleted");
|
||||
|
||||
}catch(Exception e){
|
||||
logger.error("Unable to delete such resource", e);
|
||||
logger.error("Error while trying to delete resource described by " + resource, e);
|
||||
}
|
||||
|
||||
return false;
|
||||
return deleted;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import java.io.Serializable;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A MetaDataProfileBean with its children (MetaDataFormat)
|
||||
* A MetaDataProfileBean with its children (MetaDataType and MetaDataFields)
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
public class MetaDataProfileBean implements Serializable{
|
||||
|
|
|
@ -3,10 +3,10 @@ package org.gcube.portlets.widgets.ckandatapublisherwidget.shared;
|
|||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* A dataset's resource bean.
|
||||
* A dataset's resource bean
|
||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||
*/
|
||||
public class ResourceBean implements Serializable{
|
||||
public class ResourceBeanWrapper implements Serializable{
|
||||
|
||||
private static final long serialVersionUID = -6542455246456049712L;
|
||||
private String url;
|
||||
|
@ -14,7 +14,7 @@ public class ResourceBean implements Serializable{
|
|||
private String description;
|
||||
private String id;
|
||||
|
||||
public ResourceBean(){
|
||||
public ResourceBeanWrapper(){
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class ResourceBean implements Serializable{
|
|||
* @param name
|
||||
* @param description
|
||||
*/
|
||||
public ResourceBean(String url, String name, String description) {
|
||||
public ResourceBeanWrapper(String url, String name, String description) {
|
||||
super();
|
||||
this.url = url;
|
||||
this.name = name;
|
||||
|
@ -36,7 +36,7 @@ public class ResourceBean implements Serializable{
|
|||
* @param description
|
||||
* @param id
|
||||
*/
|
||||
public ResourceBean(String url, String name, String description, String id) {
|
||||
public ResourceBeanWrapper(String url, String name, String description, String id) {
|
||||
super();
|
||||
this.url = url;
|
||||
this.name = name;
|
Loading…
Reference in New Issue