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

com.mysema.query.collections.CollUpdateClause Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 * 
 * 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.mysema.query.collections;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mysema.query.dml.UpdateClause;
import com.mysema.query.types.Expression;
import com.mysema.query.types.Path;
import com.mysema.query.types.Predicate;
import com.mysema.util.BeanMap;

/**
 * CollUpdateClause is an implementation of the UpdateClause interface for Querydsl Collections
 *
 * @author tiwe
 *
 * @param 
 */
public class CollUpdateClause implements UpdateClause> {

    private final Path expr;

    private final Map, Object> paths = new HashMap, Object>();

    private final CollQuery query;

    public CollUpdateClause(QueryEngine qe, Path expr, Iterable col) {
        this.query = new CollQuery(qe).from(expr, col);
        this.expr = expr;
    }

    public CollUpdateClause(Path expr, Iterable col) {
        this(DefaultQueryEngine.getDefault(), expr, col);
    }

    @Override
    public long execute() {
        int rv = 0;
        for (T match : query.list(expr)) {
            BeanMap beanMap = new BeanMap(match);
            for (Map.Entry,Object> entry : paths.entrySet()) {
                // TODO : support deep updates as well
                String propertyName = entry.getKey().getMetadata().getName();
                beanMap.put(propertyName, entry.getValue());
            }
            rv++;
        }
        return rv;
    }

    @Override
    public  CollUpdateClause set(Path path, U value) {
        paths.put(path, value);
        return this;
    }
    

    @Override
    public  CollUpdateClause set(Path path, Expression expression) {
        // TODO : implement
        throw new UnsupportedOperationException();
    }
    
    @Override
    public  CollUpdateClause setNull(Path path) {
        paths.put(path, null);
        return this;
    }

    @Override
    public CollUpdateClause set(List> p, List v) {
        for (int i = 0; i < p.size(); i++) {
            paths.put(p.get(i), v.get(i));
        }
        return this;
    }

    @Override
    public CollUpdateClause where(Predicate... o) {
        query.where(o);
        return this;
    }
    
    @Override
    public String toString() {
        return "update " + query;
    }

    @Override
    public boolean isEmpty() {
        return paths.isEmpty();
    }


}