1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.util.*; 18 import java.io.Serializable ; 19 20 import org.compiere.util.DB; 21 import org.compiere.util.Env; 22 import org.compiere.util.CCache; 23 import org.compiere.util.NamePair; 24 import org.compiere.util.KeyNamePair; 25 26 34 public final class MLocation extends Lookup 35 implements Serializable 36 { 37 42 public MLocation(Properties ctx, int WindowNo) 43 { 44 if (s_mCountry == null) 46 { 47 int pC_Country_ID = 0; 49 String s = Env.getPreference(ctx, 0, "C_Country_ID", true); 50 if (s.length() > 0) 51 pC_Country_ID = Integer.parseInt(s); 52 if (pC_Country_ID != 0) 53 s_mCountry = MCountry.get(ctx, pC_Country_ID); 54 if (s_mCountry == null) 55 s_mCountry = MCountry.getDefault(ctx); 56 if (s_mCountry == null) s_mCountry = MCountry.get(ctx, 100); int pC_Region_ID = 0; 61 s = Env.getPreference(ctx, 0, "C_Region_ID", true); 62 if (s.length() > 0) 63 pC_Region_ID = Integer.parseInt(s); 64 if (pC_Country_ID != 0) 65 s_mRegion = MRegion.get(ctx, pC_Region_ID); 66 if (s_mRegion == null) 67 s_mRegion = MRegion.getDefault(ctx); 68 } 69 m_ctx = ctx; 71 m_WindowNo = WindowNo; 72 load(0); 73 } 75 private int m_WindowNo; 76 private Properties m_ctx; 77 78 private X_C_Location m_loc = null; 79 private MCountry mCountry; 80 private MRegion mRegion; 81 private static MCountry s_mCountry; private static MRegion s_mRegion; 85 90 public String getDisplay (Object value) 91 { 92 if (!containsKey (value)) 93 return "<" + value.toString() + ">"; 94 return toString(); 95 } 97 102 public NamePair get (Object value) 103 { 104 if (value == null) 105 return null; 106 if (!containsKey (value)) 107 return null; 108 return new KeyNamePair (m_loc.getC_Location_ID(), toString()); 109 } 111 116 public boolean containsKey (Object key) 117 { 118 int intValue = 0; 119 if (key instanceof Integer ) 120 intValue = ((Integer )key).intValue(); 121 else if (key != null) 122 intValue = Integer.parseInt(key.toString()); 123 return load (intValue); 125 } 127 128 129 133 public int getC_Location_ID() 134 { 135 return m_loc.getC_Location_ID(); 136 } 138 142 public String getAddress1() 143 { 144 String s = m_loc.getAddress1(); 145 if (s == null) 146 return ""; 147 return s; 148 } public void setAddress1 (String Address1) 150 { 151 m_loc.setAddress1(Address1); 152 } 153 154 158 public String getAddress2() 159 { 160 String s = m_loc.getAddress2(); 161 if (s == null) 162 return ""; 163 return s; 164 } public void setAddress2 (String Address2) 166 { 167 m_loc.setAddress2(Address2); 168 } 169 170 174 public String getCityRegionPostal() 175 { 176 return parseCRP (mCountry); 177 } 179 183 public String getCity() 184 { 185 String s = m_loc.getCity(); 186 if (s == null) 187 return ""; 188 return s; 189 } public void setCity (String City) 191 { 192 m_loc.setCity(City); 193 } 194 public int getC_City_ID() 195 { 196 return m_loc.getC_City_ID(); 197 } 198 public void setC_City_ID (int C_City_ID) 199 { 200 m_loc.setC_City_ID(C_City_ID); 201 } 202 203 207 public String getPostal() 208 { 209 String postal = m_loc.getPostal(); 210 if (postal == null) 211 postal = ""; 212 return postal; 213 } public void setPostal (String Postal) 215 { 216 m_loc.setPostal(Postal); 217 } 218 219 public String getPostal_Add() 220 { 221 String add = m_loc.getPostal_Add(); 222 if (add == null) 223 add = ""; 224 return add; 225 } 226 public void setPostal_Add (String Postal_Add) 227 { 228 m_loc.setPostal_Add(Postal_Add); 229 } 230 231 232 public int getC_Region_ID() 233 { 234 return m_loc.getC_Region_ID(); 235 } 236 public void setC_Region_ID (int C_Region_ID) 237 { 238 if (C_Region_ID == 0) 239 { 240 m_loc.setValue("C_Region_ID", null); 241 mRegion = null; 242 } 243 else 244 { 245 m_loc.setC_Region_ID(C_Region_ID); 246 mRegion = MRegion.get(m_ctx, C_Region_ID); 247 } 248 } 249 250 public MRegion getRegion() 251 { 252 return mRegion; 253 } 254 public void setRegion(MRegion region) 255 { 256 mRegion = region; 257 if (region == null) 258 { 259 m_loc.setValue("C_Region_ID", null); 260 return; 261 } 262 m_loc.setC_Region_ID(region.getC_Region_ID()); 263 if (region.getC_Country_ID() != m_loc.getC_Country_ID()) 264 { 265 log.warn("setRegion - Region(" + region + ") C_Country_ID=" + region.getC_Country_ID() 266 + " overwriting Location.C_Country_ID=" + m_loc.getC_Country_ID()); 267 setC_Country_ID(region.getC_Country_ID()); 268 } 269 } 271 272 276 public String getRegionName() 277 { 278 String region = m_loc.getRegionName(); 279 if (region == null) 280 region = ""; 281 int regionID = m_loc.getC_Region_ID(); 282 if (regionID == 0) 283 return region; 284 MRegion r = MRegion.get (m_ctx, regionID); 285 if (r != null) 286 return r.getName(); 287 return region; 288 } 290 294 public void setRegionName(String RegionName) 295 { 296 m_loc.setRegionName(RegionName); 297 } 299 300 public int getC_Country_ID() 301 { 302 return m_loc.getC_Country_ID(); 303 } 304 public void setC_Country_ID (int C_Country_ID) 305 { 306 m_loc.setC_Country_ID(C_Country_ID); 307 mCountry = MCountry.get (m_ctx, C_Country_ID); 308 } 309 310 public MCountry getCountry() 311 { 312 return mCountry; 313 } 314 public void setCountry(MCountry country) 315 { 316 if (country == null) 317 throw new IllegalArgumentException ("Country cannot be null"); 318 mCountry = country; 319 m_loc.setC_Country_ID(mCountry.getC_Country_ID()); 320 } 321 322 326 public String getCountryName() 327 { 328 int countryID = m_loc.getC_Country_ID(); 329 if (countryID == 0) 330 return ""; 331 MCountry c = MCountry.get (m_ctx, countryID); 332 if (c != null) 333 return c.getName(); 334 return ""; 335 } 337 341 public void setCountryName(String country) 342 { 343 } 345 350 public String getCountry (boolean onlyForeign) 351 { 352 if (mCountry == null 353 || onlyForeign && mCountry.getC_Country_ID() == s_mCountry.getC_Country_ID()) 354 return null; 355 return mCountry.getName(); 356 } 358 public Properties getCtx() 359 { 360 return m_ctx; 361 } 363 364 365 369 public String toString() 370 { 371 if (m_loc.getC_Location_ID() == 0) 372 return ""; 374 StringBuffer retStr = new StringBuffer (getAddress1()); 375 if (getAddress2().length() > 0) 376 retStr.append(" ").append(getAddress2()); 377 retStr.append(", ").append(parseCRP (mCountry)); 379 return retStr.toString(); 381 } 383 387 public String toStringCR() 388 { 389 if (m_loc.getC_Location_ID() == 0) 390 return ""; 392 StringBuffer retStr = new StringBuffer (getAddress1()); 393 if (getAddress2().length() > 0) 394 retStr.append("\n").append(getAddress2()); 395 retStr.append("\n").append(parseCRP (mCountry)); 397 return retStr.toString(); 399 } 401 402 406 public String toStringX() 407 { 408 StringBuffer sb = new StringBuffer ("MLocation=["); 409 sb.append("ID=").append(m_loc.getC_Location_ID()) 410 .append(",").append(toString()) 411 .append("]"); 412 return sb.toString(); 413 } 415 416 417 422 public boolean load (int Location_ID) 423 { 424 if (Location_ID == 0) { 426 m_loc = new X_C_Location(m_ctx, 0); 427 m_loc.setAddress1(""); 428 m_loc.setAddress2(""); 429 m_loc.setCity(""); 430 m_loc.setPostal(""); 431 m_loc.setPostal_Add(""); 432 m_loc.setRegionName(""); 433 mCountry = s_mCountry; 434 m_loc.setC_Country_ID(s_mCountry.getC_Country_ID()); 435 mRegion = s_mRegion; 436 if (mRegion != null) 437 m_loc.setC_Region_ID(mRegion.getC_Region_ID()); 438 return true; 439 } 440 if (Location_ID == m_loc.getC_Location_ID()) return true; 442 m_loc = new X_C_Location (m_ctx, Location_ID); 444 mCountry = MCountry.get (m_ctx, m_loc.getC_Country_ID()); 445 mRegion = MRegion.get (m_ctx, m_loc.getC_Region_ID()); 446 return true; 447 } 449 454 public boolean loadBPLocation (int C_BPartner_Location_ID) 455 { 456 if (C_BPartner_Location_ID == 0) return load(0); 458 459 String sql = "SELECT * FROM C_Location l " 460 + "WHERE C_Location_ID=(SELECT C_Location_ID FROM C_BPartner_Location WHERE C_BPartner_Location_ID=?)"; 461 m_loc = null; 462 try 463 { 464 PreparedStatement pstmt = DB.prepareStatement(sql); 465 pstmt.setInt(1, C_BPartner_Location_ID); 466 ResultSet rs = pstmt.executeQuery(); 467 if (rs.next()) 468 m_loc = new X_C_Location (m_ctx, rs); 469 rs.close(); 470 pstmt.close(); 471 } 472 catch (SQLException e) 473 { 474 log.error("loadBPLocation - " + C_BPartner_Location_ID, e); 475 m_loc = null; 476 } 477 if (m_loc == null) { 479 load(0); 480 return false; 481 } 482 mCountry = MCountry.get (m_ctx, m_loc.getC_Country_ID()); 483 mRegion = MRegion.get (m_ctx, m_loc.getC_Region_ID()); 484 return true; 485 } 487 491 public int save() 492 { 493 m_loc.save(); 494 log.debug("save - C_Location_ID=" + m_loc.getC_Location_ID()); 495 return m_loc.getC_Location_ID(); 496 } 498 499 505 private String parseCRP (MCountry c) 506 { 507 if (c == null) 508 return "CountryNotFound"; 509 510 String token; 511 String inStr = c.getDisplaySequence(); 512 StringBuffer outStr = new StringBuffer (); 513 514 int i = inStr.indexOf("@"); 515 while (i != -1) 516 { 517 outStr.append (inStr.substring(0, i)); inStr = inStr.substring(i+1, inStr.length()); 520 int j = inStr.indexOf("@"); if (j < 0) 522 { 523 token = ""; j = i+1; 525 } 526 else 527 token = inStr.substring(0, j); 528 if (token.equals("C")) 530 { 531 outStr.append(getCity()); 532 } 533 else if (token.equals("R")) 534 { 535 MRegion r = MRegion.get(m_ctx, m_loc.getC_Region_ID()); 536 if (r != null) 537 outStr.append(r.getName()); 538 else if (getRegionName() != null && getRegionName().length() > 0) 539 outStr.append(getRegionName()); 540 } 541 else if (token.equals("P")) 542 { 543 outStr.append(m_loc.getPostal()); 544 } 545 else if (token.equals("A")) 546 { 547 String add = m_loc.getPostal_Add(); 548 if (add != null && add.length() > 0) 549 outStr.append("-").append(add); 550 } 551 else 552 outStr.append("@").append(token).append("@"); 553 554 inStr = inStr.substring(j+1, inStr.length()); i = inStr.indexOf("@"); 556 } 557 outStr.append(inStr); 559 if (getRegionName() != null && getRegionName().length() > 0 && c.getDisplaySequence().indexOf("@R@") == -1) 561 outStr.append(" ").append(getRegionName()); 562 563 return outStr.toString(); 564 } 566 567 572 public String getColumnName() 573 { 574 return "C_Location_ID"; 575 } 577 585 public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary) 586 { 587 log.error("getData - not implemented"); 588 return null; 589 } 591 } | Popular Tags |