1 23 24 package org.objectweb.jorm.mapper.rdb.lib; 25 26 import org.objectweb.jorm.api.PExceptionIO; 27 import org.objectweb.jorm.api.PMappingStructuresManager; 28 import org.objectweb.jorm.api.PMapper; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.api.PExceptionProtocol; 31 import org.objectweb.jorm.api.PMapCluster; 32 import org.objectweb.util.monolog.api.Logger; 33 34 import java.io.FileWriter ; 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.Collection ; 39 40 44 public class RdbPMappingStructuresManager implements PMappingStructuresManager { 45 private PMapper pMapper = null; 46 private ArrayList clusterList = new ArrayList (); 47 protected Logger logger = null; 48 49 54 void closeConnection(Object conn) throws PException { 55 pMapper.closeConnection(conn); 56 } 57 58 63 Object getConnection() throws PException { 64 Object conn = pMapper.getConnection(); 65 if (conn == null) 66 throw new PExceptionProtocol("Impossible to initialize the mapping structures without connection"); 67 return conn; 68 } 69 70 76 private RdbPMapCluster getAlwaysPMapCluster(String jcname, 77 String tname) 78 throws PException { 79 RdbPMapCluster cl = null; 80 Iterator it = clusterList.iterator(); 81 while (it.hasNext()) { 82 cl = (RdbPMapCluster) it.next(); 83 if (cl.containTable(tname)) { 84 break; 85 } 86 cl = null; 87 } 88 if (cl == null) { 89 cl = (RdbPMapCluster) getPMapCluster(jcname); 91 if (cl == null) { 92 cl = new RdbPMapCluster(jcname, tname, this); 94 clusterList.add(cl); 95 } 96 } else { 98 RdbPMapCluster cl2 = (RdbPMapCluster) getPMapCluster(jcname); 100 if ((cl != cl2) && (cl2 != null)) { 101 cl.merge(cl2); 103 clusterList.remove(cl2); 104 } else { 105 cl.classDefined(jcname); 107 } 108 } 109 return cl; 110 } 111 112 120 synchronized public void addTableColumn(String jcname, 121 String tname, 122 String cname, 123 String type, 124 boolean notnull, 125 boolean ispkcol, 126 boolean isMaster) throws PException { 127 RdbPMapCluster cl = getAlwaysPMapCluster(jcname, tname); 128 cl.addTableColumn(tname, cname, type, notnull, ispkcol, isMaster); 129 } 130 131 132 public synchronized void declareClass(String jcname) { 133 Iterator it = clusterList.iterator(); 134 RdbPMapCluster cl; 135 while (it.hasNext()) { 136 cl = (RdbPMapCluster) it.next(); 137 if (cl.containClass(jcname)) { 138 return; 139 } 140 } 141 cl = new RdbPMapCluster(jcname, this); 142 clusterList.add(cl); 143 } 144 145 public PMapper getPMapper() { 146 return pMapper; 147 } 148 149 151 156 synchronized public PMapCluster getPMapCluster(String jcname) throws PException { 157 Iterator it = clusterList.iterator(); 158 while (it.hasNext()) { 159 RdbPMapCluster cl = (RdbPMapCluster) it.next(); 160 if (cl.containClass(jcname) 161 || cl.getUnResolvedDependencies().contains(jcname)) { 162 return cl; 163 } 164 } 165 return null; 166 } 167 168 173 public Collection getPMapClusters() { 174 return clusterList; 175 } 176 177 synchronized public void addDependency(String jcname1, String jcname2) 178 throws PException { 179 RdbPMapCluster cl1 = (RdbPMapCluster) getPMapCluster(jcname1); 180 if (cl1 == null) { 181 throw new PException("You must define the class " + jcname1 182 + " before adding its dependcies (no cluster found)."); 183 } 184 RdbPMapCluster cl2 = (RdbPMapCluster) getPMapCluster(jcname2); 185 if (cl2 == null) { 186 cl1.addDependency(jcname2); 187 } else if (cl2 != cl1) { 188 cl1.merge(cl2); 189 clusterList.remove(cl2); 190 } 191 } 192 193 public void classDefined(String jcname) throws PException { 194 RdbPMapCluster cl = (RdbPMapCluster) getPMapCluster(jcname); 195 if (cl == null) { 196 throw new PException("No cluster found for the class " + jcname); 197 } 198 cl.classDefined(jcname); 199 } 200 201 205 public void setPMapper(PMapper pm) { 206 pMapper = pm; 207 } 208 209 213 public void setLogger(Logger l) { 214 logger = l; 215 } 216 } 217 | Popular Tags |