Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / DelegateExecutionImpl.java
1 /*-
2 * ============LICENSE_START=======================================================
3 * ONAP - SO
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10
11  *      http://www.apache.org/licenses/LICENSE-2.0
12
13  * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21 package org.onap.so.bpmn.common;
22
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.onap.so.bpmn.common.exceptions.MalformedBuildingBlockInputException;
28 import org.onap.so.bpmn.common.exceptions.MissingBuildingBlockInputException;
29 import org.onap.so.bpmn.common.exceptions.RequiredExecutionVariableExeception;
30 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34
35 public class DelegateExecutionImpl implements BuildingBlockExecution, Serializable {
36
37     private static final long serialVersionUID = 5559067662634919395L;
38
39     @JsonProperty
40     private final Map<String, Serializable> seedMap;
41
42     private transient DelegateExecution execution;
43     private static final String INVALID_INPUT_MISSING = "Expected variable of \"%s\" not found in execution";
44
45     private static final String MISSING_MSG =
46             "Execution variable \"gBBInput\" cannot be null when executing building blocks";
47     private static final String MALFORMED_MSG =
48             "Execution variable \"gBBInput\" must contain an element of type GeneralBuildingBlock";
49
50     public DelegateExecutionImpl(final Map<String, Serializable> seedMap) {
51         this.seedMap = seedMap;
52     }
53
54     public DelegateExecutionImpl(final DelegateExecution execution) {
55         this.seedMap = new HashMap<>();
56         execution.getVariables().forEach((key, value) -> {
57             if (value instanceof Serializable) {
58                 seedMap.put(key, (Serializable) value);
59             }
60         });
61         /* must occur for junit tests to work */
62         this.execution = execution;
63     }
64
65     @JsonIgnore
66     @Override
67     public GeneralBuildingBlock getGeneralBuildingBlock() {
68         try {
69             final GeneralBuildingBlock generalBuildingBlock = (GeneralBuildingBlock) execution.getVariable("gBBInput");
70
71             if (generalBuildingBlock == null) {
72                 throw new MissingBuildingBlockInputException(MISSING_MSG);
73             }
74
75             return generalBuildingBlock;
76         } catch (final ClassCastException e) {
77             throw new MalformedBuildingBlockInputException(MALFORMED_MSG, e);
78         }
79     }
80
81     @Override
82     public <T> T getVariable(final String key) {
83         return this.get(key);
84     }
85
86     @Override
87     public <T> T getRequiredVariable(final String key) throws RequiredExecutionVariableExeception {
88         final T result;
89
90         result = this.get(key);
91         if (result == null) {
92             throw new RequiredExecutionVariableExeception(String.format(INVALID_INPUT_MISSING, key));
93
94         }
95         return result;
96     }
97
98     @Override
99     public void setVariable(final String key, final Serializable value) {
100         this.execution.setVariable(key, value);
101     }
102
103     @JsonIgnore
104     @Override
105     public Map<ResourceKey, String> getLookupMap() {
106         return this.get("lookupKeyMap");
107     }
108
109     @JsonIgnore
110     @Override
111     public String getFlowToBeCalled() {
112         return this.get("flowToBeCalled");
113     }
114
115     @JsonIgnore
116     public DelegateExecution getDelegateExecution() {
117         return this.execution;
118     }
119
120     public void setDelegateExecution(final DelegateExecution execution) {
121         this.execution = execution;
122         this.seedMap.forEach((key, value) -> {
123             if (!execution.hasVariable(key)) {
124                 execution.setVariable(key, value);
125             }
126         });
127     }
128
129     @SuppressWarnings("unchecked")
130     protected <T> T get(final String key) {
131         final Object value = this.execution.getVariable(key);
132         return (T) value;
133     }
134
135 }