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

com.podio.item.map.MappedItemAPI Maven / Gradle / Ivy

There is a newer version: 0.7.9
Show newest version
package com.podio.item.map;

import org.apache.commons.beanutils.ConvertUtils;

import com.podio.APIFactory;
import com.podio.app.Application;
import com.podio.item.ItemsResponse;

public class MappedItemAPI {

	private final APIFactory factory;

	private final Application application;

	private final ItemMap map;

	public MappedItemAPI(APIFactory factory, int appId, Class cls) {
		super();
		this.factory = factory;
		this.application = factory.getAppAPI().getApp(appId);
		this.map = ItemMap.get(application, cls);
	}

	public int insert(T object, boolean silent) {
		return factory.getItemAPI().addItem(application.getId(),
				map.getCreate(object), silent);
	}

	public void update(T object, boolean silent) {
		factory.getItemAPI().updateItem(-1, map.getUpdate(object), silent);
	}

	public T get(Object externalId) {
		String stringExternalId = (String) ConvertUtils.convert(externalId,
				String.class);

		ItemsResponse item = factory.getItemAPI().getItemsByExternalId(
				application.getId(), stringExternalId);
		if (item.getFiltered() == 1) {
			return map.getView(item.getItems().get(0));
		} else if (item.getFiltered() == 0) {
			return null;
		} else {
			throw new RuntimeException(
					"Multiple items found with the external id " + externalId);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy