/**
 * 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.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.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.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 = HttpUtil
				.URLtoString("https://raw.githubusercontent.com/liferay/liferay-portal/"
						+ liferayVersion + "/portal-impl/src/portal.properties");

		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);
	}

}

@@ -695,32 +695,61 @@
 BufferedReader;%0A
+import java.io.Serializable;%0A
 import java.io.S
@@ -753,32 +753,32 @@
 o.StringReader;%0A
-
 import java.util
@@ -943,16 +943,125 @@
 ponse;%0A%0A
+import com.liferay.portal.kernel.cache.PortalCache;%0Aimport com.liferay.portal.kernel.cache.SingleVMPoolUtil;%0A
 import c
@@ -1356,24 +1356,73 @@
 ertiesUtil;%0A
+import com.liferay.portal.kernel.util.Validator;%0A
 import com.l
@@ -1948,67 +1948,176 @@
 %0A%0A%09%09
-String defaultPortalProperties = HttpUtil%0A%09%09%09%09.URLtoString(
+PortalCache%3CSerializable, Object%3E portalCache = SingleVMPoolUtil%0A%09%09%09%09.getCache(PortalPropertiesPrettierPortlet.class.getName());%0A%0A%09%09String defaultPortalPropertiesURL = 
 %22htt
@@ -2176,18 +2176,16 @@
 l/%22%0A%09%09%09%09
-%09%09
 + lifera
@@ -2231,18 +2231,335 @@
 perties%22
-)
 ;
+%0A%09%09String defaultPortalProperties = (String) portalCache%0A%09%09%09%09.get(defaultPortalPropertiesURL);%0A%0A%09%09if (Validator.isNull(defaultPortalProperties)) %7B%0A%09%09%09defaultPortalProperties = HttpUtil%0A%09%09%09%09%09.URLtoString(defaultPortalPropertiesURL);%0A%0A%09%09%09portalCache.put(defaultPortalPropertiesURL,%0A%09%09%09%09%09defaultPortalProperties, 60);%0A%09%09%7D
 %0A%0A%09%09Prop

Add cache to remote portal.properties files