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.query; 017 018import com.google.gwt.user.client.rpc.AsyncCallback; 019import com.smartgwt.client.types.ListGridFieldType; 020import com.smartgwt.client.util.SC; 021import com.smartgwt.client.widgets.form.DynamicForm; 022import com.smartgwt.client.widgets.form.events.SubmitValuesEvent; 023import com.smartgwt.client.widgets.form.events.SubmitValuesHandler; 024import com.smartgwt.client.widgets.form.fields.ComboBoxItem; 025import com.smartgwt.client.widgets.form.fields.SubmitItem; 026import com.smartgwt.client.widgets.form.fields.TextAreaItem; 027import com.smartgwt.client.widgets.grid.ListGrid; 028import com.smartgwt.client.widgets.grid.ListGridField; 029import com.smartgwt.client.widgets.grid.ListGridRecord; 030import java.util.Collection; 031import org.modeshape.web.client.Console; 032import org.modeshape.web.shared.Form; 033import org.modeshape.web.shared.ResultSet; 034 035/** 036 * 037 * @author kulikov 038 */ 039public class QueryForm extends Form { 040 private final static String DEFAULT_LANG = "JCR-SQL2"; 041 042 private final TextAreaItem queryEditor = new TextAreaItem(); 043 private final SubmitItem execButton = new SubmitItem("Execute"); 044 private final ComboBoxItem langBox = new ComboBoxItem("Query language"); 045 private final ListGrid grid = new ListGrid(); 046 047 private final Console console; 048 049 public QueryForm(final Console console) { 050 this.console = console; 051 setWidth100(); 052 053 grid.setWidth100(); 054 grid.setHeight(380); 055 056 DynamicForm queryForm = new DynamicForm(); 057 queryForm.setBackgroundColor("#e6f1f6"); 058 queryForm.setID("query-form-1"); 059 queryForm.setNumCols(3); 060 061 queryEditor.setName("query"); 062 queryEditor.setTitle("Query"); 063 queryEditor.setStartRow(true); 064 queryEditor.setEndRow(false); 065 queryEditor.setWidth(500); 066 067 execButton.setStartRow(false); 068 execButton.setEndRow(true); 069 queryForm.setItems(queryEditor, execButton, langBox); 070 queryForm.addSubmitValuesHandler(new SubmitValuesHandler() { 071 @Override 072 public void onSubmitValues(SubmitValuesEvent event) { 073 console.jcrService().query(console.contents().repository(), 074 console.contents().workspace(), 075 queryEditor.getEnteredValue(), 076 langBox.getEnteredValue(), 077 new AsyncCallback<ResultSet>() { 078 @Override 079 public void onFailure(Throwable caught) { 080 SC.say(caught.getMessage()); 081 } 082 083 @Override 084 public void onSuccess(ResultSet data) { 085 displayResultSet(data); 086 } 087 }); 088 } 089 }); 090 091 addMember(queryForm); 092 addMember(grid); 093 } 094 095 private void displayResultSet(ResultSet rs) { 096 String[] columnNames = rs.getColumnNames(); 097 ListGridField[] fields = new ListGridField[columnNames.length + 1]; 098 099 fields[0] = new ListGridField("icon", " "); 100 fields[0].setCanEdit(false); 101 fields[0].setImageURLPrefix("icons/bullet_"); 102 fields[0].setImageURLSuffix(".png"); 103 fields[0].setWidth(30); 104 fields[0].setType(ListGridFieldType.IMAGE); 105 106 for (int i = 1; i < fields.length; i++) { 107 fields[i] = new ListGridField(columnNames[i - 1], columnNames[i - 1]); 108 fields[i].setCanEdit(false); 109 fields[i].setShowHover(true); 110 } 111 112 grid.setFields(fields); 113 114 Collection<String[]> rows = rs.getRows(); 115 ListGridRecord[] tbl = new ListGridRecord[rows.size()]; 116 int j = 0; 117 for (String[] columns : rows) { 118 ListGridRecord rec = new ListGridRecord(); 119 rec.setAttribute("icon", "blue"); 120 for (int i = 0; i < columns.length; i++) { 121 rec.setAttribute(columnNames[i], columns[i]); 122 } 123 tbl[j++] = rec; 124 } 125 126 grid.setData(tbl); 127 grid.show(); 128 } 129 130 @Override 131 public void init() { 132 console.jcrService().supportedQueryLanguages(console.contents().repository(), 133 console.contents().workspace(),new AsyncCallback<String[]>() { 134 135 @Override 136 public void onFailure(Throwable caught) { 137 SC.say(caught.getMessage()); 138 } 139 140 @Override 141 public void onSuccess(String[] result) { 142 langBox.setValueMap(result); 143 langBox.setValue(defaultLang(result)); 144 } 145 }); 146 } 147 148 private String defaultLang(String[] options) { 149 for (String option : options) { 150 if (option.toUpperCase().equals(DEFAULT_LANG)) { 151 return option; 152 } 153 } 154 return ""; 155 } 156 157 158}