Search in sources :

Example 1 with OldDatFileCallImporter

use of de.janrufmonitor.repository.imexporter.OldDatFileCallImporter in project janrufmonitor by tbrandt77.

the class ImportAction method run.

public void run(String[] filenames) {
    try {
        String filename = filenames[0];
        TableViewer viewer = (TableViewer) this.m_app.getApplication().getViewer();
        Cursor c = new Cursor(viewer.getTable().getDisplay(), SWT.CURSOR_WAIT);
        if (!filename.endsWith(this.m_app.getApplication().getConfiguration().getProperty(CFG_OLD_JOURNAL, "ajournal.dat"))) {
            File f = new File(filename);
            String ext = f.getName();
            IImExporter ie = ImExportFactory.getInstance().getImporterByExtension(ext);
            if (ie == null && ext.lastIndexOf(".") >= 0) {
                ext = "*" + ext.substring(ext.lastIndexOf("."));
                ie = ImExportFactory.getInstance().getImporterByExtension(ext);
                if (ie == null) {
                    PropagationFactory.getInstance().fire(new Message(Message.ERROR, getI18nManager().getString(Journal.NAMESPACE, "title", "label", getLanguage()), new Exception(getI18nManager().getString(getNamespace(), "error", "description", getLanguage()))), "Tray");
                    new SWTExecuter() {

                        protected void execute() {
                            MessageDialog.openError(DisplayManager.getDefaultDisplay().getActiveShell(), getI18nManager().getString(getNamespace(), "error", "label", getLanguage()), getI18nManager().getString(getNamespace(), "error", "description", getLanguage()));
                            m_logger.warning("Import of data failed.");
                        }
                    }.start();
                }
            }
            if (ie != null && ie.getMode() != IImExporter.CALL_MODE)
                return;
            final IImExporter imp = ie;
            imp.setFilename(filename);
            viewer.getTable().getShell().setCursor(c);
            ProgressMonitorDialog pmd = new ProgressMonitorDialog(DisplayManager.getDefaultDisplay().getActiveShell());
            try {
                IRunnableWithProgress r = new IRunnableWithProgress() {

                    public void run(IProgressMonitor progressMonitor) {
                        progressMonitor.beginTask(getI18nManager().getString(getNamespace(), "importprogress", "label", getLanguage()), IProgressMonitor.UNKNOWN);
                        progressMonitor.worked(1);
                        final ICallList importedCalls = getRuntime().getCallFactory().createCallList();
                        if (imp.getMode() == IImExporter.CALL_MODE) {
                            importedCalls.add(((ICallImporter) imp).doImport());
                            m_app.getController().addElements(importedCalls);
                        }
                        if (importedCalls.size() > 0) {
                            final String msg = StringUtils.replaceString(getI18nManager().getString(getNamespace(), "success", "description", getLanguage()), "{%1}", Integer.toString(importedCalls.size()));
                            PropagationFactory.getInstance().fire(new Message(Message.INFO, getI18nManager().getString(Journal.NAMESPACE, "title", "label", getLanguage()), new Exception(msg)), "Tray");
                            new SWTExecuter() {

                                protected void execute() {
                                    m_app.updateViews(true);
                                }
                            }.start();
                        } else {
                            PropagationFactory.getInstance().fire(new Message(Message.ERROR, getI18nManager().getString(Journal.NAMESPACE, "title", "label", getLanguage()), new Exception(getI18nManager().getString(getNamespace(), "error", "description", getLanguage()))), "Tray");
                            new SWTExecuter() {

                                protected void execute() {
                                    MessageDialog.openError(DisplayManager.getDefaultDisplay().getActiveShell(), getI18nManager().getString(getNamespace(), "error", "label", getLanguage()), getI18nManager().getString(getNamespace(), "error", "description", getLanguage()));
                                    m_logger.warning("Import of data failed.");
                                    // m_app.getController().addElements(importedCalls);
                                    m_app.updateViews(true);
                                }
                            }.start();
                        }
                        progressMonitor.done();
                    }
                };
                pmd.setBlockOnOpen(false);
                pmd.run(true, false, r);
            // ModalContext.run(r, true,
            // pmd.getProgressMonitor(),
            // DisplayManager.getDefaultDisplay());
            } catch (InterruptedException e) {
                throw e;
            } catch (InvocationTargetException e) {
                throw e;
            }
            viewer.getTable().getShell().setCursor(null);
            c.dispose();
        } else {
            // do ajournal.dat migration
            int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
            MessageBox messageBox = new MessageBox(new Shell(DisplayManager.getDefaultDisplay()), style);
            messageBox.setMessage(this.getI18nManager().getString(this.getNamespace(), "migrationconfirm", "label", this.getLanguage()));
            if (messageBox.open() == SWT.YES) {
                viewer.getTable().getShell().setCursor(c);
                IImExporter imp = ImExportFactory.getInstance().getImporter("OldDatFileCallImporter");
                if (imp != null && (imp instanceof OldDatFileCallImporter)) {
                    ((OldDatFileCallImporter) imp).setDatePattern(this.m_app.getApplication().getConfiguration().getProperty(CFG_OLD_DATE, "dd.MM.yyyy HH:mm:ss"));
                    ((OldDatFileCallImporter) imp).setFilename(filename);
                    ICallList importedCalls = ((OldDatFileCallImporter) imp).doImport();
                    if (importedCalls != null) {
                        this.m_app.getController().addElements(importedCalls);
                        String msg = getI18nManager().getString(getNamespace(), "success", "description", getLanguage());
                        msg = StringUtils.replaceString(msg, "{%1}", Integer.toString(importedCalls.size()));
                        if (!suppressDialogs)
                            MessageDialog.openInformation(new Shell(DisplayManager.getDefaultDisplay()), getI18nManager().getString(getNamespace(), "success", "label", getLanguage()), msg);
                        m_app.updateViews(true);
                    }
                }
                viewer.getTable().getShell().setCursor(null);
                c.dispose();
            } else {
                return;
            }
        }
    } catch (Exception ex) {
        this.m_logger.log(Level.SEVERE, ex.getMessage(), ex);
        PropagationFactory.getInstance().fire(new Message(Message.ERROR, getNamespace(), "error", ex));
    }
}
Also used : Message(de.janrufmonitor.exception.Message) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) OldDatFileCallImporter(de.janrufmonitor.repository.imexporter.OldDatFileCallImporter) Cursor(org.eclipse.swt.graphics.Cursor) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) MessageBox(org.eclipse.swt.widgets.MessageBox) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Shell(org.eclipse.swt.widgets.Shell) ICallList(de.janrufmonitor.framework.ICallList) IImExporter(de.janrufmonitor.repository.imexport.IImExporter) TableViewer(org.eclipse.jface.viewers.TableViewer) File(java.io.File) SWTExecuter(de.janrufmonitor.ui.swt.SWTExecuter)

Aggregations

Message (de.janrufmonitor.exception.Message)1 ICallList (de.janrufmonitor.framework.ICallList)1 IImExporter (de.janrufmonitor.repository.imexport.IImExporter)1 OldDatFileCallImporter (de.janrufmonitor.repository.imexporter.OldDatFileCallImporter)1 SWTExecuter (de.janrufmonitor.ui.swt.SWTExecuter)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 Cursor (org.eclipse.swt.graphics.Cursor)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 Shell (org.eclipse.swt.widgets.Shell)1