1 16 package org.outerj.daisy.query.model.functions; 17 18 import org.outerj.daisy.query.model.AbstractFunction; 19 import org.outerj.daisy.query.model.SqlGenerationContext; 20 import org.outerj.daisy.query.model.QValueType; 21 import org.outerj.daisy.query.QueryContext; 22 import org.outerj.daisy.repository.query.QueryException; 23 import org.outerj.daisy.repository.query.EvaluationContext; 24 import org.outerj.daisy.repository.Document; 25 import org.outerj.daisy.repository.Version; 26 27 import java.sql.PreparedStatement ; 28 import java.sql.SQLException ; 29 import java.util.Locale ; 30 31 public class UserIdFunction extends AbstractFunction { 32 public static final String NAME = "UserId"; 33 34 public String getFunctionName() { 35 return NAME; 36 } 37 38 public void prepare(QueryContext context) throws QueryException { 39 super.prepare(context); 40 if (params.size() != 0) 41 throw new QueryException(NAME + " function takes no arguments."); 42 } 43 44 public void generateSqlValueExpr(StringBuffer sql, SqlGenerationContext context) throws QueryException { 45 sql.append(" ? "); 46 } 47 48 public int bindValueExpr(PreparedStatement stmt, int bindPos, QValueType valueType, EvaluationContext evaluationContext) throws SQLException , QueryException { 49 Long userId = evaluationContext.getUserId(); 50 if (userId == null) 51 throw new QueryException(NAME + " function: user ID is not available."); 52 53 stmt.setLong(bindPos, userId.longValue()); 54 return ++bindPos; 55 } 56 57 public Object evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException { 58 return evaluationContext.getUserId(); 59 } 60 61 public Object evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 62 return evaluate(null, evaluationContext); 63 } 64 65 public QValueType getValueType() { 66 return QValueType.LONG; 67 } 68 69 public String getTitle(Locale locale) { 70 return getExpression(); 71 } 72 73 public QValueType getOutputValueType() { 74 return QValueType.LONG; 75 } 76 77 public Object getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException { 78 return evaluate(null, evaluationContext); 79 } 80 } 81 | Popular Tags |