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.mobile.device.rulegroup;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManager;
021    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
022    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
023    
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.HashMap;
027    import java.util.List;
028    import java.util.Map;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Edward C. Han
035     */
036    public class DefaultActionHandlerManagerImpl implements ActionHandlerManager {
037    
038            @Override
039            public void applyActions(
040                            List<MDRAction> mdrActions, HttpServletRequest request,
041                            HttpServletResponse response)
042                    throws PortalException {
043    
044                    for (MDRAction mdrAction : mdrActions) {
045                            applyAction(mdrAction, request, response);
046                    }
047            }
048    
049            @Override
050            public ActionHandler getActionHandler(String actionType) {
051                    return _deviceActionHandlers.get(actionType);
052            }
053    
054            @Override
055            public Collection<ActionHandler> getActionHandlers() {
056                    return Collections.unmodifiableCollection(
057                            _deviceActionHandlers.values());
058            }
059    
060            @Override
061            public Collection<String> getActionHandlerTypes() {
062                    return _deviceActionHandlers.keySet();
063            }
064    
065            @Override
066            public void registerActionHandler(ActionHandler actionHandler) {
067                    ActionHandler oldActionHandler = _deviceActionHandlers.put(
068                            actionHandler.getType(), actionHandler);
069    
070                    if ((oldActionHandler != null) && _log.isWarnEnabled()) {
071                            _log.warn(
072                                    "Replacing existing action handler type " +
073                                            actionHandler.getType());
074                    }
075            }
076    
077            public void setActionHandlers(Collection<ActionHandler> actionHandlers) {
078                    for (ActionHandler actionHandler : actionHandlers) {
079                            registerActionHandler(actionHandler);
080                    }
081            }
082    
083            @Override
084            public ActionHandler unregisterActionHandler(String actionType) {
085                    return _deviceActionHandlers.remove(actionType);
086            }
087    
088            protected void applyAction(
089                            MDRAction mdrAction, HttpServletRequest request,
090                            HttpServletResponse response)
091                    throws PortalException {
092    
093                    ActionHandler actionHandler = _deviceActionHandlers.get(
094                            mdrAction.getType());
095    
096                    if (actionHandler != null) {
097                            actionHandler.applyAction(mdrAction, request, response);
098                    }
099                    else if (_log.isWarnEnabled()) {
100                            _log.warn(
101                                    "No action handler registered for type " + mdrAction.getType());
102                    }
103            }
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    DefaultActionHandlerManagerImpl.class);
107    
108            private final Map<String, ActionHandler> _deviceActionHandlers =
109                    new HashMap<>();
110    
111    }