use of de.janrufmonitor.util.formatter.Formatter 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.util.formatter.Formatter in project janrufmonitor by tbrandt77.
the class ConsoleJournal method execute.
public void execute() {
isExecuting = true;
System.out.println("Retrieving journal ...");
System.out.println("");
ICallManager m = PIMRuntime.getInstance().getCallManagerFactory().getDefaultCallManager();
ICallList l = PIMRuntime.getInstance().getCallFactory().createCallList();
if (m != null && m.isActive() && m.isSupported(IReadCallRepository.class)) {
l = ((IReadCallRepository) m).getCalls((IFilter) null);
}
System.out.println("Sorting journal for date ...");
System.out.println("");
l.sort(CallListComparator.ORDER_DATE, false);
System.out.println("Date | Caller | Number | MSN | Service (CIP)");
System.out.println("--------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < l.size(); i++) {
Formatter f = Formatter.getInstance(PIMRuntime.getInstance());
StringBuffer callLine = new StringBuffer();
callLine.append(trim(f.parse(IJAMConst.GLOBAL_VARIABLE_CALLTIME, l.get(i).getDate()), 20));
callLine.append(" | ");
callLine.append(trim(StringUtils.replaceString(f.parse(IJAMConst.GLOBAL_VARIABLE_CALLERNAME, l.get(i).getCaller()), IJAMConst.CRLF, " "), 25));
callLine.append(" | ");
callLine.append(trim(f.parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, l.get(i).getCaller()), 20));
callLine.append(" | ");
callLine.append(trim(l.get(i).getMSN().getMSN() + (l.get(i).getMSN().getAdditional().equalsIgnoreCase("") ? "" : " (" + l.get(i).getMSN().getAdditional() + ")"), 20));
callLine.append(" | ");
callLine.append(trim(l.get(i).getCIP().getAdditional(), 18));
System.out.println(callLine.toString());
}
System.out.println("");
isExecuting = false;
}
use of de.janrufmonitor.util.formatter.Formatter in project janrufmonitor by tbrandt77.
the class ExternalApplicationLauncher method receivedValidRule.
public void receivedValidRule(ICall aCall) {
String msn = aCall.getMSN().getMSN();
String command = this.m_configuration.getProperty(msn + this.CONFIG_APPLICATION, "");
if (command.length() == 0) {
this.m_logger.info("No command configured for MSN " + msn + ". Taking default command.");
command = this.m_configuration.getProperty("default" + this.CONFIG_APPLICATION, "");
} else {
this.m_logger.info("Taking command configured for MSN " + msn + ".");
}
if (command.length() > 0) {
try {
command = PathResolver.getInstance(this.getRuntime()).resolve(command);
Formatter f = Formatter.getInstance(this.getRuntime());
StringTokenizer st = new StringTokenizer(command);
String[] env = new String[st.countTokens()];
int i = 0;
while (st.hasMoreTokens()) {
env[i] = st.nextToken();
env[i] = f.parse(env[i], aCall);
i++;
}
command = f.parse(command, aCall);
// 2009/04/17: added path resolution to command if %imagepath% or else is used
if (command.indexOf("%") > -1)
command = PathResolver.getInstance(getRuntime()).resolve(command);
if (command.toLowerCase().indexOf(".bat") > 0 || command.toLowerCase().indexOf(".cmd") > 0)
command = "cmd /c start " + command;
if (this.m_logger.isLoggable(Level.INFO)) {
this.m_logger.info("Launching command: \"" + command + "\" for call " + aCall.toString());
String text = this.getRuntime().getI18nManagerFactory().getI18nManager().getString(getNamespace(), "executed", "label", this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_LANGUAGE));
PropagationFactory.getInstance().fire(new Message(Message.INFO, this.getRuntime().getI18nManagerFactory().getI18nManager().getString(getNamespace(), "title", "label", this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty(IJAMConst.GLOBAL_NAMESPACE, IJAMConst.GLOBAL_LANGUAGE)), new Exception(StringUtils.replaceString(text, "{%1}", command))), "Tray");
}
Runtime.getRuntime().exec(command);
} catch (IOException e) {
this.m_logger.severe(e.getMessage());
}
} else {
this.m_logger.info("No command configured for MSN " + msn + " and no default command available.");
}
}
use of de.janrufmonitor.util.formatter.Formatter in project janrufmonitor by tbrandt77.
the class CallerStatsAction method getStatisticItems.
public List getStatisticItems() {
final List items = new ArrayList();
ProgressMonitorDialog pmd = new ProgressMonitorDialog(DisplayManager.getDefaultDisplay().getActiveShell());
try {
IRunnableWithProgress r = new IRunnableWithProgress() {
public void run(IProgressMonitor progressMonitor) {
isCalculating = true;
progressMonitor.beginTask(getI18nManager().getString(getNamespace(), "calculating", "label", getLanguage()), IProgressMonitor.UNKNOWN);
progressMonitor.worked(1);
final Properties stat = new Properties();
if (m_cl != null) {
ICaller caller = null;
Formatter formatter = Formatter.getInstance(getRuntime());
ICall c = null;
for (int i = 0; i < m_cl.size(); i++) {
c = m_cl.get(i);
if (isOutgoingCall(c)) {
continue;
}
caller = c.getCaller();
String displayName = formatter.parse("%a:ln%, %a:fn% (%a:add%)", caller);
if (displayName.trim().length() == 0)
displayName = formatter.parse("%a:city%", caller);
String number = formatter.parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, caller);
String key = number;
// remove CRLF
displayName = StringUtils.replaceString(displayName, IJAMConst.CRLF, " ");
if (displayName.length() > 32)
displayName = displayName.substring(0, 29) + "...";
String value = stat.getProperty(key, "");
if (value.length() == 0) {
stat.setProperty(key.trim(), "1");
stat.setProperty(key + ".name", displayName);
stat.setProperty(key + ".number", number);
stat.setProperty(key + ".accepted", (isAcceptedCall(m_cl.get(i)) ? "1" : "0"));
stat.setProperty(key + ".away", (isAwayCall(m_cl.get(i)) ? "1" : "0"));
} else {
int val = new Integer(value).intValue();
val++;
stat.setProperty(key.trim(), new Integer(val).toString());
if (isAcceptedCall(m_cl.get(i))) {
val = new Integer(stat.getProperty(key + ".accepted")).intValue();
val++;
stat.setProperty(key + ".accepted", new Integer(val).toString());
}
if (isAwayCall(m_cl.get(i))) {
val = new Integer(stat.getProperty(key + ".away")).intValue();
val++;
stat.setProperty(key + ".away", new Integer(val).toString());
}
m_maxcount = Math.max(m_maxcount, val);
}
}
}
progressMonitor.worked(1);
progressMonitor.setTaskName(getI18nManager().getString(getNamespace(), "createstatistic", "label", getLanguage()));
Iterator iter = stat.keySet().iterator();
String key = null;
String count = null;
while (iter.hasNext()) {
key = (String) iter.next();
if (!(key.endsWith(".name") || key.endsWith(".number") || key.endsWith(".accepted") || key.endsWith(".away"))) {
count = stat.getProperty(key, "");
items.add(new String[] { stat.getProperty(key + ".name"), stat.getProperty(key + ".number"), stat.getProperty(key + ".accepted"), stat.getProperty(key + ".away"), count });
}
}
stat.clear();
Collections.sort(items, new StatisticComparator());
progressMonitor.done();
isCalculating = false;
}
};
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);
isCalculating = false;
} catch (InvocationTargetException e) {
m_logger.log(Level.SEVERE, e.getMessage(), e);
isCalculating = false;
}
while (isCalculating) try {
Thread.sleep(1250);
} catch (InterruptedException e) {
}
return items;
}
Aggregations