1 23 package org.objectweb.medor.optim.rdb; 24 25 import org.objectweb.medor.api.MedorException; 26 import org.objectweb.medor.optim.lib.BasicRule; 27 import org.objectweb.medor.query.api.QueryNode; 28 import org.objectweb.medor.query.api.QueryTree; 29 import org.objectweb.medor.query.rdb.api.RdbExpQueryLeaf; 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter; 32 33 import java.util.Map ; 34 35 38 public class RdbAssignRdbAdapterRule extends BasicRule { 39 40 protected Map name2Adapter = null; 41 42 public RdbAssignRdbAdapterRule() { 43 super("RdbAssignRdbAdapterRule"); 44 } 45 46 public RdbAssignRdbAdapterRule(Map m) { 47 this(); 48 name2Adapter = m; 49 } 50 51 public void setName2Adapter(Map m) { 52 name2Adapter = m; 53 } 54 55 public Map getName2Adapter() { 56 return name2Adapter; 57 } 58 59 public QueryTree rewrite(QueryTree qt, QueryNode _parent) 60 throws MedorException { 61 log.log(BasicLevel.DEBUG, "Assignation of relational adapters"); 62 if (name2Adapter != null && name2Adapter.size()>0) 63 assignAdapter(qt); 64 return qt; 65 } 66 67 protected void assignAdapter(QueryTree qt) throws MedorException { 68 if (qt instanceof RdbExpQueryLeaf) { 69 RdbExpQueryLeaf ql = (RdbExpQueryLeaf) qt; 70 String name = ql.getRdbAdapterName(); 71 if (name == null) { 72 log.log(BasicLevel.DEBUG, 73 "No adapter specified for the RdbExpQueryLeaf: " + ql.getName()); 74 return; 75 } 76 RdbAdapter adapter = (RdbAdapter) 77 name2Adapter.get(name); 78 if (adapter != null) { 79 ql.setRdbAdapter(adapter); 80 log.log(BasicLevel.DEBUG, "Assign the adapter " 81 + ql.getRdbAdapterName() + "to the RdbExpQueryLeaf: " 82 + ql.getName()); 83 } else 84 throw new MedorException("No adapter " + name 85 + " found for the RdbExpQueryLeaf: " + ql.getName()); 86 } else if (qt instanceof QueryNode) { 87 QueryTree[] qts = ((QueryNode) qt).getChildren(); 88 for (int i = 0; i < qts.length; i++) { 89 assignAdapter(qts[i]); 90 } 91 } 92 } 93 94 } 95 | Popular Tags |