PageRenderTime 85ms CodeModel.GetById 62ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/db/objects/Template.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 282 lines | 166 code | 33 blank | 83 comment | 7 complexity | 94e5b4e3caf1ef767d27904d4d874fee 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.db.objects;
 18
 19import java.io.File;
 20import java.util.HashMap;
 21import javax.swing.JComponent;
 22import javax.swing.JFileChooser;
 23import javax.swing.table.TableModel;
 24import mpv5.db.common.Context;
 25import mpv5.db.common.DatabaseObject;
 26import mpv5.db.common.QueryCriteria2;
 27import mpv5.db.common.QueryHandler;
 28import mpv5.db.common.QueryParameter;
 29import mpv5.logging.Log;
 30import mpv5.utils.export.Exportable;
 31import mpv5.utils.files.FileDirectoryHandler;
 32import mpv5.utils.images.MPIcon;
 33import mpv5.utils.text.RandomText;
 34
 35/**
 36 * This class represents Yabs Templates
 37 *
 38 */
 39public class Template extends DatabaseObject {
 40
 41    /**
 42     * Indicates no printer association
 43     */
 44    public static final String PRINTER_UNDEFINED = "printer_undefined";
 45    private static final long serialVersionUID = 1L;
 46    private String description = "";
 47    private String filename = "";
 48    private String printer = "";
 49    private int intsize;
 50    private String mimetype;
 51    private File file;
 52    private String format = DEFAULT_FORMAT;
 53    private Exportable exFile;
 54    private boolean isupdateenabled = false;
 55    private String pathtofile = "";
 56    private long lastmodified = 0;
 57    /**
 58     * Represents the default column order
 59     */
 60    public static String DEFAULT_FORMAT = "1,2,3,4,5,6,7,8,9,10,11";
 61
 62    public Template() {
 63        setContext(Context.getTemplate());
 64    }
 65
 66    @Override
 67    public JComponent getView() {
 68        return null;
 69    }
 70
 71    /**
 72     * @return the description
 73     */
 74    public String __getDescription() {
 75        return description;
 76    }
 77
 78    /**
 79     * @param description the description to set
 80     */
 81    public void setDescription(String description) {
 82        this.description = description;
 83    }
 84
 85    /**
 86     * @return the mimetype
 87     */
 88    public String __getMimetype() {
 89        return mimetype;
 90    }
 91
 92    /**
 93     * @param mimetype the mimetype to set
 94     */
 95    public void setMimetype(String mimetype) {
 96        this.mimetype = mimetype;
 97    }
 98
 99    /**
100     * @return the intsize
101     */
102    public int __getIntsize() {
103        return intsize;
104    }
105
106    /**
107     * @param intsize the intsize to set
108     */
109    public void setIntsize(int intsize) {
110        this.intsize = intsize;
111    }
112
113    /**
114     * @return the filename
115     */
116    public String __getFilename() {
117        return filename;
118    }
119
120    /**
121     * @param filename the filename to set
122     */
123    public void setFilename(String filename) {
124        this.filename = filename;
125    }
126
127    /**
128     * @return the format
129     */
130    public String __getFormat() {
131        return format;
132    }
133
134    /**
135     * @param format the format to set
136     */
137    public void setFormat(String format) {
138        this.format = format;
139    }
140
141    /**
142     * @return the printer
143     */
144    public String __getPrinter() {
145        return printer;
146    }
147
148    /**
149     * @param printer the printer to set
150     */
151    public void setPrinter(String printer) {
152        this.printer = printer;
153    }
154
155    public boolean __getisupdateenabled() {
156        return isupdateenabled;
157    }
158
159    public void setIsupdateenabled(boolean isupdateenabled) {
160        this.isupdateenabled = isupdateenabled;
161    }
162
163    public long __getLastmodified() {
164        return lastmodified;
165    }
166
167    public void setLastmodified(long lastmodified) {
168        this.lastmodified = lastmodified;
169    }
170
171    public String __getPathtofile() {
172        return pathtofile;
173    }
174
175    public void setPathtofile(String pathtofile) {
176        this.pathtofile = pathtofile;
177    }
178    MPIcon icon;
179
180    @Override
181    public mpv5.utils.images.MPIcon getIcon() {
182        if (icon == null) {
183            try {
184                Log.Debug(this, "Determining Icon for " + __getCname());
185                icon = new MPIcon(MPIcon.DIRECTORY_DEFAULT_ICONS + __getCname().substring(__getCname().lastIndexOf(".") + 1, __getCname().length()) + ".png");
186                return icon;
187            } catch (Exception e) {
188                Log.Debug(this, "Icon file not existing in " + MPIcon.DIRECTORY_DEFAULT_ICONS);
189                try {
190                    JFileChooser chooser = new JFileChooser();
191                    icon = new MPIcon(chooser.getIcon(new File(filename)));
192                    return icon;
193                } catch (Exception ez) {
194                    Log.Debug(this, ez);
195                    icon = new MPIcon(MPIcon.DIRECTORY_DEFAULT_ICONS + "folder_tar.png");
196                    return icon;
197                }
198            }
199        } else {
200            return icon;
201        }
202    }
203
204    /**
205     * Fetches the physical file from db
206     *
207     * @return
208     * @throws Exception
209     */
210    public synchronized File getFile() throws Exception {
211        if (file == null) {
212            try {
213                file = QueryHandler.instanceOf().clone(Context.getFiles()).
214                        retrieveFile(filename,
215                        new File(FileDirectoryHandler.getTempDir() + "~" + RandomText.getText() + "_" + getCname()));
216                file.deleteOnExit();
217            } catch (Exception e) {
218                delete();
219                Log.Debug(e);
220                throw e;
221            }
222        }
223        return file;
224    }
225
226    @Override
227    public boolean delete() {
228
229        QueryCriteria2 c0 = new QueryCriteria2();
230        c0.and(new QueryParameter(Context.getTemplatesToUsers(), "templatesids", ids, QueryParameter.EQUALS));
231        QueryHandler.instanceOf().clone(Context.getTemplatesToUsers()).delete(c0);
232
233        QueryCriteria2 c = new QueryCriteria2();
234        c.and(new QueryParameter(Context.getFiles(), "cname", filename, QueryParameter.EQUALS));
235        if (QueryHandler.instanceOf().clone(Context.getFiles()).delete(c)) {
236            return super.delete();
237        } else {
238            Log.Debug(this, "Cant delete file " + filename);
239            return super.delete();
240        }
241    }
242
243    /**
244     * @return the exFile
245     */
246    public Exportable getExFile() {
247        return exFile;
248    }
249
250    /**
251     * Injects a table resource
252     *
253     * @param key
254     * @param model
255     */
256    public void injectTable(String key, TableModel model) {
257        getTables().put(key, model);
258    }
259    private HashMap<String, TableModel> tables = new HashMap<String, TableModel>();
260
261    /**
262     * @return the tables
263     */
264    public HashMap<String, TableModel> getTables() {
265        return tables;
266    }
267
268    public void defineExFile(Exportable ex) {
269        this.exFile = ex;
270    }
271    private HashMap<String, Object> data = new HashMap<String, Object>();
272
273    public void injectData(HashMap<String, Object> map) {
274        data.putAll(map);
275    }
276     /**
277     * @return the tables
278     */
279    public HashMap<String, Object> getData() {
280        return data;
281    }
282}