65 lines
2.3 KiB
Java
65 lines
2.3 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.LocalCodeException;
|
|
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 void validate() throws LocalCodeException {
|
|
if (Objects.nonNull(this.context.getParent())) {
|
|
Context parent = new Queries().fetchContext(this.context.getParent().getHeader().getUUID(), this.getContextClient());
|
|
if (Objects.isNull(parent)) {
|
|
throw new LocalCodeException(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* (non-Javadoc)
|
|
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#send()
|
|
*/
|
|
@Override
|
|
protected ResponseFromResourceRegistry send() {
|
|
try {
|
|
Context created = this.getContextClient().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.");
|
|
}
|
|
|
|
}
|