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 gallery dialog for the given gallery folder.<p>
169     *
170     * @param resource the gallery folder resource
171     */
172    public void openGalleryDialog(CmsResource resource) {
173
174        try {
175            CmsObject cms = A_CmsUI.getCmsObject();
176            JSONObject conf = new JSONObject();
177            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_MODE, GalleryMode.view.name());
178            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_PATH, cms.getSitePath(resource));
179            conf.put(I_CmsGalleryProviderConstants.CONFIG_GALLERY_STORAGE_PREFIX, "");
180            conf.put(I_CmsGalleryProviderConstants.CONFIG_TAB_CONFIG, CmsGalleryTabConfiguration.TC_SELECT_ALL);
181            getRpcProxy(I_CmsGwtDialogClientRpc.class).openGalleryDialog(conf.toString());
182        } catch (JSONException e) {
183            CmsErrorDialog.showErrorDialog(e);
184        }
185    }
186
187    /**
188     * Opens the resource info dialog.<p>
189     *
190     * @param resource the resource
191     * @param startTab the start tab id
192     */
193    public void openInfoDialog(CmsResource resource, String startTab) {
194
195        getRpcProxy(I_CmsGwtDialogClientRpc.class).openInfoDialog(resource.getStructureId().toString(), startTab);
196    }
197
198    /**
199     * Opens the lock report for the given resource.<p>
200     *
201     * @param resource the resource for which to display the lock report
202     */
203    public void openLockReport(CmsResource resource) {
204
205        String dialogTitle = CmsVaadinUtils.getMessageText(org.opencms.ui.Messages.GUI_DIALOGTITLE_LOCKREPORT_0);
206        getRpcProxy(I_CmsGwtDialogClientRpc.class).openLockReport(dialogTitle, resource.getStructureId().toString());
207    }
208
209    /**
210     * Opens the publish dialog.<p>
211     */
212    public void openPublishDialog() {
213
214        openPublishDailog(null, null);
215    }
216
217    /**
218     * Opens the publish dialog for the given project.<p>
219     *
220     * @param project the project for which to open the dialog
221     */
222    public void openPublishDialog(CmsProject project) {
223
224        openPublishDailog(project, null);
225    }
226
227    /**
228     * Tells the client to open the publish dialog for the given resources.<p>
229     *
230     * @param resources the resources for which to open the publish dialog.
231     */
232    public void openPublishDialog(List<CmsResource> resources) {
233
234        openPublishDailog(null, resources);
235    }
236
237    /**
238     * Opens the 'Replace' dialog for the resource with the given structure id.<p>
239     *
240     * @param structureId the structure id
241     */
242    public void openReplaceDialog(CmsUUID structureId) {
243
244        getRpcProxy(I_CmsGwtDialogClientRpc.class).openReplaceDialog("" + structureId);
245    }
246
247    /**
248     * Shows the OpenCms about dialog.<p>
249     */
250    public void showAbout() {
251
252        getRpcProxy(I_CmsGwtDialogClientRpc.class).showAbout();
253    }
254
255    /**
256     * Shows the prewview dialog for a given resource and version.<p>
257     *
258     * @param id the structure id of the resource
259     * @param version the version
260     * @param offlineOnline indicates whether we want the offlne or online version
261     */
262    public void showPreview(CmsUUID id, Integer version, OfflineOnline offlineOnline) {
263
264        getRpcProxy(I_CmsGwtDialogClientRpc.class).showPreview("" + id, version + ":" + offlineOnline);
265    }
266
267    /**
268     * Shows the user preferences.<p>
269     */
270    public void showUserPreferences() {
271
272        getRpcProxy(I_CmsGwtDialogClientRpc.class).showUserPreferences();
273    }
274
275    /**
276     * Gets the publish data for the given resources.<p>
277     *
278     * @param directPublishResources the resources to publish
279     * @param project the project for which to open the dialog
280     *
281     * @return the publish data for the resources
282     */
283    protected CmsPublishData getPublishData(CmsProject project, List<CmsResource> directPublishResources) {
284
285        CmsPublishService publishService = new CmsPublishService();
286        CmsObject cms = A_CmsUI.getCmsObject();
287        publishService.setCms(cms);
288        List<String> pathList = new ArrayList<String>();
289        if (directPublishResources != null) {
290            for (CmsResource resource : directPublishResources) {
291                pathList.add(cms.getSitePath(resource));
292            }
293        }
294        publishService.setRequest((HttpServletRequest)(VaadinService.getCurrentRequest()));
295        try {
296            return publishService.getPublishData(
297                cms,
298                new HashMap<String, String>()/*params*/,
299                null/*workflowId*/,
300                project != null ? project.getUuid().toString() : null /*projectParam*/,
301                pathList,
302                null/*closelink*/,
303                false/*confirmation*/);
304        } catch (Exception e) {
305            LOG.error(e.getLocalizedMessage(), e);
306            return null;
307        }
308    }
309
310    /**
311     * Serializes a CmsPublishData object into string form using the GWT serialization.<p>
312     *
313     * @param data the publish data
314     *
315     * @return the serialized publish data
316     */
317    protected String getSerializedPublishData(CmsPublishData data) {
318
319        try {
320            String prefetchedData = RPC.encodeResponseForSuccess(
321                I_CmsPublishService.class.getMethod("getInitData", java.util.HashMap.class),
322                data,
323                CmsPrefetchSerializationPolicy.instance());
324            return prefetchedData;
325        } catch (Exception e) {
326            LOG.error(e.getLocalizedMessage(), e);
327            return null;
328        }
329    }
330
331    /**
332     * Opens the publish dialog for the given project.<p>
333     *
334     * @param project the project for which to open the dialog
335     * @param directPublishResources the resources for which to open the publish dialog.
336     */
337    protected void openPublishDailog(CmsProject project, List<CmsResource> directPublishResources) {
338
339        CmsPublishData publishData = getPublishData(project, directPublishResources);
340        String data = getSerializedPublishData(publishData);
341        getRpcProxy(I_CmsGwtDialogClientRpc.class).openPublishDialog(data);
342    }
343
344}