Search in sources :

Example 11 with ContentTypeImpl

use of ddf.catalog.data.impl.ContentTypeImpl in project ddf by codice.

the class AbstractCswSource method addContentTypes.

/**
     * Searches every query response for previously unknown content types
     *
     * @param response A Query Response
     */
private void addContentTypes(SourceResponse response) {
    if (response == null || response.getResults() == null) {
        return;
    }
    for (Result result : response.getResults()) {
        Metacard metacard = result.getMetacard();
        if (metacard != null) {
            final String name = metacard.getContentTypeName();
            String version = metacard.getContentTypeVersion();
            final URI namespace = metacard.getContentTypeNamespace();
            if (!StringUtils.isEmpty(name) && !contentTypes.containsKey(name)) {
                if (version == null) {
                    version = "";
                }
                contentTypes.put(name, new ContentTypeImpl(name, version, namespace));
            }
        }
    }
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) Metacard(ddf.catalog.data.Metacard) URI(java.net.URI) Result(ddf.catalog.data.Result)

Example 12 with ContentTypeImpl

use of ddf.catalog.data.impl.ContentTypeImpl in project ddf by codice.

the class SolrMetacardClientImpl method getContentTypes.

@Override
public Set<ContentType> getContentTypes() {
    Set<ContentType> finalSet = new HashSet<>();
    String contentTypeField = resolver.getField(Metacard.CONTENT_TYPE, AttributeType.AttributeFormat.STRING, true);
    String contentTypeVersionField = resolver.getField(Metacard.CONTENT_TYPE_VERSION, AttributeType.AttributeFormat.STRING, true);
    /*
         * If we didn't find the field, it most likely means it does not exist. If it does not
         * exist, then we can safely say that no content types are in this catalog provider
         */
    if (contentTypeField == null || contentTypeVersionField == null) {
        return finalSet;
    }
    SolrQuery query = new SolrQuery(contentTypeField + ":[* TO *]");
    query.setFacet(true);
    query.addFacetField(contentTypeField);
    query.addFacetPivotField(contentTypeField + "," + contentTypeVersionField);
    try {
        QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
        List<FacetField> facetFields = solrResponse.getFacetFields();
        for (Map.Entry<String, List<PivotField>> entry : solrResponse.getFacetPivot()) {
            // however, the content type names can still be obtained via the facet fields.
            if (CollectionUtils.isEmpty(entry.getValue())) {
                LOGGER.debug("No content type versions found associated with any available content types.");
                if (CollectionUtils.isNotEmpty(facetFields)) {
                    // values (content type names).
                    for (FacetField.Count currContentType : facetFields.get(0).getValues()) {
                        // unknown version, so setting it to null
                        ContentType contentType = new ContentTypeImpl(currContentType.getName(), null);
                        finalSet.add(contentType);
                    }
                }
            } else {
                for (PivotField pf : entry.getValue()) {
                    String contentTypeName = pf.getValue().toString();
                    LOGGER.debug("contentTypeName: {}", contentTypeName);
                    if (CollectionUtils.isEmpty(pf.getPivot())) {
                        // if there are no sub-pivots, that means that there are no content type
                        // versions
                        // associated with this content type name
                        LOGGER.debug("Content type does not have associated contentTypeVersion: {}", contentTypeName);
                        ContentType contentType = new ContentTypeImpl(contentTypeName, null);
                        finalSet.add(contentType);
                    } else {
                        for (PivotField innerPf : pf.getPivot()) {
                            LOGGER.debug("contentTypeVersion: {}. For contentTypeName: {}", innerPf.getValue(), contentTypeName);
                            ContentType contentType = new ContentTypeImpl(contentTypeName, innerPf.getValue().toString());
                            finalSet.add(contentType);
                        }
                    }
                }
            }
        }
    } catch (SolrServerException | IOException e) {
        LOGGER.info("Solr exception getting content types", e);
    }
    return finalSet;
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) ContentType(ddf.catalog.data.ContentType) SolrServerException(org.apache.solr.client.solrj.SolrServerException) FacetField(org.apache.solr.client.solrj.response.FacetField) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) PivotField(org.apache.solr.client.solrj.response.PivotField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Example 13 with ContentTypeImpl

use of ddf.catalog.data.impl.ContentTypeImpl in project ddf by codice.

the class SolrProviderTest method testGetContentTypesSimple.

@Test
public void testGetContentTypesSimple() throws Exception {
    deleteAllIn(provider);
    MockMetacard metacard1 = new MockMetacard(Library.getFlagstaffRecord());
    MockMetacard metacard2 = new MockMetacard(Library.getShowLowRecord());
    MockMetacard metacard3 = new MockMetacard(Library.getTampaRecord());
    metacard1.setContentTypeName(SAMPLE_CONTENT_TYPE_1);
    metacard2.setContentTypeName(SAMPLE_CONTENT_TYPE_2);
    metacard3.setContentTypeName(SAMPLE_CONTENT_TYPE_2);
    metacard3.setContentTypeVersion(SAMPLE_CONTENT_VERSION_3);
    List<Metacard> list = Arrays.asList((Metacard) metacard1, metacard2, metacard3);
    create(list);
    Set<ContentType> contentTypes = provider.getContentTypes();
    assertEquals(3, contentTypes.size());
    assertThat(contentTypes, hasItem((ContentType) new ContentTypeImpl(SAMPLE_CONTENT_TYPE_1, MockMetacard.DEFAULT_VERSION)));
    assertThat(contentTypes, hasItem((ContentType) new ContentTypeImpl(SAMPLE_CONTENT_TYPE_2, MockMetacard.DEFAULT_VERSION)));
    assertThat(contentTypes, hasItem((ContentType) new ContentTypeImpl(SAMPLE_CONTENT_TYPE_2, SAMPLE_CONTENT_VERSION_3)));
}
Also used : ContentTypeImpl(ddf.catalog.data.impl.ContentTypeImpl) Metacard(ddf.catalog.data.Metacard) ContentType(ddf.catalog.data.ContentType) Test(org.junit.Test)

Aggregations

ContentTypeImpl (ddf.catalog.data.impl.ContentTypeImpl)13 ContentType (ddf.catalog.data.ContentType)12 Metacard (ddf.catalog.data.Metacard)6 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 Matchers.anyString (org.mockito.Matchers.anyString)2 CatalogFramework (ddf.catalog.CatalogFramework)1 Result (ddf.catalog.data.Result)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)1 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)1 SourceInfoResponseImpl (ddf.catalog.operation.impl.SourceInfoResponseImpl)1 SourceDescriptor (ddf.catalog.source.SourceDescriptor)1 SourceDescriptorImpl (ddf.catalog.source.impl.SourceDescriptorImpl)1 IOException (java.io.IOException)1 URI (java.net.URI)1 List (java.util.List)1 Map (java.util.Map)1