Search in sources :

Example 1 with MergeableDescriptor

use of org.craftercms.core.xml.mergers.MergeableDescriptor in project engine by craftercms.

the class TargetedContentDescriptorMergeStrategy method getDescriptors.

@Override
public List<MergeableDescriptor> getDescriptors(Context context, CachingOptions cachingOptions, String mainDescriptorUrl, Document mainDescriptorDom, boolean mainDescriptorOptional) throws XmlMergeException {
    Set<MergeableDescriptor> results = new LinkedHashSet<>();
    List<String> candidateUrls = candidateTargetedUrlsResolver.getUrls(mainDescriptorUrl);
    for (ListIterator<String> iter = candidateUrls.listIterator(candidateUrls.size()); iter.hasPrevious(); ) {
        String candidateUrl = iter.previous();
        if (!candidateUrl.equals(mainDescriptorUrl)) {
            Document descriptorDom = getDescriptorDom(context, cachingOptions, candidateUrl);
            if (descriptorDom != null) {
                DescriptorMergeStrategy mergeStrategy = mergeStrategyResolver.getStrategy(candidateUrl, descriptorDom);
                List<MergeableDescriptor> descriptors = mergeStrategy.getDescriptors(context, cachingOptions, candidateUrl, descriptorDom, true);
                results.addAll(descriptors);
            }
        }
    }
    List<MergeableDescriptor> descriptors = super.getDescriptors(context, cachingOptions, mainDescriptorUrl, mainDescriptorDom, mainDescriptorOptional);
    results.addAll(descriptors);
    return new ArrayList<>(results);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DescriptorMergeStrategy(org.craftercms.core.xml.mergers.DescriptorMergeStrategy) ArrayList(java.util.ArrayList) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor) Document(org.dom4j.Document)

Example 2 with MergeableDescriptor

use of org.craftercms.core.xml.mergers.MergeableDescriptor in project engine by craftercms.

the class TargetedContentDescriptorMergeStrategyTest method testGetDescriptors.

@Test
public void testGetDescriptors() throws Exception {
    Item item = mock(Item.class);
    when(item.getDescriptorDom()).thenReturn(mock(Document.class));
    ContentStoreAdapter storeAdapter = mock(ContentStoreAdapter.class);
    when(storeAdapter.findItem(any(Context.class), any(CachingOptions.class), anyString(), anyBoolean())).thenReturn(item);
    Context context = mock(Context.class);
    when(context.getStoreAdapter()).thenReturn(storeAdapter);
    List<MergeableDescriptor> descriptors = mergeStrategy.getDescriptors(context, CachingOptions.DEFAULT_CACHING_OPTIONS, "/site/website/es_CR/products/index.xml", mock(Document.class), false);
    assertNotNull(descriptors);
    assertEquals(12, descriptors.size());
    assertEquals("/crafter-level-descriptor.level.xml", descriptors.get(0).getUrl());
    assertEquals("/site/crafter-level-descriptor.level.xml", descriptors.get(1).getUrl());
    assertEquals("/site/website/crafter-level-descriptor.level.xml", descriptors.get(2).getUrl());
    assertEquals("/site/website/en/crafter-level-descriptor.level.xml", descriptors.get(3).getUrl());
    assertEquals("/site/website/en/products/crafter-level-descriptor.level.xml", descriptors.get(4).getUrl());
    assertEquals("/site/website/en/products/index.xml", descriptors.get(5).getUrl());
    assertEquals("/site/website/es/crafter-level-descriptor.level.xml", descriptors.get(6).getUrl());
    assertEquals("/site/website/es/products/crafter-level-descriptor.level.xml", descriptors.get(7).getUrl());
    assertEquals("/site/website/es/products/index.xml", descriptors.get(8).getUrl());
    assertEquals("/site/website/es_CR/crafter-level-descriptor.level.xml", descriptors.get(9).getUrl());
    assertEquals("/site/website/es_CR/products/crafter-level-descriptor.level.xml", descriptors.get(10).getUrl());
    assertEquals("/site/website/es_CR/products/index.xml", descriptors.get(11).getUrl());
}
Also used : SiteContext(org.craftercms.engine.service.context.SiteContext) Context(org.craftercms.core.service.Context) Item(org.craftercms.core.service.Item) CachingOptions(org.craftercms.core.service.CachingOptions) ContentStoreAdapter(org.craftercms.core.store.ContentStoreAdapter) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor) Document(org.dom4j.Document) Test(org.junit.Test)

Aggregations

MergeableDescriptor (org.craftercms.core.xml.mergers.MergeableDescriptor)2 Document (org.dom4j.Document)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 CachingOptions (org.craftercms.core.service.CachingOptions)1 Context (org.craftercms.core.service.Context)1 Item (org.craftercms.core.service.Item)1 ContentStoreAdapter (org.craftercms.core.store.ContentStoreAdapter)1 DescriptorMergeStrategy (org.craftercms.core.xml.mergers.DescriptorMergeStrategy)1 SiteContext (org.craftercms.engine.service.context.SiteContext)1 Test (org.junit.Test)1