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