001
014
015 package com.liferay.portal.model;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.Validator;
021
022
025 public class PortletConstants {
026
027
030 public static final String DEFAULT_PREFERENCES = "<portlet-preferences />";
031
032
035 public static final String FACEBOOK_INTEGRATION_FBML = "fbml";
036
037
040 public static final String FACEBOOK_INTEGRATION_IFRAME = "iframe";
041
042
045 public static final String INSTANCE_SEPARATOR = "_INSTANCE_";
046
047
050 public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
051
052
055 public static final String USER_PRINCIPAL_STRATEGY_SCREEN_NAME =
056 "screenName";
057
058
061 public static final String USER_PRINCIPAL_STRATEGY_USER_ID = "userId";
062
063
066 public static final String USER_SEPARATOR = "_USER_";
067
068
071 public static final String WAR_SEPARATOR = "_WAR_";
072
073
083 public static String assemblePortletId(String portletId, long userId) {
084 return assemblePortletId(portletId, userId, null);
085 }
086
087
098 public static String assemblePortletId(
099 String portletId, long userId, String instanceId) {
100
101 String rootPortletId = getRootPortletId(portletId);
102
103 StringBundler sb = new StringBundler(5);
104
105 sb.append(rootPortletId);
106
107 if (userId <= 0) {
108 userId = getUserId(portletId);
109 }
110
111 if (userId > 0) {
112 sb.append(USER_SEPARATOR);
113 sb.append(userId);
114 }
115
116 if (Validator.isNull(instanceId)) {
117 instanceId = getInstanceId(portletId);
118 }
119
120 if (Validator.isNotNull(instanceId)) {
121 sb.append(INSTANCE_SEPARATOR);
122 sb.append(instanceId);
123 }
124
125 return sb.toString();
126 }
127
128
138 public static String assemblePortletId(
139 String portletId, String instanceId) {
140
141 return assemblePortletId(portletId, 0, instanceId);
142 }
143
144 public static String generateInstanceId() {
145 return StringUtil.randomString(12);
146 }
147
148
154 public static String getInstanceId(String portletId) {
155 int pos = portletId.indexOf(INSTANCE_SEPARATOR);
156
157 if (pos == -1) {
158 return null;
159 }
160
161 return portletId.substring(pos + INSTANCE_SEPARATOR.length());
162 }
163
164
170 public static String getRootPortletId(String portletId) {
171 int x = portletId.indexOf(USER_SEPARATOR);
172 int y = portletId.indexOf(INSTANCE_SEPARATOR);
173
174 if ((x == -1) && (y == -1)) {
175 return portletId;
176 }
177 else if (x != -1) {
178 return portletId.substring(0, x);
179 }
180
181 return portletId.substring(0, y);
182 }
183
184
191 public static long getUserId(String portletId) {
192 int x = portletId.indexOf(USER_SEPARATOR);
193 int y = portletId.indexOf(INSTANCE_SEPARATOR);
194
195 if (x == -1) {
196 return 0;
197 }
198
199 if (y != -1) {
200 return GetterUtil.getLong(
201 portletId.substring(x + USER_SEPARATOR.length(), y));
202 }
203
204 return GetterUtil.getLong(
205 portletId.substring(x + USER_SEPARATOR.length()));
206 }
207
208 public static boolean hasIdenticalRootPortletId(
209 String portletId1, String portletId2) {
210
211 String rootPortletId1 = getRootPortletId(portletId1);
212 String rootPortletId2 = getRootPortletId(portletId2);
213
214 return rootPortletId1.equals(rootPortletId2);
215 }
216
217
224 public static boolean hasInstanceId(String portletId) {
225 return portletId.contains(INSTANCE_SEPARATOR);
226 }
227
228
235 public static boolean hasUserId(String portletId) {
236 return portletId.contains(USER_SEPARATOR);
237 }
238
239 }