org.bonitasoft.engine.command.RuntimeCommand Maven / Gradle / Ivy
The newest version!
/**
* Copyright (C) 2023 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.command;
import java.io.Serializable;
import java.util.Map;
import org.bonitasoft.engine.service.ServiceAccessor;
/**
* @author Danila Mazour
*/
public abstract class RuntimeCommand implements Command {
@SuppressWarnings("unchecked")
protected T getParameter(final Map parameters, final String parameterName,
final String message)
throws SCommandParameterizationException {
try {
return (T) parameters.get(parameterName);
} catch (final Exception e) {
throw new SCommandParameterizationException(message);
}
}
protected T getParameter(final Map parameters, final String parameterName)
throws SCommandParameterizationException {
return getParameter(parameters, parameterName, "An error occurred while parsing " + parameterName);
}
protected Long getLongMandatoryParameter(final Map parameters, final String field)
throws SCommandParameterizationException {
final String message = "Parameters map must contain an entry " + field + " with a long value.";
final Long mandatoryParameter = getMandatoryParameter(parameters, field, message);
if (mandatoryParameter == 0L) {
throw new SCommandParameterizationException(message);
}
return mandatoryParameter;
}
protected Integer getIntegerMandatoryParameter(final Map parameters, final String field)
throws SCommandParameterizationException {
final String message = "Parameters map must contain an entry " + field + " with a int value.";
return getMandatoryParameter(parameters, field, message);
}
protected String getStringMandatoryParameter(final Map parameters, final String field)
throws SCommandParameterizationException {
final String message = "Parameters map must contain an entry " + field + " with a String value.";
return getMandatoryParameter(parameters, field, message);
}
protected T getMandatoryParameter(final Map parameters, final String field,
final String message)
throws SCommandParameterizationException {
final T value = getParameter(parameters, field, message);
if (value == null) {
throw new SCommandParameterizationException(message);
}
return value;
}
}