1 package org.tigris.scarab.om; 2 3 4 import java.math.BigDecimal ; 5 import java.sql.Connection ; 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.Date ; 9 import java.util.List ; 10 11 import org.apache.commons.lang.ObjectUtils; 12 import org.apache.fulcrum.intake.Retrievable; 13 import org.apache.torque.TorqueException; 14 import org.apache.torque.om.BaseObject; 15 import org.apache.torque.om.ComboKey; 16 import org.apache.torque.om.DateKey; 17 import org.apache.torque.om.NumberKey; 18 import org.apache.torque.om.ObjectKey; 19 import org.apache.torque.om.SimpleKey; 20 import org.apache.torque.om.StringKey; 21 import org.apache.torque.om.Persistent; 22 import org.apache.torque.util.Criteria; 23 import org.apache.torque.util.Transaction; 24 25 26 27 28 32 public abstract class BaseUserVote extends BaseObject 33 implements org.apache.fulcrum.intake.Retrievable 34 { 35 36 private static final UserVotePeer peer = 37 new UserVotePeer(); 38 39 40 41 private Integer moduleId; 42 43 44 private Integer userId; 45 46 47 private int totalVotes = 0; 48 49 50 private int usedVotes = 0; 51 52 53 58 public Integer getModuleId() 59 { 60 return moduleId; 61 } 62 63 64 69 public void setModuleId(Integer v) throws TorqueException 70 { 71 72 if (!ObjectUtils.equals(this.moduleId, v)) 73 { 74 this.moduleId = v; 75 setModified(true); 76 } 77 78 79 if (aModule != null && !ObjectUtils.equals(aModule.getModuleId(), v)) 80 { 81 aModule = null; 82 } 83 84 } 85 86 91 public Integer getUserId() 92 { 93 return userId; 94 } 95 96 97 102 public void setUserId(Integer v) throws TorqueException 103 { 104 105 if (!ObjectUtils.equals(this.userId, v)) 106 { 107 this.userId = v; 108 setModified(true); 109 } 110 111 112 if (aScarabUser != null && !ObjectUtils.equals(aScarabUser.getUserId(), v)) 113 { 114 aScarabUser = null; 115 } 116 117 } 118 119 124 public int getTotalVotes() 125 { 126 return totalVotes; 127 } 128 129 130 135 public void setTotalVotes(int v) 136 { 137 138 if (this.totalVotes != v) 139 { 140 this.totalVotes = v; 141 setModified(true); 142 } 143 144 145 } 146 147 152 public int getUsedVotes() 153 { 154 return usedVotes; 155 } 156 157 158 163 public void setUsedVotes(int v) 164 { 165 166 if (this.usedVotes != v) 167 { 168 this.usedVotes = v; 169 setModified(true); 170 } 171 172 173 } 174 175 176 177 178 private Module aModule; 179 180 186 public void setModule(Module v) throws TorqueException 187 { 188 if (v == null) 189 { 190 setModuleId((Integer ) null); 191 } 192 else 193 { 194 setModuleId(v.getModuleId()); 195 } 196 aModule = v; 197 } 198 199 200 206 public Module getModule() throws TorqueException 207 { 208 if ( !ObjectUtils.equals(getModuleId(), null) ) 209 { 210 return ModuleManager.getInstance(SimpleKey.keyFor(getModuleId())); 211 } 212 return aModule; 213 } 214 215 221 public void setModuleKey(ObjectKey key) throws TorqueException 222 { 223 224 setModuleId(new Integer (((NumberKey) key).intValue())); 225 } 226 227 228 229 private ScarabUser aScarabUser; 230 231 237 public void setScarabUser(ScarabUser v) throws TorqueException 238 { 239 if (v == null) 240 { 241 setUserId((Integer ) null); 242 } 243 else 244 { 245 setUserId(v.getUserId()); 246 } 247 aScarabUser = v; 248 } 249 250 251 257 public ScarabUser getScarabUser() throws TorqueException 258 { 259 if ( !ObjectUtils.equals(getUserId(), null) ) 260 { 261 return ScarabUserManager.getInstance(SimpleKey.keyFor(getUserId())); 262 } 263 return aScarabUser; 264 } 265 266 272 public void setScarabUserKey(ObjectKey key) throws TorqueException 273 { 274 275 setUserId(new Integer (((NumberKey) key).intValue())); 276 } 277 278 279 private static List fieldNames = null; 280 281 286 public static synchronized List getFieldNames() 287 { 288 if (fieldNames == null) 289 { 290 fieldNames = new ArrayList (); 291 fieldNames.add("ModuleId"); 292 fieldNames.add("UserId"); 293 fieldNames.add("TotalVotes"); 294 fieldNames.add("UsedVotes"); 295 fieldNames = Collections.unmodifiableList(fieldNames); 296 } 297 return fieldNames; 298 } 299 300 306 public Object getByName(String name) 307 { 308 if (name.equals("ModuleId")) 309 { 310 return getModuleId(); 311 } 312 if (name.equals("UserId")) 313 { 314 return getUserId(); 315 } 316 if (name.equals("TotalVotes")) 317 { 318 return new Integer (getTotalVotes()); 319 } 320 if (name.equals("UsedVotes")) 321 { 322 return new Integer (getUsedVotes()); 323 } 324 return null; 325 } 326 327 335 public Object getByPeerName(String name) 336 { 337 if (name.equals(UserVotePeer.MODULE_ID)) 338 { 339 return getModuleId(); 340 } 341 if (name.equals(UserVotePeer.USER_ID)) 342 { 343 return getUserId(); 344 } 345 if (name.equals(UserVotePeer.TOTAL_VOTES)) 346 { 347 return new Integer (getTotalVotes()); 348 } 349 if (name.equals(UserVotePeer.USED_VOTES)) 350 { 351 return new Integer (getUsedVotes()); 352 } 353 return null; 354 } 355 356 363 public Object getByPosition(int pos) 364 { 365 if (pos == 0) 366 { 367 return getModuleId(); 368 } 369 if (pos == 1) 370 { 371 return getUserId(); 372 } 373 if (pos == 2) 374 { 375 return new Integer (getTotalVotes()); 376 } 377 if (pos == 3) 378 { 379 return new Integer (getUsedVotes()); 380 } 381 return null; 382 } 383 384 390 public void save() throws Exception 391 { 392 save(UserVotePeer.getMapBuilder() 393 .getDatabaseMap().getName()); 394 } 395 396 406 public void save(String dbName) throws TorqueException 407 { 408 Connection con = null; 409 try 410 { 411 con = Transaction.begin(dbName); 412 save(con); 413 Transaction.commit(con); 414 } 415 catch(TorqueException e) 416 { 417 Transaction.safeRollback(con); 418 throw e; 419 } 420 } 421 422 424 private boolean alreadyInSave = false; 425 435 public void save(Connection con) throws TorqueException 436 { 437 if (!alreadyInSave) 438 { 439 alreadyInSave = true; 440 441 442 443 if (isModified()) 445 { 446 if (isNew()) 447 { 448 UserVotePeer.doInsert((UserVote)this, con); 449 setNew(false); 450 } 451 else 452 { 453 UserVotePeer.doUpdate((UserVote)this, con); 454 } 455 456 if (isCacheOnSave()) 457 { 458 UserVoteManager.putInstance(this); 459 } 460 } 461 462 alreadyInSave = false; 463 } 464 } 465 466 470 protected boolean isCacheOnSave() 471 { 472 return true; 473 } 474 475 476 477 private final SimpleKey[] pks = new SimpleKey[2]; 478 private final ComboKey comboPK = new ComboKey(pks); 479 480 485 public void setPrimaryKey(ObjectKey key) throws TorqueException 486 { 487 SimpleKey[] keys = (SimpleKey[]) key.getValue(); 488 SimpleKey tmpKey = null; 489 setModuleId(new Integer (((NumberKey)keys[0]).intValue())); 490 setUserId(new Integer (((NumberKey)keys[1]).intValue())); 491 } 492 493 499 public void setPrimaryKey( Integer moduleId, Integer userId) 500 throws TorqueException 501 { 502 setModuleId(moduleId); 503 setUserId(userId); 504 } 505 506 509 public void setPrimaryKey(String key) throws TorqueException 510 { 511 setPrimaryKey(new ComboKey(key)); 512 } 513 514 518 public ObjectKey getPrimaryKey() 519 { 520 pks[0] = SimpleKey.keyFor(getModuleId()); 521 pks[1] = SimpleKey.keyFor(getUserId()); 522 return comboPK; 523 } 524 525 529 public String getQueryKey() 530 { 531 if (getPrimaryKey() == null) 532 { 533 return ""; 534 } 535 else 536 { 537 return getPrimaryKey().toString(); 538 } 539 } 540 541 545 public void setQueryKey(String key) 546 throws TorqueException 547 { 548 setPrimaryKey(key); 549 } 550 551 557 public UserVote copy() throws TorqueException 558 { 559 UserVote copyObj = new UserVote(); 560 copyObj.setModuleId(moduleId); 561 copyObj.setUserId(userId); 562 copyObj.setTotalVotes(totalVotes); 563 copyObj.setUsedVotes(usedVotes); 564 565 copyObj.setModuleId((Integer )null); 566 copyObj.setUserId((Integer )null); 567 568 return copyObj; 569 } 570 571 577 public UserVotePeer getPeer() 578 { 579 return peer; 580 } 581 582 public String toString() 583 { 584 StringBuffer str = new StringBuffer (); 585 str.append("UserVote:\n"); 586 str.append("ModuleId = ") 587 .append(getModuleId()) 588 .append("\n"); 589 str.append("UserId = ") 590 .append(getUserId()) 591 .append("\n"); 592 str.append("TotalVotes = ") 593 .append(getTotalVotes()) 594 .append("\n"); 595 str.append("UsedVotes = ") 596 .append(getUsedVotes()) 597 .append("\n"); 598 return(str.toString()); 599 } 600 } 601 | Popular Tags |