Search in sources :

Example 1 with EditorFilterManager

use of de.janrufmonitor.ui.jface.application.editor.EditorFilterManager in project janrufmonitor by tbrandt77.

the class OutlookImportAction method run.

public void run() {
    String filter = this.m_app.getApplication().getConfiguration().getProperty("filter", "");
    EditorFilterManager efm = new EditorFilterManager();
    final IAttribute category = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CATEGORY, "");
    if (filter.length() > 0) {
        String cat = null;
        IFilter[] filters = efm.getFiltersFromString(filter);
        for (int z = 0; z < filters.length; z++) {
            if (filters[z].getType().equals(FilterType.ATTRIBUTE)) {
                AttributeFilter cf = ((AttributeFilter) filters[z]);
                IAttributeMap m = cf.getAttributeMap();
                if (m != null && m.size() > 0) {
                    Iterator it = m.iterator();
                    IAttribute a = null;
                    while (it.hasNext()) {
                        a = (IAttribute) it.next();
                        if (a.getName().equalsIgnoreCase(IJAMConst.ATTRIBUTE_NAME_CATEGORY)) {
                            cat = a.getValue();
                        }
                    }
                }
            }
        }
        if (cat != null) {
            int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
            MessageBox messageBox = new MessageBox(new Shell(DisplayManager.getDefaultDisplay()), style);
            String text = this.getI18nManager().getString("ui.jface.application.editor.action.ImportAction", "assigncategoryconfirm", "label", this.getLanguage());
            text = StringUtils.replaceString(text, "{%1}", cat);
            messageBox.setMessage(text);
            if (messageBox.open() == SWT.YES) {
                category.setValue(cat);
            }
        }
    }
    // determine subfolders
    OutlookTransformer ot = new OutlookTransformer();
    List folders = ot.getAllContactFolders();
    if (folders.size() > 0) {
        int itemCount = 0;
        String folder = null;
        for (int i = 0, j = folders.size(); i < j; i++) {
            folder = (String) folders.get(i);
            itemCount = ot.getContactCount(folder);
            if (itemCount > 0) {
                getRuntime().getConfigManagerFactory().getConfigManager().setProperty(OutlookContactManager.NAMESPACE, "subfolder_" + folder, "true");
            }
        }
        getRuntime().getConfigManagerFactory().getConfigManager().saveConfiguration();
    }
    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);
                OutlookTransformer otf = new OutlookTransformer();
                ICallerList callers = otf.getCallerListFromAllContacts();
                progressMonitor.setTaskName(getI18nManager().getString(getNamespace(), "reduce", "label", getLanguage()));
                otf.removeCallerManagerID(callers);
                // determine duplicates
                String currentCallerManagerID = m_app.getApplication().getConfiguration().getProperty(CFG_REPOSITORY);
                if (currentCallerManagerID != null && currentCallerManagerID.trim().length() > 0) {
                    ICallerManager currentCallerManager = getRuntime().getCallerManagerFactory().getCallerManager(currentCallerManagerID);
                    if (currentCallerManager != null && currentCallerManager.isSupported(IWriteCallerRepository.class) && currentCallerManager.isSupported(IIdentifyCallerRepository.class)) {
                        ICallerList addCallers = getRuntime().getCallerFactory().createCallerList();
                        // ICallerList removeCallers = getRuntime().getCallerFactory().createCallerList();
                        ICaller currentCaller = null;
                        String text = null;
                        for (int i = 0, j = callers.size(); i < j; i++) {
                            // ICaller testCaller = null;
                            currentCaller = callers.get(i);
                            text = getI18nManager().getString(getNamespace(), "check", "label", getLanguage());
                            text = StringUtils.replaceString(text, "{%1}", Formatter.getInstance(getRuntime()).parse("%a:ln%, %a:fn%", currentCaller));
                            progressMonitor.setTaskName(text);
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e1) {
                            }
                            try {
                                // testCaller = ((IIdentifyCallerRepository)currentCallerManager).getCaller(currentCaller.getPhoneNumber());
                                ((IIdentifyCallerRepository) currentCallerManager).getCaller(currentCaller.getPhoneNumber());
                            } catch (CallerNotFoundException e) {
                                if (category != null && category.getValue().length() > 0)
                                    currentCaller.setAttribute(category);
                                addCallers.add(currentCaller);
                            }
                        // if (testCaller!=null) {
                        // removeCallers.add(testCaller);
                        // }
                        }
                        progressMonitor.setTaskName(getI18nManager().getString(getNamespace(), "add", "label", getLanguage()));
                        try {
                            Thread.sleep(1250);
                        } catch (InterruptedException e1) {
                        }
                        // if (removeCallers.size()>0)
                        // m_app.getController().deleteElements(removeCallers);
                        m_app.getController().addElements(addCallers);
                    }
                } else {
                    progressMonitor.setTaskName(getI18nManager().getString(getNamespace(), "add", "label", getLanguage()));
                    m_app.getController().addElements(callers);
                }
                progressMonitor.done();
            }
        };
        pmd.setBlockOnOpen(false);
        pmd.run(true, false, r);
    // ModalContext.run(r, true, pmd.getProgressMonitor(), DisplayManager.getDefaultDisplay());
    } catch (InterruptedException e) {
        m_logger.log(Level.SEVERE, e.getMessage(), e);
    } catch (InvocationTargetException e) {
        m_logger.log(Level.SEVERE, e.getMessage(), e);
    }
    m_app.updateViews(true);
    return;
}
Also used : EditorFilterManager(de.janrufmonitor.ui.jface.application.editor.EditorFilterManager) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) OutlookTransformer(de.janrufmonitor.repository.OutlookTransformer) IIdentifyCallerRepository(de.janrufmonitor.repository.types.IIdentifyCallerRepository) ICallerManager(de.janrufmonitor.repository.ICallerManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageBox(org.eclipse.swt.widgets.MessageBox) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ICaller(de.janrufmonitor.framework.ICaller) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ICallerList(de.janrufmonitor.framework.ICallerList) IFilter(de.janrufmonitor.repository.filter.IFilter) IAttribute(de.janrufmonitor.framework.IAttribute) AttributeFilter(de.janrufmonitor.repository.filter.AttributeFilter) Iterator(java.util.Iterator) CallerNotFoundException(de.janrufmonitor.repository.CallerNotFoundException) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List)

Example 2 with EditorFilterManager

use of de.janrufmonitor.ui.jface.application.editor.EditorFilterManager in project janrufmonitor by tbrandt77.

the class ImportAction method run.

public void run(String[] filenames) {
    List ids = ImExportFactory.getInstance().getAllImporterIds(IImExporter.CALLER_MODE);
    String filename = null;
    for (int h = 0; h < filenames.length; h++) {
        filename = filenames[h];
        if (!filename.endsWith(this.m_app.getApplication().getConfiguration().getProperty(CFG_OLD_EDITOR, "cpnumber.dat"))) {
            String ext = "";
            for (int i = 0; i < ids.size(); i++) {
                final IImExporter imp = ImExportFactory.getInstance().getImporter((String) ids.get(i));
                ext = imp.getExtension();
                if (ext.startsWith("*"))
                    ext = ext.substring(1);
                if (filename.toLowerCase().endsWith(ext) && imp.getMode() == IImExporter.CALLER_MODE) {
                    imp.setFilename(filename);
                    // chedk for categorie filters
                    String filter = this.m_app.getApplication().getConfiguration().getProperty("filter", "");
                    EditorFilterManager efm = new EditorFilterManager();
                    final IAttribute category = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CATEGORY, "");
                    if (filter.length() > 0) {
                        String cat = null;
                        IFilter[] filters = efm.getFiltersFromString(filter);
                        for (int z = 0; z < filters.length; z++) {
                            if (filters[z].getType().equals(FilterType.ATTRIBUTE)) {
                                AttributeFilter cf = ((AttributeFilter) filters[z]);
                                IAttributeMap m = cf.getAttributeMap();
                                if (m != null && m.size() > 0) {
                                    Iterator it = m.iterator();
                                    IAttribute a = null;
                                    while (it.hasNext()) {
                                        a = (IAttribute) it.next();
                                        if (a.getName().equalsIgnoreCase(IJAMConst.ATTRIBUTE_NAME_CATEGORY)) {
                                            cat = a.getValue();
                                        }
                                    }
                                }
                            }
                        }
                        if (cat != null) {
                            int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
                            MessageBox messageBox = new MessageBox(new Shell(DisplayManager.getDefaultDisplay()), style);
                            String text = this.getI18nManager().getString(this.getNamespace(), "assigncategoryconfirm", "label", this.getLanguage());
                            text = StringUtils.replaceString(text, "{%1}", cat);
                            messageBox.setMessage(text);
                            if (messageBox.open() == SWT.YES) {
                                category.setValue(cat);
                            }
                        }
                    }
                    final String f = filename;
                    try {
                        ProgressMonitorDialog pmd = new ProgressMonitorDialog(DisplayManager.getDefaultDisplay().getActiveShell());
                        IRunnableWithProgress r = new IRunnableWithProgress() {

                            public void run(IProgressMonitor progressMonitor) {
                                progressMonitor.beginTask(getI18nManager().getString(getNamespace(), "importprogress", "label", getLanguage()), IProgressMonitor.UNKNOWN);
                                ICallerList importedCallers = (ICallerList) getRuntime().getCallerFactory().createCallerList();
                                if (imp.getMode() == IImExporter.CALLER_MODE) {
                                    ImportThread ti = new ImportThread((ICallerImporter) imp, (category.getValue().length() == 0 ? null : category));
                                    Thread t = new Thread(ti);
                                    t.start();
                                    while (t.isAlive()) {
                                        if (imp instanceof ITracker) {
                                            String text = getI18nManager().getString(getNamespace(), "importprogress2", "label", getLanguage());
                                            text = StringUtils.replaceString(text, "{%1}", Integer.toString(((ITracker) imp).getCurrent()));
                                            text = StringUtils.replaceString(text, "{%2}", Integer.toString(((ITracker) imp).getTotal()));
                                            progressMonitor.beginTask(text, IProgressMonitor.UNKNOWN);
                                        }
                                        try {
                                            Thread.sleep(500);
                                        } catch (InterruptedException e) {
                                        }
                                    }
                                    if (ti != null) {
                                        importedCallers = ti.getResult();
                                        m_app.getController().addElements(importedCallers);
                                    }
                                }
                                if (importedCallers.size() > 0) {
                                    progressMonitor.done();
                                    String msg = getI18nManager().getString(getNamespace(), "success", "description", getLanguage());
                                    msg = StringUtils.replaceString(msg, "{%1}", Integer.toString(importedCallers.size()));
                                    msg = StringUtils.replaceString(msg, "{%2}", f);
                                    PropagationFactory.getInstance().fire(new Message(Message.INFO, getI18nManager().getString(Editor.NAMESPACE, "title", "label", getLanguage()), new Exception(msg)), "Tray");
                                    new SWTExecuter() {

                                        protected void execute() {
                                            m_app.updateViews(true);
                                        }
                                    }.start();
                                } else {
                                    progressMonitor.done();
                                    PropagationFactory.getInstance().fire(new Message(Message.ERROR, getI18nManager().getString(Editor.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();
                                }
                            }
                        };
                        pmd.setBlockOnOpen(false);
                        pmd.run(true, false, r);
                    } catch (InterruptedException e) {
                        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                    } catch (InvocationTargetException e) {
                        this.m_logger.log(Level.SEVERE, e.getMessage(), e);
                    }
                }
            }
        } else {
            // do cpnumber.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) {
                IImExporter imp = ImExportFactory.getInstance().getImporter("OldDatFileCallerImporter");
                if (imp != null && (imp instanceof OldDatFileCallerImporter)) {
                    ((OldDatFileCallerImporter) imp).setFilename(filename);
                    ICallerList importedCallers = ((OldDatFileCallerImporter) imp).doImport();
                    if (importedCallers != null) {
                        final String f = filename;
                        this.m_app.getController().addElements(importedCallers);
                        String msg = getI18nManager().getString(getNamespace(), "success", "description", getLanguage());
                        msg = StringUtils.replaceString(msg, "{%1}", Integer.toString(importedCallers.size()));
                        msg = StringUtils.replaceString(msg, "{%2}", f);
                        PropagationFactory.getInstance().fire(new Message(Message.INFO, getI18nManager().getString(Editor.NAMESPACE, "title", "label", getLanguage()), new Exception(msg)), "Tray");
                        m_app.updateViews(true);
                    }
                }
            } else {
                return;
            }
        }
    }
}
Also used : Message(de.janrufmonitor.exception.Message) EditorFilterManager(de.janrufmonitor.ui.jface.application.editor.EditorFilterManager) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Shell(org.eclipse.swt.widgets.Shell) IImExporter(de.janrufmonitor.repository.imexport.IImExporter) Iterator(java.util.Iterator) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) SWTExecuter(de.janrufmonitor.ui.swt.SWTExecuter) ITracker(de.janrufmonitor.repository.imexport.ITracker) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessageBox(org.eclipse.swt.widgets.MessageBox) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ICallerList(de.janrufmonitor.framework.ICallerList) IFilter(de.janrufmonitor.repository.filter.IFilter) IAttribute(de.janrufmonitor.framework.IAttribute) AttributeFilter(de.janrufmonitor.repository.filter.AttributeFilter) OldDatFileCallerImporter(de.janrufmonitor.repository.imexporter.OldDatFileCallerImporter)

Aggregations

IAttribute (de.janrufmonitor.framework.IAttribute)2 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)2 ICallerList (de.janrufmonitor.framework.ICallerList)2 AttributeFilter (de.janrufmonitor.repository.filter.AttributeFilter)2 IFilter (de.janrufmonitor.repository.filter.IFilter)2 EditorFilterManager (de.janrufmonitor.ui.jface.application.editor.EditorFilterManager)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Iterator (java.util.Iterator)2 List (java.util.List)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 MessageBox (org.eclipse.swt.widgets.MessageBox)2 Shell (org.eclipse.swt.widgets.Shell)2 Message (de.janrufmonitor.exception.Message)1 ICaller (de.janrufmonitor.framework.ICaller)1 CallerNotFoundException (de.janrufmonitor.repository.CallerNotFoundException)1 ICallerManager (de.janrufmonitor.repository.ICallerManager)1 OutlookTransformer (de.janrufmonitor.repository.OutlookTransformer)1 IImExporter (de.janrufmonitor.repository.imexport.IImExporter)1