001    /**
002     * Copyright (c) 2000-2012 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.Validator;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class PortletConstants {
025    
026            /**
027             * Default preferences.
028             */
029            public static final String DEFAULT_PREFERENCES = "<portlet-preferences />";
030    
031            /**
032             * Facebook integration method for FBML.
033             */
034            public static final String FACEBOOK_INTEGRATION_FBML = "fbml";
035    
036            /**
037             * Facebook integration method for IFrame.
038             */
039            public static final String FACEBOOK_INTEGRATION_IFRAME = "iframe";
040    
041            /**
042             * Instance separator.
043             */
044            public static final String INSTANCE_SEPARATOR = "_INSTANCE_";
045    
046            /**
047             * Layout separator.
048             */
049            public static final String LAYOUT_SEPARATOR = "_LAYOUT_";
050    
051            /**
052             * User principal strategy for screen name.
053             */
054            public static final String USER_PRINCIPAL_STRATEGY_SCREEN_NAME =
055                    "screenName";
056    
057            /**
058             * User principal strategy for screen name.
059             */
060            public static final String USER_PRINCIPAL_STRATEGY_USER_ID = "userId";
061    
062            /**
063             * User separator.
064             */
065            public static final String USER_SEPARATOR = "_USER_";
066    
067            /**
068             * War file separator.
069             */
070            public static final String WAR_SEPARATOR = "_WAR_";
071    
072            /**
073             * Returns a properly assembled portlet ID from the parameters passed. If
074             * the portlet ID contains an instance ID it will be properly retained. If
075             * the portlet ID contains a user ID it will be replaced by the user ID
076             * parameter.
077             *
078             * @param portletId the portlet ID
079             * @param userId a user ID
080             */
081            public static String assemblePortletId(String portletId, long userId) {
082                    return assemblePortletId(portletId, userId, null);
083            }
084    
085            /**
086             * Returns a properly assembled portlet ID from the parameters passed. If
087             * the portlet ID contains a user ID it will be replaced by the user ID
088             * parameter. If the portlet ID contains an instance ID it will be replaced
089             * by the instance ID parameter.
090             *
091             * @param portletId the portlet ID
092             * @param userId the user ID
093             * @param instanceId an instance ID
094             */
095            public static String assemblePortletId(
096                    String portletId, long userId, String instanceId) {
097    
098                    String rootPortletId = getRootPortletId(portletId);
099    
100                    StringBundler sb = new StringBundler(5);
101    
102                    sb.append(rootPortletId);
103    
104                    if (userId <= 0) {
105                            userId = getUserId(portletId);
106                    }
107    
108                    if (userId > 0) {
109                            sb.append(USER_SEPARATOR);
110                            sb.append(userId);
111                    }
112    
113                    if (Validator.isNull(instanceId)) {
114                            instanceId = getInstanceId(portletId);
115                    }
116    
117                    if (Validator.isNotNull(instanceId)) {
118                            sb.append(INSTANCE_SEPARATOR);
119                            sb.append(instanceId);
120                    }
121    
122                    return sb.toString();
123            }
124    
125            /**
126             * Returns a properly assembled portlet ID from the parameters passed. If
127             * the portlet ID contains a user ID it will be properly retained. If the
128             * portlet ID contains an instance ID it will be replaced by the instance ID
129             * parameter.
130             *
131             * @param portletId the portlet ID
132             * @param instanceId an instance ID
133             */
134            public static String assemblePortletId(
135                    String portletId, String instanceId) {
136    
137                    return assemblePortletId(portletId, 0, instanceId);
138            }
139    
140            /**
141             * Returns the instance ID of the portlet.
142             *
143             * @param  portletId the portlet ID
144             * @return the instance ID of the portlet
145             */
146            public static String getInstanceId(String portletId) {
147                    int pos = portletId.indexOf(INSTANCE_SEPARATOR);
148    
149                    if (pos == -1) {
150                            return null;
151                    }
152    
153                    return portletId.substring(pos + INSTANCE_SEPARATOR.length());
154            }
155    
156            /**
157             * Returns the root portlet ID of the portlet.
158             *
159             * @param  portletId the portlet ID
160             * @return the root portlet ID of the portlet
161             */
162            public static String getRootPortletId(String portletId) {
163                    int x = portletId.indexOf(USER_SEPARATOR);
164                    int y = portletId.indexOf(INSTANCE_SEPARATOR);
165    
166                    if ((x == -1) && (y == -1)) {
167                            return portletId;
168                    }
169                    else if (x != -1) {
170                            return portletId.substring(0, x);
171                    }
172    
173                    return portletId.substring(0, y);
174            }
175    
176            /**
177             * Returns the user ID of the portlet. This only applies when the portlet is
178             * added by a user to a page in customizable mode.
179             *
180             * @param  portletId the portlet ID
181             * @return the user ID of the portlet
182             */
183            public static long getUserId(String portletId) {
184                    int x = portletId.indexOf(USER_SEPARATOR);
185                    int y = portletId.indexOf(INSTANCE_SEPARATOR);
186    
187                    if (x == -1) {
188                            return 0;
189                    }
190    
191                    if (y != -1) {
192                            return GetterUtil.getLong(
193                                    portletId.substring(x + USER_SEPARATOR.length(), y));
194                    }
195    
196                    return GetterUtil.getLong(
197                            portletId.substring(x + USER_SEPARATOR.length()));
198            }
199    
200            public static boolean hasIdenticalRootPortletId(
201                    String portletId1, String portletId2) {
202    
203                    String rootPortletId1 = getRootPortletId(portletId1);
204                    String rootPortletId2 = getRootPortletId(portletId2);
205    
206                    return rootPortletId1.equals(rootPortletId2);
207            }
208    
209            /**
210             * Returns <code>true</code> if the portlet ID contains an instance ID.
211             *
212             * @param  portletId the portlet ID
213             * @return <code>true</code> if the portlet ID contains an instance ID;
214             *         <code>false</code> otherwise
215             */
216            public static boolean hasInstanceId(String portletId) {
217                    return portletId.contains(INSTANCE_SEPARATOR);
218            }
219    
220            /**
221             * Returns <code>true</code> if the portlet ID contains a user ID.
222             *
223             * @param  portletId the portlet ID
224             * @return <code>true</code> if the portlet ID contains a user ID;
225             *         <code>false</code> otherwise
226             */
227            public static boolean hasUserId(String portletId) {
228                    return portletId.contains(USER_SEPARATOR);
229            }
230    
231    }