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", ""));
}
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);
});
}
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;
}
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;
}
}
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;
}
Aggregations