1 30 package com.genimen.djeneric.tools.modeler.schemagenerator; 31 32 import java.io.BufferedWriter ; 33 import java.io.FileWriter ; 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 import com.genimen.djeneric.language.Messages; 40 import com.genimen.djeneric.repository.DjExtent; 41 import com.genimen.djeneric.repository.DjPersistenceManager; 42 import com.genimen.djeneric.repository.DjProperty; 43 import com.genimen.djeneric.repository.DjRelation; 44 import com.genimen.djeneric.util.DjStringReplacer; 45 46 public abstract class SchemaGenerator 47 { 48 DjPersistenceManager _mgr; 49 50 String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); 51 String _warnings = ""; 52 53 public SchemaGenerator(DjPersistenceManager mgr) 54 { 55 _mgr = mgr; 56 } 57 58 private void writeFile(String fileName, String code) throws Exception 59 { 60 BufferedWriter w = new BufferedWriter (new FileWriter (fileName)); 61 w.write(code); 62 w.close(); 63 } 64 65 protected void resolveColumnAlias(HashMap propsByAlias, DjProperty prop) 66 { 67 DjProperty org = (DjProperty) propsByAlias.get(prop.getName()); 68 69 if (!prop.isRequired() || !org.isRequired()) 71 { 72 org.setRequired(false); 73 prop.setRequired(false); 74 } 75 76 if (prop.getLength() > org.getLength()) 78 { 79 propsByAlias.put(prop.getName(), prop); 80 } 81 if (!prop.getNativeType().equals(org.getNativeType())) 82 { 83 _warnings += Messages.getString("SchemaGenerator.Mergeonflict", prop.getName(), prop.getName(), prop.getExtent() 84 .getName()) 85 + "\n"; 86 } 87 } 88 89 protected String getTableName(DjExtent extent) 90 { 91 DjExtent se = extent; 92 while (se.getSuper() != null) 93 se = se.getSuper(); 94 return se.getName(); 95 } 96 97 protected void addAllProps(DjExtent extent, HashMap propsByAlias, boolean isSubExtent) 98 { 99 for (int p = 0; p < extent.getPropertyCount(); p++) 100 { 101 DjProperty prop = (DjProperty) extent.getProperty(p).clone(); 103 104 if (isSubExtent) 107 { 108 prop.setRequired(false); 109 } 110 if (propsByAlias.containsKey(prop.getName())) 111 { 112 resolveColumnAlias(propsByAlias, prop); 113 } 114 else 115 { 116 propsByAlias.put(prop.getName(), prop); 117 } 118 } 119 } 120 121 public void generate(String filename) throws Exception 122 { 123 _warnings = ""; 124 StringBuffer code = new StringBuffer (10000); 125 126 DjExtent[] extents = _mgr.getExtentsSorted(); 127 for (int i = 0; i < extents.length; i++) 128 { 129 if (extents[i].getSuper() != null) continue; 131 132 code.append(getTableCreateStart(extents[i])); 133 134 DjExtent[] subs = extents[i].getAllSpecializations(); 135 HashMap propsByAlias = new HashMap (); 136 137 addAllProps(extents[i], propsByAlias, false); 138 for (int s = 0; s < subs.length; s++) 139 { 140 addAllProps(subs[s], propsByAlias, true); 141 } 142 143 ArrayList sortedProps = new ArrayList (); 144 145 Iterator it = propsByAlias.values().iterator(); 146 while (it.hasNext()) 147 sortedProps.add(it.next()); 148 149 Collections.sort(sortedProps, DjProperty.getDefaultComparator()); 150 151 String typeColDef = ""; 152 boolean first = true; 153 if (extents[i].isPartOfInheritanceChain()) 154 { 155 typeColDef = getTypeColumnCreate(extents[i]); 156 first = false; 157 } 158 code.append(typeColDef); 159 160 for (int c = 0; c < sortedProps.size(); c++) 161 { 162 code.append(getColumnCreate((DjProperty) sortedProps.get(c), first, c == sortedProps.size() - 1)); 163 first = false; 164 } 165 code.append(getTableCreateEnd(extents[i])); 166 } 167 168 for (int i = 0; i < extents.length; i++) 170 { 171 if (extents[i].getSuper() != null) continue; 173 code.append(getTablePk(extents[i], i)); 174 } 175 176 int counter = 0; 178 for (int i = 0; i < extents.length; i++) 179 { 180 if (extents[i].getSuper() != null) continue; 182 DjRelation[] rels = extents[i].getMasterRelations(); 183 for (int r = 0; r < rels.length; r++) 184 { 185 code.append(getRelation(rels[r], counter++, r)); 186 } 187 } 188 189 counter = 0; 191 for (int i = 0; i < extents.length; i++) 192 { 193 if (extents[i].getSuper() != null) continue; 195 DjRelation[] rels = extents[i].getMasterRelations(); 196 for (int r = 0; r < rels.length; r++) 197 { 198 code.append(getIndex(rels[r], counter++, r)); 199 } 200 } 201 202 DjStringReplacer sr = new DjStringReplacer(code.toString()); 203 sr.replace("\n", LINE_SEPARATOR); 204 writeFile(filename, _warnings + sr.toString()); 205 } 206 207 public abstract String getTableCreateStart(DjExtent extent) throws Exception ; 208 209 public abstract String getTypeColumnCreate(DjExtent extent) throws Exception ; 210 211 public abstract String getTableCreateEnd(DjExtent extent) throws Exception ; 212 213 public abstract String getTablePk(DjExtent extent, int idx) throws Exception ; 214 215 public abstract String getRelation(DjRelation relation, int idx, int idxInTable) throws Exception ; 216 217 public abstract String getIndex(DjRelation relation, int idx, int idxInTable) throws Exception ; 218 219 public abstract String getColumnCreate(DjProperty prop, boolean isFirst, boolean isLast) throws Exception ; 220 } | Popular Tags |