2017-12-15 05:04:27 +01:00
|
|
|
package org.gcube.resourcemanagement.manager.webapp.context;
|
|
|
|
|
2017-12-15 05:21:47 +01:00
|
|
|
import java.util.Objects;
|
|
|
|
|
2017-12-15 05:04:27 +01:00
|
|
|
import org.gcube.informationsystem.model.entity.Context;
|
2017-12-17 04:42:14 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2017-12-20 05:12:05 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
|
2018-01-21 21:38:19 +01:00
|
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.LocalCodeException;
|
2018-01-10 05:12:00 +01:00
|
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException;
|
2018-01-18 06:05:35 +01:00
|
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
2017-12-15 05:04:27 +01:00
|
|
|
|
|
|
|
/**
|
2018-01-15 04:55:32 +01:00
|
|
|
* Create request for a new context.
|
2017-12-15 05:04:27 +01:00
|
|
|
*
|
|
|
|
* @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) {
|
2017-12-15 05:21:47 +01:00
|
|
|
Objects.requireNonNull(holder);
|
2017-12-15 05:04:27 +01:00
|
|
|
return new CreateRequest(holder.getContext());
|
|
|
|
}
|
|
|
|
|
2017-12-16 04:29:16 +01:00
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#validate()
|
|
|
|
*/
|
2017-12-15 05:04:27 +01:00
|
|
|
@Override
|
2018-01-21 21:38:19 +01:00
|
|
|
public void validate() throws LocalCodeException {
|
2017-12-16 05:02:32 +01:00
|
|
|
if (Objects.nonNull(this.context.getParent())) {
|
2018-01-23 05:35:19 +01:00
|
|
|
try {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2018-01-21 21:38:19 +01:00
|
|
|
throw new LocalCodeException(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST);
|
|
|
|
}
|
2017-12-16 05:02:32 +01:00
|
|
|
}
|
2017-12-16 04:29:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see org.gcube.resourcemanagement.manager.webapp.context.RequestToResourceRegistry#send()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected ResponseFromResourceRegistry send() {
|
2017-12-17 04:42:14 +01:00
|
|
|
try {
|
2018-01-21 21:38:19 +01:00
|
|
|
Context created = this.getContextClient().create(context);
|
2017-12-17 04:42:14 +01:00
|
|
|
if (Objects.nonNull(created)) {
|
|
|
|
|
2017-12-20 05:12:05 +01:00
|
|
|
} else {
|
|
|
|
return ResponseFromResourceRegistry.newFailureResponse("Invalid response from the RR (null?)");
|
2017-12-17 04:42:14 +01:00
|
|
|
}
|
2017-12-20 05:12:05 +01:00
|
|
|
} catch (ContextAlreadyPresentException cape) {
|
2018-01-18 06:05:35 +01:00
|
|
|
return ResponseFromResourceRegistry.fromException(new WebCodeException(RMCreateContextCode.CONTEXT_ALREADY_EXISTS));
|
2017-12-17 04:42:14 +01:00
|
|
|
} catch (ResourceRegistryException e) {
|
2018-01-18 06:05:35 +01:00
|
|
|
return ResponseFromResourceRegistry.fromException(new WebCodeException(RMCreateContextCode.GENERIC_ERROR_FROM_RR));
|
2017-12-17 04:42:14 +01:00
|
|
|
}
|
2017-12-16 05:02:32 +01:00
|
|
|
return ResponseFromResourceRegistry.newSuccessResponseWithMessage("Context successfully created.");
|
2017-12-15 05:04:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|