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.ugc.client.export;
029
030import org.opencms.ugc.client.CmsRequestCounter;
031import org.opencms.ugc.client.CmsRpcCallHelper;
032import org.opencms.ugc.shared.CmsUgcConstants;
033import org.opencms.ugc.shared.CmsUgcContent;
034import org.opencms.ugc.shared.CmsUgcException;
035import org.opencms.ugc.shared.rpc.I_CmsUgcEditService;
036import org.opencms.ugc.shared.rpc.I_CmsUgcEditServiceAsync;
037import org.opencms.util.CmsUUID;
038
039import org.timepedia.exporter.client.Export;
040import org.timepedia.exporter.client.ExportPackage;
041import org.timepedia.exporter.client.Exportable;
042import org.timepedia.exporter.client.NoExport;
043
044import com.google.gwt.core.client.GWT;
045import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
046import com.google.gwt.core.client.JavaScriptObject;
047import com.google.gwt.dom.client.Element;
048import com.google.gwt.user.client.Window;
049import com.google.gwt.user.client.rpc.AsyncCallback;
050import com.google.gwt.user.client.rpc.ServiceDefTarget;
051
052/**
053 * Root access point for the Javascript form editing API exported with gwt-exporter. Can be used
054 * to acquire new form editing sessions.<p>
055 */
056@Export
057@ExportPackage("opencmsugc")
058public class CmsXmlContentUgcApi implements Exportable {
059
060    /** The request counter. */
061    private CmsRequestCounter m_requestCounter = new CmsRequestCounter();
062
063    /** Service instance. */
064    @NoExport
065    public static final I_CmsUgcEditServiceAsync SERVICE = GWT.create(I_CmsUgcEditService.class);
066
067    /** The service URL. */
068    @NoExport
069    public static final String SERVICE_URL;
070
071    /**
072     * Default constructor for gwt-exporter.<p>
073     */
074    public CmsXmlContentUgcApi() {
075
076        GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
077
078            public void onUncaughtException(final Throwable e) {
079
080                handleError(e, new I_CmsErrorCallback() {
081
082                    public void call(String errorType, String message, JavaScriptObject additionalData) {
083
084                        throw new RuntimeException(e);
085                    }
086                });
087
088            }
089        });
090
091    }
092
093    static {
094        String url = Window.Location.getHref();
095        // cut off fragment, parameters, and trailing slash, then append service name
096        url = url.replaceAll("#.*$", "").replaceAll("\\?.*$", "").replaceAll("/$", "")
097            + "/org.opencms.ugc.CmsUgcEditService.gwt";
098        SERVICE_URL = url;
099        ((ServiceDefTarget)SERVICE).setServiceEntryPoint(SERVICE_URL);
100
101    }
102
103    /**
104     * Gets the request counter.<p>
105     *
106     * @return the request counter
107     */
108    @NoExport
109    public CmsRequestCounter getRequestCounter() {
110
111        return m_requestCounter;
112    }
113
114    /**
115     * Creates an RPC helper object.<p>
116     *
117     * @return the RPC helper
118     */
119    @NoExport
120    public CmsRpcCallHelper getRpcHelper() {
121
122        return new CmsRpcCallHelper(m_requestCounter);
123    }
124
125    /**
126     * Passes an exception to the given error handling callback and optionally outputs some debug info.<p>
127     *
128     * @param e the exception
129     * @param callback  the error handling callback
130     */
131    public void handleError(Throwable e, I_CmsErrorCallback callback) {
132
133        String errorCode = CmsUgcConstants.ErrorCode.errMisc.toString();
134        String message;
135        if (e instanceof CmsUgcException) {
136            CmsUgcException formException = (CmsUgcException)e;
137            errorCode = formException.getErrorCode().toString();
138            message = formException.getUserMessage();
139        } else {
140            message = e.getMessage();
141        }
142        if (callback != null) {
143            callback.call(errorCode, message, JavaScriptObject.createObject());
144        }
145    }
146
147    /**
148     * Loads a pre-created session.<p>
149     *
150     * @param sessionId the session id
151     * @param formElement the form element
152     * @param onSuccess the callback to call in case of success
153     * @param onError the callback to call in case of an error
154     */
155    public void initFormForSession(
156        final String sessionId,
157        final Element formElement,
158        final I_CmsClientCmsUgcSessionCallback onSuccess,
159        final I_CmsErrorCallback onError) {
160
161        getRpcHelper().executeRpc(SERVICE.getContent(new CmsUUID(sessionId), new AsyncCallback<CmsUgcContent>() {
162
163            public void onFailure(Throwable caught) {
164
165                handleError(caught, onError);
166            }
167
168            public void onSuccess(CmsUgcContent result) {
169
170                CmsClientUgcSession session = new CmsClientUgcSession(CmsXmlContentUgcApi.this, result);
171                session.initFormElement(formElement);
172                onSuccess.call(session);
173            }
174        }));
175    }
176
177    /**
178     * Sets the error callback for all uncaught exceptions.<p>
179     *
180     * @param callback the error callback
181     */
182    public void setErrorCallback(final I_CmsStringArrayCallback callback) {
183
184        if (callback != null) {
185            GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
186
187                public void onUncaughtException(Throwable e) {
188
189                    String[] stack = new String[e.getStackTrace().length];
190                    for (int i = 0; i < e.getStackTrace().length; i++) {
191                        StackTraceElement stackEl = e.getStackTrace()[i];
192                        stack[i] = stackEl.toString();
193                    }
194                    callback.call(stack);
195                }
196
197            });
198        } else {
199            GWT.setUncaughtExceptionHandler(null);
200        }
201    }
202
203    /**
204     * Sets the wait indicator callback.<p>
205     *
206     * @param callback a callback used to switch the wait indicator off or on.<p>
207     */
208    public void setWaitIndicatorCallback(I_CmsBooleanCallback callback) {
209
210        m_requestCounter.setCallback(callback);
211    }
212}