Relation.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * 关联关系操作。
  3. * @returns {com.hotent.platform.form.Relation}
  4. */
  5. Namespace.register("com.hotent.platform.form");
  6. com.hotent.platform.form.Relation=function(){
  7. this.relations=new Array();
  8. this.pk="";
  9. /**
  10. * 添加关联关系。
  11. * 返回
  12. * 1,表示表关联关系已经添加。
  13. * 2,表示主键换了
  14. * 0,添加关系成功
  15. */
  16. this.addRelation=function(pk,tableName,fk){
  17. var obj=new Object();
  18. obj.tableName=tableName;
  19. obj.fk=fk;
  20. var rtn=this.isExists(tableName);
  21. if(rtn) return 1;
  22. if(this.pk=="" || this.pk==pk){
  23. this.pk=pk;
  24. this.relations.push(obj);
  25. return 0;
  26. }
  27. return 2;
  28. };
  29. /**
  30. * 判断表是否存在。
  31. */
  32. this.isExists=function(tableName){
  33. for(var i=0;i<this.relations.length;i++){
  34. var obj=this.relations[i];
  35. if(obj.tableName==tableName)
  36. return true;
  37. }
  38. return false;
  39. };
  40. /**
  41. * 移除表
  42. */
  43. this.remove=function(tableName){
  44. for(var i=0;i<this.relations.length;i++){
  45. var obj=this.relations[i];
  46. if(obj.tableName==tableName){
  47. this.relations.splice(i,1);
  48. }
  49. }
  50. if(this.relations.length==0){
  51. this.pk="";
  52. }
  53. };
  54. /**
  55. * 取得关联关系。
  56. */
  57. this.getRelation=function(){
  58. var len=this.relations.length;
  59. if(len==0) return "";
  60. var sb=new StringBuffer();
  61. sb.append("<relation pk='"+this.pk+"'>");
  62. for(var i=0;i<len;i++){
  63. var tmp=this.relations[i];
  64. sb.append("<table name='"+tmp.tableName+"' fk='"+tmp.fk+"' />");
  65. }
  66. sb.append("</relation>");
  67. return sb.toString();
  68. };
  69. };