/**
 * Copyright (C) 2015 Thiago Moreira (tmoreira2020@gmail.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.
 */
package br.com.thiagomoreira.liferay.plugins;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import com.liferay.portal.kernel.cache.PortalCache;
import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.HttpUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropertiesUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class PortalPropertiesPrettierPortlet extends MVCPortlet {

	private static Log log = LogFactoryUtil
			.getLog(PortalPropertiesPrettierPortlet.class);

	public void prettify(ActionRequest request, ActionResponse response)
			throws Exception {
		UploadPortletRequest uploadPortletRequest = PortalUtil
				.getUploadPortletRequest(request);

		String liferayVersion = ParamUtil.getString(uploadPortletRequest,
				"liferayVersion");

		String defaultPortalProperties = getDefaultPortalProperties(liferayVersion);

		Properties customPortalProperties = PropertiesUtil.load(
				uploadPortletRequest.getFileAsStream("portalPropertiesFile"),
				"UTF-8");

		String currentContext = null;
		Set<String> processedContexts = new HashSet<String>();
		StringBuilder pretty = new StringBuilder();
		BufferedReader reader = new BufferedReader(new StringReader(
				defaultPortalProperties));
		String line = reader.readLine();
		StringBuilder currentComment = new StringBuilder();

		while (line != null) {
			if (line.startsWith("## ")) {
				currentContext = line;
			}
			if (line.startsWith("    #")) {
				currentComment.append("\n");
				currentComment.append(line);
			}
			if (line.length() == 0) {
				currentComment.setLength(0);
			}

			Enumeration<Object> keys = (Enumeration<Object>) customPortalProperties
					.keys();
			while (keys.hasMoreElements()) {
				Object key = (Object) keys.nextElement();
				String value = customPortalProperties.getProperty(key
						.toString());
				if (line.startsWith("    " + key.toString() + "=")
						|| line.startsWith("    #" + key.toString() + "=")) {
					if (!line.startsWith("    " + key.toString() + "=" + value)) {
						if (!processedContexts.contains(currentContext)) {
							pretty.append("\n");
							pretty.append("##");
							pretty.append("\n");
							pretty.append(currentContext);
							pretty.append("\n");
							pretty.append("##");
							processedContexts.add(currentContext);
						}
						pretty.append(currentComment);
						pretty.append("\n");
						pretty.append("    " + key + "=" + value);
						pretty.append("\n");
					} else {
						log.info("Removing property:" + key);
					}
					customPortalProperties.remove(key);
				}

			}
			line = reader.readLine();
		}

		StringBuilder customProperties = new StringBuilder();

		customProperties.insert(0, "##\n## Custom properties\n##");

		Enumeration<Object> keys = (Enumeration<Object>) customPortalProperties
				.keys();
		while (keys.hasMoreElements()) {
			Object key = (Object) keys.nextElement();
			String value = customPortalProperties.getProperty(key.toString());
			customProperties.append("\n");
			customProperties.append("    " + key + "=" + value);
		}
		customProperties.append("\n");

		pretty.insert(0, customProperties.toString());

		request.setAttribute("portalPrettyProperties", pretty.toString());

		response.setRenderParameter("liferayVersion", liferayVersion);
	}

	protected String getDefaultPortalProperties(String liferayVersion) throws IOException {
		PortalCache<Serializable, Object> portalCache = SingleVMPoolUtil
				.getCache(PortalPropertiesPrettierPortlet.class.getName());

		String defaultPortalPropertiesURL = "https://raw.githubusercontent.com/liferay/liferay-portal/"
				+ liferayVersion + "/portal-impl/src/portal.properties";
		String defaultPortalProperties = (String) portalCache
				.get(defaultPortalPropertiesURL);

		if (Validator.isNull(defaultPortalProperties)) {
			defaultPortalProperties = HttpUtil
					.URLtoString(defaultPortalPropertiesURL);

			portalCache.put(defaultPortalPropertiesURL,
					defaultPortalProperties, 60);
		}

		return defaultPortalProperties;
	}

}

@@ -2222,16 +2222,70 @@
 = null;%0A
+%09%09StringBuilder currentComment = new StringBuilder();%0A
 %09%09Set%3CSt
@@ -2511,62 +2511,8 @@
 e();
-%0A%09%09StringBuilder currentComment = new StringBuilder();
 %0A%0A%09%09

Format source