use of org.craftercms.core.service.Item in project engine by craftercms.
the class TargetedContentStoreAdapterTest method createActualAdapter.
private ContentStoreAdapter createActualAdapter() {
ContentStoreAdapter adapter = mock(ContentStoreAdapter.class);
Item idx = new Item();
idx.setName("index.xml");
idx.setUrl("/site/website/index.xml");
Item en = new Item();
en.setName("en");
en.setUrl("/site/website/en");
Item enIdx = new Item();
enIdx.setName("index.xml");
enIdx.setUrl("/site/website/en/index.xml");
Item enAboutUs = new Item();
enAboutUs.setName("about-us");
enAboutUs.setUrl("/site/website/en/about-us");
Item mainCss = new Item();
mainCss.setName("main.css");
mainCss.setUrl("/static-assets/css/main.css");
Item frIdx = new Item();
frIdx.setName("index.xml");
frIdx.setUrl("/site/website/ja/index.xml");
when(adapter.exists(any(Context.class), eq("/site/website/index.xml"))).thenReturn(true);
when(adapter.exists(any(Context.class), eq("/site/website/en"))).thenReturn(true);
when(adapter.exists(any(Context.class), eq("/site/website/en/index.xml"))).thenReturn(true);
when(adapter.exists(any(Context.class), eq("/static-assets/css/main.css"))).thenReturn(true);
when(adapter.findContent(any(Context.class), any(CachingOptions.class), eq("/site/website/index.xml"))).thenReturn(mock(Content.class));
when(adapter.findContent(any(Context.class), any(CachingOptions.class), eq("/site/website/en/index.xml"))).thenReturn(mock(Content.class));
when(adapter.findContent(any(Context.class), any(CachingOptions.class), eq("/static-assets/css/main.css"))).thenReturn(mock(Content.class));
when(adapter.findItem(any(Context.class), any(CachingOptions.class), eq("/site/website/index.xml"), anyBoolean())).thenReturn(idx);
when(adapter.findItem(any(Context.class), any(CachingOptions.class), eq("/site/website/en"), anyBoolean())).thenReturn(en);
when(adapter.findItem(any(Context.class), any(CachingOptions.class), eq("/site/website/en/index.xml"), anyBoolean())).thenReturn(enIdx);
when(adapter.findItem(any(Context.class), any(CachingOptions.class), eq("/static-assets/css/main.css"), anyBoolean())).thenReturn(mainCss);
when(adapter.findItems(any(Context.class), any(CachingOptions.class), eq("/site/website/en"), anyBoolean())).thenReturn(Arrays.asList(enIdx, enAboutUs));
when(adapter.findItems(any(Context.class), any(CachingOptions.class), eq("/site/website/ja"), anyBoolean())).thenReturn(Collections.singletonList(frIdx));
return adapter;
}
use of org.craftercms.core.service.Item in project engine by craftercms.
the class TargetedContentStoreAdapterTest method testFindItem.
@Test
public void testFindItem() throws Exception {
Context context = new TargetedContentStoreAdapter.ContextWrapper(storeAdapter, mock(Context.class));
CachingOptions cachingOptions = CachingOptions.DEFAULT_CACHING_OPTIONS;
Item item = storeAdapter.findItem(context, cachingOptions, "/site/website/en", true);
assertNotNull(item);
assertEquals("en", item.getName());
assertEquals("/site/website/en", item.getUrl());
item = storeAdapter.findItem(context, cachingOptions, "/site/website/ja_jp_jp", true);
assertNotNull(item);
assertEquals("en", item.getName());
assertEquals("/site/website/en", item.getUrl());
item = storeAdapter.findItem(context, cachingOptions, "/site/website/ja_jp_jp/index.xml", true);
assertNotNull(item);
assertEquals("index.xml", item.getName());
assertEquals("/site/website/en/index.xml", item.getUrl());
item = storeAdapter.findItem(context, cachingOptions, "/site/website/index.xml", true);
assertNotNull(item);
assertEquals("index.xml", item.getName());
assertEquals("/site/website/index.xml", item.getUrl());
item = storeAdapter.findItem(context, cachingOptions, "/static-assets/css/main.css", true);
assertNotNull(item);
assertEquals("main.css", item.getName());
assertEquals("/static-assets/css/main.css", item.getUrl());
}
use of org.craftercms.core.service.Item in project engine by craftercms.
the class TargetedContentStoreAdapterTest method testFindItems.
@Test
public void testFindItems() throws Exception {
Context context = new TargetedContentStoreAdapter.ContextWrapper(storeAdapter, mock(Context.class));
CachingOptions cachingOptions = CachingOptions.DEFAULT_CACHING_OPTIONS;
List<Item> items = storeAdapter.findItems(context, cachingOptions, "/site/website/en", true);
assertNotNull(items);
assertEquals(2, items.size());
assertEquals("index.xml", items.get(0).getName());
assertEquals("/site/website/en/index.xml", items.get(0).getUrl());
assertEquals("about-us", items.get(1).getName());
assertEquals("/site/website/en/about-us", items.get(1).getUrl());
items = storeAdapter.findItems(context, cachingOptions, "/site/website/ja_jp_jp", true);
assertNotNull(items);
assertEquals(2, items.size());
assertEquals("index.xml", items.get(0).getName());
assertEquals("/site/website/ja/index.xml", items.get(0).getUrl());
assertEquals("about-us", items.get(1).getName());
assertEquals("/site/website/en/about-us", items.get(1).getUrl());
config.setProperty(SiteProperties.MERGE_FOLDERS_CONFIG_KEY, false);
items = storeAdapter.findItems(context, cachingOptions, "/site/website/ja_jp_jp", true);
assertNotNull(items);
assertEquals(1, items.size());
assertEquals("index.xml", items.get(0).getName());
assertEquals("/site/website/ja/index.xml", items.get(0).getUrl());
}
use of org.craftercms.core.service.Item in project core by craftercms.
the class ContentStoreServiceImpl method doFindChildren.
/**
* Does the following:
* <p/>
* <ol>
* <li>Retrieves the children from the underlying repository (without their descriptors).</li>
* <li>Filters the returned list if {@link ItemFilter#runBeforeProcessing()} returns <code>true</code>.</li>
* <li>Calls {@link #getTree(Context, String)} or {@link #getItem(Context, String)} for each item in the list
* (depending on whether the item is a folder or not, and if <code>depth</code> is not null), to obtain the
* merged and processed version of each item.</li>
* <li>Filters the processed list if {@link ItemFilter#runAfterProcessing()} returns <code>true</code>.</li>
* <li>Returns the final list of processed items.</li>
* </ol>
*/
protected List<Item> doFindChildren(Context context, CachingOptions cachingOptions, String url, Integer depth, ItemFilter filter, ItemProcessor processor) throws InvalidContextException, XmlFileParseException, XmlMergeException, ItemProcessingException, StoreException {
List<Item> children = context.getStoreAdapter().findItems(context, cachingOptions, url, false);
if (children != null) {
List<Object> dependencyKeys = new ArrayList<>();
dependencyKeys.add(((CachingAwareList<Item>) children).getKey());
if (filter != null && filter.runBeforeProcessing()) {
if (logger.isDebugEnabled()) {
logger.debug("Running filter " + filter + " before processing for " + url + "...");
}
children = doFilter(children, filter, true);
}
List<Item> processedChildren = new ArrayList<Item>(children.size());
for (Item child : children) {
Item processedChild;
if (depth != null && child.isFolder()) {
processedChild = getTree(context, cachingOptions, child.getUrl(), depth, filter, processor);
} else {
processedChild = getItem(context, cachingOptions, child.getUrl(), processor);
}
processedChildren.add(processedChild);
}
if (filter != null && filter.runAfterProcessing()) {
if (logger.isDebugEnabled()) {
logger.debug("Running filter " + filter + " after processing for " + url + "...");
}
processedChildren = doFilter(processedChildren, filter, false);
}
Collections.sort(processedChildren, CompareByItemNameComparator.instance);
for (Item child : processedChildren) {
dependencyKeys.add(child.getKey());
}
CachingAwareList<Item> finalChildren = new CachingAwareList<Item>(processedChildren);
finalChildren.setDependencyKeys(dependencyKeys);
return finalChildren;
} else {
return null;
}
}
use of org.craftercms.core.service.Item in project core by craftercms.
the class ContentStoreServiceImpl method doFilter.
/**
* Filters the given list of items by using the specified filter. The <code>runningBeforeProcessing</code> flag
* is passed to indicated the filter in which phase it is being executed (after or before processing).
*/
protected List<Item> doFilter(List<Item> items, ItemFilter filter, boolean runningBeforeProcessing) {
List<Item> acceptedItems = new ArrayList<>();
List<Item> rejectedItems = new ArrayList<>();
for (Item item : items) {
if (filter.accepts(item, acceptedItems, rejectedItems, runningBeforeProcessing)) {
acceptedItems.add(item);
} else {
rejectedItems.add(item);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Items filtered from " + items + " by " + filter + ": " + acceptedItems);
}
return acceptedItems;
}
Aggregations