Search in sources :

Example 1 with DocumentSearchResult

use of org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult in project carbon-apimgt by wso2.

the class AbstractAPIManager method getAllDocumentation.

public List<Documentation> getAllDocumentation(String uuid, String organization) throws APIManagementException {
    String username = CarbonContext.getThreadLocalCarbonContext().getUsername();
    Organization org = new Organization(organization);
    UserContext ctx = new UserContext(username, org, null, null);
    List<Documentation> convertedList = null;
    try {
        DocumentSearchResult list = apiPersistenceInstance.searchDocumentation(org, uuid, 0, 0, null, ctx);
        if (list != null) {
            convertedList = new ArrayList<Documentation>();
            List<org.wso2.carbon.apimgt.persistence.dto.Documentation> docList = list.getDocumentationList();
            if (docList != null) {
                for (int i = 0; i < docList.size(); i++) {
                    convertedList.add(DocumentMapper.INSTANCE.toDocumentation(docList.get(i)));
                }
            }
        } else {
            convertedList = new ArrayList<Documentation>();
        }
    } catch (DocumentationPersistenceException e) {
        String msg = "Failed to get documentations for api/product " + uuid;
        throw new APIManagementException(msg, e);
    }
    return convertedList;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 2 with DocumentSearchResult

use of org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchDocumentation.

@Override
public DocumentSearchResult searchDocumentation(Organization org, String apiId, int start, int offset, String searchQuery, UserContext ctx) throws DocumentationPersistenceException {
    DocumentSearchResult result = null;
    Registry registryType;
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(requestedTenantDomain);
        registryType = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registryType, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String apiPath = GovernanceUtils.getArtifactPath(registryType, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String apiOrAPIProductDocPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
        String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
        String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
        if (registryType.resourceExists(apiOrAPIProductDocPath)) {
            List<Documentation> documentationList = new ArrayList<Documentation>();
            Resource resource = registryType.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
                for (String docPath : docsPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registryType.get(docPath);
                        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
                        if (searchQuery != null) {
                            if (searchQuery.toLowerCase().startsWith("name:")) {
                                String requestedDocName = searchQuery.split(":")[1];
                                if (doc.getName().equalsIgnoreCase(requestedDocName)) {
                                    documentationList.add(doc);
                                }
                            } else {
                                log.warn("Document search not implemented for the query " + searchQuery);
                            }
                        } else {
                            documentationList.add(doc);
                        }
                    }
                }
            }
            result = new DocumentSearchResult();
            result.setDocumentationList(documentationList);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get documentations for api/product " + apiId;
        throw new DocumentationPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) Documentation(org.wso2.carbon.apimgt.persistence.dto.Documentation) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) Collection(org.wso2.carbon.registry.core.Collection)

Example 3 with DocumentSearchResult

use of org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult in project carbon-apimgt by wso2.

the class APIProviderImpl method isDocumentationExist.

@Override
public boolean isDocumentationExist(String uuid, String docName, String organization) throws APIManagementException {
    boolean exist = false;
    UserContext ctx = null;
    try {
        DocumentSearchResult result = apiPersistenceInstance.searchDocumentation(new Organization(organization), uuid, 0, 0, "name:" + docName, ctx);
        if (result != null && result.getDocumentationList() != null && !result.getDocumentationList().isEmpty()) {
            String returnDocName = result.getDocumentationList().get(0).getName();
            if (returnDocName != null && returnDocName.equals(docName)) {
                exist = true;
            }
        }
    } catch (DocumentationPersistenceException e) {
        handleException("Failed to search documentation for name " + docName, e);
    }
    return exist;
}
Also used : DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)

Aggregations

DocumentSearchResult (org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult)3 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)3 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)2 UserContext (org.wso2.carbon.apimgt.persistence.dto.UserContext)2 ArrayList (java.util.ArrayList)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)1 Documentation (org.wso2.carbon.apimgt.persistence.dto.Documentation)1 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)1 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)1 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)1 Collection (org.wso2.carbon.registry.core.Collection)1 Registry (org.wso2.carbon.registry.core.Registry)1 Resource (org.wso2.carbon.registry.core.Resource)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)1