PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/adempiereSource/base/src/org/compiere/model/MBPartnerLocation.java

#
Java | 254 lines | 145 code | 19 blank | 90 comment | 50 complexity | a6c9cdda0155368ea77ddda08c27102c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /******************************************************************************
  2. * Product: Adempiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2006 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., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.model;
  18. import java.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.Properties;
  21. /**
  22. * Partner Location Model
  23. *
  24. * @author Jorg Janke
  25. * @version $Id: MBPartnerLocation.java,v 1.3 2006/07/30 00:51:03 jjanke Exp $
  26. * @author Teo Sarca, www.arhipac.ro
  27. * <li>FR [ 2788465 ] MBPartnerLocation.getForBPartner method add trxName
  28. * https://sourceforge.net/tracker/index.php?func=detail&aid=2788465&group_id=176962&atid=879335
  29. */
  30. public class MBPartnerLocation extends X_C_BPartner_Location
  31. {
  32. /**
  33. *
  34. */
  35. private static final long serialVersionUID = 1062151267747257338L;
  36. /**
  37. * Get Locations for BPartner
  38. * @param ctx context
  39. * @param C_BPartner_ID bp
  40. * @return array of locations
  41. * @deprecated Since 3.5.3a. Please use {@link #getForBPartner(Properties, int, String)}.
  42. */
  43. public static MBPartnerLocation[] getForBPartner (Properties ctx, int C_BPartner_ID)
  44. {
  45. return getForBPartner(ctx, C_BPartner_ID, null);
  46. }
  47. /**
  48. * Get Locations for BPartner
  49. * @param ctx context
  50. * @param C_BPartner_ID bp
  51. * @param trxName
  52. * @return array of locations
  53. */
  54. public static MBPartnerLocation[] getForBPartner (Properties ctx, int C_BPartner_ID, String trxName)
  55. {
  56. List<MBPartnerLocation> list = new Query(ctx, Table_Name, "C_BPartner_ID=?", trxName)
  57. .setParameters(new Object[]{C_BPartner_ID})
  58. .list();
  59. MBPartnerLocation[] retValue = new MBPartnerLocation[list.size ()];
  60. list.toArray (retValue);
  61. return retValue;
  62. } // getForBPartner
  63. /**************************************************************************
  64. * Default Constructor
  65. * @param ctx context
  66. * @param C_BPartner_Location_ID id
  67. * @param trxName transaction
  68. */
  69. public MBPartnerLocation (Properties ctx, int C_BPartner_Location_ID, String trxName)
  70. {
  71. super (ctx, C_BPartner_Location_ID, trxName);
  72. if (C_BPartner_Location_ID == 0)
  73. {
  74. setName (".");
  75. //
  76. setIsShipTo (true);
  77. setIsRemitTo (true);
  78. setIsPayFrom (true);
  79. setIsBillTo (true);
  80. }
  81. } // MBPartner_Location
  82. /**
  83. * BP Parent Constructor
  84. * @param bp partner
  85. */
  86. public MBPartnerLocation (MBPartner bp)
  87. {
  88. this (bp.getCtx(), 0, bp.get_TrxName());
  89. setClientOrg(bp);
  90. // may (still) be 0
  91. set_ValueNoCheck ("C_BPartner_ID", new Integer(bp.getC_BPartner_ID()));
  92. } // MBPartner_Location
  93. /**
  94. * Constructor from ResultSet row
  95. * @param ctx context
  96. * @param rs current row of result set to be loaded
  97. * @param trxName transaction
  98. */
  99. public MBPartnerLocation (Properties ctx, ResultSet rs, String trxName)
  100. {
  101. super(ctx, rs, trxName);
  102. } // MBPartner_Location
  103. /** Cached Location */
  104. private MLocation m_location = null;
  105. /** Unique Name */
  106. private String m_uniqueName = null;
  107. private int m_unique = 0;
  108. /**
  109. * Get Location/Address
  110. * @param requery requery
  111. * @return location
  112. */
  113. public MLocation getLocation (boolean requery)
  114. {
  115. if (m_location == null)
  116. m_location = MLocation.get (getCtx(), getC_Location_ID(), get_TrxName());
  117. return m_location;
  118. } // getLoaction
  119. /**
  120. * String Representation
  121. * @return info
  122. */
  123. public String toString ()
  124. {
  125. StringBuffer sb = new StringBuffer ("MBPartner_Location[ID=")
  126. .append(get_ID())
  127. .append(",C_Location_ID=").append(getC_Location_ID())
  128. .append(",Name=").append(getName())
  129. .append ("]");
  130. return sb.toString ();
  131. } // toString
  132. /**************************************************************************
  133. * Before Save.
  134. * - Set Name
  135. * @param newRecord new
  136. * @return save
  137. */
  138. protected boolean beforeSave (boolean newRecord)
  139. {
  140. if (getC_Location_ID() == 0)
  141. return false;
  142. // Set New Name
  143. if (!newRecord)
  144. return true;
  145. MLocation address = getLocation(true);
  146. m_uniqueName = getName();
  147. m_unique = MSysConfig.getIntValue("START_VALUE_BPLOCATION_NAME", 0, getAD_Client_ID(), getAD_Org_ID());
  148. if (m_unique < 0 || m_unique > 4)
  149. m_unique = 0;
  150. if (m_uniqueName != null && m_uniqueName.equals(".")) {
  151. // default
  152. m_uniqueName = null;
  153. makeUnique(address);
  154. }
  155. // Check uniqueness
  156. MBPartnerLocation[] locations = getForBPartner(getCtx(), getC_BPartner_ID());
  157. boolean unique = locations.length == 0;
  158. while (!unique)
  159. {
  160. unique = true;
  161. for (int i = 0; i < locations.length; i++)
  162. {
  163. MBPartnerLocation location = locations[i];
  164. if (location.getC_BPartner_Location_ID() == get_ID())
  165. continue;
  166. if (m_uniqueName.equals(location.getName()))
  167. {
  168. //m_uniqueName = null;
  169. m_unique++;
  170. makeUnique(address);
  171. unique = false;
  172. break;
  173. }
  174. }
  175. }
  176. setName (m_uniqueName);
  177. return true;
  178. } // beforeSave
  179. /**
  180. * Make name Unique
  181. * @param address address
  182. */
  183. private void makeUnique (MLocation address)
  184. {
  185. m_uniqueName = "";
  186. // 0 - City
  187. if (m_unique >= 0 || m_uniqueName.length() == 0)
  188. {
  189. String xx = address.getCity();
  190. if (xx != null && xx.length() > 0)
  191. m_uniqueName = xx;
  192. }
  193. // 1 + Address1
  194. if (m_unique >= 1 || m_uniqueName.length() == 0)
  195. {
  196. String xx = address.getAddress1();
  197. if (xx != null && xx.length() > 0)
  198. {
  199. if (m_uniqueName.length() > 0)
  200. m_uniqueName += " ";
  201. m_uniqueName += xx;
  202. }
  203. }
  204. // 2 + Address2
  205. if (m_unique >= 2 || m_uniqueName.length() == 0)
  206. {
  207. String xx = address.getAddress2();
  208. if (xx != null && xx.length() > 0)
  209. {
  210. if (m_uniqueName.length() > 0)
  211. m_uniqueName += " ";
  212. m_uniqueName += xx;
  213. }
  214. }
  215. // 3 - Region
  216. if (m_unique >= 3 || m_uniqueName.length() == 0)
  217. {
  218. String xx = address.getRegionName(true);
  219. if (xx != null && xx.length() > 0)
  220. {
  221. if (m_uniqueName.length() > 0)
  222. m_uniqueName += " ";
  223. m_uniqueName += xx;
  224. }
  225. }
  226. // 4 - ID
  227. if (m_unique >= 4 || m_uniqueName.length() == 0)
  228. {
  229. int id = get_ID();
  230. if (id == 0)
  231. id = address.get_ID();
  232. m_uniqueName += "#" + id;
  233. }
  234. } // makeUnique
  235. } // MBPartnerLocation