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.impl;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.UniqueList;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.xml.QName;
022    import com.liferay.portal.model.EventDefinition;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PortletApp;
025    import com.liferay.portal.model.PortletFilter;
026    import com.liferay.portal.model.PortletURLListener;
027    import com.liferay.portal.model.PublicRenderParameter;
028    import com.liferay.portal.model.SpriteImage;
029    
030    import java.util.HashMap;
031    import java.util.Iterator;
032    import java.util.LinkedHashMap;
033    import java.util.LinkedHashSet;
034    import java.util.List;
035    import java.util.Map;
036    import java.util.Properties;
037    import java.util.Set;
038    
039    import javax.xml.XMLConstants;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class PortletAppImpl implements PortletApp {
045    
046            public PortletAppImpl(String servletContextName) {
047                    _servletContextName = servletContextName;
048    
049                    if (Validator.isNotNull(_servletContextName)) {
050                            _contextPath = StringPool.SLASH.concat(_servletContextName);
051                            _warFile = true;
052                    }
053                    else {
054                            _warFile = false;
055                    }
056            }
057    
058            @Override
059            public void addEventDefinition(EventDefinition eventDefinition) {
060                    _eventDefinitions.add(eventDefinition);
061            }
062    
063            @Override
064            public void addPortlet(Portlet portlet) {
065                    _portlets.add(portlet);
066            }
067    
068            @Override
069            public void addPortletFilter(PortletFilter portletFilter) {
070                    _portletFilters.add(portletFilter);
071                    _portletFiltersByFilterName.put(
072                            portletFilter.getFilterName(), portletFilter);
073            }
074    
075            @Override
076            public void addPortletURLListener(PortletURLListener portletURLListener) {
077                    _portletURLListeners.add(portletURLListener);
078                    _portletURLListenersByListenerClass.put(
079                            portletURLListener.getListenerClass(), portletURLListener);
080            }
081    
082            @Override
083            public void addPublicRenderParameter(
084                    PublicRenderParameter publicRenderParameter) {
085    
086                    _publicRenderParametersByIdentifier.put(
087                            publicRenderParameter.getIdentifier(), publicRenderParameter);
088            }
089    
090            @Override
091            public void addPublicRenderParameter(String identifier, QName qName) {
092                    PublicRenderParameter publicRenderParameter =
093                            new PublicRenderParameterImpl(identifier, qName, this);
094    
095                    addPublicRenderParameter(publicRenderParameter);
096            }
097    
098            @Override
099            public void addServletURLPatterns(Set<String> servletURLPatterns) {
100                    _servletURLPatterns.addAll(servletURLPatterns);
101            }
102    
103            @Override
104            public Map<String, String[]> getContainerRuntimeOptions() {
105                    return _containerRuntimeOptions;
106            }
107    
108            @Override
109            public String getContextPath() {
110                    return _contextPath;
111            }
112    
113            @Override
114            public Map<String, String> getCustomUserAttributes() {
115                    return _customUserAttributes;
116            }
117    
118            @Override
119            public String getDefaultNamespace() {
120                    return _defaultNamespace;
121            }
122    
123            @Override
124            public Set<EventDefinition> getEventDefinitions() {
125                    return _eventDefinitions;
126            }
127    
128            @Override
129            public PortletFilter getPortletFilter(String filterName) {
130                    return _portletFiltersByFilterName.get(filterName);
131            }
132    
133            @Override
134            public Set<PortletFilter> getPortletFilters() {
135                    return _portletFilters;
136            }
137    
138            @Override
139            public List<Portlet> getPortlets() {
140                    return _portlets;
141            }
142    
143            @Override
144            public PortletURLListener getPortletURLListener(String listenerClass) {
145                    return _portletURLListenersByListenerClass.get(listenerClass);
146            }
147    
148            @Override
149            public Set<PortletURLListener> getPortletURLListeners() {
150                    return _portletURLListeners;
151            }
152    
153            @Override
154            public PublicRenderParameter getPublicRenderParameter(String identifier) {
155                    return _publicRenderParametersByIdentifier.get(identifier);
156            }
157    
158            @Override
159            public String getServletContextName() {
160                    return _servletContextName;
161            }
162    
163            @Override
164            public Set<String> getServletURLPatterns() {
165                    return _servletURLPatterns;
166            }
167    
168            @Override
169            public SpriteImage getSpriteImage(String fileName) {
170                    return _spriteImagesMap.get(fileName);
171            }
172    
173            @Override
174            public Set<String> getUserAttributes() {
175                    return _userAttributes;
176            }
177    
178            @Override
179            public boolean isWARFile() {
180                    return _warFile;
181            }
182    
183            @Override
184            public void setDefaultNamespace(String defaultNamespace) {
185                    _defaultNamespace = defaultNamespace;
186            }
187    
188            @Override
189            public void setSpriteImages(String spriteFileName, Properties properties) {
190                    Iterator<Map.Entry<Object, Object>> itr =
191                            properties.entrySet().iterator();
192    
193                    while (itr.hasNext()) {
194                            Map.Entry<Object, Object> entry = itr.next();
195    
196                            String key = (String)entry.getKey();
197                            String value = (String)entry.getValue();
198    
199                            int[] values = StringUtil.split(value, 0);
200    
201                            int offset = values[0];
202                            int height = values[1];
203                            int width = values[2];
204    
205                            SpriteImage spriteImage = new SpriteImage(
206                                    spriteFileName, key, offset, height, width);
207    
208                            _spriteImagesMap.put(key, spriteImage);
209                    }
210            }
211    
212            @Override
213            public void setWARFile(boolean warFile) {
214                    _warFile = warFile;
215            }
216    
217            private Map<String, String[]> _containerRuntimeOptions =
218                    new HashMap<String, String[]>();
219            private String _contextPath = StringPool.BLANK;
220            private Map<String, String> _customUserAttributes =
221                    new LinkedHashMap<String, String>();
222            private String _defaultNamespace = XMLConstants.NULL_NS_URI;
223            private Set<EventDefinition> _eventDefinitions =
224                    new LinkedHashSet<EventDefinition>();
225            private Set<PortletFilter> _portletFilters =
226                    new LinkedHashSet<PortletFilter>();
227            private Map<String, PortletFilter> _portletFiltersByFilterName =
228                    new HashMap<String, PortletFilter>();
229            private List<Portlet> _portlets = new UniqueList<Portlet>();
230            private Set<PortletURLListener> _portletURLListeners =
231                    new LinkedHashSet<PortletURLListener>();
232            private Map<String, PortletURLListener>
233                    _portletURLListenersByListenerClass =
234                            new HashMap<String, PortletURLListener>();
235            private Map<String, PublicRenderParameter>
236                    _publicRenderParametersByIdentifier =
237                            new HashMap<String, PublicRenderParameter>();
238            private String _servletContextName = StringPool.BLANK;
239            private Set<String> _servletURLPatterns = new LinkedHashSet<String>();
240            private Map<String, SpriteImage> _spriteImagesMap =
241                    new HashMap<String, SpriteImage>();
242            private Set<String> _userAttributes = new LinkedHashSet<String>();
243            private boolean _warFile;
244    
245    }