Example #1
0
  @SuppressWarnings("unchecked")
  @Override
  public <T> List<? extends T> findToRemove(
      Class<T> mapType, Class<?> resourceType, long resourceId) {
    String type = schemaFactory.getSchemaName(mapType);

    Table<?> table = getTable(mapType);

    Relationship reference = getRelationship(mapType, resourceType);
    TableField<?, Object> removed =
        JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD);
    TableField<?, Object> referenceField =
        JooqUtils.getTableField(metaDataManager, type, reference.getPropertyName());
    TableField<?, Object> state =
        JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.STATE_FIELD);

    if (removed == null || referenceField == null || state == null) {
      throw new IllegalArgumentException(
          "Type [" + mapType + "] is missing required removed, reference, or state column");
    }

    return (List<? extends T>)
        create()
            .selectFrom(table)
            .where(
                referenceField
                    .eq(resourceId)
                    .and(removed.isNull().or(state.eq(CommonStatesConstants.REMOVING))))
            .fetch();
  }
Example #2
0
  protected static org.jooq.Condition listToCondition(TableField<?, Object> field, List<?> list) {
    org.jooq.Condition condition = null;
    for (Object value : list) {
      if (value instanceof Condition) {
        org.jooq.Condition newCondition = toCondition(field, (Condition) value);
        condition = condition == null ? newCondition : condition.and(newCondition);
      } else {
        condition = condition == null ? field.eq(value) : condition.and(field.eq(value));
      }
    }

    return condition;
  }
Example #3
0
 protected static org.jooq.Condition toCondition(TableField<?, Object> field, Condition value) {
   Condition condition = value;
   switch (condition.getConditionType()) {
     case EQ:
       return field.eq(condition.getValue());
     case GT:
       return field.gt(condition.getValue());
     case GTE:
       return field.ge(condition.getValue());
     case IN:
       List<Object> values = condition.getValues();
       if (values.size() == 1) {
         return field.eq(values.get(0));
       } else {
         return field.in(condition.getValues());
       }
     case NOTIN:
       List<Object> vals = condition.getValues();
       if (vals.size() == 1) {
         return field.ne(vals.get(0));
       } else {
         return field.notIn(condition.getValues());
       }
     case LIKE:
       return field.like(condition.getValue().toString());
     case LT:
       return field.lt(condition.getValue());
     case LTE:
       return field.le(condition.getValue());
     case NE:
       return field.ne(condition.getValue());
     case NOTLIKE:
       return field.notLike(condition.getValue().toString());
     case NOTNULL:
       return field.isNotNull();
     case NULL:
       return field.isNull();
     case PREFIX:
       return field.like(condition.getValue() + "%");
     case OR:
       return toCondition(field, condition.getLeft()).or(toCondition(field, condition.getRight()));
     default:
       throw new IllegalArgumentException(
           "Invalid condition type [" + condition.getConditionType() + "]");
   }
 }
Example #4
0
  @SuppressWarnings("unchecked")
  @Override
  public <T> T findNonRemoved(
      Class<T> mapType,
      Class<?> leftResourceType,
      long leftResourceId,
      Class<?> rightResourceType,
      long rightResourceId) {
    String type = schemaFactory.getSchemaName(mapType);

    Table<?> table = getTable(mapType);

    Relationship leftReference = getRelationship(mapType, leftResourceType);
    Relationship rightReference = getRelationship(mapType, rightResourceType);
    TableField<?, Object> removed =
        JooqUtils.getTableField(metaDataManager, type, ObjectMetaDataManager.REMOVED_FIELD);
    TableField<?, Object> leftReferenceField =
        JooqUtils.getTableField(metaDataManager, type, leftReference.getPropertyName());
    TableField<?, Object> rightReferenceField =
        JooqUtils.getTableField(metaDataManager, type, rightReference.getPropertyName());

    if (removed == null || leftReferenceField == null || rightReferenceField == null) {
      throw new IllegalArgumentException(
          "Type [" + mapType + "] is missing required removed or references column");
    }

    return (T)
        create()
            .selectFrom(table)
            .where(
                removed
                    .isNull()
                    .and(leftReferenceField.eq(leftResourceId))
                    .and(rightReferenceField.eq(rightResourceId)))
            .fetchOne();
  }
Example #5
0
  public static org.jooq.Condition toConditions(
      ObjectMetaDataManager metaData, String type, Map<Object, Object> criteria) {
    org.jooq.Condition existingCondition = null;

    for (Map.Entry<Object, Object> entry : criteria.entrySet()) {
      Object value = entry.getValue();
      Object key = entry.getKey();
      TableField<?, Object> field = null;
      if (key == org.jooq.Condition.class) {
        if (!(value instanceof org.jooq.Condition)) {
          throw new IllegalArgumentException(
              "If key is Condition, value must be an instanceof Condition got key ["
                  + key
                  + "] value ["
                  + value
                  + "]");
        }
      } else {
        field = getTableField(metaData, type, key);
        if (field == null) {
          continue;
        }
      }

      org.jooq.Condition newCondition = null;

      if (value instanceof org.jooq.Condition) {
        newCondition = (org.jooq.Condition) value;
      } else if (value instanceof Condition) {
        newCondition = toCondition(field, (Condition) value);
      } else if (value instanceof List) {
        newCondition = listToCondition(field, (List<?>) value);
      } else if (value == null) {
        newCondition = field.isNull();
      } else {
        newCondition = field.eq(value);
      }

      if (existingCondition == null) {
        existingCondition = newCondition;
      } else {
        existingCondition = existingCondition.and(newCondition);
      }
    }

    return existingCondition;
  }
Example #6
0
  @SuppressWarnings("unchecked")
  public static <T extends UpdatableRecord<?>> T findById(
      DSLContext context, Class<T> clz, Object id) {
    if (id == null) return null;

    Table<?> table = getTableFromRecordClass(clz);
    if (table == null) return null;

    UniqueKey<?> key = table.getPrimaryKey();
    if (key == null || key.getFieldsArray().length != 1) return null;

    TableField<?, Object> keyField = (TableField<?, Object>) key.getFieldsArray()[0];

    /* Convert object because we are abusing type safety here */
    Object converted = keyField.getDataType().convert(id);

    return (T) context.selectFrom(table).where(keyField.eq(converted)).fetchOne();
  }