cs-json/src/main/java/io/cloudslang/content/json/actions/RemoveArrayEntryAction.java
/*
* Copyright 2021-2024 Open Text
* This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cloudslang.content.json.actions;
import com.hp.oo.sdk.content.annotations.Action;
import com.hp.oo.sdk.content.annotations.Output;
import com.hp.oo.sdk.content.annotations.Param;
import com.hp.oo.sdk.content.annotations.Response;
import com.hp.oo.sdk.content.plugin.ActionMetadata.MatchType;
import com.hp.oo.sdk.content.plugin.ActionMetadata.ResponseType;
import io.cloudslang.content.constants.OutputNames;
import io.cloudslang.content.constants.ResponseNames;
import io.cloudslang.content.constants.ReturnCodes;
import io.cloudslang.content.json.entities.RemoveArrayEntryInput;
import io.cloudslang.content.json.services.RemoveArrayEntryService;
import io.cloudslang.content.json.utils.Constants;
import io.cloudslang.content.utils.OutputUtilities;
import java.util.Map;
public class RemoveArrayEntryAction {
private final RemoveArrayEntryService service = new RemoveArrayEntryService();
/**
* Removes an element from a JSON array.
* All elements from the right of the element which is removed will be shifted one position to the left.
*
* @param array String representation of a JSON array. Arrays in JSON are comma separated lists of objects, enclosed in square brackets ( [ ] ).
* A normal OO list is NOT a JSON array. Examples: [{"one":1, "two":2}, 3, "four"]
* @param index The index of the element to remove from the array. The array index starts from 0 (the first item in the array).
* If the value is negative then it will remove the item starting from right to left (-1 is the index of the last item in the array).
* Valid values: -n, -n+1, ..., -1, 0, 1, ..., n-1 (for an array with n elements)
* @return a map containing the output of the operations. Keys present in the map are:
* <br><b>returnResult</b> - This is the primary output and contains the new JSON array with the requested element removed.
* If the operation failed, this field will contain an error message.
* <br><b>returnCode</b> - The returnCode of the operation: 0 for success, -1 for failure.
* <br><b>exception</b> - The exception message if the operation goes to failure.
*/
@Action(name = "Remove Array Entry",
outputs = {
@Output(OutputNames.RETURN_CODE),
@Output(OutputNames.RETURN_RESULT),
@Output(OutputNames.EXCEPTION)
},
responses = {
@Response(text = ResponseNames.SUCCESS, field = OutputNames.RETURN_CODE, value = ReturnCodes.SUCCESS,
matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.RESOLVED),
@Response(text = ResponseNames.FAILURE, field = OutputNames.RETURN_CODE, value = ReturnCodes.FAILURE,
matchType = MatchType.COMPARE_EQUAL, responseType = ResponseType.ERROR, isOnFail = true)
})
public Map<String, String> execute(
@Param(value = Constants.InputNames.ARRAY, required = true) String array,
@Param(value = Constants.InputNames.INDEX, required = true) String index) {
try {
RemoveArrayEntryInput input = new RemoveArrayEntryInput.Builder()
.array(array)
.index(index)
.build();
return this.service.execute(input);
} catch (Exception ex) {
return OutputUtilities.getFailureResultsMap(ex);
}
}
}