001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.actions;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsResource;
033import org.opencms.gwt.shared.CmsCoreData.AdeContext;
034import org.opencms.gwt.shared.CmsGwtConstants;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.ui.I_CmsDialogContext;
038import org.opencms.ui.actions.prefillpage.I_CmsPrefillPageHandler;
039import org.opencms.ui.actions.prefillpage.CmsStaticPrefillPageHandler;
040import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
041import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
042import org.opencms.workplace.CmsWorkplaceMessages;
043
044import java.util.Arrays;
045import java.util.HashMap;
046import java.util.List;
047import java.util.Locale;
048import java.util.Map;
049
050import org.apache.commons.logging.Log;
051
052/**
053 * Action class for filling in container page content from a 'prefill template' configured via
054 * the sitemap configuration.
055 */
056public class CmsPrefillPageAction extends A_CmsWorkplaceAction implements I_CmsADEAction {
057
058    /** Logger instance for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsPrefillPageAction.class);
060
061    /** Message key for the context menu - this is not in a message bundle class because it is not defined by default. */
062    public static final String GUI_EXPLORER_CONTEXT_PREFILL_PAGE_0 = "GUI_EXPLORER_CONTEXT_PREFILL_PAGE_0";
063
064    /** The sitemap attribute used to configure the prefill handler. */
065    private static final String ATTR_PREFILL_HANDLER = "template.prefill.handler";
066
067    /**
068     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
069     */
070    public void executeAction(I_CmsDialogContext context) {
071
072        CmsObject cms = context.getCms();
073        CmsResource resource = context.getResources().get(0);
074        CmsADEConfigData config = OpenCms.getADEManager().lookupConfiguration(cms, resource.getRootPath());
075        String prefillHandler = config.getAttribute(ATTR_PREFILL_HANDLER, null);
076        I_CmsPrefillPageHandler handler = null;
077        if (null == prefillHandler) {
078            handler = new CmsStaticPrefillPageHandler();
079        } else {
080            Class<?> handlerClass;
081            try {
082                handlerClass = Class.forName(prefillHandler);
083                if (I_CmsPrefillPageHandler.class.isAssignableFrom(handlerClass)) {
084                    handler = (I_CmsPrefillPageHandler)handlerClass.newInstance();
085                }
086            } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
087                if (LOG.isInfoEnabled()) {
088                    LOG.info(e, e);
089                } else {
090                    LOG.error(e);
091                }
092            }
093        }
094        if (handler != null) {
095            handler.execute(context);
096        } else {
097            LOG.error(
098                "Failed to execute prefill action with handler "
099                    + prefillHandler
100                    + ". The handler could not be initialized.");
101        }
102    }
103
104    /**
105     * @see org.opencms.ui.actions.I_CmsADEAction#getCommandClassName()
106     */
107    public String getCommandClassName() {
108
109        return "org.opencms.gwt.client.ui.contextmenu.CmsEmbeddedAction";
110    }
111
112    /**
113     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
114     */
115    public String getId() {
116
117        return "template_prefill";
118    }
119
120    /**
121     * @see org.opencms.ui.actions.I_CmsADEAction#getJspPath()
122     */
123    public String getJspPath() {
124
125        return null;
126    }
127
128    /**
129     * @see org.opencms.ui.actions.I_CmsADEAction#getParams()
130     */
131    public Map<String, String> getParams() {
132
133        Map<String, String> params = new HashMap<String, String>();
134        params.put(CmsGwtConstants.ACTION_PARAM_DIALOG_ID, this.getClass().getName());
135        params.put(CmsGwtConstants.PREFILL_MENU_PLACEHOLDER, "true");
136        return params;
137    }
138
139    /**
140     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitle(java.util.Locale)
141     */
142    @Override
143    public String getTitle(Locale locale) {
144
145        // we make the context menu entry label customizable by trying a message key first that isn't
146        // defined by default, and using the message key defined in the OpenCms core only as a fallback if that doesn't work.
147        CmsWorkplaceMessages messages = OpenCms.getWorkplaceManager().getMessages(locale);
148        String result = messages.key(GUI_EXPLORER_CONTEXT_PREFILL_PAGE_0, /*allowNull=*/true);
149        if (result == null) {
150            result = messages.key(org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_PREFILL_PAGE_DEFAULT_0);
151        }
152        return result;
153
154    }
155
156    /**
157     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
158     */
159    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
160
161        return null;
162    }
163
164    /**
165     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getVisibility(org.opencms.ui.I_CmsDialogContext)
166     */
167    @Override
168    public CmsMenuItemVisibilityMode getVisibility(I_CmsDialogContext context) {
169
170        if (!AdeContext.pageeditor.name().equals(context.getAppId())) {
171            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
172        }
173        List<CmsResource> resources = context.getResources();
174        if (resources.size() != 1) {
175            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
176        }
177
178        CmsObject cms = context.getCms();
179        CmsResource resource = resources.get(0);
180
181        // check permissions
182        if (!CmsStandardVisibilityCheck.DEFAULT.getVisibility(cms, Arrays.asList(resource)).isActive()) {
183            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
184        }
185
186        CmsADEConfigData config = OpenCms.getADEManager().lookupConfiguration(cms, resource.getRootPath());
187        String prefillHandler = config.getAttribute(ATTR_PREFILL_HANDLER, null);
188        I_CmsPrefillPageHandler handler = null;
189        if (null == prefillHandler) {
190            handler = new CmsStaticPrefillPageHandler();
191        } else {
192            Class<?> handlerClass;
193            try {
194                handlerClass = Class.forName(prefillHandler);
195                if (I_CmsPrefillPageHandler.class.isAssignableFrom(handlerClass)) {
196                    handler = (I_CmsPrefillPageHandler)handlerClass.newInstance();
197                }
198            } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
199                if (LOG.isInfoEnabled()) {
200                    LOG.info(e, e);
201                } else {
202                    LOG.error(e);
203                }
204            }
205        }
206        if (handler != null) {
207            if (handler.isExecutable(context)) {
208                return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
209            }
210        } else {
211            LOG.error(
212                "Failed to execute prefill action with handler "
213                    + prefillHandler
214                    + ". The handler could not be initialized.");
215        }
216        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
217
218    }
219
220    /**
221     * @see org.opencms.ui.actions.I_CmsADEAction#isAdeSupported()
222     */
223    public boolean isAdeSupported() {
224
225        return true;
226    }
227
228    /**
229     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
230     */
231    @Override
232    protected String getTitleKey() {
233
234        // not used - getTitle is implemented directly
235        return null;
236    }
237
238}