use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIProviderImpl method changeAPILCCheckListItems.
@Override
public boolean changeAPILCCheckListItems(APIIdentifier apiIdentifier, int checkItem, boolean checkItemValue) throws APIManagementException {
String providerTenantMode = apiIdentifier.getProviderName();
boolean success = false;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
GenericArtifact apiArtifact = getAPIArtifact(apiIdentifier);
String status = null;
try {
if (apiArtifact != null) {
if (checkItemValue && !apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
apiArtifact.checkLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
} else if (!checkItemValue && apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
apiArtifact.uncheckLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
}
success = true;
}
} catch (GovernanceException e) {
handleException("Error while setting registry lifecycle checklist items for the API: " + apiIdentifier.getApiName(), e);
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return success;
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPILifeCycleData.
@Override
public /*
* This method returns the lifecycle data for an API including current state,next states.
*
* @param apiId APIIdentifier
* @return Map<String,Object> a map with lifecycle data
*/
Map<String, Object> getAPILifeCycleData(APIIdentifier apiId) throws APIManagementException {
String path = APIUtil.getAPIPath(apiId);
Map<String, Object> lcData = new HashMap<String, Object>();
String providerTenantMode = apiId.getProviderName();
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
Resource apiSourceArtifact = registry.get(path);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when getting lifecycle data for API " + apiId;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
// Get all the actions corresponding to current state of the api artifact
String[] actions = artifact.getAllLifecycleActions(APIConstants.API_LIFE_CYCLE);
// Put next states into map
lcData.put(APIConstants.LC_NEXT_STATES, actions);
String lifeCycleState = artifact.getLifecycleState();
lcData.put(APIConstants.LC_STATUS, lifeCycleState);
LifecycleBean bean;
bean = LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) registry, configRegistry);
if (bean != null) {
ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
ArrayList<String> permissionList = new ArrayList<String>();
// Get lc properties
Property[] lifecycleProps = bean.getLifecycleProperties();
// Get roles of the current session holder
String[] roleNames = bean.getRolesOfUser();
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
// Check for permission properties if any exists
if (propValues != null && propValues.length != 0) {
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
for (String role : roleNames) {
for (String propValue : propValues) {
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX, "");
if (propValue.equals(role)) {
permissionList.add(key);
} else if (propValue.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propValue.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX)) {
permissionList.add(key);
}
}
}
}
}
}
// Check for lifecycle checklist item properties defined
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
if (propValues != null && propValues.length != 0) {
CheckListItem checkListItem = new CheckListItem();
checkListItem.setVisible("false");
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_ITEM_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
if (propValues.length > 2) {
for (String param : propValues) {
if (param.startsWith(APIConstants.LC_STATUS)) {
checkListItem.setLifeCycleStatus(param.substring(7));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_NAME)) {
checkListItem.setName(param.substring(5));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_VALUE)) {
checkListItem.setValue(param.substring(6));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_ORDER)) {
checkListItem.setOrder(param.substring(6));
}
}
}
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_ITEM_SUFFIX, "");
if (permissionList.contains(key)) {
// Set visible to true if the checklist item permits
checkListItem.setVisible("true");
}
}
if (checkListItem.matchLifeCycleStatus(lifeCycleState)) {
checkListItems.add(checkListItem);
}
}
}
lcData.put("items", checkListItems);
}
} catch (Exception e) {
handleException(e.getMessage(), e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return lcData;
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIProviderImpl method getApiOrApiProductLifecycleData.
private Map<String, Object> getApiOrApiProductLifecycleData(String status) throws APIManagementException {
Map<String, Object> lcData = new HashMap<String, Object>();
List<String> actionsList;
try {
actionsList = LCManagerFactory.getInstance().getLCManager().getAllowedActionsForState(status);
if (actionsList != null) {
String[] actionsArray = new String[actionsList.size()];
actionsArray = actionsList.toArray(actionsArray);
lcData.put(APIConstants.LC_NEXT_STATES, actionsArray);
}
ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
List<String> checklistItemsList = LCManagerFactory.getInstance().getLCManager().getCheckListItemsForState(status);
if (checklistItemsList != null) {
for (String name : checklistItemsList) {
CheckListItem item = new CheckListItem();
item.setName(name);
item.setValue("false");
checkListItems.add(item);
}
}
lcData.put("items", checkListItems);
} catch (PersistenceException e) {
throw new APIManagementException("Error while parsing the lifecycle ", e);
}
// First letter capital
status = status.substring(0, 1).toUpperCase() + status.substring(1).toLowerCase();
lcData.put(APIConstants.LC_STATUS, status);
return lcData;
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIProviderImpl method setAPIStateWorkflowDTOParameters.
/**
* Set API or API Product state change workflow parameters
*
* @param currentStatus Current state of the API or API Product
* @param action LC state change action
* @param name Name of the API or API Product
* @param context Context of the API or API Product
* @param apiType API or API Product
* @param version Version of API or API Product
* @param providerName Owner of the API or API Product
* @param apiOrApiProductId Unique ID of the API or API Product
* @param uuid Unique UUID of the API or API Product
* @param gatewayVendor Gateway Vendor
* @param workflowType Workflow Type
* @param apiStateWFExecutor WorkflowExecutor
* @return APIStateWorkflowDTO Object
*/
private APIStateWorkflowDTO setAPIStateWorkflowDTOParameters(String currentStatus, String action, String name, String context, String apiType, String version, String providerName, int apiOrApiProductId, String uuid, String gatewayVendor, String workflowType, WorkflowExecutor apiStateWFExecutor) {
WorkflowProperties workflowProperties = getAPIManagerConfiguration().getWorkflowProperties();
APIStateWorkflowDTO stateWorkflowDTO = new APIStateWorkflowDTO();
stateWorkflowDTO.setApiCurrentState(currentStatus);
stateWorkflowDTO.setApiLCAction(action);
stateWorkflowDTO.setApiName(name);
stateWorkflowDTO.setApiContext(context);
stateWorkflowDTO.setApiType(apiType);
stateWorkflowDTO.setApiVersion(version);
stateWorkflowDTO.setApiProvider(providerName);
stateWorkflowDTO.setGatewayVendor(gatewayVendor);
stateWorkflowDTO.setCallbackUrl(workflowProperties.getWorkflowCallbackAPI());
stateWorkflowDTO.setExternalWorkflowReference(apiStateWFExecutor.generateUUID());
stateWorkflowDTO.setTenantId(tenantId);
stateWorkflowDTO.setTenantDomain(this.tenantDomain);
stateWorkflowDTO.setWorkflowType(workflowType);
stateWorkflowDTO.setStatus(WorkflowStatus.CREATED);
stateWorkflowDTO.setCreatedTime(System.currentTimeMillis());
stateWorkflowDTO.setWorkflowReference(Integer.toString(apiOrApiProductId));
stateWorkflowDTO.setInvoker(this.username);
stateWorkflowDTO.setApiUUID(uuid);
String workflowDescription = "Pending lifecycle state change action: " + action;
stateWorkflowDTO.setWorkflowDescription(workflowDescription);
return stateWorkflowDTO;
}
use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPILifeCycleStatus.
@Override
public String getAPILifeCycleStatus(APIIdentifier apiIdentifier) throws APIManagementException {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (apiArtifact == null) {
String errorMessage = "API artifact is null when retrieving lifecycle status of API " + apiIdentifier.getApiName();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
return apiArtifact.getLifecycleState();
} catch (GovernanceException e) {
handleException("Failed to get the life cycle status : " + e.getMessage(), e);
return null;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
Aggregations