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.components.extensions;
029
030import org.opencms.ade.galleries.shared.CmsGalleryTabConfiguration;
031import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
032import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
033import org.opencms.ade.publish.CmsPublishService;
034import org.opencms.ade.publish.shared.CmsPublishData;
035import org.opencms.ade.publish.shared.rpc.I_CmsPublishService;
036import org.opencms.file.CmsObject;
037import org.opencms.file.CmsProject;
038import org.opencms.file.CmsResource;
039import org.opencms.gwt.CmsPrefetchSerializationPolicy;
040import org.opencms.gwt.shared.CmsHistoryVersion.OfflineOnline;
041import org.opencms.json.JSONException;
042import org.opencms.json.JSONObject;
043import org.opencms.main.CmsLog;
044import org.opencms.main.OpenCms;
045import org.opencms.ui.A_CmsUI;
046import org.opencms.ui.CmsVaadinUtils;
047import org.opencms.ui.I_CmsUpdateListener;
048import org.opencms.ui.apps.CmsAppWorkplaceUi;
049import org.opencms.ui.components.CmsErrorDialog;
050import org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc;
051import org.opencms.ui.shared.components.I_CmsGwtDialogServerRpc;
052import org.opencms.util.CmsUUID;
053
054import java.util.ArrayList;
055import java.util.HashMap;
056import java.util.List;
057
058import javax.servlet.http.HttpServletRequest;
059
060import org.apache.commons.logging.Log;
061
062import com.google.gwt.user.server.rpc.RPC;
063import com.vaadin.server.AbstractExtension;
064import com.vaadin.server.VaadinService;
065import com.vaadin.ui.UI;
066
067/**
068 * Extension used to open existing GWT based dialogs (from ADE, etc.) from the server side, for use in context menu actions.<p>
069 */
070public class CmsGwtDialogExtension extends AbstractExtension implements I_CmsGwtDialogServerRpc {
071
072    /** Logger instance for this class. */
073    private static final Log LOG = CmsLog.getLog(CmsGwtDialogExtension.class);
074
075    /** Serial version id. */
076    private static final long serialVersionUID = 1L;
077
078    /** The update listener. */
079    private I_CmsUpdateListener<String> m_updateListener;
080
081    /**
082     * Creates a new instance and binds it to a UI instance.<p>
083     *
084     * @param ui the UI to bind this extension to
085     * @param updateListener the update listener
086     */
087    public CmsGwtDialogExtension(UI ui, I_CmsUpdateListener<String> updateListener) {
088
089        extend(ui);
090        m_updateListener = updateListener;
091        registerRpc(this, I_CmsGwtDialogServerRpc.class);
092    }
093
094    /**
095     * Opens the dialog for editing pointer resources.<p>
096     *
097     * @param resource the pointer resource
098     */
099    public void editPointer(CmsResource resource) {
100
101        getRpcProxy(I_CmsGwtDialogClientRpc.class).editPointer("" + resource.getStructureId());
102    }
103
104    /**
105     * Open property editor for the resource with the given structure id.<p>
106     *
107     * @param structureId the structure id of a resource
108     * @param editName controls whether the file name should be editable
109     */
110    public void editProperties(CmsUUID structureId, boolean editName) {
111
112        getRpcProxy(I_CmsGwtDialogClientRpc.class).editProperties("" + structureId, editName);
113    }
114
115    /**
116     * @see org.opencms.ui.shared.components.I_CmsGwtDialogServerRpc#onClose(boolean)
117     */
118    public void onClose(boolean reinitUI) {
119
120        remove();
121        if (reinitUI) {
122            A_CmsUI.get().reload();
123        } else {
124            CmsAppWorkplaceUi.get().enableGlobalShortcuts();
125            if (m_updateListener != null) {
126                m_updateListener.onUpdate(new ArrayList<String>());
127            }
128        }
129    }
130
131    /**
132     * @see org.opencms.ui.shared.components.I_CmsGwtDialogServerRpc#onClose(java.util.List, long)
133     */
134    public void onClose(List<String> changedStructureIds, long delayMillis) {
135
136        remove();
137        if (delayMillis > 0) {
138            try {
139                Thread.sleep(delayMillis);
140            } catch (InterruptedException e) {
141                // ignore
142            }
143        }
144        A_CmsUI ui = A_CmsUI.get();
145        if (ui instanceof CmsAppWorkplaceUi) {
146            ((CmsAppWorkplaceUi)ui).enableGlobalShortcuts();
147        }
148
149        if (m_updateListener != null) {
150            m_updateListener.onUpdate(changedStructureIds);
151        }
152
153    }
154
155    /**
156     * Opens the categories dialog for the given resource.<p>
157     *
158     * @param resource the resource
159     */
160    public void openCategories(CmsResource resource) {
161
162        getRpcProxy(I_CmsGwtDialogClientRpc.class).openCategoriesDialog(
163            resource.getStructureId().toString(),
164            OpenCms.getWorkplaceManager().isDisplayCategorySelectionCollapsed());
165    }
166
167    /**
168     * Opens the content editor in a dialog for editing the provided file.
169     * @param structureId structure id of the file to edit.
170     * @param sitePath site path of the file to edit.
171     */
172    public void openContentEditor(String structureId, String sitePath) {
173
174        getRpcProxy(I_CmsGwtDialogClientRpc.class).openContentEditor(structureId, sitePath);
175    }
176
177    /**
178     * Opens the gallery dialog for the given gallery folder.<p>
179     *
180     * @param resource the gallery folder resource
181     */
182    public void openGalleryDialog(CmsResource resource) {
183
184        try {
185            CmsObject cms = A_CmsUI.getCmsObject();
186            JSONObject conf = new JSONObject();
187            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_MODE, GalleryMode.view.name());
188            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_PATH, cms.getSitePath(resource));
189            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_STORAGE_PREFIX, "");
190            conf.put(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG, CmsGalleryTabConfiguration.TC_SELECT_ALL);
191            getRpcProxy(I_CmsGwtDialogClientRpc.class).openGalleryDialog(conf.toString());
192        } catch (JSONException e) {
193            CmsErrorDialog.showErrorDialog(e);
194        }
195    }
196
197    /**
198     * Opens the resource info dialog.<p>
199     *
200     * @param resource the resource
201     * @param startTab the start tab id
202     */
203    public void openInfoDialog(CmsResource resource, String startTab) {
204
205        getRpcProxy(I_CmsGwtDialogClientRpc.class).openInfoDialog(resource.getStructureId().toString(), startTab);
206    }
207
208    /**
209     * Opens the lock report for the given resource.<p>
210     *
211     * @param resource the resource for which to display the lock report
212     */
213    public void openLockReport(CmsResource resource) {
214
215        String dialogTitle = CmsVaadinUtils.getMessageText(org.opencms.ui.Messages.GUI_DIALOGTITLE_LOCKREPORT_0);
216        getRpcProxy(I_CmsGwtDialogClientRpc.class).openLockReport(dialogTitle, resource.getStructureId().toString());
217    }
218
219    /**
220     * Opens the publish dialog.<p>
221     */
222    public void openPublishDialog() {
223
224        openPublishDailog(null, null);
225    }
226
227    /**
228     * Opens the publish dialog for the given project.<p>
229     *
230     * @param project the project for which to open the dialog
231     */
232    public void openPublishDialog(CmsProject project) {
233
234        openPublishDailog(project, null);
235    }
236
237    /**
238     * Tells the client to open the publish dialog for the given resources.<p>
239     *
240     * @param resources the resources for which to open the publish dialog.
241     */
242    public void openPublishDialog(List<CmsResource> resources) {
243
244        openPublishDailog(null, resources);
245    }
246
247    /**
248     * Opens the 'Replace' dialog for the resource with the given structure id.<p>
249     *
250     * @param structureId the structure id
251     */
252    public void openReplaceDialog(CmsUUID structureId) {
253
254        getRpcProxy(I_CmsGwtDialogClientRpc.class).openReplaceDialog("" + structureId);
255    }
256
257    /**
258     * Shows the OpenCms about dialog.<p>
259     */
260    public void showAbout() {
261
262        getRpcProxy(I_CmsGwtDialogClientRpc.class).showAbout();
263    }
264
265    /**
266     * Shows the prewview dialog for a given resource and version.<p>
267     *
268     * @param id the structure id of the resource
269     * @param version the version
270     * @param offlineOnline indicates whether we want the offlne or online version
271     */
272    public void showPreview(CmsUUID id, Integer version, OfflineOnline offlineOnline) {
273
274        getRpcProxy(I_CmsGwtDialogClientRpc.class).showPreview("" + id, version + ":" + offlineOnline);
275    }
276
277    /**
278     * Shows the user preferences.<p>
279     */
280    public void showUserPreferences() {
281
282        getRpcProxy(I_CmsGwtDialogClientRpc.class).showUserPreferences();
283    }
284
285    /**
286     * Gets the publish data for the given resources.<p>
287     *
288     * @param directPublishResources the resources to publish
289     * @param project the project for which to open the dialog
290     *
291     * @return the publish data for the resources
292     */
293    protected CmsPublishData getPublishData(CmsProject project, List<CmsResource> directPublishResources) {
294
295        CmsPublishService publishService = new CmsPublishService();
296        CmsObject cms = A_CmsUI.getCmsObject();
297        publishService.setCms(cms);
298        List<String> pathList = new ArrayList<String>();
299        if (directPublishResources != null) {
300            for (CmsResource resource : directPublishResources) {
301                pathList.add(cms.getSitePath(resource));
302            }
303        }
304        publishService.setRequest((HttpServletRequest)(VaadinService.getCurrentRequest()));
305        try {
306            return publishService.getPublishData(
307                cms,
308                new HashMap<String, String>()/*params*/,
309                null/*workflowId*/,
310                project != null ? project.getUuid().toString() : null /*projectParam*/,
311                pathList,
312                null/*closelink*/,
313                false/*confirmation*/);
314        } catch (Exception e) {
315            LOG.error(e.getLocalizedMessage(), e);
316            return null;
317        }
318    }
319
320    /**
321     * Serializes a CmsPublishData object into string form using the GWT serialization.<p>
322     *
323     * @param data the publish data
324     *
325     * @return the serialized publish data
326     */
327    protected String getSerializedPublishData(CmsPublishData data) {
328
329        try {
330            String prefetchedData = RPC.encodeResponseForSuccess(
331                I_CmsPublishService.class.getMethod("getInitData", java.util.HashMap.class),
332                data,
333                CmsPrefetchSerializationPolicy.instance());
334            return prefetchedData;
335        } catch (Exception e) {
336            LOG.error(e.getLocalizedMessage(), e);
337            return null;
338        }
339    }
340
341    /**
342     * Opens the publish dialog for the given project.<p>
343     *
344     * @param project the project for which to open the dialog
345     * @param directPublishResources the resources for which to open the publish dialog.
346     */
347    protected void openPublishDailog(CmsProject project, List<CmsResource> directPublishResources) {
348
349        CmsPublishData publishData = getPublishData(project, directPublishResources);
350        String data = getSerializedPublishData(publishData);
351        getRpcProxy(I_CmsGwtDialogClientRpc.class).openPublishDialog(data);
352    }
353
354}