Search in sources :

Example 16 with IMultiPhoneCaller

use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method updateContact.

public synchronized void updateContact(ICaller caller) throws GoogleContactsException {
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    if (caller == null)
        return;
    ContactsService cs = login();
    ContactEntry entry = null;
    IAttributeMap m = caller.getAttributes();
    try {
        if (caller.getAttributes().contains("entryUrl")) {
            String entryUrl = caller.getAttribute("entryUrl").getValue();
            if (entryUrl.length() > 0) {
                entry = (ContactEntry) cs.getEntry(new URL(entryUrl), ContactEntry.class);
                if (entry == null) {
                    this.m_logger.warning("Cannot update google contact: " + caller.toString());
                    return;
                }
            } else {
                this.m_logger.warning("Invalid entryUrl parameter. Cannot update google contact: " + caller.toString());
                return;
            }
        } else {
            // no update possible, contact must be new
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("No update possible dur to missing entryUrl. Creating google contact: " + caller.toString());
            ICallerList cl = getRuntime().getCallerFactory().createCallerList();
            cl.add(caller);
            this.createContacts(cl);
            return;
        }
        Name name = new Name();
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_LASTNAME))
            name.setFamilyName(new FamilyName((m.get(IJAMConst.ATTRIBUTE_NAME_LASTNAME).getValue().length() == 0 ? " " : m.get(IJAMConst.ATTRIBUTE_NAME_LASTNAME).getValue()), null));
        else
            name.setFamilyName(new FamilyName(" ", null));
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME))
            name.setGivenName(new GivenName((m.get(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME).getValue().length() == 0 ? " " : m.get(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME).getValue()), null));
        else
            name.setGivenName(new GivenName(" ", null));
        name.setFullName(new FullName(Formatter.getInstance(getRuntime()).parse("%a:ln%, %a:fn%", m), null));
        entry.setName(name);
        entry.getStructuredPostalAddresses().clear();
        entry.addStructuredPostalAddress(createPostalAddress(m));
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
            List emaillist = entry.getEmailAddresses();
            String rel = Email.Rel.HOME;
            if (emaillist.size() > 0) {
                Email eold = entry.getEmailAddresses().remove(0);
                rel = eold.getRel();
            }
            Email email = new Email();
            email.setAddress(m.get(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue());
            email.setRel(rel);
            entry.addEmailAddress(email);
        }
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_CATEGORY)) {
            String cat = m.get(IJAMConst.ATTRIBUTE_NAME_CATEGORY).getValue();
            if (this.m_reverseCategories.containsKey(cat)) {
                GroupMembershipInfo gmi = new GroupMembershipInfo();
                gmi.setHref((String) this.m_reverseCategories.get(cat));
                entry.addGroupMembershipInfo(gmi);
            }
        }
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_ACC)) {
            ExtendedProperty acc = new ExtendedProperty();
            acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_ACC);
            acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_ACC).getValue());
            entry.addExtendedProperty(acc);
        }
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_LNG)) {
            ExtendedProperty acc = new ExtendedProperty();
            acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_LNG);
            acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_LNG).getValue());
            entry.addExtendedProperty(acc);
        }
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_LAT)) {
            ExtendedProperty acc = new ExtendedProperty();
            acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_LAT);
            acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_LAT).getValue());
            entry.addExtendedProperty(acc);
        }
        PhoneNumber pn = null;
        entry.getPhoneNumbers().clear();
        if (caller instanceof IMultiPhoneCaller) {
            List phones = ((IMultiPhoneCaller) caller).getPhonenumbers();
            IPhonenumber p = null;
            for (int k = 0, l = phones.size(); k < l; k++) {
                p = (IPhonenumber) phones.get(k);
                pn = new PhoneNumber();
                pn.setPrimary(k == 0);
                IAttribute type = m.get(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + p.getTelephoneNumber());
                if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                    pn.setRel(PhoneNumber.Rel.MOBILE);
                } else if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                    pn.setRel(PhoneNumber.Rel.HOME_FAX);
                } else {
                    pn.setRel(PhoneNumber.Rel.HOME);
                }
                pn.setPhoneNumber(Formatter.getInstance(getRuntime()).parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, p));
                entry.addPhoneNumber(pn);
            }
        } else {
            pn = new PhoneNumber();
            IAttribute type = m.get(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + caller.getPhoneNumber().getTelephoneNumber());
            if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                pn.setRel(PhoneNumber.Rel.MOBILE);
            } else if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                pn.setRel(PhoneNumber.Rel.HOME_FAX);
            } else {
                pn.setRel(PhoneNumber.Rel.HOME);
            }
            pn.setPhoneNumber(Formatter.getInstance(getRuntime()).parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, caller.getPhoneNumber()));
            entry.addPhoneNumber(pn);
        }
        URL editUrl = new URL(entry.getEditLink().getHref());
        entry = (ContactEntry) cs.update(editUrl, entry);
        if (m.contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
            String file = PathResolver.getInstance(getRuntime()).resolve(m.get(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue());
            if (new File(file).exists()) {
                FileInputStream in = new FileInputStream(file);
                Link photoLink = entry.getContactPhotoLink();
                URL photoUrl = new URL(photoLink.getHref());
                GDataRequest request = cs.createRequest(GDataRequest.RequestType.UPDATE, photoUrl, new ContentType("image/jpeg"));
                if (photoLink.getEtag() != null) {
                    request.setEtag(photoLink.getEtag());
                }
                Stream.copy(in, request.getRequestStream());
                request.execute();
            }
        }
        if (entry != null && this.m_dbh != null) {
            try {
                if (caller instanceof IMultiPhoneCaller) {
                    List phones = ((IMultiPhoneCaller) caller).getPhonenumbers();
                    IPhonenumber p = null;
                    this.m_dbh.delete(caller.getUUID());
                    for (int k = 0; k < phones.size(); k++) {
                        p = (IPhonenumber) phones.get(k);
                        this.m_dbh.insert(caller.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber());
                    }
                } else {
                    IPhonenumber p = caller.getPhoneNumber();
                    this.m_dbh.delete(caller.getUUID());
                    this.m_dbh.insert(caller.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber());
                }
            } catch (SQLException e) {
                throw new GoogleContactsException(e.getMessage(), e);
            }
        } else {
            this.m_logger.warning("GoogleContacts proxy datahandler not initialized. Could not insert google contacts...");
        }
    } catch (MalformedURLException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Email(com.google.gdata.data.extensions.Email) ContentType(com.google.gdata.util.ContentType) FamilyName(com.google.gdata.data.extensions.FamilyName) SQLException(java.sql.SQLException) GivenName(com.google.gdata.data.extensions.GivenName) GDataRequest(com.google.gdata.client.Service.GDataRequest) URL(java.net.URL) FamilyName(com.google.gdata.data.extensions.FamilyName) Name(com.google.gdata.data.extensions.Name) GivenName(com.google.gdata.data.extensions.GivenName) FullName(com.google.gdata.data.extensions.FullName) ContactsService(com.google.gdata.client.contacts.ContactsService) ContactEntry(com.google.gdata.data.contacts.ContactEntry) GroupMembershipInfo(com.google.gdata.data.contacts.GroupMembershipInfo) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) List(java.util.List) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) IOException(java.io.IOException) ExtendedProperty(com.google.gdata.data.extensions.ExtendedProperty) FileInputStream(java.io.FileInputStream) ICallerList(de.janrufmonitor.framework.ICallerList) ServiceException(com.google.gdata.util.ServiceException) FullName(com.google.gdata.data.extensions.FullName) IAttribute(de.janrufmonitor.framework.IAttribute) PhoneNumber(com.google.gdata.data.extensions.PhoneNumber) File(java.io.File) Link(com.google.gdata.data.Link) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 17 with IMultiPhoneCaller

use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method preload.

public void preload() throws GoogleContactsException {
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    ICallerList cl = getRuntime().getCallerFactory().createCallerList(getMaxResults());
    ContactsService cs = login();
    try {
        URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/" + getLoginUser() + "/full");
        Query q = new Query(feedUrl);
        q.setMaxResults(getMaxResults());
        ContactFeed resultFeed = (ContactFeed) cs.getFeed(q, ContactFeed.class);
        List entries = resultFeed.getEntries();
        this.m_total = entries.size();
        this.m_logger.info("Fetched " + entries.size() + " entries from google account " + getLoginUser());
        Object o = null;
        for (int i = 0, j = entries.size(); i < j; i++) {
            o = entries.get(i);
            if (o instanceof ContactEntry) {
                // && (category==null || matchCategory(category, (ContactEntry) o))) {
                ICaller c = this.parse(cs, (ContactEntry) o);
                if (c != null) {
                    cl.add(c);
                }
            }
        }
    } catch (MalformedURLException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    // 
    if (this.m_dbh != null) {
        try {
            ICaller c = null;
            for (int i = 0, j = cl.size(); i < j; i++) {
                c = cl.get(i);
                if (c instanceof IMultiPhoneCaller) {
                    List phones = ((IMultiPhoneCaller) c).getPhonenumbers();
                    IPhonenumber pn = null;
                    this.m_dbh.delete(c.getUUID());
                    for (int k = 0; k < phones.size(); k++) {
                        pn = (IPhonenumber) phones.get(k);
                        this.m_dbh.insert(c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber());
                    }
                } else {
                    IPhonenumber pn = c.getPhoneNumber();
                    this.m_dbh.delete(c.getUUID());
                    this.m_dbh.insert(c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber());
                }
            }
            this.m_dbh.commit();
        } catch (SQLException e) {
            throw new GoogleContactsException(e.getMessage(), e);
        }
    } else {
        this.m_logger.warning("GoogleContacts proxy datahandler not initialized. Could not insert google contacts...");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Query(com.google.gdata.client.Query) SQLException(java.sql.SQLException) IOException(java.io.IOException) ContactFeed(com.google.gdata.data.contacts.ContactFeed) URL(java.net.URL) ICaller(de.janrufmonitor.framework.ICaller) ICallerList(de.janrufmonitor.framework.ICallerList) ServiceException(com.google.gdata.util.ServiceException) ContactsService(com.google.gdata.client.contacts.ContactsService) ContactEntry(com.google.gdata.data.contacts.ContactEntry) List(java.util.List) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 18 with IMultiPhoneCaller

use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.

the class PDFCallerListFilter method doExport.

public boolean doExport() {
    Document document = new Document(PageSize.A4.rotate());
    document.addCreationDate();
    document.addCreator("jAnrufmonitor");
    try {
        PdfWriter.getInstance(document, new FileOutputStream(this.m_filename));
        document.open();
        // get renderers
        List renderer = new ArrayList();
        String renderer_config = this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty("ui.jface.application.editor.Editor", "renderer");
        if (renderer_config != null && renderer_config.length() > 0) {
            StringTokenizer s = new StringTokenizer(renderer_config, ",");
            while (s.hasMoreTokens()) {
                renderer.add(RendererRegistry.getInstance().getRenderer(s.nextToken()));
            }
        }
        // get column width
        float totalWidth = 0;
        String[] cWidth = new String[renderer.size()];
        for (int i = 0, j = renderer.size(); i < j; i++) {
            cWidth[i] = getRuntime().getConfigManagerFactory().getConfigManager().getProperty("ui.jface.application.editor.Editor", "col_size_" + ((ITableCellRenderer) renderer.get(i)).getID());
            if (cWidth[i] != null && cWidth[i].length() > 0) {
                totalWidth += Float.parseFloat(cWidth[i]);
            }
            if (cWidth[i] != null && cWidth[i].length() == 0) {
                cWidth[i] = "0";
            }
        }
        float[] widths = new float[renderer.size()];
        for (int i = 0, j = renderer.size(); i < j; i++) {
            widths[i] = Float.parseFloat(cWidth[i]) / totalWidth;
        }
        PdfPTable table = new PdfPTable(widths);
        table.setHeaderRows(1);
        table.setWidthPercentage(100f);
        ITableCellRenderer t = null;
        PdfPCell cell = null;
        for (int i = 0, j = renderer.size(); i < j; i++) {
            t = (ITableCellRenderer) renderer.get(i);
            if (t == null) {
                this.m_logger.severe("No renderer found for ID: " + (String) renderer.get(i));
                this.m_logger.severe("Export to PDF format canceled...");
                return false;
            }
            cell = new PdfPCell(new Paragraph(t.getHeader()));
            cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
            table.addCell(cell);
        }
        ICaller c = null;
        String cellContent = null;
        Color iterateColor1 = new Color(0xDD, 0xDD, 0xDD);
        Color iterateColor2 = new Color(0xFF, 0xFF, 0xFF);
        cell = null;
        int type_col = -1, num_col = -1;
        for (int i = 0, j = this.m_callerList.size(); i < j; i++) {
            c = this.m_callerList.get(i);
            for (int k = 0, m = renderer.size(); k < m; k++) {
                t = (ITableCellRenderer) renderer.get(k);
                t.updateData(c);
                // find number and typ column for multiline callers
                if (t.getID().equalsIgnoreCase("NumberType"))
                    type_col = k;
                if (t.getID().equalsIgnoreCase("Number"))
                    num_col = k;
                cellContent = t.renderAsText();
                if (cellContent != null && cellContent.length() > 0) {
                    cell = new PdfPCell(new Phrase(cellContent));
                    cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
                    table.addCell(cell);
                } else {
                    cellContent = t.renderAsImageID();
                    if (cellContent != null && cellContent.length() > 0) {
                        if (cellContent.startsWith("db://")) {
                            InputStream in = ImageHandler.getInstance().getImageStream(c);
                            if (in != null) {
                                ByteArrayOutputStream out = new ByteArrayOutputStream();
                                Stream.copy(in, out, true);
                                in.close();
                                out.close();
                                Image pdfImage = Image.getInstance(out.toByteArray());
                                // pdfImage.scaleAbsolute(90.0f, 45.0f);
                                table.addCell(pdfImage);
                            } else {
                                table.addCell(" ");
                            }
                        } else if (new File(cellContent).exists()) {
                            Image pdfImage = Image.getInstance(cellContent);
                            table.addCell(pdfImage);
                        } else {
                            Image pdfImage = Image.getInstance(SWTImageManager.getInstance(PIMRuntime.getInstance()).getImagePath(cellContent));
                            table.addCell(pdfImage);
                        }
                    // ((Image pdfImage = Image.getInstance(cellContent);
                    // cell = new PdfPCell(pdfImage);
                    // cell.setBackgroundColor((i%2==0 ? iterateColor1 : iterateColor2));
                    // table.addCell(pdfImage);
                    } else {
                        cell = new PdfPCell(new Phrase(" "));
                        cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
                        table.addCell(cell);
                    }
                }
                // last column reached check for multiline caller
                if (k == m - 1 && (type_col > -1 || num_col > -1) && (c instanceof IMultiPhoneCaller) && (((IMultiPhoneCaller) c).getPhonenumbers().size() > 1)) {
                    List phones = ((IMultiPhoneCaller) c).getPhonenumbers();
                    IPhonenumber pn = null;
                    for (int y = 1, z = phones.size(); y < z; y++) {
                        pn = (IPhonenumber) phones.get(y);
                        for (int w = 0, x = renderer.size(); w < x; w++) {
                            t = (ITableCellRenderer) renderer.get(w);
                            if (t.getID().equalsIgnoreCase("NumberType") || t.getID().equalsIgnoreCase("Number")) {
                                if (t.getID().equalsIgnoreCase("NumberType")) {
                                    final IPhonenumber lpn = pn;
                                    final IAttributeMap lam = c.getAttributes();
                                    t.updateData(new ITreeItemCallerData() {

                                        public IAttributeMap getAttributes() {
                                            return lam;
                                        }

                                        public IPhonenumber getPhone() {
                                            return lpn;
                                        }
                                    });
                                } else
                                    t.updateData(pn);
                                cellContent = t.renderAsText();
                                if (cellContent != null && cellContent.length() > 0) {
                                    cell = new PdfPCell(new Phrase(cellContent));
                                    cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
                                    table.addCell(cell);
                                } else {
                                    cell = new PdfPCell(new Phrase(" "));
                                    cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
                                    table.addCell(cell);
                                }
                            } else {
                                cell = new PdfPCell(new Phrase(" "));
                                cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
                                table.addCell(cell);
                            }
                        }
                    }
                }
            }
        }
        document.add(table);
    } catch (DocumentException de) {
        this.m_logger.severe(de.getMessage());
        return false;
    } catch (IOException ioe) {
        this.m_logger.severe(ioe.getMessage());
        return false;
    } finally {
        document.close();
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) Document(com.lowagie.text.Document) Image(com.lowagie.text.Image) PdfPTable(com.lowagie.text.pdf.PdfPTable) DocumentException(com.lowagie.text.DocumentException) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) ITableCellRenderer(de.janrufmonitor.ui.jface.application.rendering.ITableCellRenderer) PdfPCell(com.lowagie.text.pdf.PdfPCell) InputStream(java.io.InputStream) Color(java.awt.Color) Phrase(com.lowagie.text.Phrase) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Paragraph(com.lowagie.text.Paragraph) ICaller(de.janrufmonitor.framework.ICaller) StringTokenizer(java.util.StringTokenizer) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ITreeItemCallerData(de.janrufmonitor.ui.jface.application.ITreeItemCallerData) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 19 with IMultiPhoneCaller

use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.

the class ChangeAction method run.

public void run() {
    Viewer v = this.m_app.getApplication().getViewer();
    if (v != null && v instanceof TreeViewer) {
        IStructuredSelection selection = (IStructuredSelection) v.getSelection();
        if (!selection.isEmpty()) {
            if (selection.getFirstElement() instanceof IMultiPhoneCaller) {
                ICaller caller = (IMultiPhoneCaller) selection.getFirstElement();
                ICaller newCaller = openCallerWizard(caller);
                if (newCaller != null) {
                    // ICallerList list = this.getRuntime().getCallerFactory().createCallerList(1);
                    // list.add(caller);
                    // this.m_app.getController().deleteElements(list);
                    // 
                    // list.clear();
                    // list.add(newCaller);
                    // 
                    // this.m_app.getController().addElements(list);
                    ICallerList list = this.getRuntime().getCallerFactory().createCallerList(1);
                    list.add(newCaller);
                    this.m_app.getController().updateElement(list);
                    this.m_app.updateViews(true);
                }
            } else if (selection.getFirstElement() instanceof ICaller) {
                ICaller caller = (ICaller) selection.getFirstElement();
                ICaller newCaller = openCallerWizard(caller);
                if (newCaller != null) {
                    ICallerList list = this.getRuntime().getCallerFactory().createCallerList(1);
                    list.add(caller);
                    this.m_app.getController().deleteElements(list);
                    list.clear();
                    list.add(newCaller);
                    this.m_app.getController().addElements(list);
                    this.m_app.updateViews(true);
                }
            }
        }
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) ICallerList(de.janrufmonitor.framework.ICallerList) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 20 with IMultiPhoneCaller

use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.

the class IdentifyAction method run.

public synchronized void run() {
    Viewer v = this.m_app.getApplication().getViewer();
    if (v != null && v instanceof Viewer) {
        final IStructuredSelection selection = (IStructuredSelection) v.getSelection();
        if (!selection.isEmpty()) {
            if (selection.getFirstElement() instanceof ICaller) {
                ProgressMonitorDialog pmd = new ProgressMonitorDialog(DisplayManager.getDefaultDisplay().getActiveShell());
                try {
                    IRunnableWithProgress r = new IRunnableWithProgress() {

                        public void run(IProgressMonitor progressMonitor) {
                            progressMonitor.beginTask(getI18nManager().getString(getNamespace(), "identifyprogress", "label", getLanguage()), IProgressMonitor.UNKNOWN);
                            progressMonitor.worked(1);
                            final ICaller caller = (ICaller) selection.getFirstElement();
                            List phones = new ArrayList();
                            if (caller instanceof IMultiPhoneCaller) {
                                phones.addAll(((IMultiPhoneCaller) caller).getPhonenumbers());
                            } else {
                                phones.add(caller.getPhoneNumber());
                            }
                            String ignoreCM = "";
                            IAttribute att = caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER);
                            if (att != null) {
                                ignoreCM = att.getValue();
                            }
                            List activeCMs = getAllActiveCallerManagers(getRuntime(), ignoreCM);
                            ICaller identifiedCaller = null;
                            // identification only makes sence if there are active CMs or if there are numbers
                            if (activeCMs.size() > 0 && phones.size() > 0) {
                                IPhonenumber pn = null;
                                for (int i = 0; i < phones.size(); i++) {
                                    pn = (IPhonenumber) phones.get(i);
                                    identifiedCaller = Identifier.identify(getRuntime(), pn, activeCMs);
                                    if (identifiedCaller != null)
                                        break;
                                }
                            }
                            progressMonitor.done();
                            if (identifiedCaller == null) {
                                progressMonitor.beginTask(getI18nManager().getString(getNamespace(), "failedidentify", "label", getLanguage()), IProgressMonitor.UNKNOWN);
                                PropagationFactory.getInstance().fire(new Message(Message.INFO, getNamespace(), "failedidentify", new Exception("Caller with number " + caller.getPhoneNumber() + " not identified...")));
                                return;
                            }
                            // remove repository flag and set all numbers
                            if (!(identifiedCaller instanceof IMultiPhoneCaller)) {
                                identifiedCaller = getRuntime().getCallerFactory().toMultiPhoneCaller(identifiedCaller);
                            }
                            ((IMultiPhoneCaller) identifiedCaller).setPhonenumbers(phones);
                            ((IMultiPhoneCaller) identifiedCaller).setPhoneNumber((IPhonenumber) phones.get(0));
                            IAttributeMap m = getRuntime().getCallerFactory().createAttributeMap();
                            m.addAll(caller.getAttributes());
                            m.addAll(identifiedCaller.getAttributes());
                            m.remove(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER);
                            identifiedCaller.setAttributes(m);
                            final ICaller theIdentifiedCaller = identifiedCaller;
                            new SWTExecuter() {

                                protected void execute() {
                                    if (MessageDialog.openConfirm(new Shell(DisplayManager.getDefaultDisplay()), getI18nManager().getString(getNamespace(), "identify", "label", getLanguage()), getI18nManager().getString(getNamespace(), "identify", "description", getLanguage()))) {
                                        ICaller newCaller = openCallerWizard(theIdentifiedCaller);
                                        if (newCaller != null) {
                                            ICallerList list = getRuntime().getCallerFactory().createCallerList(1);
                                            list.add(caller);
                                            m_app.getController().deleteElements(list);
                                            list.clear();
                                            list.add(newCaller);
                                            m_app.getController().addElements(list);
                                            m_app.updateViews(true);
                                        }
                                    }
                                }
                            }.start();
                        }
                    };
                    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);
                }
            }
        }
    }
}
Also used : Message(de.janrufmonitor.exception.Message) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) ArrayList(java.util.ArrayList) Viewer(org.eclipse.jface.viewers.Viewer) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) ICaller(de.janrufmonitor.framework.ICaller) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Shell(org.eclipse.swt.widgets.Shell) ICallerList(de.janrufmonitor.framework.ICallerList) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) SWTExecuter(de.janrufmonitor.ui.swt.SWTExecuter) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)32 ICaller (de.janrufmonitor.framework.ICaller)25 ICallerList (de.janrufmonitor.framework.ICallerList)25 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)25 List (java.util.List)25 ArrayList (java.util.ArrayList)20 SQLException (java.sql.SQLException)15 IAttribute (de.janrufmonitor.framework.IAttribute)14 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)8 IOException (java.io.IOException)8 UUID (de.janrufmonitor.util.uuid.UUID)6 File (java.io.File)5 Properties (java.util.Properties)5 ContactsService (com.google.gdata.client.contacts.ContactsService)4 ContactEntry (com.google.gdata.data.contacts.ContactEntry)4 ServiceException (com.google.gdata.util.ServiceException)4 MalformedURLException (java.net.MalformedURLException)4 URL (java.net.URL)4 Iterator (java.util.Iterator)4 ComFailException (com.jacob.com.ComFailException)3