PageRenderTime 114ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/enoa/handler/TemplateHandler.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 609 lines | 353 code | 78 blank | 178 comment | 38 complexity | 42a2b1b011b19294b4b811c2559b8f20 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. package enoa.handler;
  2. import java.awt.Color;
  3. import java.io.File;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import javax.swing.JComponent;
  7. import mpv5.db.common.Context;
  8. import mpv5.db.common.DatabaseObject;
  9. import mpv5.db.common.NodataFoundException;
  10. import mpv5.db.common.QueryHandler;
  11. import mpv5.db.common.ReturnValue;
  12. import mpv5.db.common.SaveString;
  13. import mpv5.db.common.Templateable;
  14. import mpv5.db.objects.*;
  15. import mpv5.globals.LocalSettings;
  16. import mpv5.globals.Messages;
  17. import mpv5.handler.MPEnum;
  18. import mpv5.logging.Log;
  19. import mpv5.ui.panels.DataPanel;
  20. import mpv5.usermanagement.MPSecurityManager;
  21. import mpv5.utils.export.ODTFile;
  22. import mpv5.utils.export.PDFFile;
  23. import static mpv5.globals.Constants.*;
  24. /**
  25. * This class provides {@link Template} loading and caching functionality, thread-safe
  26. */
  27. public class TemplateHandler {
  28. /**
  29. * Return true if the Template for the currently logged in user, with the given type, and matching the targets group is loaded
  30. * @param group
  31. * @param type
  32. * @return
  33. */
  34. public static synchronized boolean isLoaded(Long group, int type) {
  35. String key = null;
  36. if (group != null) {
  37. key = mpv5.db.objects.User.getCurrentUser() + "@" + type + "@" + group;
  38. } else {
  39. key = mpv5.db.objects.User.getCurrentUser() + "@" + type + "@" + 1;
  40. }
  41. if (TEMPLATE_CACHE.containsKey(key)) {
  42. return true;
  43. } else {
  44. return false;
  45. }
  46. }
  47. /**
  48. * Return true if the Template for the currently logged in user, with the given type, and matching the targets group is loaded
  49. * @param target
  50. * @param type
  51. * @return
  52. */
  53. public static synchronized boolean isLoaded(Templateable target) {
  54. String key = null;
  55. if (target != null) {
  56. key = mpv5.db.objects.User.getCurrentUser() + "@" + target.templateType() + "@" + target.templateGroupIds();
  57. } else {
  58. key = mpv5.db.objects.User.getCurrentUser() + "@" + target.templateType() + "@" + 1;
  59. }
  60. if (TEMPLATE_CACHE.containsKey(key)) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. /**
  67. * Loads a template including neccesary files, target's group
  68. * @param target
  69. * @param typ
  70. * @return
  71. */
  72. public static synchronized Template loadTemplate(Templateable target) {
  73. int groupsids = 0;
  74. if (target != null) {
  75. groupsids = target.templateGroupIds();
  76. } else {
  77. groupsids = 1;
  78. }
  79. return loadTemplate(groupsids, target.templateType());
  80. }
  81. /**
  82. *
  83. * @param t
  84. * @return
  85. */
  86. public static synchronized Template loadTemplate(Template t) {
  87. return loadTemplate(t.__getGroupsids(), Integer.valueOf(t.__getMimetype()));
  88. }
  89. /**
  90. *
  91. * @param group
  92. * @param typ
  93. * @return
  94. */
  95. public static synchronized Template loadTemplate(int group, int typ) {
  96. return loadTemplate(Long.valueOf(group), typ);
  97. }
  98. /**
  99. * Loads a template including necessary files
  100. * @param target
  101. * @param groupsids
  102. * @param typ
  103. * @return
  104. */
  105. public static synchronized Template loadTemplate(long groupsids, int typ) {
  106. ReturnValue data;
  107. if (LocalSettings.getBooleanProperty(LocalSettings.OFFICE_USE)) {
  108. Integer type = new Integer(typ);
  109. if (groupsids < 0) {
  110. groupsids = 1;
  111. }
  112. String key = mpv5.db.objects.User.getCurrentUser() + "@" + type + "@" + groupsids;
  113. if (TEMPLATE_CACHE.containsKey(key)) {
  114. return TEMPLATE_CACHE.get(key);
  115. } else {
  116. if (type != null) {
  117. data = TemplateHandler.getDefinedTemplatesFor(groupsids, type);
  118. Template preloadedTemplate = null;
  119. if (data.hasData()) {
  120. try {
  121. preloadedTemplate = (Template) DatabaseObject.getObject(Context.getTemplate(), Integer.valueOf(data.getData()[data.getData().length - 1][0].toString()));
  122. Log.Debug(TemplateHandler.class, preloadedTemplate.getFile());
  123. if (preloadedTemplate.getFile().getName().endsWith("odt")) {
  124. if (LocalSettings.getBooleanProperty(LocalSettings.OFFICE_USE)) {
  125. preloadedTemplate.defineExFile(new ODTFile(preloadedTemplate.getFile().getPath()));
  126. Log.Debug(Template.class, "Loaded template: " + preloadedTemplate);
  127. mpv5.YabsViewProxy.instance().addMessage(preloadedTemplate + Messages.LOADED.toString(), Color.GREEN);
  128. } else {
  129. // Popup.notice(Messages.NOT_POSSIBLE + "\n" + Messages.OOCONNERROR);
  130. return null;
  131. }
  132. } else {
  133. preloadedTemplate.defineExFile(new PDFFile(preloadedTemplate.getFile().getPath()));
  134. }
  135. TEMPLATE_CACHE.put(key, preloadedTemplate);
  136. } catch (Exception ex) {
  137. Log.Debug(Template.class, "Invalid template: " + data.getData()[data.getData().length - 1][0].toString());
  138. try {
  139. Log.Debug(TemplateHandler.class, "Not removing invalid template " + preloadedTemplate);
  140. //preloadedTemplate.delete();
  141. } catch (Exception e) {
  142. Log.Debug(e);
  143. }
  144. return null;
  145. }
  146. } else {
  147. try {
  148. if (!(TEMPLATE_MISSING_NOTIFICATIONS.containsKey(type.toString()) && TEMPLATE_MISSING_NOTIFICATIONS.get(type.toString()).equals(Group.getObject(Context.getGroup(), (int) groupsids)))) {
  149. mpv5.YabsViewProxy.instance().addMessage(Messages.OO_NO_TEMPLATE + ": " + TemplateHandler.getName(type) + " [" + mpv5.db.objects.User.getCurrentUser() + "] [" + Group.getObject(Context.getGroup(), (int) groupsids) + "]", Color.YELLOW);
  150. Log.Debug(Template.class, "No template found for type: " + type + " for user: " + mpv5.db.objects.User.getCurrentUser() + " in GROUP " + Group.getObject(Context.getGroup(), (int) groupsids));
  151. TEMPLATE_MISSING_NOTIFICATIONS.put(type.toString(), (Group) Group.getObject(Context.getGroup(), (int) groupsids));
  152. }
  153. } catch (NodataFoundException nodataFoundException) {
  154. Log.Debug(Template.class, nodataFoundException.getMessage());
  155. }
  156. }
  157. return preloadedTemplate;
  158. } else {
  159. return null;
  160. }
  161. }
  162. } else {
  163. return null;
  164. }
  165. }
  166. private static ReturnValue getDefinedTemplatesFor(long groupsids, Integer type) {
  167. ReturnValue data = QueryHandler.getConnection().freeQuery(
  168. "SELECT templatesids FROM templatestousers LEFT OUTER JOIN templates AS templates0 ON "
  169. + "templates0.ids = templatestousers.templatesids WHERE templatestousers.usersids="
  170. + mpv5.db.objects.User.getCurrentUser().__getIDS()
  171. + " AND "
  172. + "templates0.mimetype='" + type
  173. + "' AND templatestousers.IDS>0 "
  174. + "AND templates0.groupsids = " + groupsids, MPSecurityManager.VIEW, null);
  175. if (!data.hasData()) {
  176. data = QueryHandler.getConnection().freeQuery(
  177. "SELECT templatesids FROM templatestousers LEFT OUTER JOIN templates AS templates0 ON "
  178. + "templates0.ids = templatestousers.templatesids WHERE templatestousers.usersids="
  179. + mpv5.db.objects.User.getCurrentUser().__getIDS()
  180. + " AND "
  181. + "templates0.mimetype='" + type
  182. + "' AND templatestousers.IDS>0 "
  183. + "AND templates0.groupsids = 1", MPSecurityManager.VIEW, null);
  184. }
  185. Log.Debug(TemplateHandler.class, "gefundene Daten: " + data.hasData());
  186. return data;
  187. }
  188. /**
  189. * An enum over the available template types and their String representation
  190. * @return
  191. */
  192. public static MPEnum[] getTypes() {
  193. MPEnum[] types = new MPEnum[15];
  194. types[0] = new MPEnum() {
  195. public Integer getId() {
  196. return TYPE_BILL;
  197. }
  198. public String getName() {
  199. return Messages.TYPE_BILL.toString();
  200. }
  201. };
  202. types[1] = new MPEnum() {
  203. public Integer getId() {
  204. return TYPE_OFFER;
  205. }
  206. public String getName() {
  207. return Messages.TYPE_OFFER.toString();
  208. }
  209. };
  210. types[2] = new MPEnum() {
  211. public Integer getId() {
  212. return TYPE_ORDER;
  213. }
  214. public String getName() {
  215. return Messages.TYPE_ORDER.toString();
  216. }
  217. };
  218. types[3] = new MPEnum() {
  219. public Integer getId() {
  220. return TYPE_CONTACT;
  221. }
  222. public String getName() {
  223. return Messages.TYPE_CONTACT.toString();
  224. }
  225. };
  226. types[4] = new MPEnum() {
  227. public Integer getId() {
  228. return TYPE_DELIVERY_NOTE;
  229. }
  230. public String getName() {
  231. return Messages.TYPE_DELIVERY.toString();
  232. }
  233. };
  234. types[5] = new MPEnum() {
  235. public Integer getId() {
  236. return TYPE_ORDER_CONFIRMATION;
  237. }
  238. public String getName() {
  239. return Messages.TYPE_CONFIRMATION.toString();
  240. }
  241. };
  242. types[6] = new MPEnum() {
  243. public Integer getId() {
  244. return TYPE_PRODUCT;
  245. }
  246. public String getName() {
  247. return Messages.TYPE_PRODUCT.toString();
  248. }
  249. };
  250. types[7] = new MPEnum() {
  251. public Integer getId() {
  252. return TYPE_SERVICE;
  253. }
  254. public String getName() {
  255. return Messages.TYPE_SERVICE.toString();
  256. }
  257. };
  258. types[8] = new MPEnum() {
  259. public Integer getId() {
  260. return TYPE_REMINDER;
  261. }
  262. public String getName() {
  263. return Messages.TYPE_REMINDER.toString();
  264. }
  265. };
  266. types[9] = new MPEnum() {
  267. public Integer getId() {
  268. return TYPE_JOURNAL;
  269. }
  270. public String getName() {
  271. return Messages.TYPE_JOURNAL.toString();
  272. }
  273. };
  274. types[10] = new MPEnum() {
  275. public Integer getId() {
  276. return TYPE_PRODUCT_ORDER;
  277. }
  278. public String getName() {
  279. return Messages.TYPE_PRODUCT_ORDER.toString();
  280. }
  281. };
  282. types[11] = new MPEnum() {
  283. public Integer getId() {
  284. return TYPE_CONTACT;
  285. }
  286. public String getName() {
  287. return Messages.TYPE_CONTRACT.toString();
  288. }
  289. };
  290. types[12] = new MPEnum() {
  291. public Integer getId() {
  292. return TYPE_CONVERSATION;
  293. }
  294. public String getName() {
  295. return Messages.TYPE_CONVERSATION.toString();
  296. }
  297. };
  298. types[13] = new MPEnum() {
  299. public Integer getId() {
  300. return TYPE_MASSPRINT;
  301. }
  302. public String getName() {
  303. return Messages.TYPE_MASSPRINT.toString();
  304. }
  305. };
  306. types[14] = new MPEnum() {
  307. public Integer getId() {
  308. return TYPE_ACTIVITY;
  309. }
  310. public String getName() {
  311. return Messages.TYPE_ACTIVITY.toString();
  312. }
  313. };
  314. return types;
  315. }
  316. // /**
  317. // * (P)reload the template files
  318. // * @deprecated performance..
  319. // */
  320. // public static synchronized void cacheTemplates() {
  321. // TEMPLATE_CACHE.clear();
  322. // final List<Group> groups = new ArrayList<Group>();
  323. // try {
  324. // ArrayList<DatabaseObject> tlist = Group.getObjects(Context.getGroup(), true);
  325. // for (int i = 0; i < tlist.size(); i++) {
  326. // DatabaseObject databaseObject = tlist.get(i);
  327. // groups.add((Group) databaseObject);
  328. // }
  329. // } catch (NodataFoundException ex) {
  330. // }
  331. //
  332. // List<Templateable> targets = new ArrayList<Templateable>();
  333. // List<Integer> typs = new ArrayList<Integer>();
  334. // Item it1 = new Item();
  335. // it1.setInttype(Item.TYPE_BILL);
  336. // targets.add(it1);
  337. // typs.add(TYPE_BILL);
  338. //
  339. // Item it2 = new Item();
  340. // it2.setInttype(Item.TYPE_OFFER);
  341. // targets.add(it2);
  342. // typs.add(TYPE_OFFER);
  343. //
  344. // Item it3 = new Item();
  345. // it3.setInttype(Item.TYPE_ORDER);
  346. // targets.add(it3);
  347. // typs.add(TYPE_ORDER);
  348. //
  349. // Item it4 = new Item();
  350. // it4.setInttype(TYPE_DELIVERY_NOTE);
  351. // targets.add(it4);
  352. // typs.add(TYPE_DELIVERY_NOTE);
  353. //
  354. // Product it5 = new Product();
  355. // it5.setInttype(Product.TYPE_PRODUCT);
  356. // targets.add(it5);
  357. // typs.add(TYPE_PRODUCT);
  358. //
  359. // Product it6 = new Product();
  360. // it6.setInttype(Product.TYPE_SERVICE);
  361. // targets.add(it6);
  362. // typs.add(TYPE_SERVICE);
  363. //
  364. // Reminder it7 = new Reminder();
  365. // targets.add(it7);
  366. // typs.add(TYPE_REMINDER);
  367. //
  368. // Contact it8 = new Contact();
  369. // targets.add(it8);
  370. // typs.add(TYPE_CONTACT);
  371. //
  372. // targets.add(null);
  373. // typs.add(TYPE_JOURNAL);
  374. //
  375. // for (int i = 0; i < targets.size(); i++) {
  376. //// final Templateable databaseObject = targets.get(i);
  377. // final int type = typs.get(i);
  378. // Runnable runnable = new Runnable() {
  379. //
  380. // public void run() {
  381. // for (int j = 0; j < groups.size(); j++) {
  382. // Group group = groups.get(j);
  383. //// if (databaseObject != null && group != null) {
  384. //// ((DatabaseObject)databaseObject).setGroupsids(group.__getIDS());
  385. //// }
  386. // loadTemplate(group.__getIDS(), type);
  387. // }
  388. // }
  389. // };
  390. // new Thread(runnable).start();
  391. // }
  392. // }
  393. /**
  394. * The cache of the templates
  395. */
  396. public static HashMap<String, Template> TEMPLATE_CACHE = new HashMap<String, Template>();
  397. public static HashMap<String, Group> TEMPLATE_MISSING_NOTIFICATIONS = new HashMap<String, Group>();
  398. /**
  399. * Load a template (if not already done) and enable the given button after loading.
  400. * @param button
  401. * @param dataOwner
  402. */
  403. public static void loadTemplateFor(final JComponent button, final Templateable dataOwner) {
  404. loadTemplate(dataOwner.templateGroupIds(), dataOwner.templateType());
  405. }
  406. /**
  407. * Load a template for a specific GROUP rather than the dataOwners group (if not already done) and enable the given button after loading.
  408. * @param button
  409. * @param typ
  410. * @param groupsids
  411. */
  412. public static void loadTemplateFor(final JComponent button, final long groupsids, final int typ) {
  413. button.setEnabled(false);
  414. Runnable runnable = new Runnable() {
  415. @Override
  416. public void run() {
  417. loadTemplate(groupsids, typ);
  418. button.setEnabled(isLoaded(groupsids, typ));
  419. }
  420. };
  421. new Thread(runnable).start();
  422. }
  423. /**
  424. * Gets the String representation of the given template type
  425. * @param type
  426. * @return
  427. */
  428. public static String getName(int type) {
  429. MPEnum[] e = getTypes();
  430. for (int i = 0; i < e.length; i++) {
  431. MPEnum mPEnum = e[i];
  432. if (mPEnum.getId().intValue() == type) {
  433. return mPEnum.getName();
  434. }
  435. }
  436. return "<undefined> [" + type + "]";
  437. }
  438. /**
  439. * Load a template (if not already done) and enable the given button after loading.
  440. * @param jComponent
  441. * @param dataOwner
  442. * @param TYPE
  443. */
  444. public static void loadTemplateFor(final JComponent[] jComponent, final Templateable dataOwner) {
  445. for (int i = 0; i < jComponent.length; i++) {
  446. JComponent jComponent1 = jComponent[i];
  447. jComponent1.setEnabled(false);
  448. }
  449. Runnable runnable = new Runnable() {
  450. public void run() {
  451. loadTemplate(dataOwner);
  452. for (int i = 0; i < jComponent.length; i++) {
  453. JComponent jComponent1 = jComponent[i];
  454. jComponent1.setEnabled(isLoaded(dataOwner));
  455. }
  456. }
  457. };
  458. new Thread(runnable).start();
  459. }
  460. /**
  461. * Load a template (if not already done) and enable the given button after loading.
  462. * @param jComponent
  463. * @param typ
  464. * @param groupsids
  465. */
  466. public static void loadTemplateFor(final JComponent[] jComponent, final long groupsids, final int typ) {
  467. for (int i = 0; i < jComponent.length; i++) {
  468. JComponent jComponent1 = jComponent[i];
  469. jComponent1.setEnabled(false);
  470. }
  471. Runnable runnable = new Runnable() {
  472. @Override
  473. public void run() {
  474. loadTemplate(groupsids, typ);
  475. for (int i = 0; i < jComponent.length; i++) {
  476. JComponent jComponent1 = jComponent[i];
  477. jComponent1.setEnabled(isLoaded(groupsids, typ));
  478. }
  479. }
  480. };
  481. new Thread(runnable).start();
  482. }
  483. /**
  484. * Imports a template file
  485. * @param file
  486. * @return
  487. */
  488. public static boolean importTemplate(File file) {
  489. Template t = new Template();
  490. return QueryHandler.instanceOf().clone(Context.getFiles(), (DataPanel) null).insertFile(file, t, new SaveString(file.getName(), true));
  491. // User object = mpv5.db.objects.User.getCurrentUser();
  492. //
  493. // QueryCriteria d = new QueryCriteria();
  494. // d.add("cname", dataOwner.__getIDS() + "@" + object.__getIDS() + "@" + mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  495. // QueryHandler.instanceOf().clone(Context.getTemplatesToUsers()).delete(d);
  496. //
  497. // QueryData c = new QueryData();
  498. // c.add("usersids", object.__getIDS());
  499. // c.add("templatesids", dataOwner.__getIDS());
  500. // c.add("groupsids", mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  501. // c.add("cname", dataOwner.__getIDS() + "@" + object.__getIDS() + "@" + mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  502. // QueryHandler.instanceOf().clone(Context.getTemplatesToUsers()).insert(c, null);
  503. }
  504. /**
  505. * Exports all template Objects for give template-Type
  506. * @param Type - the templatetype
  507. * @return template[] - the template assoziated to the give templatetype
  508. */
  509. public static Template[] getTemplatesForType(long groupsids, int typ) {
  510. String key = mpv5.db.objects.User.getCurrentUser() + "@" + new Integer(typ) + "@" + groupsids;
  511. ReturnValue data = TemplateHandler.getDefinedTemplatesFor(groupsids, new Integer(typ));
  512. Iterator<Object[]> it = data.getDataIterator();
  513. Template[] templates = new Template[data.getData().length];
  514. int i = 0;
  515. while (it.hasNext()) {
  516. Object[] ret = it.next();
  517. try {
  518. templates[i++] = (Template) DatabaseObject.getObject(Context.getTemplate(), Integer.valueOf(ret[0].toString()));
  519. } catch (Exception ex) {
  520. Log.Debug(Template.class, ex);
  521. }
  522. }
  523. return templates;
  524. }
  525. /**
  526. * Clear the template cache
  527. */
  528. public static void clearCache() {
  529. TEMPLATE_CACHE.clear();
  530. }
  531. }