001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.cache.Lifecycle;
018 import com.liferay.portal.kernel.cache.ThreadLocalCache;
019 import com.liferay.portal.kernel.cache.ThreadLocalCacheManager;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.ObjectValuePair;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.LayoutConstants;
027 import com.liferay.portal.model.impl.LayoutImpl;
028 import com.liferay.portal.service.LayoutLocalServiceUtil;
029
030 import java.util.ArrayList;
031 import java.util.Deque;
032 import java.util.LinkedList;
033 import java.util.List;
034 import java.util.ListIterator;
035 import java.util.Locale;
036
037
041 public class LayoutListUtil {
042
043 public static List<LayoutDescription> getLayoutDescriptions(
044 long groupId, boolean privateLayout, String rootNodeName,
045 Locale locale) {
046
047 ThreadLocalCache<List<LayoutDescription>> threadLocalCache =
048 ThreadLocalCacheManager.getThreadLocalCache(
049 Lifecycle.REQUEST, LayoutListUtil.class.getName());
050
051 String cacheKey = buildCacheKey(
052 groupId, privateLayout, rootNodeName, locale);
053
054 List<LayoutDescription> layoutDescriptions = threadLocalCache.get(
055 cacheKey);
056
057 if (layoutDescriptions == null) {
058 layoutDescriptions = doGetLayoutDescriptions(
059 groupId, privateLayout, rootNodeName, locale);
060
061 threadLocalCache.put(cacheKey, layoutDescriptions);
062 }
063
064 return layoutDescriptions;
065 }
066
067 protected static String buildCacheKey(
068 long groupId, boolean privateLayout, String rootNodeName,
069 Locale locale) {
070
071 StringBundler sb = new StringBundler(7);
072
073 sb.append(StringUtil.toHexString(groupId));
074 sb.append(StringPool.POUND);
075 sb.append(privateLayout);
076 sb.append(StringPool.POUND);
077 sb.append(rootNodeName);
078 sb.append(StringPool.POUND);
079 sb.append(LocaleUtil.toLanguageId(locale));
080
081 return sb.toString();
082 }
083
084 protected static List<LayoutDescription> doGetLayoutDescriptions(
085 long groupId, boolean privateLayout, String rootNodeName,
086 Locale locale) {
087
088 List<LayoutDescription> layoutDescriptions =
089 new ArrayList<LayoutDescription>();
090
091 List<Layout> layouts = new ArrayList<Layout>(
092 LayoutLocalServiceUtil.getLayouts(groupId, privateLayout));
093
094 Deque<ObjectValuePair<Layout, Integer>> deque =
095 new LinkedList<ObjectValuePair<Layout, Integer>>();
096
097 Layout rootLayout = new LayoutImpl();
098
099 rootLayout.setPlid(LayoutConstants.DEFAULT_PLID);
100 rootLayout.setName(rootNodeName);
101
102 deque.push(new ObjectValuePair<Layout, Integer>(rootLayout, 0));
103
104 ObjectValuePair<Layout, Integer> objectValuePair = null;
105
106 while ((objectValuePair = deque.pollFirst()) != null) {
107 Layout currentLayout = objectValuePair.getKey();
108
109 Integer currentDepth = objectValuePair.getValue();
110
111 layoutDescriptions.add(
112 new LayoutDescription(
113 currentLayout.getPlid(), currentLayout.getName(locale),
114 currentDepth));
115
116 ListIterator<Layout> listIterator = layouts.listIterator(
117 layouts.size());
118
119 while (listIterator.hasPrevious()) {
120 Layout previousLayout = listIterator.previous();
121
122 if (previousLayout.getParentLayoutId() ==
123 currentLayout.getLayoutId()) {
124
125 listIterator.remove();
126
127 deque.push(
128 new ObjectValuePair<Layout, Integer>(
129 previousLayout, currentDepth + 1));
130 }
131 }
132 }
133
134 return layoutDescriptions;
135 }
136
137 }