001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.query.model;
017:
018: import org.outerj.daisy.query.QueryContext;
019: import org.outerj.daisy.query.EvaluationInfo;
020: import org.outerj.daisy.repository.query.QueryException;
021:
022: import java.util.Map;
023: import java.util.HashMap;
024: import java.sql.PreparedStatement;
025: import java.sql.SQLException;
026:
027: /**
028: * Implementation of the LangInSync() and LangNotInSync() conditions.
029: *
030: * <p>Reminder: when thinking about the behavior of these conditions, one should think
031: * what makes sense from the export use case: we need to be able to select the documents
032: * which need to be provided to the translation agency.
033: */
034: public class LangInOrNotInSync extends AbstractPredicateExpr {
035: private String liveLast;
036: private boolean inSync;
037:
038: public LangInOrNotInSync(String liveLast, boolean inSync) {
039: this .liveLast = liveLast;
040: this .inSync = inSync;
041: }
042:
043: private String getName() {
044: return inSync ? "LangInSync" : "LangNotInSync";
045: }
046:
047: public void prepare(QueryContext context) throws QueryException {
048: if (liveLast.equalsIgnoreCase("live")) {
049: liveLast = "live";
050: } else if (liveLast.equalsIgnoreCase("last")) {
051: liveLast = "last";
052: } else {
053: throw new QueryException("Invalid argument for "
054: + getName() + ": \"" + liveLast + "\".");
055: }
056: }
057:
058: public boolean evaluate(ExprDocData data,
059: EvaluationInfo evaluationInfo) throws QueryException {
060: throw new RuntimeException(getName()
061: + " cannot be dynamically evaluated.");
062: }
063:
064: private static final ParamString DOCVARIANTS_JOIN_EXPR = new ParamString(
065: " left outer join document_variants {alias} on ("
066: + " {document_variants}.doc_id = {alias}.doc_id "
067: + " and {document_variants}.ns_id = {alias}.ns_id "
068: + " and {document_variants}.branch_id = {alias}.branch_id "
069: + " and {alias}.lang_id = {versions}.synced_with_lang_id"
070: + " )");
071:
072: private static final ParamString NOT_IN_SYNC_TEST_EXPR = new ParamString(
073: " {versions}.synced_with_version_id is null" /* if synced-with is not set, than not in sync */
074: + " or ("
075: + " {versions}.synced_with_version_id is not null"
076: + " and {alias}.{last-live}_major_change_version_id is not null" /* there needs to be a major change */
077: + " and {versions}.synced_with_version_id < {alias}.{last-live}_major_change_version_id"
078: + " )");
079:
080: private static final ParamString IN_SYNC_TEST_EXPR = new ParamString(
081: " {alias}.{last-live}_major_change_version_id is not null "
082: + " and {versions}.synced_with_version_id is not null "
083: + " and {versions}.synced_with_version_id >= {alias}.{last-live}_major_change_version_id");
084:
085: public void generateSql(StringBuilder sql,
086: SqlGenerationContext context) throws QueryException {
087: String alias = context.getNewAliasCounter();
088: String docvariantsAlias = "docvariants" + alias;
089: String currentDocVariants = context.getDocumentVariantsTable()
090: .getName();
091: String versionsTable = context.getVersionsTable().getName();
092:
093: final Map<String, String> params = new HashMap<String, String>();
094: params.put("alias", docvariantsAlias);
095: params.put("document_variants", currentDocVariants);
096: params.put("last-live", liveLast);
097: params.put("versions", versionsTable);
098:
099: SqlGenerationContext.Table table = new SqlGenerationContext.Table() {
100: public String getName() {
101: return "does_not_matter";
102: }
103:
104: public String getJoinExpression(boolean searchLastVersion) {
105: return DOCVARIANTS_JOIN_EXPR.toString(params);
106: }
107:
108: public int bindJoin(PreparedStatement stmt, int bindPos,
109: EvaluationInfo evaluationInfo) {
110: return bindPos;
111: }
112: };
113: context.needsJoinWithTable(table);
114:
115: sql.append(" (");
116: if (inSync)
117: sql.append(IN_SYNC_TEST_EXPR.toString(params));
118: else
119: sql.append(NOT_IN_SYNC_TEST_EXPR.toString(params));
120: sql.append(") ");
121: }
122:
123: public int bindSql(PreparedStatement stmt, int bindPos,
124: EvaluationInfo evaluationInfo) throws SQLException {
125: return bindPos;
126: }
127:
128: public AclConditionViolation isAclAllowed() {
129: return new AclConditionViolation(getName()
130: + " is not allowed in ACL conditions");
131: }
132:
133: public Tristate appliesTo(ExprDocData data,
134: EvaluationInfo evaluationInfo) {
135: throw new IllegalStateException();
136: }
137:
138: public void collectAccessRestrictions(
139: AccessRestrictions restrictions) {
140: // do noting
141: }
142: }
|