Java tutorial
/* * Copyright 2002-2016 Jalal Kiswani. * * Licensed 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 com.jk.framework.dao.dynamic.meta; import java.sql.Types; import java.util.Date; import org.apache.commons.lang.StringEscapeUtils; import com.jk.framework.dao.DaoUtil; import com.jk.framework.dao.sql.query.Operator; import com.jk.framework.dao.sql.query.QueryComponent; /** * The Class Field. * * @author Jalal Kiswani */ public class Field implements QueryComponent { FieldMeta meta; Object value; /** * Instantiates a new field. * * @param meta * the meta */ public Field(final FieldMeta meta) { this.meta = meta; } /** * Instantiates a new field. * * @param meta * the meta * @param value * the value */ public Field(final FieldMeta meta, final Object value) { setMeta(meta); setValue(value); } /** * Gets the database string. * * @return the database string */ public String getDatabaseString() { if (getValue() == null) { return "NULL"; } return "'" + StringEscapeUtils.escapeSql(getValue()) + "'"; } /** * Gets the field name. * * @return the field name */ public String getFieldName() { return getMeta().getName(); } /** * Gets the meta. * * @return the meta */ public FieldMeta getMeta() { return this.meta; } /** * Gets the sql equality. * * @return the sql equality */ public String getSqlEquality() { if (getValue() == null) { return "" + this.meta.getName() + " IS NULL "; } return "" + this.meta.getName() + " = '" + getValue().toString() + "'"; } /** * Gets the value. * * @return the value */ public String getValue() { return getValue(null); } /** * Gets the value. * * @param defalutValue * the defalut value * @return the value */ public String getValue(final String defalutValue) { if (this.value == null) { return defalutValue; } return this.value.toString(); } /** * Gets the value as boolean. * * @return the value as boolean */ public Boolean getValueAsBoolean() { if (getValue() == null) { return false; } if (getValueObject() instanceof Boolean) { return ((Boolean) getValueObject()).booleanValue(); } return getValueObject().toString().equals("1"); // return null; } /** * Gets the value as double. * * @return the value as double */ public double getValueAsDouble() { return getValueAsDouble(-1); } /** * Gets the value as double. * * @param defaultValue * the default value * @return the value as double */ public double getValueAsDouble(final float defaultValue) { if (getValue() == null || getValue().trim().equals("")) { return defaultValue; } if (getMeta().getType() == Types.DATE || getMeta().getType() == Types.TIME) { final Date date = (Date) getValueObject(); return date.getTime(); } return Double.parseDouble(getValue()); } /** * Gets the value as float. * * @return the value as float */ public float getValueAsFloat() { return getValueAsFloat(-1); } /** * Gets the value as float. * * @param defaultValue * the default value * @return the value as float */ public float getValueAsFloat(final float defaultValue) { if (getValue() == null || getValue().trim().equals("")) { return defaultValue; } if (getMeta().getType() == Types.DATE || getMeta().getType() == Types.TIME) { final Date date = (Date) getValueObject(); return date.getTime(); } return Float.parseFloat(getValue()); } /** * Gets the value as integer. * * @return the value as integer */ public int getValueAsInteger() { return getValueAsInteger(-1); } /** * Gets the value as integer. * * @param defaultValue * the default value * @return the value as integer */ public int getValueAsInteger(final int defaultValue) { return getValue() == null || getValue().trim().equals("") ? defaultValue : Integer.parseInt(getValue()); } /** * Gets the value object. * * @return the value object */ public Object getValueObject() { return this.value; } /** * Gets the value object. * * @param defaultValue * the default value * @return the value object */ public Object getValueObject(final Object defaultValue) { return getValueObject() == null ? defaultValue : getValueObject(); } /** * Checks if is empty. * * @return true, if is empty */ public boolean isEmpty() { return getValue() == null || getValue().toString().equals(""); } /* (non-Javadoc) * @see com.jk.framework.dao.sql.query.QueryComponent#isInline() */ @Override public boolean isInline() { return true; } /** * Checks if is visible. * * @return true, if is visible */ public boolean isVisible() { return getMeta().isVisible(); } /** * Sets the meta. * * @param meta * the new meta */ public void setMeta(final FieldMeta meta) { this.meta = meta; } /** * Sets the value. * * @param value * the new value */ public void setValue(final Object value) { this.value = value; } /* (non-Javadoc) * @see com.jk.framework.dao.sql.query.QueryComponent#toQueryElement() */ @Override public Object toQueryElement() { return getMeta().getFullQualifiedName() + Operator.EQUAL + getValue(); } /** * To sql equality. * * @return the string */ public String toSqlEquality() { final String value = DaoUtil.fixStringValue(getValue()); if (getValue() == null || getValue().equals("")) { return this.meta.getName() + "='' OR " + this.meta.getName() + " IS NULL"; } else { return this.meta.getName() + "='" + (getValue() == null ? "" : value) + "'"; } } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { final StringBuffer buffer = new StringBuffer(); buffer.append(this.meta.toString() + " - Value = "); buffer.append(this.value); return buffer.toString(); } }