001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web.client.admin;
017
018import com.google.gwt.core.client.GWT;
019import com.google.gwt.user.client.rpc.AsyncCallback;
020import com.smartgwt.client.types.FormMethod;
021import com.smartgwt.client.util.SC;
022import com.smartgwt.client.widgets.Canvas;
023import com.smartgwt.client.widgets.Label;
024import com.smartgwt.client.widgets.events.ClickEvent;
025import com.smartgwt.client.widgets.events.ClickHandler;
026import com.smartgwt.client.widgets.form.DynamicForm;
027import com.smartgwt.client.widgets.form.fields.HiddenItem;
028import com.smartgwt.client.widgets.layout.VLayout;
029import org.modeshape.web.shared.BackupParams;
030
031/**
032 *
033 * @author kulikov
034 */
035public class BackupDownloadControl extends VLayout {
036
037    private final DynamicForm form = new DynamicForm();
038    private final HiddenItem fileField = new HiddenItem("file");
039    
040    private final AdminView adminView;
041    private final BackupOptionsDialog optionsDialog = new BackupOptionsDialog(this);
042    
043    public BackupDownloadControl(final AdminView adminView) {
044        super();
045        this.adminView = adminView;
046        
047        setStyleName("admin-control");
048
049        form.setItems(fileField);
050        
051        Label label = new Label("Backup & Download");
052        label.setStyleName("button-label");
053        label.setHeight(25);
054        label.setIcon("icons/data.png");
055        label.addClickHandler(new ClickHandler() {
056            @Override
057            public void onClick(ClickEvent event) {
058                optionsDialog.showModal();
059            }
060        });
061
062        Canvas text = new Canvas();
063        text.setAutoHeight();
064        text.setContents("Create backups of an entire repository (even when "
065                + "the repository is in use) and download zip archive "
066                + "This works regardless of where the repository content " + "is persisted.");
067
068        addMember(label);
069        addMember(text);
070        addMember(form);
071
072    }
073
074    protected void backupAndDownload(BackupParams params) {
075        final String name = Long.toString(System.currentTimeMillis());
076        fileField.setValue(name);
077        adminView.jcrService().backup(adminView.repository(), name, params, new AsyncCallback<Object>() {
078            @Override
079            public void onFailure(Throwable caught) {
080                SC.say(caught.getMessage());
081            }
082
083            @SuppressWarnings("synthetic-access")
084            @Override
085            public void onSuccess(Object result) {
086                form.setAction(GWT.getModuleBaseForStaticFiles() + "backup/da?file=" + name);
087                form.setMethod(FormMethod.GET);
088                form.submitForm();
089            }
090        });
091    }
092}