Search in sources :

Example 11 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class IncludeDescriptorsProcessorTest method testProcess.

@Test
public void testProcess() throws Exception {
    Item item = new Item();
    item.setDescriptorUrl(DESCRIPTOR1_URL);
    item.setDescriptorDom(descriptorDom1);
    item = processor.process(context, DEFAULT_CACHING_OPTIONS, item);
    assertNotNull(item.getDescriptorDom());
    assertEquals(EXPECTED_XML, item.getDescriptorDom().asXML().replace("\n", ""));
}
Also used : Item(org.craftercms.core.service.Item) Test(org.junit.Test)

Example 12 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class IncludeDescriptorsProcessorTest method setUpTestStoreService.

private void setUpTestStoreService() {
    storeService = mock(ContentStoreService.class);
    when(storeService.findItem(context, DEFAULT_CACHING_OPTIONS, DESCRIPTOR2_URL, processor)).thenAnswer(invocationOnMock -> {
        Item item2 = new Item();
        item2.setDescriptorUrl(DESCRIPTOR2_URL);
        item2.setDescriptorDom(descriptorDom2);
        return processor.process(context, DEFAULT_CACHING_OPTIONS, item2);
    });
    when(storeService.findItem(context, DEFAULT_CACHING_OPTIONS, DESCRIPTOR3_URL, processor)).thenAnswer(invocationOnMock -> {
        Item item3 = new Item();
        item3.setDescriptorUrl(DESCRIPTOR3_URL);
        item3.setDescriptorDom(descriptorDom3);
        return processor.process(context, DEFAULT_CACHING_OPTIONS, item3);
    });
}
Also used : Item(org.craftercms.core.service.Item) ContentStoreService(org.craftercms.core.service.ContentStoreService)

Example 13 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class ContentStoreServiceImpl method doMerging.

/**
 * Executes merging for the specified {@link Item}:
 * <p/>
 * <ol>
 * <li>Gets the {@link org.craftercms.core.xml.mergers.DescriptorMergeStrategy} for the item's descriptor from
 * the {@link DescriptorMergeStrategyResolver}.</li>
 * <li>Gets the actual descriptors to merge from the returned merge strategy.</li>
 * <li>Retrieves the descriptor documents from the underlying repository.</li>
 * <li>Merges the descriptor documents.</li>
 * <li>Returns the item with the merged descriptor document.</li>
 * </ol>
 */
protected Item doMerging(Context context, CachingOptions cachingOptions, Item item) throws CrafterException {
    if (context.isMergingOn()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Doing merge for " + item + "...");
        }
        String mainDescriptorUrl = item.getDescriptorUrl();
        Document mainDescriptorDom = item.getDescriptorDom();
        DescriptorMergeStrategy strategy = mergeStrategyResolver.getStrategy(mainDescriptorUrl, mainDescriptorDom);
        if (strategy == null) {
            logger.warn("No merge strategy was found for " + mainDescriptorUrl + ". Merging skipped");
            return item;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Merge strategy for " + mainDescriptorUrl + ": " + strategy);
        }
        List<MergeableDescriptor> descriptorsToMerge = strategy.getDescriptors(context, cachingOptions, mainDescriptorUrl, mainDescriptorDom);
        if (descriptorsToMerge == null) {
            throw new XmlMergeException("There aren't any descriptors to merge for " + mainDescriptorUrl);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Descriptors to merge for " + mainDescriptorUrl + ": " + descriptorsToMerge);
        }
        List<Document> documentsToMerge = new ArrayList<Document>(descriptorsToMerge.size());
        for (MergeableDescriptor descriptorToMerge : descriptorsToMerge) {
            String descriptorUrl = descriptorToMerge.getUrl();
            Item descriptorItem = context.getStoreAdapter().findItem(context, cachingOptions, descriptorUrl, true);
            if (descriptorItem != null) {
                Document descriptorDom = descriptorItem.getDescriptorDom();
                if (descriptorDom == null && !descriptorToMerge.isOptional()) {
                    throw new XmlMergeException("Descriptor file " + descriptorUrl + " not found and is marked as " + "required for merging");
                }
                documentsToMerge.add(descriptorDom);
                item.addDependencyKey(descriptorItem.getKey());
            } else if (!descriptorToMerge.isOptional()) {
                throw new XmlMergeException("Descriptor file " + descriptorUrl + " not found and is marked as required for merging");
            }
        }
        Document mergedDoc = merger.merge(documentsToMerge);
        if (logger.isDebugEnabled()) {
            logger.debug("Merged descriptor DOM for " + item + ":\n" + XmlUtils.documentToPrettyString(mergedDoc));
        }
        item.setDescriptorDom(mergedDoc);
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) DescriptorMergeStrategy(org.craftercms.core.xml.mergers.DescriptorMergeStrategy) ArrayList(java.util.ArrayList) MergeableDescriptor(org.craftercms.core.xml.mergers.MergeableDescriptor) Document(org.dom4j.Document) XmlMergeException(org.craftercms.core.exception.XmlMergeException)

Example 14 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class ContentStoreServiceImpl method doFindTree.

@Override
protected Tree doFindTree(Context context, CachingOptions cachingOptions, String url, int depth, ItemFilter filter, ItemProcessor processor) throws InvalidContextException, XmlFileParseException, XmlMergeException, ItemProcessingException, StoreException {
    // correct set of descriptor files to merge (like all the impl of AbstractInheritFromHierarchyMergeStrategy).
    if (!url.startsWith("/")) {
        url = "/" + url;
    }
    Item item = findItem(context, cachingOptions, url, processor);
    if (item != null) {
        Tree tree = new Tree(item);
        if (depth == ContentStoreService.UNLIMITED_TREE_DEPTH || depth >= 1) {
            if (depth >= 1) {
                depth--;
            }
            CachingAwareList<Item> treeChildren = (CachingAwareList<Item>) doFindChildren(context, cachingOptions, url, depth, filter, processor);
            if (treeChildren != null) {
                tree.setChildren(treeChildren.getActualList());
                tree.addDependencyKeys(treeChildren.getDependencyKeys());
            }
        }
        return tree;
    } else {
        return null;
    }
}
Also used : Item(org.craftercms.core.service.Item) Tree(org.craftercms.core.service.Tree) CachingAwareList(org.craftercms.core.util.cache.impl.CachingAwareList)

Example 15 with Item

use of org.craftercms.core.service.Item in project core by craftercms.

the class ContentStoreServiceImpl method doFindItem.

/**
 * Returns the content store item for the given url, returning null if not found.
 * <p/>
 * <p>After acquiring the item from the {@link ContentStoreAdapter}, the item's descriptor is merged (according
 * to its {@link org.craftercms.core.xml.mergers.DescriptorMergeStrategy}) with related descriptors, and the
 * final item is then processed.</p>
 */
@Override
protected Item doFindItem(Context context, CachingOptions cachingOptions, String url, ItemProcessor processor) throws InvalidContextException, XmlFileParseException, XmlMergeException, ItemProcessingException, StoreException {
    // correct set of descriptor files to merge (like all the impl of AbstractInheritFromHierarchyMergeStrategy).
    if (!url.startsWith("/")) {
        url = "/" + url;
    }
    Item item = context.getStoreAdapter().findItem(context, cachingOptions, url, true);
    if (item != null) {
        // Create a copy of the item, since it will be modified
        item = new Item(item);
        if (item.getDescriptorDom() != null) {
            item = doMerging(context, cachingOptions, item);
            item = doProcessing(context, cachingOptions, item, processor);
        } else {
            item = doProcessing(context, cachingOptions, item, processor);
            // Since there was no merging, add the original key (from the store adapter item) as dependency key.
            // The store service item key will be set later.
            item.addDependencyKey(item.getKey());
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item)

Aggregations

Item (org.craftercms.core.service.Item)55 Test (org.junit.Test)21 Context (org.craftercms.core.service.Context)19 CachingOptions (org.craftercms.core.service.CachingOptions)8 Document (org.dom4j.Document)8 ArrayList (java.util.ArrayList)7 ContentStoreAdapter (org.craftercms.core.store.ContentStoreAdapter)7 CachingAwareList (org.craftercms.core.util.cache.impl.CachingAwareList)5 InheritLevelsMergeStrategyTest (org.craftercms.core.xml.mergers.impl.strategies.InheritLevelsMergeStrategyTest)4 DecoratedStoreAdapterContext (org.craftercms.engine.util.store.decorators.DecoratedStoreAdapterContext)4 PathNotFoundException (org.craftercms.core.exception.PathNotFoundException)3 TextMetaDataExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataExtractingProcessor)3 Content (org.craftercms.core.service.Content)3 ContentStoreService (org.craftercms.core.service.ContentStoreService)3 Tree (org.craftercms.core.service.Tree)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 StoreException (org.craftercms.core.exception.StoreException)2 ItemProcessor (org.craftercms.core.processors.ItemProcessor)2 ItemProcessorPipeline (org.craftercms.core.processors.impl.ItemProcessorPipeline)2 TextMetaDataCollectionExtractingProcessor (org.craftercms.core.processors.impl.TextMetaDataCollectionExtractingProcessor)2