PageRenderTime 47ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/print/FilePrintJob.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 148 lines | 108 code | 20 blank | 20 comment | 22 complexity | 15c59584f91066b0936fdda907ada678 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.print;
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import mpv5.db.common.DatabaseObject;
  22. import mpv5.utils.files.FileDirectoryHandler;
  23. import mpv5.utils.files.FileReaderWriter;
  24. import mpv5.utils.files.TextDatFile;
  25. import mpv5.utils.xml.XMLWriter;
  26. /**
  27. *
  28. *
  29. */
  30. public class FilePrintJob {
  31. private DatabaseObject dbobj;
  32. private ArrayList<DatabaseObject> dbobjarr;
  33. public FilePrintJob(ArrayList<DatabaseObject> dataOwner) {
  34. this.dbobjarr = dataOwner;
  35. }
  36. public FilePrintJob(DatabaseObject dataOwner) {
  37. this.dbobj = dataOwner;
  38. }
  39. public void toCSV() {
  40. TextDatFile f = new TextDatFile();
  41. String[] head = null;
  42. String[][] dat = null;
  43. String name = null;
  44. if (dbobj != null) {
  45. dbobjarr = new ArrayList<DatabaseObject>();
  46. dbobjarr.add(dbobj);
  47. name = dbobj.__getCname();
  48. }
  49. if (dbobjarr != null) {
  50. List<String[]> data = dbobjarr.get(0).getValues();
  51. if (name == null) {
  52. name = dbobjarr.get(0).getDbIdentity();
  53. }
  54. head = new String[data.size()];
  55. for (int i = 0; i < data.size(); i++) {
  56. head[i] = data.get(i)[0];
  57. }
  58. dat = new String[dbobjarr.size()][data.size()];
  59. for (int i = 0; i < dbobjarr.size(); i++) {
  60. data = dbobjarr.get(0).getValues();
  61. for (int k = 0; k < data.size(); k++) {
  62. dat[i][k] = data.get(k)[1];
  63. }
  64. }
  65. }
  66. f.setHeader(head);
  67. f.setData(dat);
  68. mpv5.YabsViewProxy.instance().showFilesaveDialogFor(f.createFile(name));
  69. }
  70. public void toVCF() {
  71. String name = "contacts.vcf";
  72. ArrayList<String[]> specs = new ArrayList<String[]>();
  73. specs.add(new String[]{"N:", "cname;prename;;title"});
  74. specs.add(new String[]{"FN:", "cnumber"});
  75. specs.add(new String[]{"ORG:", "companyid"});
  76. specs.add(new String[]{"EMAIL;type=WORK:", "mailaddress"});
  77. specs.add(new String[]{"URL;type=WORK:", "website"});
  78. specs.add(new String[]{"TEL;type=HOME:", "mainphone"});
  79. specs.add(new String[]{"TEL;type=FAX:", "fax"});
  80. specs.add(new String[]{"TEL;type=WORK:", "workphone"});
  81. specs.add(new String[]{"TEL;type=WORK:", "mobilephone"});
  82. specs.add(new String[]{"ADR;type=WORK:", ";;street;city;;zip;"});
  83. specs.add(new String[]{"NOTE;QUOTED-PRINTABLE:", "notes"});
  84. if (dbobj != null) {
  85. dbobjarr = new ArrayList<DatabaseObject>();
  86. dbobjarr.add(dbobj);
  87. name = dbobj.__getCname();
  88. }
  89. File f = FileDirectoryHandler.getTempFile(name, "vcf");
  90. FileReaderWriter rw = new FileReaderWriter(f);
  91. if (dbobjarr != null) {
  92. for (int i = 0; i < dbobjarr.size(); i++) {
  93. DatabaseObject databaseObject = dbobjarr.get(i);
  94. List<String[]> data = databaseObject.getValues();
  95. if (name == null) {
  96. name = databaseObject.getDbIdentity();
  97. }
  98. for (int h = 0; h < data.size(); h++) {
  99. for (int idx = 0; idx < specs.size(); idx++) {
  100. String[] elem = specs.get(idx);
  101. if (elem[1].contains(data.get(h)[0].toLowerCase())) {
  102. elem[1] = elem[1].replace(data.get(h)[0].toLowerCase(), data.get(h)[1]).replaceAll("[\\r\\n]", " ");
  103. }
  104. }
  105. }
  106. String vCard = "BEGIN:VCARD\n";
  107. for (int j = 0; j < specs.size(); j++) {
  108. String[] strings = specs.get(j);
  109. vCard += strings[0] + strings[1] + "\n";
  110. }
  111. vCard += "VERSION:3.0\n" + "END:VCARD\n";
  112. rw.write(vCard);
  113. }
  114. }
  115. mpv5.YabsViewProxy.instance().showFilesaveDialogFor(f);
  116. }
  117. public void toXML() {
  118. XMLWriter xmlw = new XMLWriter();
  119. xmlw.newDoc(true);
  120. String name = dbobj.__getCname();
  121. if (dbobj != null) {
  122. dbobjarr = new ArrayList<DatabaseObject>();
  123. dbobjarr.add(dbobj);
  124. xmlw.add(dbobjarr);
  125. }
  126. mpv5.YabsViewProxy.instance().showFilesaveDialogFor(xmlw.createFile(name));
  127. }
  128. }