View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.client.ui.dist.views;
17  
18  import org.argeo.slc.SlcException;
19  import org.argeo.slc.SlcNames;
20  import org.argeo.slc.SlcTypes;
21  import org.argeo.slc.client.ui.dist.DistPlugin;
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.custom.SashForm;
24  import org.eclipse.swt.layout.FillLayout;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.layout.GridLayout;
27  import org.eclipse.swt.widgets.Button;
28  import org.eclipse.swt.widgets.Composite;
29  import org.eclipse.swt.widgets.Event;
30  import org.eclipse.swt.widgets.Label;
31  import org.eclipse.swt.widgets.Listener;
32  import org.eclipse.swt.widgets.Text;
33  
34  /** Query SLC Repo to get some artifacts given some predefined parameters */
35  public class QueryBundlesForm extends AbstractQueryArtifactsView implements
36  		SlcNames, SlcTypes {
37  	// private static final Log log = LogFactory.getLog(QueryBundlesForm.class);
38  	public static final String ID = DistPlugin.PLUGIN_ID + ".queryBundlesForm";
39  
40  	// widgets
41  	private Button executeBtn;
42  	private Text symbolicName;
43  	private Text importedPackage;
44  	private Text exportedPackage;
45  	private SashForm sashForm;
46  
47  	private Composite top, bottom;
48  
49  	@Override
50  	public void createPartControl(Composite parent) {
51  
52  		sashForm = new SashForm(parent, SWT.VERTICAL);
53  		sashForm.setSashWidth(4);
54  		// Enable the different parts to fill the whole page when the tab is
55  		// maximized
56  		sashForm.setLayout(new FillLayout());
57  
58  		top = new Composite(sashForm, SWT.NONE);
59  		top.setLayout(new GridLayout(1, false));
60  
61  		bottom = new Composite(sashForm, SWT.NONE);
62  		bottom.setLayout(new GridLayout(1, false));
63  
64  		sashForm.setWeights(new int[] { 25, 75 });
65  
66  		createQueryForm(top);
67  		createResultPart(bottom);
68  	}
69  
70  	public void createQueryForm(Composite parent) {
71  		Label lbl;
72  		GridData gd;
73  
74  		GridLayout gl = new GridLayout(2, false);
75  		gl.marginTop = 5;
76  		parent.setLayout(gl);
77  
78  		// Bundle Name
79  		lbl = new Label(parent, SWT.SINGLE);
80  		lbl.setText("Symbolic name");
81  		symbolicName = new Text(parent, SWT.SINGLE | SWT.BORDER);
82  		gd = new GridData(GridData.FILL_HORIZONTAL);
83  		gd.grabExcessHorizontalSpace = true;
84  		symbolicName.setLayoutData(gd);
85  
86  		// imported package
87  		lbl = new Label(parent, SWT.SINGLE);
88  		lbl.setText("Imported package");
89  		importedPackage = new Text(parent, SWT.SINGLE | SWT.BORDER);
90  		gd = new GridData(GridData.FILL_HORIZONTAL);
91  		gd.grabExcessHorizontalSpace = true;
92  		importedPackage.setLayoutData(gd);
93  
94  		// exported package
95  		lbl = new Label(parent, SWT.SINGLE);
96  		lbl.setText("Exported package");
97  		exportedPackage = new Text(parent, SWT.SINGLE | SWT.BORDER);
98  		gd = new GridData(GridData.FILL_HORIZONTAL);
99  		gd.grabExcessHorizontalSpace = true;
100 		exportedPackage.setLayoutData(gd);
101 
102 		executeBtn = new Button(parent, SWT.PUSH);
103 		executeBtn.setText("Search");
104 		gd = new GridData();
105 		gd.horizontalSpan = 2;
106 		executeBtn.setLayoutData(gd);
107 
108 		Listener executeListener = new Listener() {
109 			private static final long serialVersionUID = 6267263421349073712L;
110 
111 			public void handleEvent(Event event) {
112 				refreshQuery();
113 			}
114 		};
115 		executeBtn.addListener(SWT.Selection, executeListener);
116 	}
117 
118 	public void refreshQuery() {
119 		String queryStr = generateStatement();
120 		executeQuery(queryStr);
121 		bottom.layout();
122 		sashForm.layout();
123 	}
124 
125 	private String generateStatement() {
126 		try {
127 			// shortcuts
128 			boolean hasFirstClause = false;
129 			boolean ipClause = importedPackage.getText() != null
130 					&& !importedPackage.getText().trim().equals("");
131 			boolean epClause = exportedPackage.getText() != null
132 					&& !exportedPackage.getText().trim().equals("");
133 
134 			StringBuffer sb = new StringBuffer();
135 			// Select
136 			sb.append("select " + SBA + ".*, " + SAVB + ".* ");
137 			sb.append(" from " + SAVB);
138 
139 			// join
140 			sb.append(" inner join ");
141 			sb.append(SBA);
142 			sb.append(" on isdescendantnode(" + SBA + ", " + SAVB + ") ");
143 			if (ipClause) {
144 				sb.append(" inner join ");
145 				sb.append(SIP);
146 				sb.append(" on isdescendantnode(" + SIP + ", " + SBA + ") ");
147 			}
148 
149 			if (epClause) {
150 				sb.append(" inner join ");
151 				sb.append(SEP);
152 				sb.append(" on isdescendantnode(" + SEP + ", " + SBA + ") ");
153 			}
154 
155 			// where
156 			sb.append(" where ");
157 			if (symbolicName.getText() != null
158 					&& !symbolicName.getText().trim().equals("")) {
159 				sb.append(SBA + ".[" + SLC_SYMBOLIC_NAME + "] like '"
160 						+ symbolicName.getText().replace('*', '%') + "'");
161 				hasFirstClause = true;
162 			}
163 
164 			if (ipClause) {
165 				if (hasFirstClause)
166 					sb.append(" AND ");
167 				sb.append(SIP + ".[" + SLC_NAME + "] like '"
168 						+ importedPackage.getText().replace('*', '%') + "'");
169 				hasFirstClause = true;
170 			}
171 
172 			if (epClause) {
173 				if (hasFirstClause)
174 					sb.append(" AND ");
175 				sb.append(SEP + ".[" + SLC_NAME + "] like '"
176 						+ exportedPackage.getText().replace('*', '%') + "'");
177 			}
178 			return sb.toString();
179 		} catch (Exception e) {
180 			throw new SlcException(
181 					"Cannot generate where statement to get artifacts", e);
182 		}
183 	}
184 
185 	@Override
186 	public void setFocus() {
187 		executeBtn.setFocus();
188 	}
189 }