/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.eclipse.datatools.connectivity.oda.openmrs.ui.impl; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; import org.eclipse.datatools.connectivity.oda.IConnection; import org.eclipse.datatools.connectivity.oda.IParameterMetaData; import org.eclipse.datatools.connectivity.oda.IQuery; import org.eclipse.datatools.connectivity.oda.IResultSetMetaData; import org.eclipse.datatools.connectivity.oda.OdaException; import org.eclipse.datatools.connectivity.oda.design.DataSetDesign; import org.eclipse.datatools.connectivity.oda.design.DesignFactory; import org.eclipse.datatools.connectivity.oda.design.ResultSetColumns; import org.eclipse.datatools.connectivity.oda.design.ResultSetDefinition; import org.eclipse.datatools.connectivity.oda.design.ui.designsession.DesignSessionUtil; import org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage; import org.eclipse.datatools.connectivity.oda.design.util.DesignUtil; import org.eclipse.datatools.connectivity.oda.openmrs.impl.Filter; import org.eclipse.datatools.connectivity.oda.openmrs.impl.QueryBuilder; import org.eclipse.datatools.connectivity.oda.openmrs.impl.Token; import org.eclipse.datatools.connectivity.oda.openmrs.impl.XMLParser; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Text; /** * Implementation of the ODA data set designer page for an user to create or * edit an OpenMRS Data Set. This is the first page displayed to the user and * its primary purpose is for the user to select the tokens desired for the data * set. Further refinement can be made by the other pages. */ public class TokenSelectionPage extends DataSetWizardPage { private static final String DEFAULT_DATASTYLE = "mostrecent"; private static final String PASSWORD = "PASSWORD"; private static final String USERNAME = "USERNAME"; private static final String ALL_TOKENS = "All Tokens"; private static final String DATASTYLE = "DATASTYLE"; private static final String TOKENTAG = "TOKENTAG"; private static final String FILTER = "FILTER"; private static final String TOKENS = "TOKENS"; private static final String DEFAULT_SPLITTERS = ":OBSERVATIONDATE:ENCOUNTERDATE:ENCOUNTERTYPE:OBSERVATIONLOCATION"; private Logger log = Logger.getLogger(TokenSelectionPage.class.getName()); private static String FILTER_MESSAGE = "First select a filter.", COLUMN_MESSAGE = "Select at least one token by tag.", DONE_MESSAGE = "Data set OK, continue adding tokens if desired."; private transient Combo m_filterCombo, m_tagCombo; private transient Composite composite, m_columnComp, m_tokenComp, m_columnButtonComp, searchComp, m_Tag; // private transient ScrolledComposite scrolledComposite; private transient List m_columnList, m_chosenColumnList; private transient Text searchText; private transient Button m_rightButton, m_leftButton, searchButton; private String[] tags, filters; private String selectedTag, selectedFilter; private java.util.List filterList; private java.util.List columns, selectedColumns; private java.util.List tokenCache; private boolean hasTagChanged = false; /** * Constructor. * * @param pageName the page name */ public TokenSelectionPage(String pageName) { super(pageName); setTitle(pageName); setMessage(FILTER_MESSAGE); setPageComplete(false); } /** * Constructor. * * @param pageName the page name * @param title the title * @param titleImage the title image */ public TokenSelectionPage(String pageName, String title, ImageDescriptor titleImage) { super(pageName, title, titleImage); setMessage(FILTER_MESSAGE); setPageComplete(false); } /** * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#createPageCustomControl(org.eclipse.swt.widgets.Composite) */ public void createPageCustomControl(Composite parent) { log.setLevel(Level.INFO); setControl(createPageControl(parent)); initializeControl(); } /** * Creates custom control for user-defined query text. * * @param parent the parent * * @return the control */ private Control createPageControl(Composite parent) { // ScrolledComposite test = new ScrolledComposite(parent, SWT.BORDER); /* * composite.getShell().setSize(405, 405); GridData parentData = new * GridData(SWT.FILL, SWT.FILL, false, false); parentData.heightHint = * 405; parentData.widthHint = 405; * composite.getShell().setLayoutData(parentData); */ /* * final ScrolledComposite scrolledComposite = new * ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); * scrolledComposite.setExpandHorizontal(true); * scrolledComposite.setExpandVertical(true); */ composite = new Composite(parent, SWT.BORDER); composite.setSize(400, 600); composite.setLayout(new GridLayout(2, false)); GridData gridData = new GridData(SWT.FILL, SWT.FILL, false, false); gridData.heightHint = 400; gridData.widthHint = 600; composite.setLayoutData(gridData); // scrolledComposite.setContent(composite); // final Point minSize = composite.computeSize(SWT.DEFAULT, // SWT.DEFAULT); // scrolledComposite.setMinSize(minSize); setupFilterGUI(); setupTagSelectionGUI(); setupSearchGUI(); setupTokenSelectionGUI(); return composite; } /** * Setup the filter GUI that shows the list of available cohorts to choose * from. */ private void setupFilterGUI() { Label filterLabel = new Label(composite, SWT.NONE); filterLabel.setLayoutData(new GridData(SWT.LEFT, 1, false, false)); filterLabel.setText("Select a Filter"); m_filterCombo = new Combo(composite, SWT.READ_ONLY); GridData filterData = new GridData(SWT.FILL, 1, true, false); filterData.horizontalSpan = 2; m_filterCombo.setLayoutData(filterData); m_filterCombo.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { selectedFilter = m_filterCombo.getText(); for (Filter f : filterList) { if (f.getName().equals(selectedFilter)) { selectedFilter = f.getId(); InformationHolder.setPropertyValue(FILTER, selectedFilter); log.info(InformationHolder.getPropertyValue(FILTER)); } } m_tagCombo.setEnabled(true); m_columnList.setEnabled(true); m_tagCombo.select(0); selectedTag = m_tagCombo.getText(); populateColumns(""); validateData(); } public void widgetDefaultSelected(SelectionEvent e) { } }); } /** * Populate the filter drop down with the available cohorts */ private void populateFilters() { try { IConnection conn = new org.eclipse.datatools.connectivity.oda.openmrs.impl.Driver() .getConnection(null); java.util.Properties connProps = DesignUtil .convertDataSourceProperties(getInitializationDesign() .getDataSourceDesign()); conn.open(connProps); filterList = XMLParser.getFilters(getFullPath(connProps), connProps .getProperty(USERNAME), connProps.getProperty(PASSWORD)); filters = new String[filterList.size()]; for (int i = 0; i < filterList.size(); i++) { filters[i] = filterList.get(i).getName(); } m_filterCombo.setItems(filters); conn.close(); } catch (OdaException e) { // TODO need some logging here e.printStackTrace(); } } /** * Setup the token tag drop down in the GUI. */ private void setupTagSelectionGUI() { m_Tag = new Composite(composite, SWT.NONE); m_Tag.setLayout(new GridLayout(2, false)); GridData tagData = new GridData(SWT.FILL, SWT.FILL, true, false); // columnCompData.horizontalSpan = 2; m_Tag.setLayoutData(tagData); Label tagLabel = new Label(m_Tag, SWT.NONE); tagLabel.setLayoutData(new GridData(SWT.LEFT, 1, false, false)); tagLabel.setText("Token Tags"); m_tagCombo = new Combo(m_Tag, SWT.READ_ONLY); m_tagCombo.setLayoutData(new GridData(SWT.FILL, 1, true, false)); m_tagCombo.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { selectedTag = m_tagCombo.getText(); InformationHolder.setPropertyValue(TOKENTAG, selectedTag); hasTagChanged = true; populateColumns(""); validateData(); } public void widgetDefaultSelected(SelectionEvent e) { } }); m_tagCombo.setEnabled(false); } /** * Setup the GUI that allows the user to select tokens. There is a column on * the left with the available tokens and a column on the right with the * selected tokens. In between the two colums are arrows so that the user * can move available tokens to the selected tokens columns. */ private void setupTokenSelectionGUI() { m_columnComp = new Composite(composite, SWT.NONE); m_columnComp.setLayout(new GridLayout(3, false)); GridData columnCompData = new GridData(SWT.FILL, SWT.FILL, true, false); columnCompData.horizontalSpan = 2; m_columnComp.setLayoutData(columnCompData); Label columnLabel = new Label(m_columnComp, SWT.NONE); GridData columnLabelData = new GridData(SWT.LEFT, 1, false, false); columnLabelData.horizontalSpan = 3; columnLabel.setLayoutData(columnLabelData); columnLabel .setText("Select Columns (Use arrows to move columns to right box)"); m_tokenComp = new Composite(m_columnComp, SWT.NONE); m_tokenComp.setLayout(new GridLayout(2, false)); m_tokenComp .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); // m_tagCombo.setEnabled(false); m_columnList = new List(m_tokenComp, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); GridData columnData = new GridData(SWT.FILL, SWT.FILL, true, false); columnData.horizontalSpan = 2; columnData.heightHint = 200; m_columnList.setLayoutData(columnData); m_columnList.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { m_rightButton.setEnabled(m_columnList.getSelectionCount() > 0); m_leftButton.setEnabled(false); } public void widgetDefaultSelected(SelectionEvent e) { } }); m_columnList.addMouseListener(new MouseAdapter() { public void mouseDoubleClick(MouseEvent e) { addColumns(); validateData(); } }); m_columnList.setEnabled(false); m_columnButtonComp = new Composite(m_columnComp, SWT.NONE); m_columnButtonComp.setLayout(new GridLayout(1, false)); GridData columnButtonCompData = new GridData(SWT.FILL, SWT.FILL, false, false); m_columnButtonComp.setLayoutData(columnButtonCompData); m_rightButton = new Button(m_columnButtonComp, SWT.ARROW | SWT.RIGHT); GridData rightData = new GridData(SWT.FILL, SWT.FILL, false, false); m_rightButton.setLayoutData(rightData); m_rightButton.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { addColumns(); validateData(); } public void widgetDefaultSelected(SelectionEvent e) { } }); m_rightButton.setEnabled(false); m_leftButton = new Button(m_columnButtonComp, SWT.ARROW | SWT.LEFT); GridData leftData = new GridData(SWT.FILL, SWT.FILL, false, false); m_leftButton.setLayoutData(leftData); m_leftButton.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { removeColumns(); validateData(); } public void widgetDefaultSelected(SelectionEvent e) { } }); m_leftButton.setEnabled(false); m_chosenColumnList = new List(m_columnComp, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); GridData chosenColumnData = new GridData(SWT.FILL, SWT.FILL, true, false); m_chosenColumnList.setLayoutData(chosenColumnData); m_chosenColumnList.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { m_leftButton .setEnabled(m_chosenColumnList.getSelectionCount() > 0); m_rightButton.setEnabled(false); log.info("Selected Token: " + m_chosenColumnList.getSelection()[0]); } public void widgetDefaultSelected(SelectionEvent e) { } }); m_chosenColumnList.addMouseListener(new MouseAdapter() { public void mouseDoubleClick(MouseEvent e) { removeColumns(); validateData(); } }); m_chosenColumnList.setEnabled(false); } /** * Setup the search GUI. This consists of an input text box and a search * button to commit the search. The search is done on the tokens in the * available token column that was populated by choosing the desired token * tag. */ private void setupSearchGUI() { searchComp = new Composite(composite, SWT.NONE); searchComp.setLayout(new GridLayout(3, false)); GridData searchData = new GridData(SWT.FILL, SWT.FILL, true, false); searchData.horizontalSpan = 2; searchComp.setLayoutData(searchData); Label searchLabel = new Label(searchComp, SWT.NONE); searchLabel.setLayoutData(new GridData(SWT.LEFT, 1, false, false)); searchLabel.setText("Find Tokens"); searchText = new Text(searchComp, SWT.SINGLE | SWT.BORDER); searchText.setLayoutData(new GridData(SWT.FILL, 1, true, false)); searchText.setTextLimit(50); searchText.setToolTipText("Enter your search"); searchButton = new Button(searchComp, SWT.PUSH); searchButton.setLayoutData(new GridData(SWT.RIGHT, 1, false, false)); searchButton.setText("Search"); searchButton.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { searchText.selectAll(); searchTokens(); } public void widgetDefaultSelected(SelectionEvent e) { } }); } /** * Search tokens. * * @see populateColumns(String) */ private void searchTokens() { populateColumns(searchText.getText()); } private String getFullPath(java.util.Properties connProps) { String path = connProps.getProperty("PATH"); // Make sure there is a slash at the end of the path if (!path.endsWith("/")) path += "/"; return path + "moduleServlet/logicws/api/"; } /** * Gets the available token tags and populates the drop down. */ private void populateTokenTags() { log.info("Entered populateTokenTags method"); try { IConnection conn = new org.eclipse.datatools.connectivity.oda.openmrs.impl.Driver() .getConnection(null); java.util.Properties connProps = DesignUtil .convertDataSourceProperties(getInitializationDesign() .getDataSourceDesign()); conn.open(connProps); java.util.List tagList = XMLParser.getTokenTags( getFullPath(connProps), connProps.getProperty(USERNAME), connProps.getProperty(PASSWORD)); tags = new String[tagList.size()]; for (int i = 0; i < tagList.size(); i++) { tags[i] = tagList.get(i); } m_tagCombo.setItems(tags); m_tagCombo.add(ALL_TOKENS, 0); InformationHolder.setPropertyValue(TOKENTAG, ALL_TOKENS); conn.close(); } catch (OdaException e) { // TODO need some logging here e.printStackTrace(); } } /** * Uses the tag that was selected to populate the available column with the * appropriate tokens. * * @param searchString the search string */ private void populateColumns(String searchString) { try { // we only want to hit the logicws if the tag has been changed and // we don't have the list of columns if (tokenCache == null || hasTagChanged) { IConnection conn = new org.eclipse.datatools.connectivity.oda.openmrs.impl.Driver() .getConnection(null); java.util.Properties connProps = DesignUtil .convertDataSourceProperties(getInitializationDesign() .getDataSourceDesign()); conn.open(connProps); String tag = selectedTag.equals(ALL_TOKENS) ? "" : selectedTag; tokenCache = XMLParser.getTokens( getFullPath(connProps), connProps.getProperty(USERNAME), connProps .getProperty(PASSWORD), tag); columns = new java.util.ArrayList(); for (int i = 0; i < tokenCache.size(); i++) { if (tokenCache.get(i).getName().toLowerCase().indexOf( searchString.toLowerCase()) > -1) { columns.add(tokenCache.get(i).getName()); } } m_columnList.setItems(columns .toArray(new String[columns.size()])); m_columnList.update(); hasTagChanged = false; conn.close(); } //search on the existing token cache else { columns = new java.util.ArrayList(); for (int i = 0; i < tokenCache.size(); i++) { if (tokenCache.get(i).getName().toLowerCase().indexOf( searchString.toLowerCase()) > -1) { columns.add(tokenCache.get(i).getName()); } } m_columnList.setItems(columns .toArray(new String[columns.size()])); m_columnList.update(); } } catch (OdaException e) { // TODO need some logging here e.printStackTrace(); } } /** * Called when the user moves a column from the left pane to selected * columns right pane. */ private void addColumns() { if (selectedColumns == null) { selectedColumns = new java.util.ArrayList(); } for (String s : m_columnList.getSelection()) { selectedColumns.add(s); Collections.sort(selectedColumns); m_chosenColumnList.add(s); String[] items = m_chosenColumnList.getItems(); java.util.Arrays.sort(items); m_chosenColumnList.setItems(items); } // update the properties to share between the pages String columnProp = ""; if (InformationHolder.getPropertyValue(TOKENS) != null) { String tokens = InformationHolder.getPropertyValue(TOKENS); for (String t : selectedColumns) { if (tokens.contains("{" + t + "}")) { String modifier = Utils .getModifierInfoForTokenFromTokenProperty(t, tokens); String split = Utils.getSplitsForTokenFromTokenProperty(t, tokens); String aggregate = Utils .getAggregateNameForTokenFromTokenProperty(t, tokens); String value = Utils .getAggregateValueForTokenFromTokenProperty(t, tokens); if (modifier != null && split != null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + modifier + split; else if (modifier != null && split == null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + modifier; else if (modifier == null && split != null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + split; else columnProp += "|" + aggregate + " " + value + " {" + t + "}"; } else { columnProp += "|LAST 1 {" + t + "}" + DEFAULT_SPLITTERS; } } InformationHolder.setPropertyValue(TOKENS, columnProp.substring(1)); } else { for (String t : selectedColumns) columnProp += "|LAST 1 {" + t + "}" + DEFAULT_SPLITTERS; InformationHolder.setPropertyValue(TOKENS, columnProp.substring(1)); } log.info(InformationHolder.getPropertyValue(TOKENS)); m_rightButton.setEnabled(false); m_chosenColumnList.setEnabled(true); } /** * Called when a user removes a token from the selected token column. */ private void removeColumns() { for (String s : m_chosenColumnList.getSelection()) { selectedColumns.remove(s); m_chosenColumnList.remove(s); } // update the properties to share between the pages String columnProp = ""; if (InformationHolder.getPropertyValue(TOKENS) != null) { String tokens = InformationHolder.getPropertyValue(TOKENS); for (String t : selectedColumns) { if (tokens.contains("{" + t + "}")) { String modifier = Utils .getModifierInfoForTokenFromTokenProperty(t, tokens); String split = Utils.getSplitsForTokenFromTokenProperty(t, tokens); String aggregate = Utils .getAggregateNameForTokenFromTokenProperty(t, tokens); String value = Utils .getAggregateValueForTokenFromTokenProperty(t, tokens); if (modifier != null && split != null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + modifier + split; else if (modifier != null && split == null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + modifier; else if (modifier == null && split != null) columnProp += "|" + aggregate + " " + value + " {" + t + "}" + split; else columnProp += "|" + aggregate + " " + value + " {" + t + "}"; } else columnProp += "|LAST 1 {" + t + "}" + DEFAULT_SPLITTERS; } InformationHolder.setPropertyValue(TOKENS, columnProp.substring(1)); } else { for (String t : selectedColumns) columnProp += "|LAST 1 {" + t + "}" + DEFAULT_SPLITTERS; InformationHolder.setPropertyValue(TOKENS, columnProp.substring(1)); } m_leftButton.setEnabled(false); if (m_chosenColumnList.getItemCount() == 0) m_chosenColumnList.setEnabled(false); } /** * Initializes the page control with the last edited data set design. */ private void initializeControl() { /* * To optionally restore the designer state of the previous design * session, use getInitializationDesignerState(); */ composite.layout(); log.info("Initializing token selection"); // Restores the last saved data set design DataSetDesign dataSetDesign = getInitializationDesign(); if (dataSetDesign == null) return; // nothing to initialize if (InformationHolder.hasDestroyed()) InformationHolder.start(dataSetDesign); // setup the default data type if its not set if (InformationHolder.getPropertyValue(DATASTYLE) == null) { InformationHolder.setPropertyValue(DATASTYLE, DEFAULT_DATASTYLE); } if (InformationHolder.getPropertyValue(FILTER) != null) { selectedFilter = InformationHolder.getPropertyValue(FILTER); } if (InformationHolder.getPropertyValue(TOKENS) != null) { selectedColumns = new java.util.ArrayList(); for (String t : QueryBuilder .getTokenNamesFromStorage(InformationHolder .getPropertyValue(TOKENS))) { selectedColumns.add(t); } } if (InformationHolder.getPropertyValue(TOKENTAG) != null) { selectedTag = InformationHolder.getPropertyValue(TOKENTAG); } if (dataSetDesign.getQueryText() == null) return; // nothing to initialize // initialize control validateData(); populateFilters(); populateTokenTags(); try { log.info(selectedTag); // log.info(selectedColumns.toString()); log.info(selectedFilter); if (selectedColumns != null && selectedFilter != null) { log.info("Repopulating selected filter, tags, and columns"); for (Filter f : filterList) { if (f.getId().equals(selectedFilter)) { log.info("Setting selected filter: " + f.getName()); m_filterCombo.setText(f.getName()); break; } } m_tagCombo.setText(selectedTag); populateColumns(""); for (String s : selectedColumns) { // m_columnList.select(m_columnList.indexOf(s)); m_columnList.remove(m_columnList.indexOf(s)); m_chosenColumnList.add(s); } m_tagCombo.setEnabled(true); m_columnList.setEnabled(true); m_chosenColumnList.setEnabled(true); } else { selectedTag = null; selectedColumns = null; selectedFilter = null; } log.info("Trying to set the height"); // m_tokenComp.layout(); // composite.layout(); // final Point minSize = composite.computeSize(SWT.DEFAULT, // SWT.DEFAULT); // scrolledComposite.setMinSize(minSize); // composite.getShell().layout(); } catch (Exception e) { // Something has changed, so deselect everything e.printStackTrace(); log.info(e.toString()); selectedTag = null; selectedColumns = null; selectedFilter = null; m_filterCombo.deselectAll(); m_tagCombo.deselectAll(); m_columnList.removeAll(); m_chosenColumnList.removeAll(); } /* * To optionally honor the request for an editable or read-only design * session, use isSessionEditable(); */ } /** * Obtains the user-defined query text of this data set from page control. * * @return query text */ private String getQueryText() { log.info(InformationHolder.getPropertyValue(TOKENS)); log.info(InformationHolder.getPropertyValue(DATASTYLE)); String queryText = QueryBuilder.getQuery(InformationHolder .getPropertyValue(TOKENS), InformationHolder .getPropertyValue(FILTER), InformationHolder .getPropertyValue(DATASTYLE)); log.info(queryText); return queryText; } /** * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#collectDataSetDesign(org.eclipse.datatools.connectivity.oda.design.DataSetDesign) */ protected DataSetDesign collectDataSetDesign(DataSetDesign design) { if (!hasValidData()) return design; savePage(design); return design; } /** * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#collectResponseState() */ protected void collectResponseState() { super.collectResponseState(); /* * To optionally assign a custom response state, for inclusion in the * ODA design session response, use setResponseSessionStatus( * SessionStatus status ); setResponseDesignerState( DesignerState * customState ); */ } /** * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#canLeave() */ protected boolean canLeave() { return isPageComplete(); } /** * Validates the user-defined value in the page control exists and not a * blank text. Set page message accordingly. */ private void validateData() { // Check filter choice if (m_filterCombo == null || selectedFilter == null || selectedFilter.equals("")) { setMessage(FILTER_MESSAGE); setPageComplete(false); } // Check columns else if (m_columnList == null || selectedColumns == null || selectedColumns.size() == 0) { setMessage(COLUMN_MESSAGE); setPageComplete(false); } else { log.info("Page is complete"); setMessage(DONE_MESSAGE); setPageComplete(true); } } /** * Indicates whether the custom page has valid data to proceed with defining * a data set. * * @return true, if checks for valid data */ private boolean hasValidData() { validateData(); return canLeave(); } /** * Gets the blank page properties. * * @return the blank page properties */ private static java.util.Properties getBlankPageProperties() { java.util.Properties prop = new java.util.Properties(); prop.setProperty(FILTER, ""); prop.setProperty(TOKENTAG, ""); prop.setProperty(TOKENS, ""); prop.setProperty(DATASTYLE, ""); return prop; } /** * Saves the user-defined value in this page, and updates the specified * dataSetDesign with the latest design definition. * * @param dataSetDesign the data set design */ private void savePage(DataSetDesign dataSetDesign) { // save user-defined query text log.info("Saving token selection page"); log.info("datastyle: " + InformationHolder.getPropertyValue(DATASTYLE)); if (InformationHolder.hasDestroyed()) { log.info("destroyed"); InformationHolder.start(dataSetDesign); } log.info("datastyle: " + InformationHolder.getPropertyValue(DATASTYLE)); String queryText = getQueryText(); dataSetDesign.setQueryText(queryText); if (dataSetDesign.getPublicProperties() == null) { try { dataSetDesign.setPublicProperties(DesignSessionUtil .createDataSetPublicProperties(dataSetDesign .getOdaExtensionDataSourceId(), dataSetDesign .getOdaExtensionDataSetId(), getBlankPageProperties())); } catch (OdaException e) { // TODO need some logging here e.printStackTrace(); } } if (dataSetDesign.getPublicProperties() != null) { if (dataSetDesign.getPublicProperties().findProperty(FILTER) != null) dataSetDesign.getPublicProperties().findProperty(FILTER) .setNameValue(FILTER, InformationHolder.getPropertyValue(FILTER)); if (dataSetDesign.getPublicProperties().findProperty(TOKENTAG) != null) dataSetDesign.getPublicProperties().findProperty(TOKENTAG) .setNameValue(TOKENTAG, InformationHolder.getPropertyValue(TOKENTAG)); if (dataSetDesign.getPublicProperties().findProperty(TOKENS) != null) dataSetDesign.getPublicProperties().findProperty(TOKENS) .setNameValue(TOKENS, InformationHolder.getPropertyValue(TOKENS)); if (dataSetDesign.getPublicProperties().findProperty(DATASTYLE) != null) dataSetDesign.getPublicProperties().findProperty(DATASTYLE) .setNameValue(DATASTYLE, InformationHolder.getPropertyValue(DATASTYLE)); } // obtain query's current runtime metadata, and maps it to the // dataSetDesign IConnection conn = null; try { // obtain and open a live connection conn = new org.eclipse.datatools.connectivity.oda.openmrs.impl.Driver() .getConnection(null); java.util.Properties connProps = DesignUtil .convertDataSourceProperties(getInitializationDesign() .getDataSourceDesign()); conn.open(connProps); // update the data set design with the query's current runtime // metadata updateDesign(dataSetDesign, conn, queryText); } catch (OdaException e) { // not able to get current metadata, reset previous derived metadata dataSetDesign.setResultSets(null); dataSetDesign.setParameters(null); e.printStackTrace(); } finally { closeConnection(conn); } log.info("Done saving token selection page"); } /** * Updates the given dataSetDesign with the queryText and its derived * metadata obtained from the ODA runtime connection. * * @param dataSetDesign the data set design * @param conn the conn * @param queryText the query text * * @throws OdaException the oda exception */ private void updateDesign(DataSetDesign dataSetDesign, IConnection conn, String queryText) throws OdaException { IQuery query = conn.newQuery(null); query.prepare(queryText); try { IResultSetMetaData md = query.getMetaData(); updateResultSetDesign(md, dataSetDesign); } catch (OdaException e) { // no result set definition available, reset previous derived // metadata dataSetDesign.setResultSets(null); e.printStackTrace(); } // proceed to get parameter design definition try { IParameterMetaData paramMd = query.getParameterMetaData(); updateParameterDesign(paramMd, dataSetDesign); } catch (OdaException ex) { // no parameter definition available, reset previous derived // metadata dataSetDesign.setParameters(null); ex.printStackTrace(); } } /** * Updates the specified data set design's result set definition based on * the specified runtime metadata. * * @param md runtime result set metadata instance * @param dataSetDesign data set design instance to update * * @throws OdaException the oda exception */ private void updateResultSetDesign(IResultSetMetaData md, DataSetDesign dataSetDesign) throws OdaException { ResultSetColumns columns = DesignSessionUtil .toResultSetColumnsDesign(md); ResultSetDefinition resultSetDefn = DesignFactory.eINSTANCE .createResultSetDefinition(); // resultSetDefn.setName( value ); // result set name resultSetDefn.setResultSetColumns(columns); // no exception in conversion; go ahead and assign to specified // dataSetDesign dataSetDesign.setPrimaryResultSet(resultSetDefn); dataSetDesign.getResultSets().setDerivedMetaData(true); } /** * Updates the specified data set design's parameter definition based on the * specified runtime metadata. Unimplemented, as parameters are unsupported. * * @param paramMd the param md * @param dataSetDesign the data set design * * @throws OdaException the oda exception */ private void updateParameterDesign(IParameterMetaData paramMd, DataSetDesign dataSetDesign) throws OdaException { } /** * Attempts to close given ODA connection. * * @param conn the conn */ private void closeConnection(IConnection conn) { try { if (conn != null && conn.isOpen()) conn.close(); } catch (OdaException e) { // TODO need some logging here e.printStackTrace(); } } /** * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#refresh(org.eclipse.datatools.connectivity.oda.design.DataSetDesign) */ protected void refresh(DataSetDesign dataSetDesign) { if (InformationHolder.hasDestroyed()) InformationHolder.start(dataSetDesign); refresh(); } /** * Called when users are moving between pages and the data needs to be * refreshed. */ protected void refresh() { selectedColumns = new java.util.ArrayList(); log.info(InformationHolder.getPropertyValue(FILTER)); log.info(InformationHolder.getPropertyValue(TOKENS)); if (InformationHolder.getPropertyValue(FILTER) != null) { selectedFilter = InformationHolder.getPropertyValue(FILTER); } if (InformationHolder.getPropertyValue(TOKENS) != null) { selectedColumns = new java.util.ArrayList(); for (String t : QueryBuilder .getTokenNamesFromStorage(InformationHolder .getPropertyValue(TOKENS))) { selectedColumns.add(t); } } // m_tokenComp.layout(); // composite.getShell().setSize(400, 600); // composite.getShell().pack(); // Point minSize = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT); // scrolledComposite.setSize(minSize); // composite.getShell().sets.layout(); } /* * @see org.eclipse.datatools.connectivity.oda.design.ui.wizards.DataSetWizardPage#cleanup() */ protected void cleanup() { log.info("cleanup"); InformationHolder.destroy(); } }