All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencastproject.assetmanager.impl.query.DeleteQueryContribution Maven / Gradle / Ivy

There is a newer version: 16.7
Show newest version
/**
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community 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://opensource.org/licenses/ecl2.txt
 *
 * 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.opencastproject.assetmanager.impl.query;

import org.opencastproject.assetmanager.impl.persistence.QPropertyDto;
import org.opencastproject.assetmanager.impl.persistence.QSnapshotDto;

import com.entwinemedia.fn.Fn;
import com.entwinemedia.fn.Stream;
import com.entwinemedia.fn.data.Opt;
import com.mysema.query.types.EntityPath;
import com.mysema.query.types.expr.BooleanExpression;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

/**
 * Collect contributions to a JPA query.
 * Each of the builder methods creates a new instance.
 */
@ParametersAreNonnullByDefault
public final class DeleteQueryContribution {
  // CHECKSTYLE:OFF
  final Stream> from;

  /**
   * Where clause constructor function.
   * (EntityPath from) -> (BooleanExpression | null)
   */
  final Fn, BooleanExpression> where;

  final Opt targetPredicate;

  final String name;

  private static final Fn, BooleanExpression> NO_WHERE = new Fn, BooleanExpression>() {
    @Override public BooleanExpression apply(EntityPath entityPathBase) {
      return null;
    }
  };
  // CHECKSTYLE:ON

  public DeleteQueryContribution(
          Stream> from,
          Fn, BooleanExpression> where,
          Opt targetPredicate,
          String name) {
    this.from = from;
    this.where = where;
    this.targetPredicate = targetPredicate;
    this.name = name;
  }

  /**
   * Create an empty contribution.
   */
  public static DeleteQueryContribution mk() {
    return new DeleteQueryContribution(Stream.>empty(), NO_WHERE, Opt.none(), "");
  }

  /**
   * Create a copy of contribution c.
   */
  public static DeleteQueryContribution mk(DeleteQueryContribution c) {
    return new DeleteQueryContribution(c.from, c.where, c.targetPredicate, c.name);
  }

  DeleteQueryContribution from(Stream> from) {
    return new DeleteQueryContribution((Stream>) from, where, targetPredicate, name);
  }

  DeleteQueryContribution targetPredicate(@Nullable BooleanExpression targetPredicate) {
    return new DeleteQueryContribution(from, where, Opt.nul(targetPredicate), name);
  }

  DeleteQueryContribution targetPredicate(Opt targetPredicate) {
    return new DeleteQueryContribution(from, where, targetPredicate, name);
  }

  DeleteQueryContribution where(Fn, BooleanExpression> where) {
    return new DeleteQueryContribution(from, where, targetPredicate, name);
  }

  DeleteQueryContribution where(final Where where) {
    return new DeleteQueryContribution(from, toFn(where), targetPredicate, name);
  }

  DeleteQueryContribution where(@Nullable final BooleanExpression where) {
    final Fn, BooleanExpression> w = new Fn, BooleanExpression>() {
      @Override public BooleanExpression apply(EntityPath entityPath) {
        return where;
      }
    };
    return new DeleteQueryContribution(from, w, targetPredicate, name);
  }

  DeleteQueryContribution name(String name) {
    return new DeleteQueryContribution(from, where, targetPredicate, name);
  }

  /* -- */

  static Fn, BooleanExpression> toFn(final Where where) {
    return new Fn, BooleanExpression>() {
      @Override public BooleanExpression apply(EntityPath from) {
        if (from instanceof QSnapshotDto) {
          return where.fromSnapshot((QSnapshotDto) from);
        } else if (from instanceof QPropertyDto) {
          return where.fromProperty((QPropertyDto) from);
        } else {
          throw new RuntimeException("BUG");
        }
      }
    };
  }

  /* -- */

  @ParametersAreNonnullByDefault
  interface Where {
    BooleanExpression fromSnapshot(QSnapshotDto e);
    BooleanExpression fromProperty(QPropertyDto p);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy