2021-01-04 19:55:29 +01:00
|
|
|
package org.gcube.vremanagement.contextmanager.operators.resources;
|
2020-12-29 20:33:20 +01:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidContextException;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.exceptions.InvalidParameterException;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.exceptions.OperationException;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.operators.context.CustomContextOperator;
|
2021-01-04 19:55:29 +01:00
|
|
|
import org.gcube.vremanagement.contextmanager.model.operators.parameters.AddResourceParameters;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.operators.parameters.OperatorParameters;
|
2020-12-29 20:33:20 +01:00
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.Context;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.Context.Type;
|
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.ContextRetriever;
|
2021-01-04 19:55:29 +01:00
|
|
|
import org.gcube.vremanagement.contextmanager.model.types.ScopedResource;
|
2020-12-29 20:33:20 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
2021-01-04 19:55:29 +01:00
|
|
|
public class AddResourceOperator extends CustomContextOperator<ScopedResource, AddResourceParameters> {
|
2020-12-29 20:33:20 +01:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
private Logger log;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
private ResourceManager resourceManager;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<Type> getAllowedContextType() {
|
|
|
|
return new HashSet<>(Arrays.asList(Type.values()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getOperationId() {
|
|
|
|
return "add-resource";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return "Adds resource to context";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-04 19:55:29 +01:00
|
|
|
public ScopedResource execute(Context context, AddResourceParameters params) throws OperationException {
|
|
|
|
log.debug("executing operation {}",this.getOperationId());
|
2020-12-29 20:33:20 +01:00
|
|
|
try {
|
2021-01-04 19:55:29 +01:00
|
|
|
ScopedResource res = resourceManager.addResourceToContext(context, params.getResourceId());
|
2020-12-29 20:33:20 +01:00
|
|
|
return res;
|
|
|
|
}catch (InvalidContextException e) {
|
|
|
|
//it should never happen
|
|
|
|
throw new InvalidParameterException("unknown exception");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ScopedResource undo(Context context, OperatorParameters params) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-04 19:55:29 +01:00
|
|
|
protected AddResourceParameters checkAndTrasformParameters(Context context, OperatorParameters params)
|
2020-12-29 20:33:20 +01:00
|
|
|
throws InvalidParameterException {
|
2021-01-04 19:55:29 +01:00
|
|
|
AddResourceParameters addParams = AddResourceParameters.class.cast(params);
|
2020-12-29 20:33:20 +01:00
|
|
|
if (addParams.getResourceId()==null)
|
|
|
|
throw new InvalidParameterException("resourceId is null");
|
|
|
|
return addParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|