Search in sources :

Example 6 with APIProductIdentifier

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"));
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Tier(org.wso2.carbon.apimgt.api.model.Tier) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with APIProductIdentifier

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);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 8 with APIProductIdentifier

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);
                }
            }
        }
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) PreparedStatement(java.sql.PreparedStatement)

Example 9 with APIProductIdentifier

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);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 10 with APIProductIdentifier

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;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)48 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)33 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)22 PreparedStatement (java.sql.PreparedStatement)19 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)19 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)19 Connection (java.sql.Connection)18 ArrayList (java.util.ArrayList)18 ResultSet (java.sql.ResultSet)17 SQLException (java.sql.SQLException)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 API (org.wso2.carbon.apimgt.api.model.API)14 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)14 HashSet (java.util.HashSet)13 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)13 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)13 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)13 Tier (org.wso2.carbon.apimgt.api.model.Tier)12 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)12