63 lines
2.1 KiB
Java
63 lines
2.1 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.codeexceptions.WebCodeException;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
|
|
|
/**
|
|
* Create request for a new 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 WebCodeException(RMCreateContextCode.CONTEXT_ALREADY_EXISTS));
|
|
} catch (ResourceRegistryException e) {
|
|
return ResponseFromResourceRegistry.fromException(new WebCodeException(RMCreateContextCode.GENERIC_ERROR_FROM_RR));
|
|
}
|
|
return ResponseFromResourceRegistry.newSuccessResponseWithMessage("Context successfully created.");
|
|
}
|
|
|
|
}
|