// Path: restler-core/src/main/java/org/restler/client/CallExecutor.java
// public interface CallExecutor {
// 
//     Object execute(Call call);
// 
// }


import org.restler.client.Call;
import org.restler.client.CallEnhancer;
import org.restler.client.CallExecutor;
import org.restler.client.RestlerException;
import org.restler.spring.data.proxy.ResourceProxy;
import org.restler.spring.data.util.Repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
package org.restler.spring.data.calls;



public class SaveSeveralCallEnhancer extends CustomCallEnhancer<SaveSeveralCallEnhancer.SaveSeveralCall> {
    private final Repositories repositories;

    public SaveSeveralCallEnhancer(Repositories repositories) {
        super(SaveSeveralCall.class);
        this.repositories = repositories;
    }

    @Override
    protected Object enhance(SaveSeveralCall call, CallExecutor callExecutor) {
        Iterable<Object> objectsForSave = call.getObjectsForSave();

        List<Object> result = new ArrayList<>();
        for(Object objectForSave : objectsForSave) {
            Repository repository;

            if(objectForSave instanceof ResourceProxy) {
                repository = repositories.getByResourceClass(((ResourceProxy)objectForSave).getObject().getClass()).orElse(null);
            } else {
                repository = repositories.getByResourceClass(objectForSave.getClass()).orElse(null);
            }

            if (repository == null || !(repository instanceof CrudRepository)) {
                throw new RestlerException("Could not find repository for " + objectForSave);
            }
            result.add(((CrudRepository)repository).save(objectForSave));
        }

// Path: restler-core/src/main/java/org/restler/client/CallExecutor.java
// public interface CallExecutor {
// 
//     Object execute(Call call);
// 
// }


// Path: restler-spring-data-rest/src/main/java/org/restler/spring/data/calls/SaveSeveralCallEnhancer.java
import org.restler.client.Call;
import org.restler.client.CallEnhancer;
import org.restler.client.CallExecutor;
import org.restler.client.RestlerException;
import org.restler.spring.data.proxy.ResourceProxy;
import org.restler.spring.data.util.Repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

package org.restler.spring.data.calls;



public class SaveSeveralCallEnhancer extends CustomCallEnhancer<SaveSeveralCallEnhancer.SaveSeveralCall> {
    private final Repositories repositories;

    public SaveSeveralCallEnhancer(Repositories repositories) {
        super(SaveSeveralCall.class);
        this.repositories = repositories;
    }

    @Override
    protected Object enhance(SaveSeveralCall call, CallExecutor callExecutor) {
        Iterable<Object> objectsForSave = call.getObjectsForSave();

        List<Object> result = new ArrayList<>();
        for(Object objectForSave : objectsForSave) {
            Repository repository;

            if(objectForSave instanceof ResourceProxy) {
                repository = repositories.getByResourceClass(((ResourceProxy)objectForSave).getObject().getClass()).orElse(null);
            } else {
                repository = repositories.getByResourceClass(objectForSave.getClass()).orElse(null);
            }

            if (repository == null || !(repository instanceof CrudRepository)) {
                throw new RestlerException("Could not find repository for " + objectForSave);
            }
            result.add(((CrudRepository)repository).save(objectForSave));
        }

        return result;