PageRenderTime 44ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/compiere-330/base/src/org/compiere/model/MAttributeSet.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 495 lines | 344 code | 29 blank | 122 comment | 99 complexity | 8fad8652b7a8f3f0e119d5b59eb69d11 MD5 | raw file
  1. /******************************************************************************
  2. * Product: Compiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved. *
  4. * This program is free software, you can redistribute it and/or modify it *
  5. * under the terms version 2 of the GNU General Public License as published *
  6. * by the Free Software Foundation. This program is distributed in the hope *
  7. * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied *
  8. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  9. * See the GNU General Public License for more details. *
  10. * You should have received a copy of the GNU General Public License along *
  11. * with this program, if not, write to the Free Software Foundation, Inc., *
  12. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
  13. * For the text or an alternative of this public license, you may reach us *
  14. * ComPiere, Inc., 3600 Bridge Parkway #102, Redwood City, CA 94065, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.model;
  18. import java.sql.*;
  19. import java.util.*;
  20. import java.util.logging.*;
  21. import org.compiere.util.*;
  22. /**
  23. * Product Attribute Set
  24. *
  25. * @author Jorg Janke
  26. * @version $Id: MAttributeSet.java,v 1.3 2006/07/30 00:51:05 jjanke Exp $
  27. */
  28. public class MAttributeSet extends X_M_AttributeSet
  29. {
  30. /**
  31. *
  32. */
  33. private static final long serialVersionUID = 1L;
  34. /**
  35. * Get MAttributeSet from Cache
  36. * @param ctx context
  37. * @param M_AttributeSet_ID id
  38. * @return MAttributeSet
  39. */
  40. public static MAttributeSet get (Ctx ctx, int M_AttributeSet_ID)
  41. {
  42. Integer key = Integer.valueOf (M_AttributeSet_ID);
  43. MAttributeSet retValue = s_cache.get (ctx, key);
  44. if (retValue != null)
  45. return retValue;
  46. retValue = new MAttributeSet (ctx, M_AttributeSet_ID, null);
  47. if (retValue.get_ID () != 0)
  48. s_cache.put (key, retValue);
  49. return retValue;
  50. } // get
  51. /** Cache */
  52. private static final CCache<Integer,MAttributeSet> s_cache
  53. = new CCache<Integer,MAttributeSet> ("M_AttributeSet", 20);
  54. /**
  55. * Standard constructor
  56. * @param ctx context
  57. * @param M_AttributeSet_ID id
  58. * @param trx transaction
  59. */
  60. public MAttributeSet (Ctx ctx, int M_AttributeSet_ID, Trx trx)
  61. {
  62. super (ctx, M_AttributeSet_ID, trx);
  63. if (M_AttributeSet_ID == 0)
  64. {
  65. // setName (null);
  66. setIsGuaranteeDate (false);
  67. setIsGuaranteeDateMandatory (false);
  68. setIsLot (false);
  69. setIsLotMandatory (false);
  70. setIsSerNo (false);
  71. setIsSerNoMandatory (false);
  72. setIsInstanceAttribute(false);
  73. setMandatoryType (MANDATORYTYPE_NotMandatory);
  74. }
  75. } // MAttributeSet
  76. /**
  77. * Load constructor
  78. * @param ctx context
  79. * @param rs result set
  80. * @param trx transaction
  81. */
  82. public MAttributeSet (Ctx ctx, ResultSet rs, Trx trx)
  83. {
  84. super(ctx, rs, trx);
  85. } // MAttributeSet
  86. /** Instance Attributes */
  87. private MAttribute[] m_instanceAttributes = null;
  88. /** Instance Attributes */
  89. private MAttribute[] m_productAttributes = null;
  90. /** Entry Exclude */
  91. private X_M_AttributeSetExclude[] m_excludes = null;
  92. /** Lot create Exclude */
  93. private X_M_LotCtlExclude[] m_excludeLots = null;
  94. /** Serial No create Exclude */
  95. private X_M_SerNoCtlExclude[] m_excludeSerNos = null;
  96. /**
  97. * Get Attribute Array
  98. * @param instanceAttributes true if for instance
  99. * @return instance or product attribute array
  100. */
  101. public MAttribute[] getMAttributes (boolean instanceAttributes)
  102. {
  103. if ((m_instanceAttributes == null && instanceAttributes)
  104. || m_productAttributes == null && !instanceAttributes)
  105. {
  106. String sql = "SELECT mau.M_Attribute_ID "
  107. + "FROM M_AttributeUse mau"
  108. + " INNER JOIN M_Attribute ma ON (mau.M_Attribute_ID=ma.M_Attribute_ID) "
  109. + "WHERE mau.IsActive='Y' AND ma.IsActive='Y'"
  110. + " AND mau.M_AttributeSet_ID=? AND ma.IsInstanceAttribute=? " // #1,2
  111. + "ORDER BY mau.SeqNo";
  112. ArrayList<MAttribute> list = new ArrayList<MAttribute>();
  113. PreparedStatement pstmt = null;
  114. try
  115. {
  116. pstmt = DB.prepareStatement(sql, get_Trx());
  117. pstmt.setInt(1, getM_AttributeSet_ID());
  118. pstmt.setString(2, instanceAttributes ? "Y" : "N");
  119. ResultSet rs = pstmt.executeQuery();
  120. while (rs.next())
  121. {
  122. MAttribute ma = new MAttribute (getCtx(), rs.getInt(1), get_Trx());
  123. list.add (ma);
  124. }
  125. rs.close();
  126. pstmt.close();
  127. pstmt = null;
  128. }
  129. catch (SQLException ex)
  130. {
  131. log.log(Level.SEVERE, sql, ex);
  132. }
  133. try
  134. {
  135. if (pstmt != null)
  136. pstmt.close();
  137. }
  138. catch (SQLException ex1)
  139. {
  140. }
  141. pstmt = null;
  142. // Differentiate attributes
  143. if (instanceAttributes)
  144. {
  145. m_instanceAttributes = new MAttribute[list.size()];
  146. list.toArray (m_instanceAttributes);
  147. }
  148. else
  149. {
  150. m_productAttributes = new MAttribute[list.size()];
  151. list.toArray (m_productAttributes);
  152. }
  153. }
  154. //
  155. if (instanceAttributes)
  156. {
  157. if (isInstanceAttribute() != m_instanceAttributes.length > 0)
  158. setIsInstanceAttribute(m_instanceAttributes.length > 0);
  159. }
  160. // Return
  161. if (instanceAttributes)
  162. return m_instanceAttributes;
  163. return m_productAttributes;
  164. } // getMAttributes
  165. /**
  166. * Something is Mandatory
  167. * @return true if something is mandatory
  168. */
  169. public boolean isMandatory()
  170. {
  171. return !MANDATORYTYPE_NotMandatory.equals(getMandatoryType())
  172. || isLotMandatory()
  173. || isSerNoMandatory()
  174. || isGuaranteeDateMandatory();
  175. } // isMandatory
  176. /**
  177. * Is always mandatory
  178. * @return mandatory
  179. */
  180. public boolean isMandatoryAlways()
  181. {
  182. return MANDATORYTYPE_AlwaysMandatory.equals(getMandatoryType());
  183. } // isMandatoryAlways
  184. /**
  185. * Is Mandatory when Shipping
  186. * @return true if required for shipping
  187. */
  188. public boolean isMandatoryShipping()
  189. {
  190. return MANDATORYTYPE_WhenShipping.equals(getMandatoryType());
  191. } // isMandatoryShipping
  192. /**
  193. * Exclude entry
  194. * @param AD_Column_ID column
  195. * @param isSOTrx sales order
  196. * @return true if excluded
  197. */
  198. public boolean excludeEntry (int AD_Column_ID, boolean isSOTrx)
  199. {
  200. if (m_excludes == null)
  201. {
  202. ArrayList<X_M_AttributeSetExclude> list = new ArrayList<X_M_AttributeSetExclude>();
  203. String sql = "SELECT * FROM M_AttributeSetExclude WHERE IsActive='Y' AND M_AttributeSet_ID=?";
  204. PreparedStatement pstmt = null;
  205. try
  206. {
  207. pstmt = DB.prepareStatement(sql, (Trx) null);
  208. pstmt.setInt (1, getM_AttributeSet_ID());
  209. ResultSet rs = pstmt.executeQuery ();
  210. while (rs.next ())
  211. list.add (new X_M_AttributeSetExclude (getCtx(), rs, null));
  212. rs.close ();
  213. pstmt.close ();
  214. pstmt = null;
  215. }
  216. catch (Exception e)
  217. {
  218. log.log (Level.SEVERE, sql, e);
  219. }
  220. try
  221. {
  222. if (pstmt != null)
  223. pstmt.close ();
  224. pstmt = null;
  225. }
  226. catch (Exception e)
  227. {
  228. pstmt = null;
  229. }
  230. m_excludes = new X_M_AttributeSetExclude[list.size ()];
  231. list.toArray (m_excludes);
  232. }
  233. // Find it
  234. if (m_excludes != null && m_excludes.length > 0)
  235. {
  236. MColumn column = MColumn.get(getCtx(), AD_Column_ID);
  237. for (X_M_AttributeSetExclude element : m_excludes) {
  238. if (element.getAD_Table_ID() == column.getAD_Table_ID()
  239. && element.isSOTrx() == isSOTrx)
  240. return true;
  241. }
  242. }
  243. return false;
  244. } // excludeEntry
  245. /**
  246. * Exclude Lot creation
  247. * @param AD_Column_ID column
  248. * @param isSOTrx SO
  249. * @return true if excluded
  250. */
  251. public boolean isExcludeLot (int AD_Column_ID, boolean isSOTrx)
  252. {
  253. if (getM_LotCtl_ID() == 0)
  254. return true;
  255. if (m_excludeLots == null)
  256. {
  257. ArrayList<X_M_LotCtlExclude> list = new ArrayList<X_M_LotCtlExclude>();
  258. String sql = "SELECT * FROM M_LotCtlExclude WHERE IsActive='Y' AND M_LotCtl_ID=?";
  259. PreparedStatement pstmt = null;
  260. try
  261. {
  262. pstmt = DB.prepareStatement(sql, (Trx) null);
  263. pstmt.setInt (1, getM_LotCtl_ID());
  264. ResultSet rs = pstmt.executeQuery ();
  265. while (rs.next ())
  266. list.add (new X_M_LotCtlExclude (getCtx(), rs, null));
  267. rs.close ();
  268. pstmt.close ();
  269. pstmt = null;
  270. }
  271. catch (Exception e)
  272. {
  273. log.log (Level.SEVERE, sql, e);
  274. }
  275. try
  276. {
  277. if (pstmt != null)
  278. pstmt.close ();
  279. pstmt = null;
  280. }
  281. catch (Exception e)
  282. {
  283. pstmt = null;
  284. }
  285. m_excludeLots = new X_M_LotCtlExclude[list.size ()];
  286. list.toArray (m_excludeLots);
  287. }
  288. // Find it
  289. if (m_excludeLots != null && m_excludeLots.length > 0)
  290. {
  291. MColumn column = MColumn.get(getCtx(), AD_Column_ID);
  292. for (X_M_LotCtlExclude element : m_excludeLots) {
  293. if (element.getAD_Table_ID() == column.getAD_Table_ID()
  294. && element.isSOTrx() == isSOTrx)
  295. return true;
  296. }
  297. }
  298. return false;
  299. } // isExcludeLot
  300. /**
  301. * Exclude SerNo creation
  302. * @param AD_Column_ID column
  303. * @param isSOTrx SO
  304. * @return true if excluded
  305. */
  306. public boolean isExcludeSerNo (int AD_Column_ID, boolean isSOTrx)
  307. {
  308. if (getM_SerNoCtl_ID() == 0)
  309. return true;
  310. if (m_excludeSerNos == null)
  311. {
  312. ArrayList<X_M_SerNoCtlExclude> list = new ArrayList<X_M_SerNoCtlExclude>();
  313. String sql = "SELECT * FROM M_SerNoCtlExclude WHERE IsActive='Y' AND M_SerNoCtl_ID=?";
  314. PreparedStatement pstmt = null;
  315. try
  316. {
  317. pstmt = DB.prepareStatement(sql, (Trx) null);
  318. pstmt.setInt (1, getM_SerNoCtl_ID());
  319. ResultSet rs = pstmt.executeQuery ();
  320. while (rs.next ())
  321. list.add (new X_M_SerNoCtlExclude (getCtx(), rs, null));
  322. rs.close ();
  323. pstmt.close ();
  324. pstmt = null;
  325. }
  326. catch (Exception e)
  327. {
  328. log.log (Level.SEVERE, sql, e);
  329. }
  330. try
  331. {
  332. if (pstmt != null)
  333. pstmt.close ();
  334. pstmt = null;
  335. }
  336. catch (Exception e)
  337. {
  338. pstmt = null;
  339. }
  340. m_excludeSerNos = new X_M_SerNoCtlExclude[list.size ()];
  341. list.toArray (m_excludeSerNos);
  342. }
  343. // Find it
  344. if (m_excludeSerNos != null && m_excludeSerNos.length > 0)
  345. {
  346. MColumn column = MColumn.get(getCtx(), AD_Column_ID);
  347. for (X_M_SerNoCtlExclude element : m_excludeSerNos) {
  348. if (element.getAD_Table_ID() == column.getAD_Table_ID()
  349. && element.isSOTrx() == isSOTrx)
  350. return true;
  351. }
  352. }
  353. return false;
  354. } // isExcludeSerNo
  355. /**
  356. * Get Lot Char Start
  357. * @return defined or \u00ab
  358. */
  359. public String getLotCharStart()
  360. {
  361. String s = super.getLotCharSOverwrite ();
  362. if (s != null && s.length() == 1 && !s.equals(" "))
  363. return s;
  364. return "\u00ab";
  365. } // getLotCharStart
  366. /**
  367. * Get Lot Char End
  368. * @return defined or \u00bb
  369. */
  370. public String getLotCharEnd()
  371. {
  372. String s = super.getLotCharEOverwrite ();
  373. if (s != null && s.length() == 1 && !s.equals(" "))
  374. return s;
  375. return "\u00bb";
  376. } // getLotCharEnd
  377. /**
  378. * Get SerNo Char Start
  379. * @return defined or #
  380. */
  381. public String getSerNoCharStart()
  382. {
  383. String s = super.getSerNoCharSOverwrite ();
  384. if (s != null && s.length() == 1 && !s.equals(" "))
  385. return s;
  386. return "#";
  387. } // getSerNoCharStart
  388. /**
  389. * Get SerNo Char End
  390. * @return defined or none
  391. */
  392. public String getSerNoCharEnd()
  393. {
  394. String s = super.getSerNoCharEOverwrite ();
  395. if (s != null && s.length() == 1 && !s.equals(" "))
  396. return s;
  397. return "";
  398. } // getSerNoCharEnd
  399. /**
  400. * Before Save.
  401. * - set instance attribute flag
  402. * @param newRecord new
  403. * @return true
  404. */
  405. @Override
  406. protected boolean beforeSave (boolean newRecord)
  407. {
  408. if (!isInstanceAttribute()
  409. && (isSerNo() || isLot() || isGuaranteeDate()) )
  410. setIsInstanceAttribute(true);
  411. return true;
  412. } // beforeSave
  413. /**
  414. * After Save.
  415. * - Verify Instance Attribute
  416. * @param newRecord new
  417. * @param success success
  418. * @return success
  419. */
  420. @Override
  421. protected boolean afterSave (boolean newRecord, boolean success)
  422. {
  423. // Set Instance Attribute
  424. if (!isInstanceAttribute())
  425. {
  426. String sql = "UPDATE M_AttributeSet mas"
  427. + " SET IsInstanceAttribute='Y' "
  428. + "WHERE M_AttributeSet_ID=" + getM_AttributeSet_ID()
  429. + " AND IsInstanceAttribute='N'"
  430. + " AND (IsSerNo='Y' OR IsLot='Y' OR IsGuaranteeDate='Y'"
  431. + " OR EXISTS (SELECT * FROM M_AttributeUse mau"
  432. + " INNER JOIN M_Attribute ma ON (mau.M_Attribute_ID=ma.M_Attribute_ID) "
  433. + "WHERE mau.M_AttributeSet_ID=mas.M_AttributeSet_ID"
  434. + " AND mau.IsActive='Y' AND ma.IsActive='Y'"
  435. + " AND ma.IsInstanceAttribute='Y')"
  436. + ")";
  437. int no = DB.executeUpdate(sql, get_Trx());
  438. if (no != 0)
  439. {
  440. log.warning("Set Instance Attribute");
  441. setIsInstanceAttribute(true);
  442. }
  443. }
  444. // Reset Instance Attribute
  445. if (isInstanceAttribute() && !isSerNo() && !isLot() && !isGuaranteeDate())
  446. {
  447. String sql = "UPDATE M_AttributeSet mas"
  448. + " SET IsInstanceAttribute='N' "
  449. + "WHERE M_AttributeSet_ID=" + getM_AttributeSet_ID()
  450. + " AND IsInstanceAttribute='Y'"
  451. + " AND IsSerNo='N' AND IsLot='N' AND IsGuaranteeDate='N'"
  452. + " AND NOT EXISTS (SELECT * FROM M_AttributeUse mau"
  453. + " INNER JOIN M_Attribute ma ON (mau.M_Attribute_ID=ma.M_Attribute_ID) "
  454. + "WHERE mau.M_AttributeSet_ID=mas.M_AttributeSet_ID"
  455. + " AND mau.IsActive='Y' AND ma.IsActive='Y'"
  456. + " AND ma.IsInstanceAttribute='Y')";
  457. int no = DB.executeUpdate(sql, get_Trx());
  458. if (no != 0)
  459. {
  460. log.warning("Reset Instance Attribute");
  461. setIsInstanceAttribute(false);
  462. }
  463. }
  464. return success;
  465. } // afterSave
  466. } // MAttributeSet