blob: d034cca43fcb948330893ba9b34bc4e0a64dacc2 [file] [log] [blame]
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.ofbiz.entity.condition;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javolution.context.ObjectFactory;
import javolution.lang.Reusable;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericModelException;
import org.ofbiz.entity.config.model.Datasource;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
/**
* Base class for condition expression values.
*
*/
@SuppressWarnings("serial")
public abstract class EntityConditionValue extends EntityConditionBase {
public static EntityConditionValue CONSTANT_NUMBER(Number value) { return ConstantNumberValue.createConstantNumberValue(value); }
public static class ConstantNumberValue extends EntityConditionValue implements Reusable {
protected static ConstantNumberValue createConstantNumberValue(Number value) {
ConstantNumberValue cnv = factory.object();
cnv.init(value);
return cnv;
}
protected static final ObjectFactory<ConstantNumberValue> factory = new ObjectFactory<ConstantNumberValue>() {
protected ConstantNumberValue create() {
return new ConstantNumberValue();
}
};
private Number value;
protected void init(Number value) {
this.value = value;
}
@Override
public void accept(EntityConditionVisitor visitor) {
visitor.acceptEntityConditionValue(this);
}
@Override
public void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix, Datasource datasourceinfo) {
sql.append(value);
}
@Override
public EntityConditionValue freeze() {
return this;
}
@Override
public ModelField getModelField(ModelEntity modelEntity) {
return null;
}
@Override
public Object getValue(Delegator delegator, Map<String, ? extends Object> map) {
return value;
}
@Override
public void reset() {
this.value = value;
}
@Override
public void validateSql(org.ofbiz.entity.model.ModelEntity modelEntity) {
}
@Override
public void visit(EntityConditionVisitor visitor) {
visitor.acceptObject(value);
}
}
public abstract ModelField getModelField(ModelEntity modelEntity);
public void addSqlValue(StringBuilder sql, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, boolean includeTableNamePrefix,
Datasource datasourceinfo) {
addSqlValue(sql, emptyAliases, modelEntity, entityConditionParams, includeTableNamePrefix, datasourceinfo);
}
public abstract void addSqlValue(StringBuilder sql, Map<String, String> tableAliases, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
boolean includeTableNamePrefix, Datasource datasourceinfo);
public abstract void validateSql(ModelEntity modelEntity) throws GenericModelException;
public Object getValue(GenericEntity entity) {
if (entity == null) {
return null;
}
return getValue(entity.getDelegator(), entity);
}
public abstract Object getValue(Delegator delegator, Map<String, ? extends Object> map);
public abstract EntityConditionValue freeze();
public abstract void visit(EntityConditionVisitor visitor);
public void accept(EntityConditionVisitor visitor) {
throw new IllegalArgumentException("accept not implemented");
}
public void toString(StringBuilder sb) {
addSqlValue(sb, null, new ArrayList<EntityConditionParam>(), false, null);
}
@Override
public String toString() {
StringBuilder sql = new StringBuilder();
toString(sql);
return sql.toString();
}
}