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