use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.
the class MacAddressBookProxy method updateProxyDatabase.
private void updateProxyDatabase(ICallerList callers) {
ICaller c = null;
for (int i = 0, j = callers.size(); i < j; i++) {
c = callers.get(i);
if (c instanceof IMultiPhoneCaller) {
try {
// clean up cache
if (this.m_dbh != null) {
this.m_dbh.delete(c.getUUID());
// this.m_dbh.deleteAttributes(c.getUUID());
}
} catch (SQLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
for (int i = 0, j = callers.size(); i < j; i++) {
c = callers.get(i);
if (c instanceof IMultiPhoneCaller) {
List pns = ((IMultiPhoneCaller) c).getPhonenumbers();
IPhonenumber pn = null;
for (int k = 0, l = pns.size(); k < l; k++) {
pn = (IPhonenumber) pns.get(k);
if (this.m_dbh != null) {
try {
this.m_dbh.insert(c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber());
} catch (SQLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
} else {
if (this.m_dbh != null) {
try {
this.m_dbh.delete(c.getUUID());
this.m_dbh.deleteAttributes(c.getUUID());
this.m_dbh.insert(c.getUUID(), c.getPhoneNumber().getIntAreaCode(), c.getPhoneNumber().getAreaCode(), c.getPhoneNumber().getCallNumber());
} catch (SQLException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
}
}
use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.
the class VcfFileCallerExporter method doExport.
public boolean doExport() {
File db = new File(m_filename);
try {
StringBuffer vcf = new StringBuffer();
Formatter f = Formatter.getInstance(PIMRuntime.getInstance());
String number_pattern = "+%intareacode% (%areacode%) %callnumber%";
ICaller c = null;
for (int i = 0, j = this.m_callerList.size(); i < j; i++) {
c = this.m_callerList.get(i);
vcf.append("BEGIN:VCARD");
vcf.append(CRLF);
vcf.append("VERSION:3.0");
vcf.append(CRLF);
vcf.append("N;CHARSET=ISO-8859-1:");
vcf.append(c.getName().getLastname());
vcf.append(";");
vcf.append(c.getName().getFirstname());
vcf.append(";;");
vcf.append(CRLF);
vcf.append("FN;CHARSET=ISO-8859-1:");
vcf.append(c.getName().getFirstname());
vcf.append(" ");
vcf.append(c.getName().getLastname());
vcf.append(CRLF);
if (c.getName().getAdditional().trim().length() > 0)
vcf.append("ORG;CHARSET=ISO-8859-1:");
vcf.append(c.getName().getAdditional());
vcf.append(CRLF);
vcf.append("SORT-STRING:");
vcf.append(c.getName().getLastname());
vcf.append(CRLF);
vcf.append("CLASS:PRIVATE");
vcf.append(CRLF);
if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
vcf.append("EMAIL;TYPE=home:");
vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue());
vcf.append(CRLF);
}
vcf.append("ADR;TYPE=home;CHARSET=ISO-8859-1:;;");
vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_STREET));
vcf.append(" ");
vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_STREET_NO));
vcf.append(";");
vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_CITY));
vcf.append(";;");
vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE));
vcf.append(";");
vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_COUNTRY));
vcf.append(CRLF);
if (c instanceof IMultiPhoneCaller) {
List pns = ((IMultiPhoneCaller) c).getPhonenumbers();
IPhonenumber p = null;
for (int k = 0, l = pns.size(); k < l; k++) {
p = (IPhonenumber) pns.get(k);
if (p.isClired())
continue;
String numbertype = this.getPhoneType(p, c);
if (PhonenumberAnalyzer.getInstance(PIMRuntime.getInstance()).isInternal(p)) {
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
vcf.append("TEL;TYPE=home:");
vcf.append(p.getCallNumber());
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
vcf.append("TEL;TYPE=fax,home:");
vcf.append(p.getCallNumber());
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
vcf.append("TEL;TYPE=cell:");
vcf.append(p.getCallNumber());
vcf.append(CRLF);
}
} else {
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
vcf.append("TEL;TYPE=home:");
vcf.append(f.parse(number_pattern, p));
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
vcf.append("TEL;TYPE=fax,home:");
vcf.append(f.parse(number_pattern, p));
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
vcf.append("TEL;TYPE=cell:");
vcf.append(f.parse(number_pattern, p));
vcf.append(CRLF);
}
}
}
} else {
String numbertype = this.getPhoneType(c.getPhoneNumber(), c);
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
vcf.append("TEL;TYPE=home:");
vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
vcf.append("TEL;TYPE=fax,home:");
vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
vcf.append(CRLF);
}
if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
vcf.append("TEL;TYPE=cell:");
vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
vcf.append(CRLF);
}
}
if (ImageHandler.getInstance().hasImage(c)) {
vcf.append("PHOTO;ENCODING=b;TYPE=JPEG:");
InputStream fim = ImageHandler.getInstance().getImageStream(c);
if (c != null) {
ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
// finalize lines with '\n ' instead of '\n'
Base64Encoder b64 = new Base64Encoder(encodedOut, " ".getBytes());
Stream.copy(fim, b64);
b64.flush();
b64.close();
String imagedata = new String(encodedOut.toByteArray());
int size = imagedata.length();
vcf.append(imagedata.substring(0, 43));
int z = 43;
while (z < imagedata.length()) {
vcf.append(imagedata.substring(z, Math.min((z + 69), size)));
z += 69;
}
vcf.append(CRLF);
}
} else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
// add embedded photo as base 64 encoded object
String file = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
File img = new File(PathResolver.getInstance().resolve(file));
if (img.exists()) {
vcf.append("PHOTO;ENCODING=b;TYPE=JPEG:");
FileInputStream fim = new FileInputStream(img);
ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
// finalize lines with '\n ' instead of '\n'
Base64Encoder b64 = new Base64Encoder(encodedOut, " ".getBytes());
Stream.copy(fim, b64);
b64.flush();
b64.close();
String imagedata = new String(encodedOut.toByteArray());
int size = imagedata.length();
vcf.append(imagedata.substring(0, 43));
int z = 43;
while (z < imagedata.length()) {
vcf.append(imagedata.substring(z, Math.min((z + 69), size)));
z += 69;
}
vcf.append(CRLF);
}
}
if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_GEO_LAT) && c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_GEO_LNG)) {
// add geo tagging
vcf.append("GEO:");
vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT).getValue());
vcf.append(";");
vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG).getValue());
vcf.append(CRLF);
}
vcf.append("PRODID:-//jAnrufmonitor//www.janrufmonitor.de//Version 5.0");
vcf.append(CRLF);
vcf.append("UID:JAM-UID-");
vcf.append(c.getUUID());
vcf.append(CRLF);
vcf.append("REV:");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
vcf.append(sdf.format(new Date()));
vcf.append(CRLF);
vcf.append("END:VCARD");
vcf.append(CRLF);
vcf.append(CRLF);
}
FileOutputStream fos = new FileOutputStream(db);
ByteArrayInputStream bin = new ByteArrayInputStream(vcf.toString().getBytes());
Stream.copy(bin, fos, true);
} catch (FileNotFoundException ex) {
this.m_logger.severe("File not found: " + m_filename);
return false;
} catch (IOException ex) {
this.m_logger.severe("IOException on file " + m_filename);
return false;
}
return true;
}
use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.
the class FritzBoxCallerExporter method toXml.
private void toXml(ICaller c, StringBuffer s) {
s.append("<contact>");
s.append(IJAMConst.CRLF);
s.append("<category></category>");
s.append(IJAMConst.CRLF);
s.append("<person>");
s.append(IJAMConst.CRLF);
s.append("<realName>");
try {
if (c.getName().getFullname().trim().length() == 0) {
s.append(StringUtils.replaceString(c.getName().getAdditional(), "&", "&"));
} else
s.append(StringUtils.replaceString(c.getName().getFullname(), "&", "&"));
} catch (Exception e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
s.append("</realName>");
s.append(IJAMConst.CRLF);
s.append("<imageURL></imageURL>");
s.append(IJAMConst.CRLF);
s.append("</person>");
s.append(IJAMConst.CRLF);
s.append("<numbers>");
s.append(IJAMConst.CRLF);
if (c instanceof IMultiPhoneCaller) {
List numbers = ((IMultiPhoneCaller) c).getPhonenumbers();
IPhonenumber pn = null;
String type = null;
for (int i = 0; i < numbers.size(); i++) {
pn = (IPhonenumber) numbers.get(i);
if (!PhonenumberAnalyzer.getInstance(PIMRuntime.getInstance()).isInternal(pn) && !pn.isClired()) {
type = "home";
if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + pn.getTelephoneNumber())) {
type = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + pn.getTelephoneNumber()).getValue();
if (type.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE))
type = "home";
if (type.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE))
type = "mobile";
if (type.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE))
type = "work_fax";
}
s.append("<number type=\"" + type + "\">");
s.append((pn.getIntAreaCode().equalsIgnoreCase(m_intArea) ? "0" + pn.getTelephoneNumber() : "00" + pn.getIntAreaCode() + pn.getTelephoneNumber()));
s.append("</number>");
s.append(IJAMConst.CRLF);
}
}
} else {
s.append("<number type=\"home\">");
s.append((c.getPhoneNumber().getIntAreaCode().equalsIgnoreCase(m_intArea) ? "0" + c.getPhoneNumber().getTelephoneNumber() : "00" + c.getPhoneNumber().getIntAreaCode() + c.getPhoneNumber().getTelephoneNumber()));
s.append("</number>");
s.append(IJAMConst.CRLF);
}
s.append("</numbers>");
s.append(IJAMConst.CRLF);
s.append("<services>");
s.append(IJAMConst.CRLF);
if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
s.append("<email classifier=\"private\">");
s.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue());
s.append("</email>");
s.append(IJAMConst.CRLF);
}
s.append("</services>");
s.append(IJAMConst.CRLF);
s.append("</contact>");
s.append(IJAMConst.CRLF);
}
use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.
the class AbstractMultiPhoneCallerDatabaseHandler method getCaller.
@SuppressWarnings("resource")
public ICaller getCaller(IPhonenumber pn) throws SQLException {
if (!isConnected())
try {
this.connect();
} catch (ClassNotFoundException e) {
throw new SQLException(e.getMessage());
}
PreparedStatement ps = this.getStatement("SELECT_CALLER_PHONE");
String p = pn.getTelephoneNumber();
ICaller c = null;
ResultSet rs = null;
// check for internal telephone system numbers
if (this.isInternalNumber(pn)) {
ps.setString(1, p);
rs = ps.executeQuery();
while (rs.next()) {
this.m_logger.info("Found exact match of call number: " + p);
try {
c = Serializer.toCaller(rs.getString("content").getBytes(), this.getRuntime());
if (c instanceof IMultiPhoneCaller) {
IPhonenumber cp = null;
for (int i = 0, j = ((IMultiPhoneCaller) c).getPhonenumbers().size(); i < j; i++) {
cp = (IPhonenumber) ((IMultiPhoneCaller) c).getPhonenumbers().get(i);
if (cp.getTelephoneNumber().startsWith(p)) {
this.m_logger.info("Found correct phonenumber match: " + p + " = " + cp.getTelephoneNumber());
((IMultiPhoneCaller) c).getPhonenumbers().clear();
c.setPhoneNumber(cp);
return c;
}
}
}
} catch (SerializerException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
int maxLength = this.maxInternalNumberLength();
// p must be an internal number but has no entry in this caller manager
if (p.length() < maxLength)
return null;
// check for international call
if (p.startsWith(this.getPrefix())) {
this.m_logger.info("Found international call number: " + p);
ICaller internationaCaller = null;
ICallerManager cmg = this.getRuntime().getCallerManagerFactory().getDefaultCallerManager();
if (cmg != null && cmg.isActive() && cmg.isSupported(IIdentifyCallerRepository.class)) {
try {
internationaCaller = ((IIdentifyCallerRepository) cmg).getCaller(pn);
} catch (CallerNotFoundException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
if (internationaCaller != null)
pn = internationaCaller.getPhoneNumber();
ps.setString(1, pn.getTelephoneNumber());
rs = ps.executeQuery();
while (rs.next()) {
try {
c = Serializer.toCaller(rs.getString("content").getBytes(), this.getRuntime());
if (c instanceof IMultiPhoneCaller) {
IPhonenumber cp = null;
for (int i = 0, j = ((IMultiPhoneCaller) c).getPhonenumbers().size(); i < j; i++) {
cp = (IPhonenumber) ((IMultiPhoneCaller) c).getPhonenumbers().get(i);
if (pn.getTelephoneNumber().equalsIgnoreCase(cp.getTelephoneNumber()) && pn.getIntAreaCode().equalsIgnoreCase(cp.getIntAreaCode())) {
this.m_logger.info("Found correct phonenumber match: " + p + " = " + cp.getTelephoneNumber());
((IMultiPhoneCaller) c).getPhonenumbers().clear();
c.setPhoneNumber(cp);
// found international number
return c;
}
}
} else if (c instanceof ICaller) {
if (pn.getTelephoneNumber().equalsIgnoreCase(c.getPhoneNumber().getTelephoneNumber()) && pn.getIntAreaCode().equalsIgnoreCase(c.getPhoneNumber().getIntAreaCode())) {
// found international number
return c;
}
}
} catch (SerializerException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
// check for extension
if (pn.getIntAreaCode() == null || pn.getIntAreaCode().length() == 0) {
pn.setIntAreaCode(this.getDefaultIntAreaCode());
}
PreparedStatement ps2 = this.getStatement("SELECT_PHONE_REF_COUNT");
if (ps2 == null)
return null;
boolean multiprocess = false;
for (int i = 0; i < p.length() - maxLength; i++) {
ps2.setString(1, p.substring(0, p.length() - i) + "%");
rs = ps2.executeQuery();
while (rs.next()) {
multiprocess = rs.getInt(1) > 1;
}
if (!multiprocess) {
ps.setString(1, p.substring(0, p.length() - i) + "%");
rs = ps.executeQuery();
while (rs.next()) {
try {
c = Serializer.toCaller(rs.getString("content").getBytes(), this.getRuntime());
ICaller nc = this.process(c, pn, p);
if (nc != null)
return nc;
} catch (SerializerException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
} else {
// added 2008/04/18: processing of multiple callers is possible
PreparedStatement ps3 = this.getStatement("SELECT_PHONE_REF");
ps3.setString(1, p.substring(0, p.length() - i) + "%");
rs = ps3.executeQuery();
List uuids = new ArrayList(2);
while (rs.next()) {
uuids.add(rs.getString(1));
}
// process all UUIDs
String uuid = null;
ps3 = this.getStatement("SELECT_CALLER_PHONE2");
for (int j = 0; j < uuids.size(); j++) {
uuid = (String) uuids.get(j);
ps3.setString(1, uuid);
rs = ps3.executeQuery();
while (rs.next()) {
try {
c = Serializer.toCaller(rs.getString("content").getBytes(), this.getRuntime());
ICaller nc = this.process(c, pn, p);
if (nc != null)
return nc;
} catch (SerializerException e) {
this.m_logger.log(Level.SEVERE, e.getMessage(), e);
}
}
}
}
}
return null;
}
use of de.janrufmonitor.framework.IMultiPhoneCaller in project janrufmonitor by tbrandt77.
the class AbstractMultiPhoneCallerDatabaseHandler method process.
private ICaller process(ICaller c, IPhonenumber pn, String p) {
if (c instanceof IMultiPhoneCaller) {
this.m_logger.info("Found multi phone caller.");
IPhonenumber cp = null;
for (int x = 0, j = ((IMultiPhoneCaller) c).getPhonenumbers().size(); x < j; x++) {
cp = (IPhonenumber) ((IMultiPhoneCaller) c).getPhonenumbers().get(x);
if (p.startsWith(cp.getTelephoneNumber()) && pn.getIntAreaCode().equalsIgnoreCase(cp.getIntAreaCode())) {
// found extension phone
String extension = p.substring(cp.getTelephoneNumber().length(), p.length());
this.m_logger.info("Found call extension -" + extension + " for call number: " + p);
c.setUUID(new UUID().toString());
((IMultiPhoneCaller) c).getPhonenumbers().clear();
cp.setCallNumber(cp.getCallNumber() + extension);
c.setPhoneNumber(cp);
// add attributes
IAttribute att = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_EXTENSION, extension);
c.setAttribute(att);
att = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CENTRAL_NUMBER_OF_EXTENSION, p.substring(0, p.length() - extension.length()));
c.setAttribute(att);
return c;
}
}
} else if (c instanceof ICaller) {
if (p.startsWith(c.getPhoneNumber().getTelephoneNumber()) && pn.getIntAreaCode().equalsIgnoreCase(c.getPhoneNumber().getIntAreaCode())) {
// found extension phone
String extension = p.substring(c.getPhoneNumber().getTelephoneNumber().length(), p.length());
this.m_logger.info("Found call extension -" + extension + " for call number: " + p);
c.setUUID(new UUID().toString());
c.getPhoneNumber().setTelephoneNumber(p);
c.getPhoneNumber().setCallNumber(c.getPhoneNumber().getCallNumber() + extension);
// add attributes
IAttribute att = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_EXTENSION, extension);
c.setAttribute(att);
att = getRuntime().getCallerFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CENTRAL_NUMBER_OF_EXTENSION, p.substring(0, p.length() - extension.length()));
c.setAttribute(att);
return c;
}
}
return null;
}
Aggregations