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.client.ui.dist.DistPlugin;
21  import org.eclipse.swt.SWT;
22  import org.eclipse.swt.custom.SashForm;
23  import org.eclipse.swt.layout.FillLayout;
24  import org.eclipse.swt.layout.GridData;
25  import org.eclipse.swt.layout.GridLayout;
26  import org.eclipse.swt.widgets.Button;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Event;
29  import org.eclipse.swt.widgets.Label;
30  import org.eclipse.swt.widgets.Listener;
31  import org.eclipse.swt.widgets.Text;
32  
33  /** Query SLC Repo to get some artifacts given some predefined parameters */
34  public class QueryArtifactsForm extends AbstractQueryArtifactsView implements
35  		SlcNames {
36  	// private static final Log log = LogFactory.getLog(QueryArtifactsForm.class);
37  	public static final String ID = DistPlugin.PLUGIN_ID + ".queryArtifactsForm";
38  
39  	// widgets
40  	private Button executeBtn;
41  	private Text groupId;
42  	private Text artifactId;
43  	private Text version;
44  	private SashForm sashForm;
45  
46  	private Composite top, bottom;
47  
48  	@Override
49  	public void createPartControl(Composite parent) {
50  
51  		sashForm = new SashForm(parent, SWT.VERTICAL);
52  		sashForm.setSashWidth(4);
53  		// Enable the different parts to fill the whole page when the tab is
54  		// maximized
55  		sashForm.setLayout(new FillLayout());
56  
57  		top = new Composite(sashForm, SWT.NONE);
58  		top.setLayout(new GridLayout(1, false));
59  
60  		bottom = new Composite(sashForm, SWT.NONE);
61  		bottom.setLayout(new GridLayout(1, false));
62  
63  		sashForm.setWeights(new int[] { 25, 75 });
64  
65  		createQueryForm(top);
66  		createResultPart(bottom);
67  	}
68  
69  	public void createQueryForm(Composite parent) {
70  		Label lbl;
71  		GridData gd;
72  
73  		GridLayout gl = new GridLayout(2, false);
74  		gl.marginTop = 5;
75  		parent.setLayout(gl);
76  
77  		// lbl = new Label(parent, SWT.SINGLE);
78  		// lbl.setText("Query by coordinates");
79  		// gd = new GridData();
80  		// gd.horizontalSpan = 2;
81  		// lbl.setLayoutData(gd);
82  
83  		// Group ID
84  		lbl = new Label(parent, SWT.SINGLE);
85  		lbl.setText("Group ID");
86  		groupId = new Text(parent, SWT.SINGLE | SWT.BORDER);
87  		gd = new GridData(GridData.FILL_HORIZONTAL);
88  		gd.grabExcessHorizontalSpace = true;
89  		groupId.setLayoutData(gd);
90  
91  		// Artifact ID
92  		lbl = new Label(parent, SWT.SINGLE);
93  		lbl.setText("Artifact ID");
94  		artifactId = new Text(parent, SWT.SINGLE | SWT.BORDER);
95  		gd = new GridData(GridData.FILL_HORIZONTAL);
96  		gd.grabExcessHorizontalSpace = true;
97  		artifactId.setLayoutData(gd);
98  
99  		// Version
100 		lbl = new Label(parent, SWT.SINGLE);
101 		lbl.setText("Version");
102 		version = new Text(parent, SWT.SINGLE | SWT.BORDER);
103 		gd = new GridData(GridData.FILL_HORIZONTAL);
104 		gd.grabExcessHorizontalSpace = true;
105 		version.setLayoutData(gd);
106 
107 		executeBtn = new Button(parent, SWT.PUSH);
108 		executeBtn.setText("Search");
109 		gd = new GridData();
110 		gd.horizontalSpan = 2;
111 		executeBtn.setLayoutData(gd);
112 
113 		Listener executeListener = new Listener() {
114 			private static final long serialVersionUID = 7161585477628774129L;
115 
116 			public void handleEvent(Event event) {
117 				refreshQuery();
118 			}
119 		};
120 		executeBtn.addListener(SWT.Selection, executeListener);
121 	}
122 
123 	public void refreshQuery() {
124 		String queryStr = generateSelectStatement() + generateFromStatement()
125 				+ generateWhereStatement();
126 		executeQuery(queryStr);
127 		bottom.layout();
128 		sashForm.layout();
129 	}
130 
131 	private String generateWhereStatement() {
132 		try {
133 			boolean hasFirstClause = false;
134 			StringBuffer sb = new StringBuffer(" where ");
135 
136 			if (groupId.getText() != null
137 					&& !groupId.getText().trim().equals("")) {
138 				sb.append("[" + SLC_GROUP_ID + "] like '"
139 						+ groupId.getText().replace('*', '%') + "'");
140 				hasFirstClause = true;
141 			}
142 
143 			if (artifactId.getText() != null
144 					&& !artifactId.getText().trim().equals("")) {
145 				if (hasFirstClause)
146 					sb.append(" AND ");
147 				sb.append("[" + SLC_ARTIFACT_ID + "] like '"
148 						+ artifactId.getText().replace('*', '%') + "'");
149 				hasFirstClause = true;
150 			}
151 
152 			if (version.getText() != null
153 					&& !version.getText().trim().equals("")) {
154 				if (hasFirstClause)
155 					sb.append(" AND ");
156 				sb.append("[" + SLC_ARTIFACT_VERSION + "] like '"
157 						+ version.getText().replace('*', '%') + "'");
158 			}
159 
160 			return sb.toString();
161 		} catch (Exception e) {
162 			throw new SlcException(
163 					"Cannot generate where statement to get artifacts", e);
164 		}
165 	}
166 
167 	@Override
168 	public void setFocus() {
169 		executeBtn.setFocus();
170 	}
171 }