/src/mpv5/utils/print/FilePrintJob.java
Java | 148 lines | 108 code | 20 blank | 20 comment | 22 complexity | 15c59584f91066b0936fdda907ada678 MD5 | raw file
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 */ 17package mpv5.utils.print; 18 19import java.io.File; 20import java.util.ArrayList; 21import java.util.List; 22import mpv5.db.common.DatabaseObject; 23import mpv5.utils.files.FileDirectoryHandler; 24import mpv5.utils.files.FileReaderWriter; 25import mpv5.utils.files.TextDatFile; 26import mpv5.utils.xml.XMLWriter; 27 28/** 29 * 30 * 31 */ 32public class FilePrintJob { 33 34 private DatabaseObject dbobj; 35 private ArrayList<DatabaseObject> dbobjarr; 36 37 public FilePrintJob(ArrayList<DatabaseObject> dataOwner) { 38 this.dbobjarr = dataOwner; 39 } 40 41 public FilePrintJob(DatabaseObject dataOwner) { 42 this.dbobj = dataOwner; 43 } 44 45 public void toCSV() { 46 TextDatFile f = new TextDatFile(); 47 String[] head = null; 48 String[][] dat = null; 49 String name = null; 50 51 if (dbobj != null) { 52 dbobjarr = new ArrayList<DatabaseObject>(); 53 dbobjarr.add(dbobj); 54 name = dbobj.__getCname(); 55 } 56 57 if (dbobjarr != null) { 58 59 List<String[]> data = dbobjarr.get(0).getValues(); 60 if (name == null) { 61 name = dbobjarr.get(0).getDbIdentity(); 62 } 63 head = new String[data.size()]; 64 for (int i = 0; i < data.size(); i++) { 65 head[i] = data.get(i)[0]; 66 } 67 dat = new String[dbobjarr.size()][data.size()]; 68 for (int i = 0; i < dbobjarr.size(); i++) { 69 data = dbobjarr.get(0).getValues(); 70 for (int k = 0; k < data.size(); k++) { 71 dat[i][k] = data.get(k)[1]; 72 } 73 } 74 } 75 76 f.setHeader(head); 77 f.setData(dat); 78 mpv5.YabsViewProxy.instance().showFilesaveDialogFor(f.createFile(name)); 79 } 80 81 public void toVCF() { 82 String name = "contacts.vcf"; 83 ArrayList<String[]> specs = new ArrayList<String[]>(); 84 specs.add(new String[]{"N:", "cname;prename;;title"}); 85 specs.add(new String[]{"FN:", "cnumber"}); 86 specs.add(new String[]{"ORG:", "companyid"}); 87 specs.add(new String[]{"EMAIL;type=WORK:", "mailaddress"}); 88 specs.add(new String[]{"URL;type=WORK:", "website"}); 89 specs.add(new String[]{"TEL;type=HOME:", "mainphone"}); 90 specs.add(new String[]{"TEL;type=FAX:", "fax"}); 91 specs.add(new String[]{"TEL;type=WORK:", "workphone"}); 92 specs.add(new String[]{"TEL;type=WORK:", "mobilephone"}); 93 specs.add(new String[]{"ADR;type=WORK:", ";;street;city;;zip;"}); 94 specs.add(new String[]{"NOTE;QUOTED-PRINTABLE:", "notes"}); 95 96 97 if (dbobj != null) { 98 dbobjarr = new ArrayList<DatabaseObject>(); 99 dbobjarr.add(dbobj); 100 name = dbobj.__getCname(); 101 } 102 File f = FileDirectoryHandler.getTempFile(name, "vcf"); 103 FileReaderWriter rw = new FileReaderWriter(f); 104 105 if (dbobjarr != null) { 106 for (int i = 0; i < dbobjarr.size(); i++) { 107 DatabaseObject databaseObject = dbobjarr.get(i); 108 List<String[]> data = databaseObject.getValues(); 109 if (name == null) { 110 name = databaseObject.getDbIdentity(); 111 } 112 for (int h = 0; h < data.size(); h++) { 113 for (int idx = 0; idx < specs.size(); idx++) { 114 String[] elem = specs.get(idx); 115 if (elem[1].contains(data.get(h)[0].toLowerCase())) { 116 elem[1] = elem[1].replace(data.get(h)[0].toLowerCase(), data.get(h)[1]).replaceAll("[\\r\\n]", " "); 117 } 118 } 119 } 120 String vCard = "BEGIN:VCARD\n"; 121 for (int j = 0; j < specs.size(); j++) { 122 String[] strings = specs.get(j); 123 vCard += strings[0] + strings[1] + "\n"; 124 } 125 vCard += "VERSION:3.0\n" + "END:VCARD\n"; 126 rw.write(vCard); 127 } 128 } 129 130 mpv5.YabsViewProxy.instance().showFilesaveDialogFor(f); 131 } 132 133 public void toXML() { 134 XMLWriter xmlw = new XMLWriter(); 135 xmlw.newDoc(true); 136 String name = dbobj.__getCname(); 137 138 if (dbobj != null) { 139 dbobjarr = new ArrayList<DatabaseObject>(); 140 dbobjarr.add(dbobj); 141 142 xmlw.add(dbobjarr); 143 } 144 145 146 mpv5.YabsViewProxy.instance().showFilesaveDialogFor(xmlw.createFile(name)); 147 } 148}