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    import java.security.InvalidParameterException;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Jorge Ferrer
027     */
028    public class PortletInstance {
029    
030            public static final int PORTLET_INSTANCE_KEY_MAX_LENGTH =
031                    255 - PortletInstance._INSTANCE_SEPARATOR.length() +
032                            PortletInstance._USER_SEPARATOR.length() + 39;
033    
034            public static PortletInstance fromPortletInstanceKey(
035                    String portletInstanceKey) {
036    
037                    return new PortletInstance(
038                            _getPortletName(portletInstanceKey), _getUserId(portletInstanceKey),
039                            _getInstanceId(portletInstanceKey));
040            }
041    
042            public PortletInstance(String portletName) {
043                    this(portletName, StringUtil.randomString(12));
044            }
045    
046            public PortletInstance(String portletName, long userId) {
047                    this(portletName, userId, null);
048            }
049    
050            public PortletInstance(String portletName, long userId, String instanceId) {
051                    validatePortletName(portletName);
052    
053                    _portletName = portletName;
054                    _userId = userId;
055                    _instanceId = instanceId;
056            }
057    
058            public PortletInstance(String portletName, String instanceId) {
059                    this(portletName, 0, instanceId);
060            }
061    
062            public String getInstanceId() {
063                    return _instanceId;
064            }
065    
066            public String getPortletInstanceKey() {
067                    StringBundler sb = new StringBundler(5);
068    
069                    sb.append(_portletName);
070    
071                    if (_userId > 0) {
072                            sb.append(_USER_SEPARATOR);
073                            sb.append(_userId);
074                    }
075    
076                    if (Validator.isNotNull(_instanceId)) {
077                            sb.append(_INSTANCE_SEPARATOR);
078                            sb.append(_instanceId);
079                    }
080    
081                    return sb.toString();
082            }
083    
084            public String getPortletName() {
085                    return _portletName;
086            }
087    
088            public long getUserId() {
089                    return _userId;
090            }
091    
092            public boolean hasIdenticalPortletName(PortletInstance portletInstance) {
093                    return hasIdenticalPortletName(portletInstance.getPortletName());
094            }
095    
096            public boolean hasIdenticalPortletName(String portletName) {
097                    return _portletName.equals(portletName);
098            }
099    
100            public boolean hasInstanceId() {
101                    return Validator.isNotNull(_instanceId);
102            }
103    
104            public boolean hasUserId() {
105                    if (_userId > 0) {
106                            return true;
107                    }
108    
109                    return false;
110            }
111    
112            @Override
113            public String toString() {
114                    return getPortletInstanceKey();
115            }
116    
117            private static String _getInstanceId(String portletInstanceKey) {
118                    int index = portletInstanceKey.indexOf(_INSTANCE_SEPARATOR);
119    
120                    if (index == -1) {
121                            return null;
122                    }
123    
124                    return portletInstanceKey.substring(
125                            index + _INSTANCE_SEPARATOR.length());
126            }
127    
128            private static String _getPortletName(String portletInstanceKey) {
129                    int x = portletInstanceKey.indexOf(_USER_SEPARATOR);
130                    int y = portletInstanceKey.indexOf(_INSTANCE_SEPARATOR);
131    
132                    if ((x == -1) && (y == -1)) {
133                            return portletInstanceKey;
134                    }
135                    else if (x != -1) {
136                            return portletInstanceKey.substring(0, x);
137                    }
138    
139                    return portletInstanceKey.substring(0, y);
140            }
141    
142            private static long _getUserId(String portletInstanceKey) {
143                    int x = portletInstanceKey.indexOf(_USER_SEPARATOR);
144                    int y = portletInstanceKey.indexOf(_INSTANCE_SEPARATOR);
145    
146                    if (x == -1) {
147                            return 0;
148                    }
149    
150                    if (y != -1) {
151                            return GetterUtil.getLong(
152                                    portletInstanceKey.substring(x + _USER_SEPARATOR.length(), y));
153                    }
154    
155                    return GetterUtil.getLong(
156                            portletInstanceKey.substring(x + _USER_SEPARATOR.length()));
157            }
158    
159            private void validatePortletName(String portletName) {
160                    for (String keyword : _PORTLET_NAME_KEYWORDS) {
161                            if (!portletName.contains(keyword)) {
162                                    continue;
163                            }
164    
165                            throw new InvalidParameterException(
166                                    "The portletName '" + portletName +
167                                            "' must not contain the keyword " + keyword);
168                    }
169            }
170    
171            private static final String _INSTANCE_SEPARATOR = "_INSTANCE_";
172    
173            private static final String _USER_SEPARATOR = "_USER_";
174    
175            private final String[] _PORTLET_NAME_KEYWORDS =
176                    {_INSTANCE_SEPARATOR, _USER_SEPARATOR};
177    
178            private final String _instanceId;
179            private final String _portletName;
180            private final long _userId;
181    
182    }