/*
* Copyright 2011 France Télécom
*
* 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.
* 
* Author is Julien Wajsberg <julien.wajsberg@orange.com>
*/

package ro.isdc.wro.taglib.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;

import org.apache.commons.io.FilenameUtils;

import ro.isdc.wro.http.support.ServletContextAttributeHelper;
import ro.isdc.wro.model.WroModel;
import ro.isdc.wro.model.group.Group;
import ro.isdc.wro.model.resource.Resource;
import ro.isdc.wro.model.resource.ResourceType;

public class WroConfig {
	private static final String WRO_BASE_URL = "/wro/";

	private static WroConfig instance;

	private Map<String, FilesGroup> groups;
	private ServletContext servletContext;
	private boolean initialized = false;

	public static WroConfig getInstance() throws ConfigurationException {
		if (instance == null) {
			throw new ConfigurationException("The instance was not created.");
		}
		/* lazy initialization because we need to be in a request thread */
		synchronized(instance) {
			if (!instance.initialized) {
				instance.loadConfig();
				instance.loadMinimizedFiles();
				instance.initialized = true;
			}
		}
		return instance;
	}

	private void loadConfig() throws ConfigurationException {
		ServletContextAttributeHelper helper = new ServletContextAttributeHelper(servletContext);
		WroModel model = helper.getManagerFactory().create().getModelFactory().create();
		
		groups = new HashMap<String, FilesGroup>();
		
		for(Group group: model.getGroups()) {
			String groupName = group.getName();
			List<String> jsFiles = getFilesFor(group, ResourceType.JS);
			List<String> cssFiles = getFilesFor(group, ResourceType.CSS);
			FilesGroup filesGroup = new FilesGroup(groupName);
			filesGroup.put("js", jsFiles);
			filesGroup.put("css", cssFiles);
			groups.put(groupName, filesGroup);
		}
	}

	private List<String> getFilesFor(Group group, ResourceType resourceType) {
		List<String> result = new ArrayList<String>();
		Group filteredGroup = group.collectResourcesOfType(resourceType);
		for (Resource resource: filteredGroup.getResources()) {
			String file = resource.getUri();
			result.add(file);
		}

		return result;
	}

	/*
	private Collection<String> getAllPathsForGlob(String globFile) {
		Collection<String> result = new HashSet<String>();
		Collection<String> resources = getAllResourcePaths();
		AntPathStringMatcher matcher = new AntPathStringMatcher(globFile);
		for (String resource : resources) {
			if (matcher.match(resource)) {
				result.add(resource);
			}
		}

		return result;
	}

	private Collection<String> getAllResourcePaths() {
		Collection<String> result = new HashSet<String>();
		LinkedList<String> queue = new LinkedList<String>();
		queue.add("/"); // initial
		String current;

		while ((current = queue.poll()) != null) {
			Set<String> resourcePaths = servletContext
					.getResourcePaths(current);
			if (resourcePaths == null) {
				continue; // should not happen
			}

			for (String path : resourcePaths) {
				if (path.endsWith("/")) { // directory
					queue.add(path);
				} else {
					result.add(path);
				}
			}
		}

		return result;
	}
	*/

	private void loadMinimizedFiles() {
		
		@SuppressWarnings("unchecked")
		Set<String> resourcePaths = servletContext
				.getResourcePaths(WRO_BASE_URL);
		if (resourcePaths == null) {
			return;
		}

		for (String path : resourcePaths) {
			String basename = FilenameUtils.getBaseName(path);
			String groupName = basename.substring(0, basename.lastIndexOf('-'));
			if (groups.containsKey(groupName)) {
				String type = FilenameUtils.getExtension(path);
				FilesGroup group = groups.get(groupName);
				group.putMinimizedFile(type, path);
			}
		}
	}

	/* package */static synchronized void createInstance(ServletContext context) {
		if (instance == null) {
			instance = new WroConfig();
			instance.servletContext = context;
		}
	}

	public FilesGroup getGroup(String groupName) {
		if (initialized) {
			return groups.get(groupName);
		} else {
			throw new ConfigurationException(
					"WroConfig was not correctly initialized");
		}
	}
}

@@ -2768,971 +2768,8 @@
 %09%7D%0A%0A
-%09/*%0A%09private Collection%3CString%3E getAllPathsForGlob(String globFile) %7B%0A%09%09Collection%3CString%3E result = new HashSet%3CString%3E();%0A%09%09Collection%3CString%3E resources = getAllResourcePaths();%0A%09%09AntPathStringMatcher matcher = new AntPathStringMatcher(globFile);%0A%09%09for (String resource : resources) %7B%0A%09%09%09if (matcher.match(resource)) %7B%0A%09%09%09%09result.add(resource);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return result;%0A%09%7D%0A%0A%09private Collection%3CString%3E getAllResourcePaths() %7B%0A%09%09Collection%3CString%3E result = new HashSet%3CString%3E();%0A%09%09LinkedList%3CString%3E queue = new LinkedList%3CString%3E();%0A%09%09queue.add(%22/%22); // initial%0A%09%09String current;%0A%0A%09%09while ((current = queue.poll()) != null) %7B%0A%09%09%09Set%3CString%3E resourcePaths = servletContext%0A%09%09%09%09%09.getResourcePaths(current);%0A%09%09%09if (resourcePaths == null) %7B%0A%09%09%09%09continue; // should not happen%0A%09%09%09%7D%0A%0A%09%09%09for (String path : resourcePaths) %7B%0A%09%09%09%09if (path.endsWith(%22/%22)) %7B // directory%0A%09%09%09%09%09queue.add(path);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09result.add(path);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09return result;%0A%09%7D%0A%09*/%0A%0A
 %09pri

remove useless comments