1 13 package info.magnolia.cms.gui.controlx.search; 14 15 import info.magnolia.cms.gui.query.DateSearchQueryParameter; 16 import info.magnolia.cms.gui.query.SearchQueryExpression; 17 import info.magnolia.cms.gui.query.SearchQueryOperator; 18 import info.magnolia.cms.gui.query.StringSearchQueryParameter; 19 20 import java.text.DecimalFormat ; 21 import java.text.NumberFormat ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.TimeZone ; 25 26 import javax.swing.text.NumberFormatter ; 27 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.lang.time.DateFormatUtils; 30 31 32 38 class QueryBuilder { 39 40 43 private StringBuffer statement = new StringBuffer (); 44 45 48 private RepositorySearchListModel model; 49 50 54 protected QueryBuilder(RepositorySearchListModel model) { 55 this.model = model; 56 this.build(this.model.getQuery().getRootExpression()); 57 this.addSelect(); 58 } 59 60 64 protected String getSQLStatement() { 65 return this.statement.toString(); 66 } 67 68 71 private void addSelect() { 72 StringBuffer select = new StringBuffer ("select * from "); 73 select.append(this.model.getNodeType()); 74 if (StringUtils.isNotEmpty(this.model.getSearchPath())) { 75 this.statement.append(" jcr:path like '"); 76 this.statement.append(this.model.getSearchPath()); 77 this.statement.append("%'"); 78 } 79 if (this.statement.length() > 0) { 80 select.append(" where"); 81 } 82 this.statement.insert(0, select.toString()); 83 } 84 85 88 100 101 105 private void build(SearchQueryExpression expression) { 106 if (expression == null) { 107 return; 108 } 109 this.build(expression.getLeft()); 110 this.statement.append(" "); 111 this.statement.append(toJCRExpression(expression)); 112 this.build(expression.getRight()); 113 } 114 115 120 private String toJCRExpression(SearchQueryExpression expression) { 121 if (expression instanceof SearchQueryOperator) { 122 return StringUtils.defaultString(((SearchQueryOperator) expression).getOperator()); 124 } 125 else if (expression instanceof StringSearchQueryParameter) { 126 return toStringJCRExpression((StringSearchQueryParameter) expression); 127 } 128 else if (expression instanceof DateSearchQueryParameter) { 129 return getDateJCRExpression((DateSearchQueryParameter) expression); 130 } 131 return StringUtils.EMPTY; 132 } 133 134 139 private String getDateJCRExpression(DateSearchQueryParameter param) { 140 Date date = param.getValue(); 141 if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.TODAY)) { 142 date = new Date (); 143 } 144 145 StringBuffer buffer = new StringBuffer (); 146 buffer.append(param.getName()); 147 if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.BEFORE)) { 148 buffer.append(" <= "); 149 } 150 else if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.AFTER)) { 151 buffer.append(" >= "); 152 } 153 else if (param.getConstraint().equalsIgnoreCase(DateSearchQueryParameter.IS)) { 154 buffer.append(" = "); 155 } 156 157 buffer.append("TIMESTAMP '"); 158 buffer.append(DateFormatUtils.format(date, "yyyy-MM-dd")); 159 buffer.append("T00:00:00.000"); 160 161 TimeZone timezone = TimeZone.getDefault(); 162 int milis = Math.abs(timezone.getRawOffset()); 163 if(milis == 0){ 164 buffer.append("Z"); 165 } 166 else{ 167 if(timezone.getRawOffset() > 0){ 168 buffer.append("+"); 169 } 170 else{ 171 buffer.append("-"); 172 } 173 174 int hours = milis / (1000 * 60 * 60); 175 int minutes = (milis - hours * 1000 * 60 * 60) / (1000 * 60); 176 DecimalFormat format = new DecimalFormat ("00"); 177 buffer.append(format.format(hours)).append(":").append(format.format(minutes)); 178 } 179 buffer.append("'"); 180 return buffer.toString(); 181 } 182 183 187 private String toStringJCRExpression(StringSearchQueryParameter param) { 188 if (param.getConstraint().equals(StringSearchQueryParameter.CONTAINS)) { 189 return "contains(" + param.getName() + ",'" + param.getValue() + "*')"; 190 } 191 192 else if (param.getConstraint().equals(StringSearchQueryParameter.CONTAINS_NOT)) { 193 return "not contains(" + param.getName() + ",'*" + param.getValue() + "*')"; 194 } 195 196 else if (param.getConstraint().equals(StringSearchQueryParameter.ENDS)) { 197 return "contains(" + param.getName() + ",'*" + param.getValue() + "')"; 198 } 199 200 else if (param.getConstraint().equals(StringSearchQueryParameter.STARTS)) { 201 return "contains(" + param.getName() + ",'" + param.getValue() + "*')"; 202 } 203 204 else if (param.getConstraint().equals(StringSearchQueryParameter.IS)) { 205 return param.getName() + " = '" + param.getValue() + "'"; 206 } 207 208 else if (param.getConstraint().equals(StringSearchQueryParameter.IS_NOT)) { 209 return param.getName() + " <> '" + param.getValue() + "'"; 210 } 211 else{ 212 return param.getName() + " " + param.getConstraint() + " '" + param.getValue() + "'"; 213 } 214 } 215 216 } 217 | Popular Tags |