You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB
Java

package org.gcube.resourcemanagement.manager.webapp.context;
import java.util.Objects;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
import org.gcube.resourcemanagement.manager.io.exceptions.CodeException;
import org.gcube.resourcemanagement.manager.io.exceptions.RMCode;
/**
* Actions that can be performed on the context.
*
* @author Manuele Simi (ISTI-CNR)
*
*/
public final class CreateRequest extends RequestToResourceRegistry {
private Context context;
private CreateRequest(Context context) {
this.context = context;
}
public static CreateRequest fromHolder(ContextHolder holder) {
Objects.requireNonNull(holder);
return new CreateRequest(holder.getContext());
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#validate()
*/
@Override
public CreateRequest validate() throws Exception {
//TODO: to implement
if (Objects.nonNull(this.context.getParent())) {
throw new Exception("Unable to manage a context with parent.");
}
return this;
}
/* (non-Javadoc)
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#send()
*/
@Override
protected ResponseFromResourceRegistry send() {
try {
Context created = resourceRegistryContextClient.create(context);
if (Objects.nonNull(created)) {
} else {
return ResponseFromResourceRegistry.newFailureResponse("Invalid response from the RR (null?)");
}
} catch (ContextAlreadyPresentException cape) {
return ResponseFromResourceRegistry.fromException(new CodeException(RMCode.CONTEXT_ALREADY_EXIST));
} catch (ResourceRegistryException e) {
return ResponseFromResourceRegistry.fromException(new CodeException(RMCode.GENERIC_ERROR_FROM_RR));
}
return ResponseFromResourceRegistry.newSuccessResponseWithMessage("Context successfully created.");
}
}