1 28 package net.sf.jasperreports.engine.query; 29 30 import java.util.ArrayList ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import net.sf.jasperreports.engine.JRDataset; 35 import net.sf.jasperreports.engine.JRQuery; 36 import net.sf.jasperreports.engine.JRQueryChunk; 37 import net.sf.jasperreports.engine.JRRuntimeException; 38 import net.sf.jasperreports.engine.JRValueParameter; 39 import net.sf.jasperreports.engine.fill.JRFillParameter; 40 41 47 public abstract class JRAbstractQueryExecuter implements JRQueryExecuter 48 { 49 protected final JRDataset dataset; 50 private final Map parametersMap; 51 52 private String queryString; 53 private List parameterNames; 54 55 56 protected JRAbstractQueryExecuter(JRDataset dataset, Map parametersMap) 57 { 58 this.dataset = dataset; 59 this.parametersMap = parametersMap; 60 61 queryString = ""; 62 parameterNames = new ArrayList (); 63 } 64 65 66 71 protected void parseQuery() 72 { 73 JRQuery query = dataset.getQuery(); 74 75 if (query != null) 76 { 77 JRQueryChunk[] chunks = query.getChunks(); 78 if (chunks != null && chunks.length > 0) 79 { 80 StringBuffer sbuffer = new StringBuffer (); 81 JRQueryChunk chunk = null; 82 for(int i = 0; i < chunks.length; i++) 83 { 84 chunk = chunks[i]; 85 switch (chunk.getType()) 86 { 87 case JRQueryChunk.TYPE_PARAMETER_CLAUSE : 88 { 89 String parameterName = chunk.getText(); 90 Object parameterValue = getParameterValue(parameterName); 91 sbuffer.append(String.valueOf(parameterValue)); 92 break; 93 } 94 case JRQueryChunk.TYPE_PARAMETER : 95 { 96 sbuffer.append(getParameterReplacement(chunk.getText())); 97 parameterNames.add(chunk.getText()); 98 break; 99 } 100 case JRQueryChunk.TYPE_TEXT : 101 default : 102 { 103 sbuffer.append(chunk.getText()); 104 break; 105 } 106 } 107 } 108 109 queryString = sbuffer.toString(); 110 } 111 } 112 } 113 114 115 121 protected String getQueryString() 122 { 123 return queryString; 124 } 125 126 127 132 protected List getCollectedParameterNames() 133 { 134 return parameterNames; 135 } 136 137 138 143 protected Object getParameterValue(String parameterName) 144 { 145 JRValueParameter parameter = getValueParameter(parameterName); 146 return parameter.getValue(); 147 } 148 149 150 157 protected JRFillParameter getParameter(String parameterName) 158 { 159 JRFillParameter parameter = (JRFillParameter) parametersMap.get(parameterName); 160 161 if (parameter == null) 162 { 163 throw new JRRuntimeException("Parameter \"" + parameterName + "\" does not exist."); 164 } 165 166 return parameter; 167 } 168 169 170 176 protected JRValueParameter getValueParameter(String parameterName) 177 { 178 JRValueParameter parameter = (JRValueParameter) parametersMap.get(parameterName); 179 180 if (parameter == null) 181 { 182 throw new JRRuntimeException("Parameter \"" + parameterName + "\" does not exist."); 183 } 184 185 return parameter; 186 } 187 188 189 196 protected abstract String getParameterReplacement(String parameterName); 197 } 198 | Popular Tags |