1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.components.sql;
22 
23 import com.servingxml.app.ParameterDescriptor;
24 import com.servingxml.components.common.TrueFalseEnum;
25 import com.servingxml.util.MessageFormatter;
26 import com.servingxml.util.ServingXmlMessages;
27 import com.servingxml.util.ServingXmlException;
28 import com.servingxml.util.Name;
29 import com.servingxml.util.QualifiedName;
30 import com.servingxml.ioc.components.ConfigurationContext;
31 import com.servingxml.components.xsltconfig.XsltConfiguration;
32 import com.servingxml.components.quotesymbol.QuoteSymbol;
33 import com.servingxml.expr.substitution.EscapeSubstitutionVariables;
34 import com.servingxml.expr.substitution.DoEscapeSubstitutionVariables;
35 import com.servingxml.app.Environment;
36 
37 /**
38  * The <code>SqlQueryAssembler</code> implements an assembler for
39  * assembling system <code>SqlQuery</code> objects.
40  *
41  *
42  * @author Daniel A. Parker (daniel.parker@servingxml.com)
43  */
44 
45 public class SqlQueryAssembler {
46   private static final Name DEFAULT_RECORD_TYPE_NAME = new QualifiedName("record");
47 
48   private ParameterDescriptor[] parameterDescriptors = ParameterDescriptor.EMPTY_ARRAY;
49   private Name recordTypeName = DEFAULT_RECORD_TYPE_NAME;
50   private XsltConfiguration xsltConfiguration;
51   private String trimLeading = TrueFalseEnum.FALSE.toString();
52   private String trimTrailing = TrueFalseEnum.FALSE.toString();
53   private EscapeSubstitutionVariables escapeVariables = new DoEscapeSubstitutionVariables('\'', "''");
54 
setRecordType(Name recordTypeName)55   public void setRecordType(Name recordTypeName) {
56     this.recordTypeName = recordTypeName;
57   }
58 
setTrim(String value)59   public void setTrim(String value) {
60     this.trimLeading = value;
61     this.trimTrailing = value;
62   }
63 
setTrimLeading(String trimLeading)64   public void setTrimLeading(String trimLeading) {
65     this.trimLeading = trimLeading;
66   }
67 
setTrimTrailing(String trimTrailing)68   public void setTrimTrailing(String trimTrailing) {
69     this.trimTrailing = trimTrailing;
70   }
71 
injectComponent(ParameterDescriptor[] parameterDescriptors)72   public void injectComponent(ParameterDescriptor[] parameterDescriptors) {
73     this.parameterDescriptors = parameterDescriptors;
74   }
75 
injectComponent(XsltConfiguration xsltConfiguration)76   public void injectComponent(XsltConfiguration xsltConfiguration) {
77     this.xsltConfiguration = xsltConfiguration;
78   }
79 
injectComponent(EscapeSubstitutionVariables escapeVariables)80   public void injectComponent(EscapeSubstitutionVariables escapeVariables) {
81     this.escapeVariables = escapeVariables;
82   }
83 
injectComponent(QuoteSymbol quoteSymbol)84   public void injectComponent(QuoteSymbol quoteSymbol) {
85     if (quoteSymbol.getEscapeSequence().length() > 0) {
86       this.escapeVariables = new DoEscapeSubstitutionVariables(quoteSymbol.getCharacter(),quoteSymbol.getEscapeSequence());
87     } else {
88       this.escapeVariables = EscapeSubstitutionVariables.DO_NOT_ESCAPE;
89     }
90   }
91 
assemble(final ConfigurationContext context)92   public SqlQuery assemble(final ConfigurationContext context) {
93     Environment env = new Environment(parameterDescriptors,context.getQnameContext());
94 
95     if (xsltConfiguration == null) {
96       xsltConfiguration = XsltConfiguration.getDefault();
97     }
98 
99     if (recordTypeName.isEmpty()) {
100       String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_REQUIRED,
101         context.getElement().getTagName(),"recordType");
102       throw new ServingXmlException(message);
103     }
104     TrueFalseEnum trimLeadingIndicator;
105     try {
106       trimLeadingIndicator = TrueFalseEnum.parse(trimLeading);
107     } catch (ServingXmlException e) {
108       String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_VALUE_INVALID,
109         context.getElement().getTagName(), "trimLeading");
110       e = e.supplementMessage(message);
111       throw e;
112     }
113 
114     TrueFalseEnum trimTrailingIndicator;
115     try {
116       trimTrailingIndicator = TrueFalseEnum.parse(trimTrailing);
117     } catch (ServingXmlException e) {
118       String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_VALUE_INVALID,
119         context.getElement().getTagName(), "trimTrailing");
120       e = e.supplementMessage(message);
121       throw e;
122     }
123 
124     SqlStatementFactory factory = new SqlStatementFactory(env,xsltConfiguration.getVersion(), escapeVariables);
125     SqlStatement statement = factory.createSqlStatement(context);
126     SqlQuery query = new SqlQueryImpl(statement, recordTypeName,
127                             trimLeadingIndicator.booleanValue(), trimTrailingIndicator.booleanValue());
128     if (parameterDescriptors.length > 0) {
129       query = new SqlQueryPrefilter(query, parameterDescriptors);
130     }
131 
132     return query;
133   }
134 }
135 
136