1 19 20 21 package org.apache.cayenne.map; 22 23 import java.io.Serializable ; 24 25 import org.apache.cayenne.util.CayenneMapEntry; 26 import org.apache.cayenne.util.XMLEncoder; 27 import org.apache.cayenne.util.XMLSerializable; 28 29 36 37 public class DbKeyGenerator implements CayenneMapEntry, XMLSerializable, Serializable { 38 39 public static final String ORACLE_TYPE = "ORACLE"; 40 public static final String NAMED_SEQUENCE_TABLE_TYPE = "NAMED_SEQUENCE_TABLE"; 41 42 protected String name; 43 protected DbEntity dbEntity; 44 protected String generatorType; 45 protected Integer keyCacheSize; 46 protected String generatorName; 47 48 public DbKeyGenerator() { 49 } 50 51 public DbKeyGenerator(String name) { 52 this.name = name; 53 } 54 55 public String getName() { 56 return name; 57 } 58 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 public Object getParent() { 64 return getDbEntity(); 65 } 66 67 public void setParent(Object parent) { 68 if (parent != null && !(parent instanceof DbEntity)) { 69 throw new IllegalArgumentException ("Expected null or DbEntity, got: " 70 + parent); 71 } 72 73 setDbEntity((DbEntity) parent); 74 } 75 76 81 public void encodeAsXML(XMLEncoder encoder) { 82 if (getGeneratorType() == null) { 83 return; 84 } 85 86 encoder.println("<db-key-generator>"); 87 encoder.indent(1); 88 89 encoder.print("<db-generator-type>"); 90 encoder.print(getGeneratorType()); 91 encoder.println("</db-generator-type>"); 92 93 if (getGeneratorName() != null) { 94 encoder.print("<db-generator-name>"); 95 encoder.print(getGeneratorName()); 96 encoder.println("</db-generator-name>"); 97 } 98 99 if (getKeyCacheSize() != null) { 100 encoder.print("<db-key-cache-size>"); 101 encoder.print(String.valueOf(getKeyCacheSize())); 102 encoder.println("</db-key-cache-size>"); 103 } 104 105 encoder.indent(-1); 106 encoder.println("</db-key-generator>"); 107 } 108 109 public DbEntity getDbEntity() { 110 return dbEntity; 111 } 112 113 public void setDbEntity(DbEntity dbEntity) { 114 this.dbEntity = dbEntity; 115 } 116 117 public void setGeneratorType(String generatorType) { 118 this.generatorType = generatorType; 119 if (this.generatorType != null) { 120 this.generatorType = this.generatorType.trim().toUpperCase(); 121 if (!(ORACLE_TYPE.equals(this.generatorType) || NAMED_SEQUENCE_TABLE_TYPE 122 .equals(this.generatorType))) 123 this.generatorType = null; 124 } 125 } 126 127 public String getGeneratorType() { 128 return generatorType; 129 } 130 131 public void setKeyCacheSize(Integer keyCacheSize) { 132 this.keyCacheSize = keyCacheSize; 133 if (this.keyCacheSize != null && this.keyCacheSize.intValue() < 1) { 134 this.keyCacheSize = null; 135 } 136 } 137 138 public Integer getKeyCacheSize() { 139 return keyCacheSize; 140 } 141 142 public void setGeneratorName(String generatorName) { 143 this.generatorName = generatorName; 144 if (this.generatorName != null) { 145 this.generatorName = this.generatorName.trim(); 146 if (this.generatorName.length() == 0) 147 this.generatorName = null; 148 } 149 } 150 151 public String getGeneratorName() { 152 return generatorName; 153 } 154 155 public String toString() { 156 return "{Type=" 157 + generatorType 158 + ", Name=" 159 + generatorName 160 + ", Cache=" 161 + keyCacheSize 162 + "}"; 163 } 164 } 165 | Popular Tags |