001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class PortletConstants {
026    
027            /**
028             * Default preferences.
029             */
030            public static final String DEFAULT_PREFERENCES = "<portlet-preferences />";
031    
032            /**
033             * Facebook integration method for FBML.
034             */
035            public static final String FACEBOOK_INTEGRATION_FBML = "fbml";
036    
037            /**
038             * Facebook integration method for IFrame.
039             */
040            public static final String FACEBOOK_INTEGRATION_IFRAME = "iframe";
041    
042            /**
043             * Instance separator.
044             */
045            public static final String INSTANCE_SEPARATOR = "_INSTANCE_";
046    
047            /**
048             * Layout separator.
049             */
050            public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
051    
052            /**
053             * User principal strategy for screen name.
054             */
055            public static final String USER_PRINCIPAL_STRATEGY_SCREEN_NAME =
056                    "screenName";
057    
058            /**
059             * User principal strategy for screen name.
060             */
061            public static final String USER_PRINCIPAL_STRATEGY_USER_ID = "userId";
062    
063            /**
064             * User separator.
065             */
066            public static final String USER_SEPARATOR = "_USER_";
067    
068            /**
069             * War file separator.
070             */
071            public static final String WAR_SEPARATOR = "_WAR_";
072    
073            /**
074             * Returns a properly assembled portlet ID from the parameters passed. If
075             * the portlet ID contains an instance ID it will be properly retained. If
076             * the portlet ID contains a user ID it will be replaced by the user ID
077             * parameter.
078             *
079             * @param  portletId the portlet ID
080             * @param  userId a user ID
081             * @return the properly assembled portlet ID
082             */
083            public static String assemblePortletId(String portletId, long userId) {
084                    return assemblePortletId(portletId, userId, null);
085            }
086    
087            /**
088             * Returns a properly assembled portlet ID from the parameters passed. If
089             * the portlet ID contains a user ID it will be replaced by the user ID
090             * parameter. If the portlet ID contains an instance ID it will be replaced
091             * by the instance ID parameter.
092             *
093             * @param  portletId the portlet ID
094             * @param  userId the user ID
095             * @param  instanceId an instance ID
096             * @return the properly assembled portlet ID
097             */
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            /**
129             * Returns a properly assembled portlet ID from the parameters passed. If
130             * the portlet ID contains a user ID it will be properly retained. If the
131             * portlet ID contains an instance ID it will be replaced by the instance ID
132             * parameter.
133             *
134             * @param  portletId the portlet ID
135             * @param  instanceId an instance ID
136             * @return the properly assembled portlet ID
137             */
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            /**
149             * Returns the instance ID of the portlet.
150             *
151             * @param  portletId the portlet ID
152             * @return the instance ID of the portlet
153             */
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            /**
165             * Returns the root portlet ID of the portlet.
166             *
167             * @param  portletId the portlet ID
168             * @return the root portlet ID of the portlet
169             */
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            /**
185             * Returns the user ID of the portlet. This only applies when the portlet is
186             * added by a user to a page in customizable mode.
187             *
188             * @param  portletId the portlet ID
189             * @return the user ID of the portlet
190             */
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            /**
218             * Returns <code>true</code> if the portlet ID contains an instance ID.
219             *
220             * @param  portletId the portlet ID
221             * @return <code>true</code> if the portlet ID contains an instance ID;
222             *         <code>false</code> otherwise
223             */
224            public static boolean hasInstanceId(String portletId) {
225                    return portletId.contains(INSTANCE_SEPARATOR);
226            }
227    
228            /**
229             * Returns <code>true</code> if the portlet ID contains a user ID.
230             *
231             * @param  portletId the portlet ID
232             * @return <code>true</code> if the portlet ID contains a user ID;
233             *         <code>false</code> otherwise
234             */
235            public static boolean hasUserId(String portletId) {
236                    return portletId.contains(USER_SEPARATOR);
237            }
238    
239    }