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

it.netgrid.woocommerce.jersey.bulk.TemplateBulkService Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package it.netgrid.woocommerce.jersey.bulk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;

import it.netgrid.woocommerce.CrudObject;
import it.netgrid.woocommerce.jersey.FilterQuery;
import it.netgrid.woocommerce.model.BatchOperation;
import it.netgrid.woocommerce.model.response.CountResponse;
import it.netgrid.woocommerce.BulkService;

public abstract class TemplateBulkService, ID, C, R, S> implements BulkService {
	
	public static final String FILTER_QUERY_FORMAT = "filter[%s]";
	
	private final WebTarget target;
	
	protected TemplateBulkService(WebTarget target) {
		this.target = target;
	}
	
	private List getUpdatedRows(BatchOperation ba) {
		List result = new ArrayList();
		List create, update, delete;
		create = ba.getCreate();
		update = ba.getUpdate();
		delete = ba.getDelete();
		if(create != null) result.addAll(create);
		if(update != null) result.addAll(update);
		if(delete != null) result.addAll(delete);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List write(List items, C context) {
		Builder builder = this.initBuilder(this.getWritePath(context), null, null);
		Entity > request = Entity.entity(new BatchOperation(null,items,null),MediaType.APPLICATION_JSON_TYPE);
		BatchOperation result = builder.post( request, new BatchOperation().getClass() );
		return getUpdatedRows(result);
	}
	
	@Override
	public List read(C context) {
		return this.read(context, null, null);
	}

	@Override
	public List read(C context, FilterQuery filter, Map queryParams) {
		Builder builder = this.initBuilder(this.getReadPath(context), filter, queryParams);
		S result = builder.get(this.getResponseClass());
		return this.getResult(result);
	}

	@Override
	public Integer count(C context, FilterQuery filter, Map queryParams) {
		Builder builder = this.initBuilder(this.getReadPath(context), filter, queryParams);
		CountResponse result = builder.get(CountResponse.class);
		return result.getCount();
	}
	
	public Builder initBuilder(String path, FilterQuery filter, Map queryParams) {
		WebTarget target = this.target;
		if(path != null && !path.trim().equals("")) {
			target = target.path(path);
		}
		
		if(filter != null) {
			Map flat = filter.toFlatMap();
			for(String param : flat.keySet()) {
				target = target.queryParam(param, flat.get(param));
			}
		}
		
		// handle queryParams
		if(queryParams != null) {
			for(String param : queryParams.keySet()) {
				target = target.queryParam(param, queryParams.get(param));
			}
		}
		
		return target.request(MediaType.APPLICATION_JSON_TYPE);
	}
	
	public Entity getRequestEntity(List object) {
		R request = this.getWriteRequest(object);
		return Entity.entity(request,MediaType.APPLICATION_JSON_TYPE);
	}
	
	public abstract String getWritePath(C context);
	public abstract String getReadPath(C context);
	public abstract String getCountPath(C context);
	
	public abstract Class getResponseClass();

	public abstract R getWriteRequest(List items);
	public abstract List getResult(S network);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy