PageRenderTime 113ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/objects/Account.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 505 lines | 301 code | 58 blank | 146 comment | 28 complexity | 6ae73988e2ab8871d0d9dfa47af66e46 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.db.objects;
  18. import mpv5.db.common.DataNotCachedException;
  19. import java.util.ArrayList;
  20. import java.util.Enumeration;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import javax.swing.Icon;
  26. import javax.swing.JComponent;
  27. import javax.swing.tree.DefaultMutableTreeNode;
  28. import javax.swing.tree.DefaultTreeModel;
  29. import mpv5.db.common.Context;
  30. import mpv5.db.common.DatabaseObject;
  31. import mpv5.db.common.NodataFoundException;
  32. import mpv5.db.common.QueryCriteria;
  33. import mpv5.db.common.QueryHandler;
  34. import mpv5.globals.Messages;
  35. import mpv5.logging.Log;
  36. import mpv5.ui.frames.MPView;
  37. import mpv5.utils.arrays.ArrayUtilities;
  38. import mpv5.utils.images.MPIcon;
  39. /**
  40. *
  41. *
  42. */
  43. public class Account extends DatabaseObject {
  44. private static ArrayList<DatabaseObject> accounts;
  45. /**
  46. * ANLAGEGUT KOSTEN Eigenkapital UNKOSTEN EINKOMMEN HAFTUNG
  47. * @param type
  48. * @return
  49. */
  50. public static String getTypeString(int type) {
  51. switch (type) {
  52. case Account.OTHER:
  53. return Messages.MISC.toString();
  54. case Account.COST:
  55. return Messages.COST.toString();
  56. case Account.EXPENSE:
  57. return Messages.EXPENSE.toString();
  58. case Account.INCOME:
  59. return Messages.INCOME.toString();
  60. case Account.TAXES:
  61. return Messages.TAXES.toString();
  62. case Account.RESERVE:
  63. return Messages.RESERVE.toString();
  64. default:
  65. return "N/A";
  66. }
  67. }
  68. /**
  69. * Cache accounts
  70. * @return
  71. */
  72. public synchronized static ArrayList<DatabaseObject> cacheAccounts() {
  73. Runnable runnable = new Runnable() {
  74. @Override
  75. public void run() {
  76. try {
  77. accounts = DatabaseObject.getObjects(Context.getAccounts(), false);
  78. } catch (NodataFoundException ex) {
  79. Log.Debug(ex);
  80. }
  81. }
  82. };
  83. new Thread(runnable).start();
  84. return accounts;
  85. }
  86. public Account() {
  87. setContext(Context.getAccounts());
  88. }
  89. private int intparentaccount;
  90. private int intaccountclass;
  91. public final static int OTHER = 0;
  92. public final static int COST = 1;//Direkte Kosten (Wareneinkauf),
  93. public final static int EXPENSE = 2;//Sonstige Kosten, Aufwandskonto
  94. public final static int INCOME = 3;//Ertragskonto
  95. public final static int TAXES = 4;//Steuern
  96. public final static int RESERVE = 5;
  97. private String description = "";
  98. private double taxvalue;
  99. private int intaccounttype;
  100. private int intprofitfid;
  101. private int inttaxfid;
  102. private int inttaxuid;
  103. private String frame = "buildin";
  104. private String hierarchypath = "";
  105. @Override
  106. public JComponent getView() {
  107. // throw new UnsupportedOperationException("Not supported yet.");
  108. return null;
  109. }
  110. /**
  111. * @return the description
  112. */
  113. public String __getDescription() {
  114. return description;
  115. }
  116. /**
  117. * @param description the description to set
  118. */
  119. public void setDescription(String description) {
  120. this.description = description;
  121. }
  122. /**
  123. * A (preferably cached) view to the accounts
  124. * @return
  125. * @throws DataNotCachedException
  126. */
  127. public synchronized static ArrayList<DatabaseObject> getAccounts() throws DataNotCachedException {
  128. if (accounts != null) {
  129. return accounts;
  130. } else {
  131. throw new DataNotCachedException(Context.getAccounts());
  132. }
  133. }
  134. /**
  135. * Get all Items which are assigned to this account
  136. * @return
  137. * @throws mpv5.db.common.NodataFoundException
  138. */
  139. public List<Item> getItemsInAccount() throws NodataFoundException {
  140. List<Item> tmp = DatabaseObject.getReferencedObjects((Item) DatabaseObject.getObject(Context.getItem()), Context.getItemsToAccounts());
  141. QueryCriteria c = new QueryCriteria("defaultaccountsids", this.__getIDS());
  142. ArrayList<Item> tmp2 = DatabaseObject.getObjects(new Item(), c);
  143. tmp.addAll(tmp2);
  144. return tmp;
  145. }
  146. /**
  147. * Get all additional accounts where the given item is currently assigned to
  148. * @param item
  149. * @return
  150. * @throws mpv5.db.common.NodataFoundException
  151. */
  152. public static ArrayList<Account> getAccountsOfItem(Item item) throws NodataFoundException {
  153. Object[][] tmp = QueryHandler.instanceOf().clone(Context.getItemsToAccounts()).select("accountsids", new String[]{"itemsids", item.__getIDS().toString(), ""});
  154. ArrayList<Account> l = new ArrayList<Account>();
  155. l.add((Account) DatabaseObject.getObject(Context.getAccounts(), item.__getAccountsids()));
  156. for (int i = 0; i < tmp.length; i++) {
  157. int id = Integer.valueOf(tmp[i][0].toString());
  158. l.add((Account) DatabaseObject.getObject(Context.getAccounts(), id));
  159. }
  160. return l;
  161. }
  162. /**
  163. *
  164. * <li> 1: Assets: Aktiva,
  165. * <li>2: Cost: direkte Kosten (Wareneinkauf),
  166. * <li>3: Expenses: sonstige Kosten, Aufwandskonto,
  167. * <li>4: Income: Ertragskonto,
  168. * <li>5: Liablities: Verbindlichkeiten (Mittelherkunft),
  169. * <li>6: Equity: Passiva
  170. *
  171. * @return the accounttype
  172. */
  173. public int __getIntaccounttype() {
  174. return intaccounttype;
  175. }
  176. /**
  177. *
  178. * <li> 1: Assets: Aktiva,
  179. * <li>2: Cost: direkte Kosten (Wareneinkauf),
  180. * <li>3: Expenses: sonstige Kosten, Aufwandskonto,
  181. * <li>4: Income: Ertragskonto,
  182. * <li>5: Liablities: Verbindlichkeiten (Mittelherkunft),
  183. * <li>6: Equity: Passiva
  184. *
  185. * @param accounttype the accounttype to set
  186. */
  187. public void setIntaccounttype(int accounttype) {
  188. this.intaccounttype = accounttype;
  189. }
  190. /**
  191. * @return the intaccountclass
  192. */
  193. public int __getIntaccountclass() {
  194. return intaccountclass;
  195. }
  196. /**
  197. * @param intaccountclass the intaccountclass to set
  198. */
  199. public void setIntaccountclass(int intaccountclass) {
  200. this.intaccountclass = intaccountclass;
  201. }
  202. /**
  203. * @return the intparentaccount
  204. */
  205. public int __getIntparentaccount() {
  206. return intparentaccount;
  207. }
  208. /**
  209. * @param intparentaccount the intparentaccount to set
  210. */
  211. public void setIntparentaccount(int intparentaccount) {
  212. this.intparentaccount = intparentaccount;
  213. }
  214. /**
  215. * Create a tree model
  216. * @param data
  217. * @param rootNode
  218. * @return
  219. */
  220. public static DefaultTreeModel toTreeModel(ArrayList<Account> data, Account rootNode) {
  221. DefaultMutableTreeNode node1 = null;
  222. if (data.size() > 0) {
  223. node1 = new DefaultMutableTreeNode(rootNode);
  224. data.remove(rootNode);//remove root if in list
  225. try {
  226. mpv5.YabsViewProxy.instance().setWaiting(true);
  227. node1 = addToParents(node1, data);
  228. } catch (Exception e) {
  229. Log.Debug(e);
  230. } finally {
  231. mpv5.YabsViewProxy.instance().setWaiting(false);
  232. }
  233. }
  234. DefaultTreeModel model = new DefaultTreeModel(node1);
  235. return model;
  236. }
  237. @SuppressWarnings("unchecked")
  238. private static DefaultMutableTreeNode addToParents(DefaultMutableTreeNode firstnode, ArrayList<Account> dobjlist) {
  239. for (int i = 0; i < dobjlist.size(); i++) {
  240. Account dobj = dobjlist.get(i);
  241. if (dobj.__getIntparentaccount() <= 0 && firstnode.isRoot()) {
  242. // Log.Debug(Account.class, "Node is root child, adding it to root and removing it from the list.");
  243. firstnode.add(new DefaultMutableTreeNode(dobj));
  244. // Log.Debug(Account.class, "Added 1st " + dobj);
  245. dobjlist.remove(dobj);//First level groups
  246. i--;
  247. } else {
  248. // Log.Debug(Account.class, "Check " + dobj);
  249. int parentid = dobj.__getIntparentaccount();
  250. if (((Account) firstnode.getUserObject()).__getIDS().intValue() == parentid) {
  251. firstnode.add(new DefaultMutableTreeNode(dobj));
  252. // Log.Debug(Account.class, " Parent " + firstnode);
  253. // Log.Debug(Account.class, " Added " + dobj);
  254. dobjlist.remove(dobj);
  255. i--;
  256. } else {
  257. @SuppressWarnings("unchecked")
  258. Enumeration<DefaultMutableTreeNode> nodes = firstnode.children();
  259. while (nodes.hasMoreElements()) {
  260. addToParents(nodes.nextElement(), dobjlist);
  261. }
  262. }
  263. }
  264. }
  265. return firstnode;
  266. }
  267. /**
  268. * Create a tree model
  269. * @param data
  270. * @param rootaccount
  271. * @return
  272. */
  273. public static DefaultTreeModel toTreeModel2(ArrayList<Account> data, Account rootaccount) {
  274. HashMap<Account, DefaultMutableTreeNode> map = new HashMap<Account, DefaultMutableTreeNode>();
  275. for (Account c : data) {
  276. map.put(c, new DefaultMutableTreeNode(c));
  277. }
  278. DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootaccount);
  279. getChildrenOf(root, data, map);
  280. DefaultTreeModel model = new DefaultTreeModel(root);
  281. return model;
  282. }
  283. private static synchronized void getChildrenOf(DefaultMutableTreeNode node, ArrayList<Account> data, HashMap<Account, DefaultMutableTreeNode> map) {
  284. for (int i = 0; i < data.size(); i++) {
  285. Account account = data.get(i);
  286. DefaultMutableTreeNode anode = map.get(account);
  287. if(account.__getIntparentaccount() == ((Account)node.getUserObject()).__getIDS()){
  288. node.add(anode);
  289. data.remove(account);
  290. i--;
  291. getChildrenOf(anode, data, map);
  292. }
  293. }
  294. }
  295. /**
  296. * @return the taxvalue
  297. */
  298. public double __getTaxvalue() {
  299. return taxvalue;
  300. }
  301. /**
  302. * @param taxvalue the taxvalue to set
  303. */
  304. public void setTaxvalue(double taxvalue) {
  305. this.taxvalue = taxvalue;
  306. }
  307. @Override
  308. public boolean delete() {
  309. try {
  310. ArrayList<Account> childs = DatabaseObject.getObjects(getContext(), new QueryCriteria("intparentaccount", ids));
  311. for (int i = 0; i < childs.size(); i++) {
  312. DatabaseObject databaseObject = childs.get(i);
  313. if (!databaseObject.delete()) {
  314. return false;
  315. }
  316. }
  317. } catch (NodataFoundException ex) {
  318. Log.Debug(this, ex.getMessage());
  319. }
  320. try {
  321. return super.delete();
  322. } catch (Exception e) {
  323. return false;
  324. }
  325. }
  326. @Override
  327. public MPIcon getIcon() {
  328. return null;
  329. }
  330. /**
  331. * @return the frame
  332. */
  333. public String __getFrame() {
  334. return frame;
  335. }
  336. /**
  337. * @param frame the frame to set
  338. */
  339. public void setFrame(String frame) {
  340. this.frame = frame;
  341. }
  342. /**
  343. * @return the intprofitfid
  344. */
  345. public int __getIntprofitfid() {
  346. return intprofitfid;
  347. }
  348. /**
  349. * @param intprofitfid the intprofitfid to set
  350. */
  351. public void setIntprofitfid(int intprofitfid) {
  352. this.intprofitfid = intprofitfid;
  353. }
  354. /**
  355. * @return the inttaxfid
  356. */
  357. public int __getInttaxfid() {
  358. return inttaxfid;
  359. }
  360. /**
  361. * @param inttaxfid the inttaxfid to set
  362. */
  363. public void setInttaxfid(int inttaxfid) {
  364. this.inttaxfid = inttaxfid;
  365. }
  366. /**
  367. * @return the inttaxuid
  368. */
  369. public int __getInttaxuid() {
  370. return inttaxuid;
  371. }
  372. /**
  373. * @param inttaxuid the inttaxuid to set
  374. */
  375. public void setInttaxuid(int inttaxuid) {
  376. this.inttaxuid = inttaxuid;
  377. }
  378. /**
  379. * @return the hierarchypath
  380. */
  381. public String __getHierarchypath() {
  382. if (hierarchypath == null || hierarchypath.equals("")) {
  383. int intp = __getIDS();
  384. do {
  385. try {
  386. Account p = (Account) getObject(Context.getAccounts(), intp);
  387. hierarchypath = Group.GROUPSEPARATOR + p + hierarchypath;
  388. intp = p.__getIntparentaccount();
  389. } catch (NodataFoundException ex) {
  390. break;
  391. }
  392. } while (intp >= 1);
  393. }
  394. return hierarchypath.replaceFirst(Group.GROUPSEPARATOR, "");
  395. }
  396. /**
  397. * @param hierarchypath the hierarchypath to set
  398. */
  399. public void setHierarchypath(String hierarchypath) {
  400. this.hierarchypath = hierarchypath;
  401. }
  402. @Override
  403. public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) {
  404. super.resolveReferences(map);
  405. try {
  406. if (map.containsKey("intaccounttype")) {
  407. map.put("type", getTypeString(Integer.valueOf(map.get("intaccounttype").toString())));
  408. map.remove("intaccounttype");
  409. }
  410. } catch (NumberFormatException numberFormatException) {
  411. //already resolved?
  412. Log.Debug(numberFormatException);
  413. }
  414. if (map.containsKey("intparentaccount")) {
  415. try {
  416. try {
  417. map.put("parentaccount", DatabaseObject.getObject(Context.getAccounts(), Integer.valueOf(map.get("intparentaccount").toString())).__getCname());
  418. map.remove("intparentaccount");
  419. } catch (NodataFoundException ex) {
  420. map.put("parentaccount", null);
  421. Log.Debug(this, ex.getMessage());
  422. }
  423. } catch (NumberFormatException numberFormatException) {
  424. //already resolved?
  425. }
  426. }
  427. return map;
  428. }
  429. /**
  430. * Safely import a database object from external sources (xml, csv etc)<br/>
  431. * Override this for ensuring the existance of DObject specific mandatory values.
  432. * @return
  433. */
  434. @Override
  435. public boolean saveImport() {
  436. Log.Debug(this, "Starting import..");
  437. Log.Debug(this, "Setting IDS to -1");
  438. ids = -1;
  439. Log.Debug(this, "Setting intaddedby to " + mpv5.db.objects.User.getCurrentUser().__getIDS());
  440. setIntaddedby(mpv5.db.objects.User.getCurrentUser().__getIDS());
  441. if (__getGroupsids() <= 0 || !DatabaseObject.exists(Context.getGroup(), __getGroupsids())) {
  442. Log.Debug(this, "Setting groups to users group.");
  443. setGroupsids(mpv5.db.objects.User.getCurrentUser().__getGroupsids());
  444. }
  445. return save();
  446. }
  447. }