All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cognifide.slice.cq.impl.PageChildrenProviderImpl Maven / Gradle / Ivy

The newest version!
package com.cognifide.slice.cq.impl;

/*
 * #%L
 * Slice - CQ Add-on
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2012 Cognifide Limited
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cognifide.slice.api.execution.ExecutionContextStack;
import com.cognifide.slice.api.scope.ContextScoped;
import com.cognifide.slice.cq.PageChildrenProvider;
import com.cognifide.slice.cq.RecursiveMode;
import com.day.cq.commons.Filter;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.google.inject.Inject;

@ContextScoped
public class PageChildrenProviderImpl implements PageChildrenProvider {

	private final ExecutionContextStack currentExecutionContext;

	private final PageManager pageManager;

	@Inject
	public PageChildrenProviderImpl(final PageManager pageManger,
			final ExecutionContextStack currentExecutionContext) {
		this.pageManager = pageManger;
		this.currentExecutionContext = currentExecutionContext;
	}

	@Override
	public List getChildPages(final String path, final Filter filter,
			final RecursiveMode recurseMode) {
		if (StringUtils.isBlank(path)) {
			return new ArrayList();
		}

		final String absolutePagePath = currentExecutionContext.getAbsolutePath(path);
		final Page page = pageManager.getPage(absolutePagePath);

		return getChildren(page, filter, recurseMode);
	}

	@Override
	public List getChildPages(final String path, final RecursiveMode recurseMode) {
		return getChildPages(path, new AllPageFilter(), recurseMode);
	}

	private List getChildren(final Page page, final Filter filter,
			final RecursiveMode recurseMode) {
		ArrayList result = new ArrayList();
		if (null == page) {
			return result;
		}

		final Iterator children = page.listChildren(filter);
		while (children.hasNext()) {
			final Page child = children.next();
			result.add(child.getPath());

			if (recurseMode == RecursiveMode.RECURSIVE) {
				result.addAll(getChildren(child, filter, recurseMode));
			}
		}

		return result;
	}

	private static final class AllPageFilter implements Filter {
		@Override
		public boolean includes(Page page) {
			return true;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy