/*
 * oxTrust is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.gluu.oxtrust.util.jsf;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.gluu.oxtrust.model.User;
import org.gluu.oxtrust.service.PermissionService;
import org.xdi.ldap.model.Entry;
import org.xdi.model.DisplayNameEntry;
import org.xdi.service.LookupService;
import org.xdi.service.cdi.util.CdiUtil;
import org.xdi.util.StringHelper;

//import org.gluu.site.model.Entry;

/**
 * Tag library to help display data in facelets
 * 
 * @author Yuriy Movchan Date: 11.09.2010
 */
public class JsfFunctions {

	public static DisplayNameEntry getDisplayNameEntry(String dn) {
		if (dn == null) {
			return null;
		}

		try {
			return CdiUtil.bean(LookupService.class).getDisplayNameEntry(dn);
		} catch (Exception ex) {
			return null;
		}
	}

	public static List<DisplayNameEntry> getDisplayNameEntries(String baseDn, List<? extends Entry> entries) {
		if ((baseDn == null) || (entries == null)) {
			return null;
		}

		try {
			return CdiUtil.bean(LookupService.class).getDisplayNameEntriesByEntries(baseDn, entries);
		} catch (Exception ex) {
			return null;
		}
	}

	public static String encodeString(String str) {
		if ((str == null) || (str.length() == 0)) {
			return str;
		}
		try {
			return (new URI(null, str, null)).toString();
		} catch (URISyntaxException ex) {
			return null;
		}
	}

	public static String splitByLines(String str, int maxLength) {
		if ((str == null) || (str.length() == 0) || (maxLength == 0)) {
			return str;
		}

		String tmpStr = StringEscapeUtils.escapeHtml(str);

		StringBuilder result = new StringBuilder();
		int startIndex = -1;
		int oldStartIndex = -1;
		while (startIndex + 1 < tmpStr.length()) {
			startIndex = tmpStr.indexOf(' ', startIndex + 1);
			if (startIndex == -1) {
				break;
			}
			if ((startIndex - oldStartIndex + 1) > maxLength) {
				result.append(tmpStr.substring(oldStartIndex + 1, startIndex));
				result.append("<br/>");
				oldStartIndex = startIndex;
			}
			startIndex++;
		}

		if (oldStartIndex < tmpStr.length()) {
			result.append(tmpStr.substring(oldStartIndex + 1));
		}

		return result.toString();
	}

	public static String trimFileName(String str, int maxLength) {
		if ((str == null) || (str.length() == 0) || (maxLength < 10)) {
			return str;
		}

		int length = str.length();
		int middleMaxLength = (maxLength - 3) / 2;

		if (length <= maxLength) {
			return str;
		}

		return str.substring(0, middleMaxLength - 1) + "..." + str.substring(length - middleMaxLength - 2);
	}

	public static String getColor(String color, String defaultColor) {
		String tmpColor = StringHelper.isEmpty(color) ? defaultColor : color;
		return "#" + tmpColor + ";";
	}

	public static String getPersonDisplayName(User person) {
		if (person == null) {
			return null;
		}

		if (StringHelper.isEmpty(person.getDisplayName())) {
			return person.getUid();
		}

		return person.getDisplayName();
	}

	public static boolean hasPermission(Object target, String action) {
		return CdiUtil.bean(PermissionService.class).hasPermission(target, action);
	}

}

@@ -250,20 +250,70 @@
 va.util.
-List
+ArrayList;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -3308,10 +3308,153 @@
 n);%0A%09%7D%0A%0A
+%09public static List%3CMap.Entry%3C?, ?%3E%3E toList(Map%3C?, ?%3E map) %7B%0A%09%09return map != null ? new ArrayList%3CMap.Entry%3C?, ?%3E%3E(map.entrySet()) : null;%0A%09%7D%0A%0A
 %7D%0A

Add JSF function to convert Set<Map.Entry> to List