Java tutorial
/* * Copyright (c) 2013 @iSQWEN. All rights reserved. * * 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 org.faster.orm.criteria; import org.faster.util.Strings; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.LogicalExpression; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Restrictions; import static org.apache.commons.lang3.StringUtils.isBlank; import static org.faster.orm.criteria.GenericCriteria.ALL; import static org.faster.orm.criteria.GenericCriteria.NOT_NULL; import static org.faster.orm.criteria.GenericCriteria.NULL; /** * ???render?? * * @author sqwen */ public class CriteriaRender { private CriteriaRender() { } public static final void renderIntegerField(DetachedCriteria dc, String fieldName, String value) { if (isBlank(value) || ALL.equalsIgnoreCase(value)) { return; } if (NULL.equalsIgnoreCase(value)) { dc.add(Restrictions.isNull(fieldName)); return; } if (NOT_NULL.equalsIgnoreCase(value)) { dc.add(Restrictions.isNotNull(fieldName)); } } /** * ?? * * @param dc * ?? * @param fieldName * ?? * @param value * */ public static void renderField(DetachedCriteria dc, String fieldName, Object value) { if (value == null) { return; } if (value instanceof String) { String stringValue = (String) value; if (isBlank(stringValue) || ALL.equalsIgnoreCase(stringValue)) { return; } if (NULL.equalsIgnoreCase(stringValue)) { dc.add(Restrictions.isNull(fieldName)); return; } if (NOT_NULL.equalsIgnoreCase(stringValue)) { dc.add(Restrictions.isNotNull(fieldName)); return; } } dc.add(Restrictions.eq(fieldName, value)); } /** * ?like? * * @param dc * ?? * @param fieldName * ?? * @param value * */ public static void renderFieldLike(DetachedCriteria dc, String fieldName, String value) { if (isBlank(value) || ALL.equalsIgnoreCase(value)) { return; } if (NULL.equalsIgnoreCase(value)) { dc.add(Restrictions.isNull(fieldName)); return; } if (NOT_NULL.equalsIgnoreCase(value)) { dc.add(Restrictions.isNotNull(fieldName)); return; } dc.add(Restrictions.ilike(fieldName, value, MatchMode.ANYWHERE)); } /** * ?ID * * @param dc * ? * @param subObject * ??? : vendor * @param subObjectId * ?ID */ public static void renderSubObjectId(DetachedCriteria dc, String subObject, Object subObjectId) { if (subObjectId == null) { return; } dc.createAlias(subObject, subObject); dc.add(Restrictions.eq(subObject + ".id", subObjectId)); } /** * ? * * @param dc * * @param subObjectField * ?vendor.name * @param value * ? */ public static void renderSubObjectFieldEq(DetachedCriteria dc, String subObjectField, Object value) { if (value == null) { return; } String[] ss = subObjectField.split("\\."); dc.createAlias(ss[0], ss[0]); dc.add(Restrictions.eq(subObjectField, value)); } /** * ? * * @param dc * * @param subObjectField * ?vendor.name * @param value * ? */ public static void renderSubObjectFieldLike(DetachedCriteria dc, String subObjectField, String value) { if (isBlank(value)) { return; } String[] ss = subObjectField.split("\\."); dc.createAlias(ss[0], ss[0]); dc.add(Restrictions.ilike(subObjectField, value.trim(), MatchMode.ANYWHERE)); } /** * ? * * @param dc * ?? * @param fieldName * ??type * @param fieldValues * ???1,2,3 */ public static void renderFieldByIntegerFilterValues(DetachedCriteria dc, String fieldName, String fieldValues, String delimiter) { if (isBlank(fieldValues)) { return; } Integer[] array = Strings.toIntegerArray(fieldValues.trim(), delimiter); if (array.length == 1) { dc.add(Restrictions.eq(fieldName, array[0])); } else { dc.add(Restrictions.in(fieldName, array)); } } public static void renderFieldByLongFilterValues(DetachedCriteria dc, String fieldName, String fieldValues, String delimiter) { if (isBlank(fieldValues)) { return; } Long[] array = Strings.toLongArray(fieldValues.trim(), delimiter); if (array.length == 1) { dc.add(Restrictions.eq(fieldName, array[0])); } else { dc.add(Restrictions.in(fieldName, array)); } } public static void renderFieldByStringFilterValues(DetachedCriteria dc, String fieldName, String fieldValues, String delimiter) { if (isBlank(fieldValues)) { return; } if (fieldValues.contains(delimiter)) { String[] multiValue = Strings.toStringArray(fieldValues, delimiter); if (multiValue.length == 1) { dc.add(Restrictions.eq(fieldName, multiValue[0])); } else { dc.add(Restrictions.in(fieldName, multiValue)); } } else { dc.add(Restrictions.eq(fieldName, fieldValues)); } } /** * ?ID?? * * @param dc * ?? * @param subObjects * ??? * @param filterValues * ??? */ public static void renderSubObjectIdsByIntegerFilterValues(DetachedCriteria dc, String[] subObjects, String filterValues) { if (subObjects == null || subObjects.length == 0 || isBlank(filterValues)) { return; } Integer[] array = Strings.toIntegerArray(filterValues.trim(), ","); for (String fieldName : subObjects) { dc.createAlias(fieldName, fieldName); } if (array.length == 1) { if (subObjects.length == 1) { dc.add(Restrictions.eq(subObjects[0] + ".id", array[0])); } else { LogicalExpression le = Restrictions.or(Restrictions.eq(subObjects[0] + ".id", array[0]), Restrictions.eq(subObjects[1] + ".id", array[0])); for (int i = 2; i < subObjects.length; i++) { le = Restrictions.or(le, Restrictions.eq(subObjects[i] + ".id", array[0])); } dc.add(le); } } else { if (subObjects.length == 1) { dc.add(Restrictions.in(subObjects[0] + ".id", array)); } else { LogicalExpression le = Restrictions.or(Restrictions.in(subObjects[0] + ".id", array), Restrictions.in(subObjects[1] + ".id", array)); for (int i = 2; i < subObjects.length; i++) { le = Restrictions.or(le, Restrictions.eq(subObjects[i] + ".id", array)); } dc.add(le); } } } /** * ????? * * @param dc * ? * @param integersFiledName * ?????mgwIds1011,1980,508 * @param id * */ public static void renderIntegersField(DetachedCriteria dc, String integersFiledName, Integer id) { if (id == null) { return; } LogicalExpression criteria = buildLogicalExpressionForIntegersField(integersFiledName, id); if (criteria != null) { dc.add(criteria); } } /** * ? * * @param integersFiledName * ??? * @param id * ? * @return ? */ public static LogicalExpression buildLogicalExpressionForIntegersField(String integersFiledName, Integer id) { if (id == null) { return null; } return Restrictions.or(Restrictions.eq(integersFiledName, id + ""), Restrictions.or(Restrictions.like(integersFiledName, id + ",", MatchMode.START), Restrictions.or(Restrictions.like(integersFiledName, "," + id, MatchMode.END), Restrictions.like(integersFiledName, "," + id + ",", MatchMode.ANYWHERE)))); } /** * ? * * @param dc * ? * @param integersFiledName * ??? * @param integerIds * ?? */ public static void renderIntegersField(DetachedCriteria dc, String integersFiledName, String integerIds) { if (isBlank(integerIds)) { return; } Integer[] ids = Strings.toIntegerArray(integerIds.trim(), ","); LogicalExpression criteria = buildLogicalExpressionForIntegersField(integersFiledName, ids[0]); for (int i = 1; i < ids.length; i++) { LogicalExpression newCriteria = buildLogicalExpressionForIntegersField(integersFiledName, ids[i]); criteria = Restrictions.or(criteria, newCriteria); } dc.add(criteria); } }