use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycleTransition in project carbon-apimgt by wso2.
the class ImportUtils method getLifeCycleAction.
/**
* This method returns the lifecycle action which can be used to transit from currentStatus to targetStatus.
*
* @param tenantDomain Tenant domain
* @param currentStatus Current status to do status transition
* @param targetStatus Target status to do status transition
* @return Lifecycle action or null if target is not reachable
* @throws APIImportExportException If getting lifecycle action failed
*/
public static String getLifeCycleAction(String tenantDomain, String currentStatus, String targetStatus, APIProvider provider) throws APIManagementException {
// No need to change the lifecycle if both the statuses are same
if (StringUtils.equalsIgnoreCase(currentStatus, targetStatus)) {
return null;
}
LifeCycle lifeCycle = new LifeCycle();
// Parse DOM of APILifeCycle
try {
String data = provider.getLifecycleConfiguration(tenantDomain);
DocumentBuilderFactory factory = APIUtil.getSecuredDocumentBuilder();
DocumentBuilder builder = factory.newDocumentBuilder();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
Document doc = builder.parse(inputStream);
Element root = doc.getDocumentElement();
// Get all nodes with state
NodeList states = root.getElementsByTagName("state");
int nStates = states.getLength();
for (int i = 0; i < nStates; i++) {
Node node = states.item(i);
Node id = node.getAttributes().getNamedItem("id");
if (id != null && !id.getNodeValue().isEmpty()) {
LifeCycleTransition lifeCycleTransition = new LifeCycleTransition();
NodeList transitions = node.getChildNodes();
int nTransitions = transitions.getLength();
for (int j = 0; j < nTransitions; j++) {
Node transition = transitions.item(j);
// Add transitions
if (ImportExportConstants.NODE_TRANSITION.equals(transition.getNodeName())) {
Node target = transition.getAttributes().getNamedItem("target");
Node action = transition.getAttributes().getNamedItem("event");
if (target != null && action != null) {
lifeCycleTransition.addTransition(target.getNodeValue().toLowerCase(), action.getNodeValue());
}
}
}
lifeCycle.addLifeCycleState(id.getNodeValue().toLowerCase(), lifeCycleTransition);
}
}
} catch (ParserConfigurationException | SAXException e) {
throw new APIManagementException("Error parsing APILifeCycle for tenant: " + tenantDomain, e);
} catch (UnsupportedEncodingException e) {
throw new APIManagementException("Error parsing unsupported encoding for APILifeCycle in tenant: " + tenantDomain, e);
} catch (IOException e) {
throw new APIManagementException("Error reading APILifeCycle for tenant: " + tenantDomain, e);
}
// Retrieve lifecycle action
LifeCycleTransition transition = lifeCycle.getTransition(currentStatus.toLowerCase());
if (transition != null) {
return transition.getAction(targetStatus.toLowerCase());
}
return null;
}
Aggregations