001: package org.drools.brms.client.admin;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.drools.brms.client.common.ErrorPopup;
020: import org.drools.brms.client.common.FormStyleLayout;
021: import org.drools.brms.client.common.HTMLFileManagerFields;
022: import org.drools.brms.client.common.LoadingPopup;
023:
024: import com.google.gwt.core.client.GWT;
025: import com.google.gwt.user.client.Window;
026: import com.google.gwt.user.client.ui.Button;
027: import com.google.gwt.user.client.ui.ClickListener;
028: import com.google.gwt.user.client.ui.Composite;
029: import com.google.gwt.user.client.ui.FileUpload;
030: import com.google.gwt.user.client.ui.FormHandler;
031: import com.google.gwt.user.client.ui.FormPanel;
032: import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
033: import com.google.gwt.user.client.ui.FormSubmitEvent;
034: import com.google.gwt.user.client.ui.HTML;
035: import com.google.gwt.user.client.ui.HorizontalPanel;
036: import com.google.gwt.user.client.ui.Widget;
037:
038: /**
039: *
040: * @author Fernando Meyer
041: */
042: public class BackupManager extends Composite {
043:
044: public BackupManager() {
045:
046: FormStyleLayout widtab = new FormStyleLayout(
047: "images/backup_large.png", "Import/Export");
048:
049: widtab.addAttribute("", new HTML(
050: "<i>Import and Export rules repository</i>"));
051: widtab.addRow(new HTML("<hr/>"));
052: widtab.addAttribute("Import from an xml file",
053: newImportWidget());
054: widtab.addAttribute("Export to a zip file", newExportWidget());
055: widtab.addRow(new HTML("<hr/>"));
056: // widtab.addAttribute( "Delete rules repository",
057: // cleanRepository() );
058:
059: initWidget(widtab);
060:
061: }
062:
063: private Widget newExportWidget() {
064: HorizontalPanel horiz = new HorizontalPanel();
065:
066: Button create = new Button("Export");
067: create.addClickListener(new ClickListener() {
068: public void onClick(Widget w) {
069: exportRepository();
070: }
071: });
072:
073: horiz.add(create);
074: return horiz;
075: }
076:
077: // private Widget cleanRepository() {
078: // HorizontalPanel horiz = new HorizontalPanel();
079: //
080: // Button delete = new Button( "Execute" );
081: // delete.addClickListener( new ClickListener() {
082: // public void onClick(Widget w) {
083: // if ( Window.confirm( "Are you REALLY REALLY sure you want to erase you repository contents?" ) ) {
084: // RepositoryServiceFactory.getService().clearRulesRepository( new GenericCallback() {
085: // public void onSuccess(Object data) {
086: // Window.alert( "Rules repository deleted." );
087: // }
088: // });
089: // } else {
090: // Window.alert( "Operation cancelled" );
091: // }
092: // }
093: // } );
094: //
095: // horiz.add( delete );
096: // return horiz;
097: // }
098:
099: private Widget newImportWidget() {
100:
101: final FormPanel uploadFormPanel = new FormPanel();
102: uploadFormPanel.setAction(GWT.getModuleBaseURL() + "backup");
103: uploadFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
104: uploadFormPanel.setMethod(FormPanel.METHOD_POST);
105:
106: HorizontalPanel panel = new HorizontalPanel();
107: uploadFormPanel.setWidget(panel);
108:
109: final FileUpload upload = new FileUpload();
110: upload
111: .setName(HTMLFileManagerFields.FILE_UPLOAD_FIELD_NAME_IMPORT);
112: panel.add(upload);
113:
114: panel.add(new Button("Import", new ClickListener() {
115: public void onClick(Widget sender) {
116: doImportFile(uploadFormPanel);
117: }
118:
119: private void doImportFile(final FormPanel uploadFormPanel) {
120: if (Window
121: .confirm("Are you sure you want to import? this will erase any content in the "
122: + "repository currently?")) {
123: LoadingPopup
124: .showMessage("Importing repository, please wait, as this could take some time...");
125: uploadFormPanel.submit();
126: }
127: }
128: }));
129:
130: uploadFormPanel.addFormHandler(new FormHandler() {
131: public void onSubmitComplete(FormSubmitCompleteEvent event) {
132: if (event.getResults().indexOf("OK") > -1) {
133: Window
134: .alert("Rules repository imported successfully. Please refresh your browser (F5) to show the new content. ");
135: } else {
136: ErrorPopup
137: .showMessage("Unable to import into the repository. Consult the server logs for error messages.");
138: }
139: LoadingPopup.close();
140: }
141:
142: public void onSubmit(FormSubmitEvent event) {
143: if (upload.getFilename().length() == 0) {
144: Window
145: .alert("You did not specify an exported repository filename !");
146: event.setCancelled(true);
147: } else if (!upload.getFilename().endsWith(".xml")) {
148: Window
149: .alert("Please specify a valid repository xml file.");
150: event.setCancelled(true);
151: }
152:
153: }
154: });
155:
156: return uploadFormPanel;
157: }
158:
159: private void exportRepository() {
160:
161: LoadingPopup
162: .showMessage("Exporting repository, please wait, as this could take some time...");
163:
164: Window.open(
165: GWT.getModuleBaseURL() + "backup?"
166: + HTMLFileManagerFields.FORM_FIELD_REPOSITORY
167: + "=true", "downloading",
168: "resizable=no,scrollbars=yes,status=no");
169:
170: LoadingPopup.close();
171: }
172:
173: }
|