use of com.google.gdata.data.extensions.ExtendedProperty in project janrufmonitor by tbrandt77.
the class GoogleContactsProxy method parse.
private synchronized ICaller parse(ContactsService cs, ContactEntry e) {
if (e == null)
return null;
if (e.getPhoneNumbers().size() == 0)
return null;
String uuid = parseUUID(e);
IAttributeMap m = getRuntime().getCallerFactory().createAttributeMap();
m.addAll(parseName(e));
if (e.hasStructuredPostalAddresses()) {
StructuredPostalAddress pa = (StructuredPostalAddress) e.getStructuredPostalAddresses().get(0);
m.addAll(parseAddress(pa));
}
// 2012/10/20: added email support
if (e.hasEmailAddresses()) {
List emaillist = e.getEmailAddresses();
if (emaillist.size() > 0) {
Email email = (Email) emaillist.get(0);
m.add(parseEmail(email));
}
}
List gphones = e.getPhoneNumbers();
List pl = new ArrayList(gphones.size());
PhoneNumber p = null;
IPhonenumber pn = null;
for (int i = 0, j = gphones.size(); i < j; i++) {
p = (PhoneNumber) gphones.get(i);
pn = PhonenumberAnalyzer.getInstance(getRuntime()).toIdentifiedPhonenumber(p.getPhoneNumber());
if (pn != null) {
pl.add(pn);
m.add(parseNumberType(pn, p));
}
}
if (pl.size() == 0)
return null;
if (e.getExtendedProperties() != null) {
ExtendedProperty exp = null;
for (int i = 0, j = e.getExtendedProperties().size(); i < j; i++) {
exp = (ExtendedProperty) e.getExtendedProperties().get(i);
if (exp.getName().equalsIgnoreCase(IJAMConst.ATTRIBUTE_NAME_GEO_ACC) && exp.getValue().length() > 0) {
m.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_ACC, exp.getValue()));
}
if (exp.getName().equalsIgnoreCase(IJAMConst.ATTRIBUTE_NAME_GEO_LNG) && exp.getValue().length() > 0) {
m.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG, exp.getValue()));
}
if (exp.getName().equalsIgnoreCase(IJAMConst.ATTRIBUTE_NAME_GEO_LAT) && exp.getValue().length() > 0) {
m.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT, exp.getValue()));
}
}
}
m.add(getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CALLERMANAGER, GoogleContactsCallerManager.ID));
m.add(getRuntime().getCallerFactory().createAttribute("entryUrl", e.getSelfLink().getHref()));
IAttribute a = parseCategory(e);
if (a != null)
m.add(a);
try {
a = parseImage(cs, e);
if (a != null) {
m.add(a);
}
} catch (GoogleContactsException ex) {
this.m_logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return getRuntime().getCallerFactory().createCaller(uuid, null, pl, m);
}
use of com.google.gdata.data.extensions.ExtendedProperty 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);
}
}
}
use of com.google.gdata.data.extensions.ExtendedProperty 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);
}
}
Aggregations