2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.dist
.views
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Calendar
;
20 import java
.util
.GregorianCalendar
;
21 import java
.util
.Iterator
;
22 import java
.util
.List
;
24 import javax
.jcr
.RepositoryException
;
25 import javax
.jcr
.Session
;
26 import javax
.jcr
.query
.Query
;
27 import javax
.jcr
.query
.QueryResult
;
28 import javax
.jcr
.query
.Row
;
29 import javax
.jcr
.query
.RowIterator
;
31 import org
.apache
.commons
.logging
.Log
;
32 import org
.apache
.commons
.logging
.LogFactory
;
33 import org
.argeo
.ArgeoException
;
34 import org
.argeo
.eclipse
.ui
.GenericTableComparator
;
35 import org
.argeo
.slc
.client
.ui
.dist
.utils
.ArtifactsTableConfigurer
;
36 import org
.argeo
.slc
.jcr
.SlcTypes
;
37 import org
.eclipse
.core
.runtime
.IStatus
;
38 import org
.eclipse
.core
.runtime
.Status
;
39 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
40 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
41 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
42 import org
.eclipse
.jface
.viewers
.LabelProvider
;
43 import org
.eclipse
.jface
.viewers
.TableViewer
;
44 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
45 import org
.eclipse
.jface
.viewers
.Viewer
;
46 import org
.eclipse
.swt
.SWT
;
47 import org
.eclipse
.swt
.graphics
.Image
;
48 import org
.eclipse
.swt
.layout
.GridData
;
49 import org
.eclipse
.swt
.layout
.GridLayout
;
50 import org
.eclipse
.swt
.widgets
.Composite
;
51 import org
.eclipse
.swt
.widgets
.Table
;
52 import org
.eclipse
.ui
.part
.ViewPart
;
54 /** Factorizes useful methods to build a query view in a sashForm */
55 public abstract class AbstractQueryArtifactsView
extends ViewPart
implements
57 private static final Log log
= LogFactory
58 .getLog(AbstractQueryArtifactsView
.class);
61 final protected static String SAVB
= "[" + SLC_ARTIFACT_VERSION_BASE
+ "]";
62 final protected static String SBA
= "[" + SLC_BUNDLE_ARTIFACT
+ "]";
63 final protected static String SIP
= "[" + SLC_IMPORTED_PACKAGE
+ "]";
64 final protected static String SEP
= "[" + SLC_EXPORTED_PACKAGE
+ "]";
66 /* DEPENDENCY INJECTION */
67 private Session session
;
68 private List
<String
> columnProperties
;
71 private TableViewer viewer
;
72 private List
<TableViewerColumn
> tableViewerColumns
= new ArrayList
<TableViewerColumn
>();
73 private ArtifactsTableConfigurer tableConfigurer
;
74 private GenericTableComparator comparator
;
76 // to be set by client to display all columns
77 private boolean displayAllColumns
= false;
79 protected void createResultPart(Composite parent
) {
80 viewer
= new TableViewer(parent
);
81 Table table
= viewer
.getTable();
82 table
.getParent().setLayout(new GridLayout(1, false));
83 table
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
84 viewer
.getTable().setHeaderVisible(true);
85 viewer
.getTable().setLinesVisible(true);
87 viewer
.setLabelProvider(new ViewLabelProvider());
88 viewer
.setContentProvider(new ViewContentProvider());
89 // viewer.addDoubleClickListener(new GenericDoubleClickListener());
91 tableConfigurer
= new ArtifactsTableConfigurer(viewer
, 1,
92 GenericTableComparator
.DESCENDING
);
94 comparator
= tableConfigurer
.getComparator();
95 viewer
.setComparator(comparator
);
98 protected void executeQuery(String statement
) {
100 Calendar stStamp
= new GregorianCalendar();
101 if (log
.isDebugEnabled()) {
102 log
.debug("Executed query: " + statement
);
104 QueryResult qr
= session
.getWorkspace().getQueryManager()
105 .createQuery(statement
, Query
.JCR_SQL2
).execute();
107 if (log
.isDebugEnabled()) {
108 Calendar enStamp
= new GregorianCalendar();
109 long duration
= enStamp
.getTimeInMillis()
110 - stStamp
.getTimeInMillis();
111 log
.debug("Query executed in : " + duration
/ 1000 + "s.");
114 // remove previous columns
115 for (TableViewerColumn tvc
: tableViewerColumns
)
116 tvc
.getColumn().dispose();
118 // If a pre(-defined list of columns has been injected, we use it,
119 // otherwise we display all results of the resultSet
120 if (!displayAllColumns
&& columnProperties
!= null) {
123 Iterator
<String
> it
= columnProperties
.iterator();
124 while (it
.hasNext()) {
125 String columnName
= it
.next();
127 TableViewerColumn tvc
= new TableViewerColumn(viewer
,
129 tableConfigurer
.configureColumn(columnName
, tvc
, i
);
130 tvc
.setLabelProvider(tableConfigurer
131 .getLabelProvider(columnName
));
132 tableViewerColumns
.add(tvc
);
137 for (final String columnName
: qr
.getColumnNames()) {
138 TableViewerColumn tvc
= new TableViewerColumn(viewer
,
140 // Small hack to remove prefix from the column name
141 // String tmpStr = columnName.substring(columnName
142 // .lastIndexOf(".") + 1);
143 tableConfigurer
.configureColumn(columnName
, tvc
, i
);
144 tvc
.setLabelProvider(tableConfigurer
145 .getLabelProvider(columnName
));
146 tableViewerColumns
.add(tvc
);
150 // We must create a local list because query result can be read only
153 List
<Row
> rows
= new ArrayList
<Row
>();
154 RowIterator rit
= qr
.getRows();
155 while (rit
.hasNext()) {
156 rows
.add(rit
.nextRow());
158 viewer
.setInput(rows
);
159 } catch (RepositoryException e
) {
160 throw new ArgeoException("Cannot read query result", e
);
163 } catch (RepositoryException e
) {
164 ErrorDialog
.openError(null, "Error", "Cannot execute JCR query: "
165 + statement
, new Status(IStatus
.ERROR
,
166 "org.argeo.eclipse.ui.jcr", e
.getMessage()));
171 * Client must use this method to display all columns of the result set
172 * instead of a limited predifined and injected set
174 public void displayAllColumns(boolean flag
) {
175 displayAllColumns
= flag
;
178 // Can be overridden by subclasses.
179 protected String
generateSelectStatement() {
180 StringBuffer sb
= new StringBuffer("select " + SAVB
+ ".* ");
181 return sb
.toString();
184 protected String
generateFromStatement() {
185 StringBuffer sb
= new StringBuffer(" from ");
188 return sb
.toString();
192 protected class ViewContentProvider
implements IStructuredContentProvider
{
194 public void inputChanged(Viewer arg0
, Object arg1
, Object arg2
) {
197 public void dispose() {
200 @SuppressWarnings("unchecked")
201 public Object
[] getElements(Object obj
) {
202 return ((List
<String
[]>) obj
).toArray();
206 protected class ViewLabelProvider
extends LabelProvider
implements
207 ITableLabelProvider
{
208 public String
getColumnText(Object obj
, int index
) {
209 if (!(obj
instanceof String
[]))
210 return "Object is not properly formatted ";
212 String
[] value
= (String
[]) obj
;
217 public Image
getColumnImage(Object obj
, int index
) {
222 /* DEPENDENCY INJECTION */
223 public void setSession(Session session
) {
224 this.session
= session
;
227 public void setColumnProperties(List
<String
> columnProperties
) {
228 this.columnProperties
= columnProperties
;