/*
 * Copyright 2012-2014 the original author or authors.
 *
 * 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.
 */

package net.nicoll.boot.metadata;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.configurationmetadata.ConfigurationMetadataGroup;
import org.springframework.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.configurationmetadata.ConfigurationMetadataRepository;
import org.springframework.configurationmetadata.ConfigurationMetadataRepositoryJsonLoader;
import org.springframework.configurationmetadata.ConfigurationMetadataSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 *
 * @author Stephane Nicoll
 */
public class Sample {

	public static void main(String[] args) throws IOException {
		ConfigurationMetadataRepositoryJsonLoader loader = new ConfigurationMetadataRepositoryJsonLoader();
		ConfigurationMetadataRepository repo = loader.loadAll(getResources());
		for (ConfigurationMetadataGroup group : repo.getAllGroups().values()) {
			System.out.println("========================================");
			StringBuilder sb = new StringBuilder();
			for (ConfigurationMetadataSource source : group.getSources().values()) {
				sb.append(source.getType()).append(" ");
			}
			System.out.println("Group --- " + group.getId() + "(" + sb.toString().trim() + ")");
			System.out.println("========================================");
			for (ConfigurationMetadataProperty property : group.getProperties().values()) {
				System.out.println("-- " + property.getId() + " (" + property.getType() + ")");
			}
		}

	}


	private static List<InputStream> getResources() throws IOException {
		Resource[] resources = new PathMatchingResourcePatternResolver()
				.getResources("classpath*:/META-INF/spring-configuration-metadata.json");
		List<InputStream> result = new ArrayList<InputStream>();
		for (Resource resource : resources) {
			result.add(resource.getInputStream());
		}
		return result;
	}
}

@@ -734,16 +734,75 @@
 ayList;%0A
+import java.util.Collections;%0Aimport java.util.Comparator;%0A
 import j
@@ -816,16 +816,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -1651,21 +1651,21 @@
 s());%0A%09%09
-for (
+List%3C
 Configur
@@ -1686,45 +1686,195 @@
 roup
+%3E
  group
- : repo.getAllGroups().values()
+s%0A%09%09%09%09= new ArrayList%3CConfigurationMetadataGroup%3E(repo.getAllGroups().values());%0A%09%09Collections.sort(groups, new GroupComparator());%0A%09%09for (ConfigurationMetadataGroup group : groups
 ) %7B%0A
@@ -2263,29 +2263,29 @@
 =====%22);%0A%09%09%09
-for (
+List%3C
 Configuratio
@@ -2301,24 +2301,25 @@
 Property
+%3E
  propert
 y : grou
@@ -2314,42 +2314,215 @@
 pert
-y : group.getProperties().values()
+ies =%0A%09%09%09%09%09new ArrayList%3CConfigurationMetadataProperty%3E(group.getProperties().values());%0A%09%09%09Collections.sort(properties, new PropertyComparator());%0A%09%09%09for (ConfigurationMetadataProperty property : properties
 ) %7B%0A
@@ -2980,16 +2980,16 @@
 ));%0A%09%09%7D%0A
-
 %09%09return
@@ -2999,11 +2999,685 @@
 sult;%0A%09%7D
+%0A%0A%09private static class GroupComparator implements Comparator%3CConfigurationMetadataGroup%3E %7B%0A%0A%09%09@Override%0A%09%09public int compare(ConfigurationMetadataGroup o1, ConfigurationMetadataGroup o2) %7B%0A%09%09%09if (ConfigurationMetadataRepository.ROOT_GROUP.equals(o1.getId())) %7B%0A%09%09%09%09return -1;%0A%09%09%09%7D%0A%09%09%09if (ConfigurationMetadataRepository.ROOT_GROUP.equals(o2.getId())) %7B%0A%09%09%09%09return 1;%0A%09%09%09%7D%0A%09%09%09return o1.getId().compareTo(o2.getId());%0A%09%09%7D%0A%09%7D%0A%0A%09private static class PropertyComparator implements Comparator%3CConfigurationMetadataProperty%3E %7B%0A%09%09@Override%0A%09%09public int compare(ConfigurationMetadataProperty o1, ConfigurationMetadataProperty o2) %7B%0A%09%09%09return o1.getId().compareTo(o2.getId());%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add ordering of groups and properties