001    /**
002     * Copyright (c) 2000-2013 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             * @return the properly assembled portlet ID
081             */
082            public static String assemblePortletId(String portletId, long userId) {
083                    return assemblePortletId(portletId, userId, null);
084            }
085    
086            /**
087             * Returns a properly assembled portlet ID from the parameters passed. If
088             * the portlet ID contains a user ID it will be replaced by the user ID
089             * parameter. If the portlet ID contains an instance ID it will be replaced
090             * by the instance ID parameter.
091             *
092             * @param  portletId the portlet ID
093             * @param  userId the user ID
094             * @param  instanceId an instance ID
095             * @return the properly assembled portlet ID
096             */
097            public static String assemblePortletId(
098                    String portletId, long userId, String instanceId) {
099    
100                    String rootPortletId = getRootPortletId(portletId);
101    
102                    StringBundler sb = new StringBundler(5);
103    
104                    sb.append(rootPortletId);
105    
106                    if (userId <= 0) {
107                            userId = getUserId(portletId);
108                    }
109    
110                    if (userId > 0) {
111                            sb.append(USER_SEPARATOR);
112                            sb.append(userId);
113                    }
114    
115                    if (Validator.isNull(instanceId)) {
116                            instanceId = getInstanceId(portletId);
117                    }
118    
119                    if (Validator.isNotNull(instanceId)) {
120                            sb.append(INSTANCE_SEPARATOR);
121                            sb.append(instanceId);
122                    }
123    
124                    return sb.toString();
125            }
126    
127            /**
128             * Returns a properly assembled portlet ID from the parameters passed. If
129             * the portlet ID contains a user ID it will be properly retained. If the
130             * portlet ID contains an instance ID it will be replaced by the instance ID
131             * parameter.
132             *
133             * @param  portletId the portlet ID
134             * @param  instanceId an instance ID
135             * @return the properly assembled portlet ID
136             */
137            public static String assemblePortletId(
138                    String portletId, String instanceId) {
139    
140                    return assemblePortletId(portletId, 0, instanceId);
141            }
142    
143            /**
144             * Returns the instance ID of the portlet.
145             *
146             * @param  portletId the portlet ID
147             * @return the instance ID of the portlet
148             */
149            public static String getInstanceId(String portletId) {
150                    int pos = portletId.indexOf(INSTANCE_SEPARATOR);
151    
152                    if (pos == -1) {
153                            return null;
154                    }
155    
156                    return portletId.substring(pos + INSTANCE_SEPARATOR.length());
157            }
158    
159            /**
160             * Returns the root portlet ID of the portlet.
161             *
162             * @param  portletId the portlet ID
163             * @return the root portlet ID of the portlet
164             */
165            public static String getRootPortletId(String portletId) {
166                    int x = portletId.indexOf(USER_SEPARATOR);
167                    int y = portletId.indexOf(INSTANCE_SEPARATOR);
168    
169                    if ((x == -1) && (y == -1)) {
170                            return portletId;
171                    }
172                    else if (x != -1) {
173                            return portletId.substring(0, x);
174                    }
175    
176                    return portletId.substring(0, y);
177            }
178    
179            /**
180             * Returns the user ID of the portlet. This only applies when the portlet is
181             * added by a user to a page in customizable mode.
182             *
183             * @param  portletId the portlet ID
184             * @return the user ID of the portlet
185             */
186            public static long getUserId(String portletId) {
187                    int x = portletId.indexOf(USER_SEPARATOR);
188                    int y = portletId.indexOf(INSTANCE_SEPARATOR);
189    
190                    if (x == -1) {
191                            return 0;
192                    }
193    
194                    if (y != -1) {
195                            return GetterUtil.getLong(
196                                    portletId.substring(x + USER_SEPARATOR.length(), y));
197                    }
198    
199                    return GetterUtil.getLong(
200                            portletId.substring(x + USER_SEPARATOR.length()));
201            }
202    
203            public static boolean hasIdenticalRootPortletId(
204                    String portletId1, String portletId2) {
205    
206                    String rootPortletId1 = getRootPortletId(portletId1);
207                    String rootPortletId2 = getRootPortletId(portletId2);
208    
209                    return rootPortletId1.equals(rootPortletId2);
210            }
211    
212            /**
213             * Returns <code>true</code> if the portlet ID contains an instance ID.
214             *
215             * @param  portletId the portlet ID
216             * @return <code>true</code> if the portlet ID contains an instance ID;
217             *         <code>false</code> otherwise
218             */
219            public static boolean hasInstanceId(String portletId) {
220                    return portletId.contains(INSTANCE_SEPARATOR);
221            }
222    
223            /**
224             * Returns <code>true</code> if the portlet ID contains a user ID.
225             *
226             * @param  portletId the portlet ID
227             * @return <code>true</code> if the portlet ID contains a user ID;
228             *         <code>false</code> otherwise
229             */
230            public static boolean hasUserId(String portletId) {
231                    return portletId.contains(USER_SEPARATOR);
232            }
233    
234    }