Search in sources :

Example 56 with IAttribute

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

the class Serializer method toString.

private static String toString(IMultiPhoneCaller caller, boolean includeImage) throws SerializerException {
    StringBuffer serialized = new StringBuffer(128);
    try {
        List pns = caller.getPhonenumbers();
        if (pns.size() > 0) {
            // add type
            serialized.append("mpc");
            serialized.append(m_token);
            // add phonelist size
            serialized.append(pns.size());
            serialized.append(m_token);
            for (int i = 0, j = pns.size(); i < j; i++) {
                // add int area code
                serialized.append(encode((((IPhonenumber) pns.get(i)).getIntAreaCode().length() == 0 ? BLANK : ((IPhonenumber) pns.get(i)).getIntAreaCode())));
                serialized.append(m_token);
                // add area code
                serialized.append(encode((((IPhonenumber) pns.get(i)).getAreaCode().length() == 0 ? BLANK : ((IPhonenumber) pns.get(i)).getAreaCode())));
                serialized.append(m_token);
                // add call number
                serialized.append(encode((((IPhonenumber) pns.get(i)).getCallNumber().length() == 0 ? BLANK : ((IPhonenumber) pns.get(i)).getCallNumber())));
                serialized.append(m_token);
            }
        } else {
            // add int area code
            serialized.append(encode((caller.getPhoneNumber().getIntAreaCode().length() == 0 ? BLANK : caller.getPhoneNumber().getIntAreaCode())));
            serialized.append(m_token);
            // add area code
            serialized.append(encode((caller.getPhoneNumber().getAreaCode().length() == 0 ? BLANK : caller.getPhoneNumber().getAreaCode())));
            serialized.append(m_token);
            // add call number
            serialized.append(encode((caller.getPhoneNumber().getCallNumber().length() == 0 ? BLANK : caller.getPhoneNumber().getCallNumber())));
            serialized.append(m_token);
        }
        // add firstname
        serialized.append(encode((caller.getName().getFirstname().length() == 0 ? BLANK : caller.getName().getFirstname())));
        serialized.append(m_token);
        // add lastname
        serialized.append(encode((caller.getName().getLastname().length() == 0 ? BLANK : caller.getName().getLastname())));
        serialized.append(m_token);
        // add additional
        serialized.append(encode((caller.getName().getAdditional().length() == 0 ? BLANK : caller.getName().getAdditional())));
        serialized.append(m_token);
        // add caller UUID
        serialized.append(encode((caller.getUUID().length() == 0 ? BLANK : caller.getUUID())));
        serialized.append(m_token);
        // add attributes
        IAttributeMap al = caller.getAttributes();
        if (al.size() == 0)
            serialized.append(BLANK);
        if (includeImage && ImageHandler.getInstance().hasImage(caller)) {
            InputStream in = ImageHandler.getInstance().getImageStream(caller);
            if (in != null) {
                ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                Base64Encoder b64 = new Base64Encoder(encodedOut);
                Stream.copy(in, b64);
                b64.flush();
                b64.close();
                final String imgdata = StringUtils.replaceString(new String(encodedOut.toByteArray()), m_imglftoken, IMGLFTOKEN_ESCAPE_SYMBOL);
                encodedOut.close();
                IAttribute img = new IAttribute() {

                    public void setName(String name) {
                    }

                    public void setValue(String value) {
                    }

                    public String getName() {
                        return IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY;
                    }

                    public String getValue() {
                        return imgdata;
                    }
                };
                caller.getAttributes().add(img);
                caller.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
                // caller.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEREF);
                caller.getAttributes().remove(IJAMConst.ATTRIBUTE_NAME_IMAGEURL);
            }
        }
        Iterator i = al.iterator();
        IAttribute att = null;
        while (i.hasNext()) {
            att = (IAttribute) i.next();
            serialized.append(encode(att.getName()));
            serialized.append(EQUAL);
            serialized.append(encodeAttributeValue(att.getValue()));
            serialized.append(m_atoken);
        }
    } catch (Throwable t) {
        throw new SerializerException(t.getMessage());
    }
    return serialized.toString();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IAttribute(de.janrufmonitor.framework.IAttribute) Iterator(java.util.Iterator) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) List(java.util.List) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 57 with IAttribute

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

the class Serializer method toCaller.

/**
 * Deserializes a byte array stream into an caller object.
 *
 * @param caller caller as a byte array representation.
 * @param runtime runtime to used
 * @return caller object
 * @throws SerializerException
 */
public static ICaller toCaller(byte[] caller, IRuntime runtime) throws SerializerException {
    if (runtime == null)
        throw new SerializerException("Runtime object is not set but required.");
    String callerString = new String(caller);
    // tokenize the input
    StringTokenizer st = new StringTokenizer(callerString, m_token);
    if (st.countTokens() < 8) {
        throw new SerializerException("Caller format is invalid: " + callerString);
    }
    try {
        // Check for new mpc format
        String test = st.nextToken().trim();
        List phones = null;
        if (test.equalsIgnoreCase("mpc")) {
            int phone_count = Integer.parseInt(st.nextToken().trim());
            phones = new ArrayList(phone_count);
            for (int i = 0; i < phone_count; i++) {
                // build number
                IPhonenumber pn = runtime.getCallerFactory().createPhonenumber(// token 1
                decode(st.nextToken().trim()), decode(st.nextToken().trim()), decode(st.nextToken().trim()));
                if (pn.getCallNumber().length() == 0) {
                    pn.setClired(true);
                }
                phones.add(pn);
            }
        } else {
            phones = new ArrayList(1);
            // build number
            IPhonenumber pn = runtime.getCallerFactory().createPhonenumber(// token 1
            test, // token 2
            decode(st.nextToken().trim()), // token 3
            decode(st.nextToken().trim()));
            if (pn.getCallNumber().length() == 0) {
                pn.setClired(true);
            }
            phones.add(pn);
        }
        // build name
        IName name = runtime.getCallerFactory().createName(// token 4
        decode(st.nextToken().trim()), // token 5
        decode(st.nextToken().trim()), // token 6
        decode(st.nextToken().trim()));
        // token 7
        String UUID = decode(st.nextToken().trim());
        // build attributes
        String attString = decode(st.nextToken().trim());
        IAttributeMap attList = runtime.getCallerFactory().createAttributeMap();
        if (attString.length() > 0) {
            StringTokenizer ast = new StringTokenizer(attString, m_atoken);
            String attrib = null;
            while (ast.hasMoreTokens()) {
                attrib = ast.nextToken().trim();
                if (attrib.indexOf(EQUAL) > -1) {
                    IAttribute att = runtime.getCallerFactory().createAttribute(decode(attrib.substring(0, attrib.indexOf(EQUAL))), decodeAttributeValue(attrib.substring(attrib.indexOf(EQUAL) + EQUAL.length())));
                    attList.add(att);
                }
            }
            // check for imagebinary
            if (attList.contains(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY)) {
                IAttribute imgbinary = attList.get(IJAMConst.ATTRIBUTE_NAME_IMAGEBINARY);
                if (imgbinary != null) {
                    imgbinary.setValue(StringUtils.replaceString(imgbinary.getValue(), IMGLFTOKEN_ESCAPE_SYMBOL, m_imglftoken));
                }
            }
        }
        // create an IMultiPhoneCaller object
        if (test.equalsIgnoreCase("mpc") || phones.size() > 1) {
            return runtime.getCallerFactory().createCaller(UUID, name, phones, attList);
        }
        // create an ICaller object
        return runtime.getCallerFactory().createCaller(UUID, name, (IPhonenumber) phones.get(0), attList);
    } catch (Exception e) {
        throw new SerializerException(e.toString());
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) ArrayList(java.util.ArrayList) List(java.util.List) IName(de.janrufmonitor.framework.IName) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 58 with IAttribute

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

the class Serializer method toString.

private static String toString(ICall call, boolean includeImage) throws SerializerException {
    StringBuffer serialized = new StringBuffer(256);
    try {
        // add caller
        serialized.append(toString(call.getCaller(), includeImage));
        serialized.append(m_ctoken);
        // add call data
        serialized.append(encode((call.getMSN().getMSN().length() == 0 ? BLANK : call.getMSN().getMSN())));
        serialized.append(m_token);
        serialized.append(encode((call.getMSN().getAdditional().length() == 0 ? BLANK : call.getMSN().getAdditional())));
        serialized.append(m_token);
        serialized.append(encode((call.getCIP().getCIP().length() == 0 ? BLANK : call.getCIP().getCIP())));
        serialized.append(m_token);
        serialized.append(encode((call.getCIP().getAdditional().length() == 0 ? BLANK : call.getCIP().getAdditional())));
        serialized.append(m_token);
        serialized.append(encode((call.getUUID().length() == 0 ? BLANK : call.getUUID())));
        serialized.append(m_token);
        serialized.append(encode(Long.toString(call.getDate().getTime())));
        serialized.append(m_token);
        IAttributeMap al = call.getAttributes();
        if (al.size() == 0)
            serialized.append(BLANK);
        Iterator i = al.iterator();
        IAttribute att = null;
        while (i.hasNext()) {
            att = (IAttribute) i.next();
            serialized.append(encode(att.getName()));
            serialized.append(EQUAL);
            serialized.append(encodeAttributeValue(att.getValue()));
            serialized.append(m_atoken);
        }
    } catch (Throwable t) {
        throw new SerializerException(t.getMessage());
    }
    return serialized.toString();
}
Also used : Iterator(java.util.Iterator) IAttribute(de.janrufmonitor.framework.IAttribute) IAttributeMap(de.janrufmonitor.framework.IAttributeMap)

Example 59 with IAttribute

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

the class ImageHandler method getImage.

public File getImage(ICaller caller) {
    IImageProvider provider = null;
    File image = null;
    IAttribute cm_att = caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER);
    if (cm_att != null && cm_att.getValue().length() > 0) {
        provider = (IImageProvider) this.m_providers.get(cm_att.getValue());
        if (provider != null) {
            if (this.m_logger.isLoggable(Level.INFO))
                this.m_logger.info("Using IImageProvider <" + provider.getID() + "> for getting image file.");
            image = provider.getImage(caller);
            boolean isForceImage = Boolean.parseBoolean(System.getProperty(IJAMConst.SYSTEM_UI_FORCEIMAGE, "false"));
            if (isForceImage) {
                if (image != null && image.exists())
                    return image;
            } else {
                return ((image != null && image.exists()) ? image : null);
            }
        }
    }
    return this.getImageDefault(caller);
}
Also used : IImageProvider(de.janrufmonitor.util.io.IImageProvider) IAttribute(de.janrufmonitor.framework.IAttribute) File(java.io.File)

Example 60 with IAttribute

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

the class GoogleContactsProxy method createContacts.

public synchronized void createContacts(ICallerList cl) throws GoogleContactsException {
    this.m_current = 0;
    this.m_total = 0;
    this.m_total = cl.size();
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    if (cl.size() == 0)
        return;
    ContactsService cs = login();
    ICaller caller = null;
    ContactEntry entry = null;
    for (int i = 0, j = cl.size(); i < j; i++) {
        this.m_current++;
        caller = cl.get(i);
        IAttributeMap m = caller.getAttributes();
        try {
            entry = new ContactEntry();
            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.addStructuredPostalAddress(createPostalAddress(m));
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
                String emails = m.get(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue();
                Email email = new Email();
                email.setAddress(emails);
                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;
            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 postUrl = new URL("https://www.google.com/m8/feeds/contacts/" + getLoginUser() + "/full");
            entry = (ContactEntry) cs.insert(postUrl, 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"));
                    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;
                        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.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) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (IOException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (ServiceException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), 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) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) List(java.util.List) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) IOException(java.io.IOException) ExtendedProperty(com.google.gdata.data.extensions.ExtendedProperty) FileInputStream(java.io.FileInputStream) ICaller(de.janrufmonitor.framework.ICaller) 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)

Aggregations

IAttribute (de.janrufmonitor.framework.IAttribute)111 ICaller (de.janrufmonitor.framework.ICaller)44 IAttributeMap (de.janrufmonitor.framework.IAttributeMap)40 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)39 List (java.util.List)34 ICallerList (de.janrufmonitor.framework.ICallerList)31 ArrayList (java.util.ArrayList)29 Iterator (java.util.Iterator)25 ICall (de.janrufmonitor.framework.ICall)19 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)15 SQLException (java.sql.SQLException)15 File (java.io.File)14 AttributeFilter (de.janrufmonitor.repository.filter.AttributeFilter)12 IOException (java.io.IOException)12 Message (de.janrufmonitor.exception.Message)11 ComFailException (com.jacob.com.ComFailException)10 Date (java.util.Date)10 IMsn (de.janrufmonitor.framework.IMsn)9 Dispatch (com.jacob.com.Dispatch)8 UUID (de.janrufmonitor.util.uuid.UUID)8