use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class RegistryPersistenceUtilTestCase method testcreateAPIProductArtifactContent.
@Test
public void testcreateAPIProductArtifactContent() throws APIPersistenceException, APIManagementException, RegistryException {
APIProduct product = new APIProduct(new APIProductIdentifier("pubuser", "TestAPIProd", "1.0.0"));
GenericArtifact genericArtifact = new GenericArtifactImpl(new QName("", "TestAPIProd", ""), "application/vnd.wso2-api+xml");
List<APICategory> categories = new ArrayList<APICategory>();
APICategory category = new APICategory();
category.setName("testcategory");
categories.add(category);
product.setApiCategories(categories);
Set<Tier> availableTiers = new HashSet<Tier>();
availableTiers.add(new Tier("Unlimited"));
availableTiers.add(new Tier("Gold"));
product.setAvailableTiers(availableTiers);
GenericArtifact retArtifact = RegistryPersistenceUtil.createAPIProductArtifactContent(genericArtifact, product);
Assert.assertEquals("API name does not match", product.getId().getName(), retArtifact.getAttribute("overview_name"));
Assert.assertEquals("API version does not match", product.getId().getVersion(), retArtifact.getAttribute("overview_version"));
Assert.assertEquals("API provider does not match", product.getId().getProviderName(), retArtifact.getAttribute("overview_provider"));
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method deleteAPIProductRevision.
/**
* Delete API Product revision database records
*
* @param apiRevision content of the revision
* @throws APIManagementException if an error occurs when restoring an API revision
*/
public void deleteAPIProductRevision(APIRevision apiRevision) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
// Retrieve API ID
APIProductIdentifier apiProductIdentifier = APIUtil.getAPIProductIdentifierFromUUID(apiRevision.getApiUUID());
int apiId = getAPIID(apiRevision.getApiUUID(), connection);
int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()));
// Removing related revision entries from AM_REVISION table
PreparedStatement removeAMRevisionStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.DELETE_API_REVISION);
removeAMRevisionStatement.setString(1, apiRevision.getRevisionUUID());
removeAMRevisionStatement.executeUpdate();
// Removing related revision entries from AM_API_PRODUCT_MAPPING table
PreparedStatement removeProductMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_PRODUCT_MAPPING_BY_REVISION_UUID);
removeProductMappingsStatement.setInt(1, apiId);
removeProductMappingsStatement.setString(2, apiRevision.getRevisionUUID());
removeProductMappingsStatement.executeUpdate();
// Removing related revision entries from AM_API_URL_MAPPING table
PreparedStatement removeURLMappingsStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_PRODUCT_REVISION_ENTRIES_IN_AM_API_URL_MAPPING_BY_REVISION_UUID);
removeURLMappingsStatement.setString(1, apiRevision.getRevisionUUID());
removeURLMappingsStatement.executeUpdate();
// Removing related revision entries from AM_API_CLIENT_CERTIFICATE table
PreparedStatement removeClientCertificatesStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_API_CLIENT_CERTIFICATE_BY_REVISION_UUID);
removeClientCertificatesStatement.setInt(1, apiId);
removeClientCertificatesStatement.setString(2, apiRevision.getRevisionUUID());
removeClientCertificatesStatement.executeUpdate();
// Removing related revision entries from AM_GRAPHQL_COMPLEXITY table
PreparedStatement removeGraphQLComplexityStatement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_REVISION_ENTRIES_IN_AM_GRAPHQL_COMPLEXITY_BY_REVISION_UUID);
removeGraphQLComplexityStatement.setInt(1, apiId);
removeGraphQLComplexityStatement.setString(2, apiRevision.getRevisionUUID());
removeGraphQLComplexityStatement.executeUpdate();
// Removing related revision entries for operation policies
deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
connection.commit();
} catch (SQLException e) {
connection.rollback();
handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
} catch (SQLException e) {
handleException("Failed to delete API Revision entry of API Product UUID " + apiRevision.getApiUUID(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method setAssociatedAPIProducts.
private void setAssociatedAPIProducts(String uuid, Map<Integer, URITemplate> uriTemplates) throws SQLException {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SQLConstants.GET_API_PRODUCT_URI_TEMPLATE_ASSOCIATION_SQL)) {
ps.setString(1, uuid);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
String productName = rs.getString("API_NAME");
String productVersion = rs.getString("API_VERSION");
String productProvider = rs.getString("API_PROVIDER");
int uriTemplateId = rs.getInt("URL_MAPPING_ID");
URITemplate uriTemplate = uriTemplates.get(uriTemplateId);
if (uriTemplate != null) {
APIProductIdentifier productIdentifier = new APIProductIdentifier(productProvider, productName, productVersion);
uriTemplate.addUsedByProduct(productIdentifier);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method setAPIProductFromDB.
/**
* Get product Id from the product name and the provider.
*
* @param product product identifier
* @throws APIManagementException exception
*/
public void setAPIProductFromDB(APIProduct product) throws APIManagementException {
APIProductIdentifier apiProductIdentifier = product.getId();
String currentApiUuid;
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(product.getUuid());
if (apiRevision != null && apiRevision.getApiUUID() != null) {
currentApiUuid = apiRevision.getApiUUID();
} else {
currentApiUuid = product.getUuid();
}
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_API_PRODUCT_SQL)) {
prepStmt.setString(1, currentApiUuid);
try (ResultSet rs = prepStmt.executeQuery()) {
if (rs.next()) {
product.setProductId(rs.getInt("API_ID"));
product.setProductLevelPolicy(rs.getString("API_TIER"));
} else {
String msg = "Unable to find the API Product : " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " in the database";
throw new APIMgtResourceNotFoundException(msg);
}
}
} catch (SQLException e) {
handleException("Error while locating API Product: " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " from the database", e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAPIProductIdentifierFromUUID.
/**
* Get API Product Identifier by the product's UUID.
*
* @param uuid uuid of the API
* @return API Identifier
* @throws APIManagementException if an error occurs
*/
public APIProductIdentifier getAPIProductIdentifierFromUUID(String uuid) throws APIManagementException {
APIProductIdentifier identifier = null;
String sql = SQLConstants.GET_API_IDENTIFIER_BY_UUID_SQL;
try (Connection connection = APIMgtDBUtil.getConnection()) {
PreparedStatement prepStmt = connection.prepareStatement(sql);
prepStmt.setString(1, uuid);
try (ResultSet resultSet = prepStmt.executeQuery()) {
while (resultSet.next()) {
String provider = resultSet.getString(1);
String name = resultSet.getString(2);
String version = resultSet.getString(3);
identifier = new APIProductIdentifier(APIUtil.replaceEmailDomain(provider), name, version, uuid);
}
}
} catch (SQLException e) {
handleException("Failed to retrieve the API Product Identifier details for UUID : " + uuid, e);
}
return identifier;
}
Aggregations