1 24 25 package org.ofbiz.entity.condition; 26 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.ofbiz.entity.GenericDelegator; 31 import org.ofbiz.entity.GenericModelException; 32 import org.ofbiz.entity.config.DatasourceInfo; 33 import org.ofbiz.entity.model.ModelEntity; 34 import org.ofbiz.entity.model.ModelField; 35 36 45 public abstract class EntityFunction extends EntityConditionValue { 46 47 public static interface Fetcher { 48 Object getValue(Object value); 49 } 50 51 public static final int ID_LENGTH = 1; 52 public static final int ID_TRIM = 2; 53 public static final int ID_UPPER = 3; 54 public static final int ID_LOWER = 4; 55 56 public static class LENGTH extends EntityFunction { 57 public static Fetcher FETCHER = new Fetcher() { 58 public Object getValue(Object value) { return new Integer (value.toString().length()); } 59 }; 60 public LENGTH(EntityConditionValue nested) { super(FETCHER, ID_LENGTH, "LENGTH", nested); } 61 public LENGTH(Object value) { super(FETCHER, ID_LENGTH, "LENGTH", value); } 62 }; 63 64 public static class TRIM extends EntityFunction { 65 public static Fetcher FETCHER = new Fetcher() { 66 public Object getValue(Object value) { return value.toString().trim(); } 67 }; 68 public TRIM(EntityConditionValue nested) { super(FETCHER, ID_TRIM, "TRIM", nested); } 69 public TRIM(Object value) { super(FETCHER, ID_TRIM, "TRIM", value); } 70 }; 71 72 public static class UPPER extends EntityFunction { 73 public static Fetcher FETCHER = new Fetcher() { 74 public Object getValue(Object value) { return value.toString().toUpperCase(); } 75 }; 76 public UPPER(EntityConditionValue nested) { super(FETCHER, ID_UPPER, "UPPER", nested); } 77 public UPPER(Object value) { super(FETCHER, ID_UPPER, "UPPER", value); } 78 }; 79 80 public static class LOWER extends EntityFunction { 81 public static Fetcher FETCHER = new Fetcher() { 82 public Object getValue(Object value) { return value.toString().toLowerCase(); } 83 }; 84 public LOWER(EntityConditionValue nested) { super(FETCHER, ID_LOWER, "LOWER", nested); } 85 public LOWER(Object value) { super(FETCHER, ID_LOWER, "LOWER", value); } 86 }; 87 88 protected int idInt; 89 protected String codeString; 90 protected EntityConditionValue nested; 91 protected Object value; 92 protected Fetcher fetcher; 93 94 protected EntityFunction(Fetcher fetcher, int id, String code, EntityConditionValue nested) { 95 this.fetcher = fetcher; 96 idInt = id; 97 codeString = code; 98 this.nested = nested; 99 } 100 101 protected EntityFunction(Fetcher fetcher, int id, String code, Object value) { 102 this.fetcher = fetcher; 103 idInt = id; 104 codeString = code; 105 if (value instanceof EntityConditionValue) { 106 this.nested = (EntityConditionValue) value; 107 } else { 108 this.value = value; 109 } 110 } 111 112 public EntityConditionValue freeze() { 113 if (nested != null) { 114 return new EntityFunction(fetcher, idInt, codeString, nested.freeze()) {}; 115 } else { 116 return new EntityFunction(fetcher, idInt, codeString, value) {}; 117 } 118 } 119 120 public String getCode() { 121 if (codeString == null) 122 return "null"; 123 else 124 return codeString; 125 } 126 127 public int getId() { 128 return idInt; 129 } 130 131 public int hashCode() { 132 return codeString.hashCode(); 133 } 134 135 public boolean equals(Object obj) { 136 if (!(obj instanceof EntityFunction)) return false; 137 EntityFunction otherFunc = (EntityFunction) obj; 138 return (this.idInt == otherFunc.idInt && 139 (this.nested != null ? nested.equals(otherFunc.nested) : otherFunc.nested == null) && 140 (this.value != null ? value.equals(otherFunc.value) : otherFunc.value == null)); 141 } 142 143 public void addSqlValue(StringBuffer sql, Map tableAliases, ModelEntity modelEntity, List entityConditionParams, boolean includeTableNamePrefix, DatasourceInfo datasourceinfo) { 144 sql.append(codeString).append('('); 145 if (nested != null) { 146 nested.addSqlValue(sql, tableAliases, modelEntity, entityConditionParams, includeTableNamePrefix, datasourceinfo); 147 } else { 148 addValue(sql, null, value, entityConditionParams); 149 } 150 sql.append(')'); 151 } 152 153 public void visit(EntityConditionVisitor visitor) { 154 if (nested != null) { 155 visitor.acceptEntityConditionValue(nested); 156 } else { 157 visitor.acceptObject(value); 158 } 159 } 160 161 public void accept(EntityConditionVisitor visitor) { 162 visitor.acceptEntityFunction(this); 163 } 164 165 public ModelField getModelField(ModelEntity modelEntity) { 166 if (nested != null) { 167 return nested.getModelField(modelEntity); 168 } 169 return null; 170 } 171 172 public void validateSql(ModelEntity modelEntity) throws GenericModelException { 173 if (nested != null) { 174 nested.validateSql(modelEntity); 175 } 176 } 177 178 public Object getValue(GenericDelegator delegator, Map map) { 179 Object value = nested != null ? nested.getValue(delegator, map) : this.value; 180 return value != null ? fetcher.getValue(value) : null; 181 } 182 } 183 | Popular Tags |