From ae1b0237857f71acd321a1cec3292f18ce31c118 Mon Sep 17 00:00:00 2001 From: ValentinoAvonEFSA Date: Fri, 19 Jan 2018 14:26:48 +0100 Subject: [PATCH] adapted with dcf web service framework --- .../DataCollectionContentProvider.java | 26 ++++ .../DataCollectionLabelProvider.java | 82 +++++++++++ .../DataCollectionsListDialog.java | 130 ++++++++++++++++++ .../GetAvailableDataCollections.java | 41 ++++++ src/dataset/DatasetList.java | 6 +- src/i18n_messages/rcl_messages_en.properties | 11 ++ src/report/DownloadReportDialog.java | 50 ++----- src/report/Report.java | 3 +- src/report/ReportDownloader.java | 59 +++++++- src/table_dialog/DatasetContentProvider.java | 1 - src/table_dialog/DatasetListDialog.java | 2 - src/table_dialog/TableDialog.java | 6 +- 12 files changed, 366 insertions(+), 51 deletions(-) create mode 100644 src/data_collection/DataCollectionContentProvider.java create mode 100644 src/data_collection/DataCollectionLabelProvider.java create mode 100644 src/data_collection/DataCollectionsListDialog.java create mode 100644 src/data_collection/GetAvailableDataCollections.java diff --git a/src/data_collection/DataCollectionContentProvider.java b/src/data_collection/DataCollectionContentProvider.java new file mode 100644 index 0000000..c400d0d --- /dev/null +++ b/src/data_collection/DataCollectionContentProvider.java @@ -0,0 +1,26 @@ +package data_collection; + +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.Viewer; + +import data_collection.DcfDataCollectionsList; + +public class DataCollectionContentProvider implements IStructuredContentProvider { + + @Override + public void dispose() {} + + @Override + public void inputChanged(Viewer arg0, Object arg1, Object arg2) {} + + @Override + public Object[] getElements(Object arg0) { + + if (arg0 instanceof DcfDataCollectionsList) { + DcfDataCollectionsList list = (DcfDataCollectionsList) arg0; + return list.toArray(); + } + + return null; + } +} diff --git a/src/data_collection/DataCollectionLabelProvider.java b/src/data_collection/DataCollectionLabelProvider.java new file mode 100644 index 0000000..75b5732 --- /dev/null +++ b/src/data_collection/DataCollectionLabelProvider.java @@ -0,0 +1,82 @@ +package data_collection; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.ILabelProviderListener; +import org.eclipse.swt.graphics.Image; + +import dataset.Dataset; + +/** + * Label provider of the {@link Dataset} + * @author avonva + * + */ +public class DataCollectionLabelProvider extends ColumnLabelProvider { + + public static final String STD_DATE_FORMAT = "yyyy-MM-dd"; + + private String key; + public DataCollectionLabelProvider(String key) { + this.key = key; + } + + @Override + public void addListener(ILabelProviderListener arg0) {} + + @Override + public void dispose() {} + + @Override + public boolean isLabelProperty(Object arg0, String arg1) { + return false; + } + + @Override + public void removeListener(ILabelProviderListener arg0) {} + + @Override + public Image getImage(Object arg0) { + return null; + } + + @Override + public String getText(Object arg0) { + + IDcfDataCollection dc = (IDcfDataCollection) arg0; + + String text = null; + switch(key) { + case "id": + text = String.valueOf(dc.getId()); + break; + case "code": + text = dc.getCode(); + break; + case "description": + text = dc.getDescription(); + break; + case "activeFrom": + DateFormat sdf = new SimpleDateFormat(STD_DATE_FORMAT); + text = sdf.format(dc.getActiveFrom()); + break; + case "activeTo": + sdf = new SimpleDateFormat(STD_DATE_FORMAT); + text = sdf.format(dc.getActiveTo()); + break; + case "category": + text = dc.getCategory(); + break; + case "resId": + text = dc.getResourceId(); + break; + default: + text = ""; + break; + } + + return text; + } +} diff --git a/src/data_collection/DataCollectionsListDialog.java b/src/data_collection/DataCollectionsListDialog.java new file mode 100644 index 0000000..97d83fd --- /dev/null +++ b/src/data_collection/DataCollectionsListDialog.java @@ -0,0 +1,130 @@ +package data_collection; + +import org.eclipse.jface.viewers.DoubleClickEvent; +import org.eclipse.jface.viewers.IDoubleClickListener; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TableViewerColumn; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +import i18n_messages.Messages; + +/** + * Dialog showing a list of data collections + * @author avonva + * + */ +public class DataCollectionsListDialog extends Dialog { + + private IDcfDataCollectionsList list; + private IDcfDataCollection selectedDc; + + public DataCollectionsListDialog(IDcfDataCollectionsList list, Shell parent, int style) { + super(parent, style); + this.list = list; + } + + public DataCollectionsListDialog(IDcfDataCollectionsList list, Shell parent) { + this(list, parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + } + + private void createContents(Shell shell) { + + TableViewer table = new TableViewer(shell, SWT.BORDER | SWT.SINGLE + | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION | SWT.NONE); + + table.getTable().setHeaderVisible(true); + table.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + table.setContentProvider(new DataCollectionContentProvider()); + + table.addDoubleClickListener(new IDoubleClickListener() { + @Override + public void doubleClick(DoubleClickEvent arg0) { + select(shell, arg0.getSelection()); + } + }); + + String[][] headers = new String[][] { + {"code", Messages.get("dc.header.code")}, + {"description", Messages.get("dc.header.description")}, + {"activeFrom", Messages.get("dc.header.active.from")}, + {"activeTo", Messages.get("dc.header.active.to")} + }; + + for (String[] header : headers) { + // Add the column to the parent table + TableViewerColumn col = new TableViewerColumn(table, SWT.NONE); + col.getColumn().setText(header[1]); + col.setLabelProvider(new DataCollectionLabelProvider(header[0])); + col.getColumn().setWidth(150); + } + + table.setInput(list); + + Button button = new Button(shell, SWT.PUSH); + button.setText(Messages.get("dc.dialog.button")); + button.setEnabled(false); + button.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + select(shell, table.getSelection()); + } + }); + button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false)); + + table.addSelectionChangedListener(new ISelectionChangedListener() { + + @Override + public void selectionChanged(SelectionChangedEvent arg0) { + button.setEnabled(!arg0.getSelection().isEmpty()); + } + }); + } + + private void select(Shell shell, ISelection selection) { + + if (selection.isEmpty()) + return; + + IStructuredSelection iSel = (IStructuredSelection) selection; + + selectedDc = (IDcfDataCollection) iSel.getFirstElement(); + + shell.close(); + } + + public IDcfDataCollection open() { + + Shell shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.APPLICATION_MODAL); + shell.setLayout(new GridLayout(1, false)); + shell.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false)); + + shell.setText(Messages.get("dc.dialog.title")); + shell.setImage(getParent().getImage()); + + createContents(shell); + + shell.pack(); + shell.open(); + + Display display = getParent().getDisplay(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) { + display.sleep(); + } + } + + return selectedDc; + } +} diff --git a/src/data_collection/GetAvailableDataCollections.java b/src/data_collection/GetAvailableDataCollections.java new file mode 100644 index 0000000..240f456 --- /dev/null +++ b/src/data_collection/GetAvailableDataCollections.java @@ -0,0 +1,41 @@ +package data_collection; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collection; + +import app_config.PropertiesReader; + +public class GetAvailableDataCollections { + + /** + * Get the available data collections codes starting from the + * starting year of the data collection defined in the configuration + * file. + * @return + */ + public static Collection getCodes() { + + Collection dcCodes = new ArrayList<>(); + + // add test data collection + dcCodes.add(PropertiesReader.getTestDataCollectionCode()); + + // add all the data collections from the starting year + // to today + Calendar today = Calendar.getInstance(); + int currentYear = today.get(Calendar.YEAR); + int startingYear = PropertiesReader.getDataCollectionStartingYear(); + + // if other years are needed + if (currentYear >= startingYear) { + + // add also the other years + for (int i = currentYear; i >= startingYear; --i) { + dcCodes.add(PropertiesReader.getDataCollectionCode(String.valueOf(i))); + } + } + + return dcCodes; + } +} diff --git a/src/dataset/DatasetList.java b/src/dataset/DatasetList.java index 0fa784b..1ede772 100644 --- a/src/dataset/DatasetList.java +++ b/src/dataset/DatasetList.java @@ -12,7 +12,7 @@ * @author avonva * */ -public class DatasetList extends ArrayList implements IDcfDatasetsList { +public class DatasetList extends ArrayList implements IDcfDatasetsList { /** * @@ -298,7 +298,7 @@ public DatasetList getDownloadableDatasets(String validSenderIdPattern) { } @Override - public boolean addElem(IDcfDataset dataset) { + public boolean add(IDataset dataset) { // we know its a dataset instance Dataset d = (Dataset) dataset; @@ -308,7 +308,7 @@ public boolean addElem(IDcfDataset dataset) { } @Override - public IDcfDataset create() { + public IDataset create() { return new Dataset(); } } diff --git a/src/i18n_messages/rcl_messages_en.properties b/src/i18n_messages/rcl_messages_en.properties index 38577e6..53bb486 100644 --- a/src/i18n_messages/rcl_messages_en.properties +++ b/src/i18n_messages/rcl_messages_en.properties @@ -44,6 +44,17 @@ dataset.header.sender.id=Sender id dataset.header.status=DCF status dataset.header.revision=Dataset revision +### data collections list dialog +dc.dialog.title=Your data collections +dc.dialog.button=Open datasets +dc.header.id=Id +dc.header.code=Code +dc.header.description=Description +dc.header.active.from=Active from +dc.header.active.to=Active to +dc.header.category=Category +dc.header.resource.id=Resource id + ### progress bar send.progress.title=Sending report diff --git a/src/report/DownloadReportDialog.java b/src/report/DownloadReportDialog.java index da7525d..07fd59a 100644 --- a/src/report/DownloadReportDialog.java +++ b/src/report/DownloadReportDialog.java @@ -1,8 +1,5 @@ package report; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -13,6 +10,7 @@ import org.eclipse.swt.widgets.Shell; import app_config.PropertiesReader; +import data_collection.IDcfDataCollection; import dataset.Dataset; import dataset.DatasetList; import dataset.IDataset; @@ -34,6 +32,7 @@ public class DownloadReportDialog extends DatasetListDialog { private DatasetList allDatasets; private DatasetList downloadableDatasets; private String validSenderIdPattern; + private IDcfDataCollection dc; /** * @@ -42,9 +41,9 @@ public class DownloadReportDialog extends DatasetListDialog { * dataset must follow to be considered downloadable (used to filter * the datasets) */ - public DownloadReportDialog(Shell parent, String validSenderIdPattern) { - + public DownloadReportDialog(IDcfDataCollection dc, Shell parent, String validSenderIdPattern) { super(parent, Messages.get("download.title"), Messages.get("download.button")); + this.dc = dc; this.validSenderIdPattern = validSenderIdPattern; this.allDatasets = new DatasetList(); this.downloadableDatasets = new DatasetList(); @@ -52,10 +51,6 @@ public DownloadReportDialog(Shell parent, String validSenderIdPattern) { public void loadDatasets() { - Shell parent = getParent(); - - parent.setCursor(parent.getDisplay().getSystemCursor(SWT.CURSOR_WAIT)); - // prepare downloadableDatasets and allDatasets lists initDatasets(validSenderIdPattern); @@ -63,8 +58,6 @@ public void loadDatasets() { this.downloadableDatasets.sort(); this.setList(downloadableDatasets); - - parent.setCursor(parent.getDisplay().getSystemCursor(SWT.CURSOR_ARROW)); } /** @@ -77,36 +70,15 @@ public void loadDatasets() { */ private void initDatasets(String validSenderIdPattern) { - Collection dcCodes = new ArrayList<>(); - dcCodes.add(PropertiesReader.getTestDataCollectionCode()); // add test dc - - Calendar today = Calendar.getInstance(); - int currentYear = today.get(Calendar.YEAR); - int startingYear = PropertiesReader.getDataCollectionStartingYear(); - - // if other years are needed - if (currentYear >= startingYear) { - - // add also the other years - for (int i = currentYear; i >= startingYear; --i) { - dcCodes.add(PropertiesReader.getDataCollectionCode(String.valueOf(i))); - } - } - datasetsList = new DatasetList(); - - // for each data collection get the datasets - // and save them in the output - for (String dcCode : dcCodes) { + + GetDatasetsList req = new GetDatasetsList<>(User.getInstance(), dc.getCode(), datasetsList); + try { + + req.getList(); - GetDatasetsList req = new GetDatasetsList(User.getInstance(), dcCode, datasetsList); - try { - - req.getList(); - - } catch (SOAPException e) { - e.printStackTrace(); - } + } catch (SOAPException e) { + e.printStackTrace(); } allDatasets.addAll(datasetsList.getDownloadableDatasets(validSenderIdPattern)); diff --git a/src/report/Report.java b/src/report/Report.java index fe86390..8971687 100644 --- a/src/report/Report.java +++ b/src/report/Report.java @@ -14,6 +14,7 @@ import app_config.PropertiesReader; import dataset.Dataset; import dataset.DatasetList; +import dataset.IDataset; import dataset.RCLDatasetStatus; import message.MessageConfigBuilder; import message.MessageResponse; @@ -196,7 +197,7 @@ public DatasetList getDatasets() throws MySOAPException, ReportException { DatasetList output = new DatasetList(); // check if the Report is in the DCF - GetDatasetsList request = new GetDatasetsList(User.getInstance(), PropertiesReader + GetDatasetsList request = new GetDatasetsList<>(User.getInstance(), PropertiesReader .getDataCollectionCode(this.getYear()), output); String senderDatasetId = this.getSenderId(); diff --git a/src/report/ReportDownloader.java b/src/report/ReportDownloader.java index 3522520..046f84e 100644 --- a/src/report/ReportDownloader.java +++ b/src/report/ReportDownloader.java @@ -1,13 +1,24 @@ package report; +import java.util.Collection; + +import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Shell; import amend_manager.ReportImporter; +import data_collection.DataCollectionsListDialog; +import data_collection.DcfDataCollectionsList; +import data_collection.GetAvailableDataCollections; +import data_collection.IDcfDataCollection; +import data_collection.IDcfDataCollectionsList; import dataset.Dataset; import dataset.DatasetList; import i18n_messages.Messages; import progress_bar.FormProgressBar; import progress_bar.ProgressListener; +import soap.GetDataCollectionsList; +import soap.MySOAPException; +import user.User; /** * Download a report into the database @@ -21,14 +32,56 @@ public abstract class ReportDownloader { public ReportDownloader(Shell shell) { this.shell = shell; } + + /** + * Get only the available data collections for which the user is registered + * @return + * @throws MySOAPException + */ + private IDcfDataCollectionsList getAvailableDcList() throws MySOAPException { + + IDcfDataCollectionsList output = new DcfDataCollectionsList(); + GetDataCollectionsList req = new GetDataCollectionsList<>(User.getInstance(), output); + + req.getList(); + + Collection validDcs = GetAvailableDataCollections.getCodes(); + + IDcfDataCollectionsList filteredOutput = new DcfDataCollectionsList(); + + for(IDcfDataCollection dc : output) { + // remove not valid data collection + if (validDcs.contains(dc.getCode())) { + filteredOutput.add(dc); + } + } + + return filteredOutput; + } /** * Download a dataset from the dcf * @param validSenderId + * @throws MySOAPException */ - public void download() { + public void download() throws MySOAPException { + + shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT)); + + // select the data collection + IDcfDataCollectionsList list = getAvailableDcList(); + + shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_ARROW)); + + DataCollectionsListDialog dcDialog = new DataCollectionsListDialog(list, shell); + + IDcfDataCollection selectedDc = dcDialog.open(); + + if (selectedDc == null) + return; - DownloadReportDialog dialog = getDialog(); + // open the list of datasets related to that data collection + DownloadReportDialog dialog = getDownloadDialog(selectedDc); dialog.open(); @@ -127,7 +180,7 @@ public void run() { * Here it is possible to customize the columns that should be shown. * @return */ - public abstract DownloadReportDialog getDialog(); + public abstract DownloadReportDialog getDownloadDialog(IDcfDataCollection dataCollection); public abstract boolean askConfirmation(); diff --git a/src/table_dialog/DatasetContentProvider.java b/src/table_dialog/DatasetContentProvider.java index 8a0b621..1f355c5 100644 --- a/src/table_dialog/DatasetContentProvider.java +++ b/src/table_dialog/DatasetContentProvider.java @@ -28,5 +28,4 @@ public Object[] getElements(Object arg0) { return null; } - } diff --git a/src/table_dialog/DatasetListDialog.java b/src/table_dialog/DatasetListDialog.java index 83564e7..58e91e1 100644 --- a/src/table_dialog/DatasetListDialog.java +++ b/src/table_dialog/DatasetListDialog.java @@ -111,8 +111,6 @@ public void addRevisionCol() { public void open() { - dialog.pack(); - this.dialog.setSize(dialog.getSize().x, 500); dialog.open(); // Event loop diff --git a/src/table_dialog/TableDialog.java b/src/table_dialog/TableDialog.java index 9355515..a30c68a 100644 --- a/src/table_dialog/TableDialog.java +++ b/src/table_dialog/TableDialog.java @@ -117,7 +117,7 @@ public TableDialog(Shell parent, String title, boolean createPopUp, boolean addS /** * Create the interface */ - protected void create() { + protected Shell create() { // new shell if required if (createPopUp) { @@ -143,7 +143,7 @@ protected void create() { if (!this.panel.isTableDefined()) { System.err.println("Error. Cannot instantiate TableDialog without a table. Please check addWidgets()."); - return; + return null; } this.panel.setMenu(createMenu()); @@ -250,6 +250,8 @@ public void handleEvent(Event arg0) { // make dialog longer dialog.setSize(dialog.getSize().x, dialog.getSize().y + 50); + + return dialog; }