<commit_before>package de.dvdb.infrastructure.amazon;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import com.amazon.soap.ecs.AWSECommerceService;
import com.amazon.soap.ecs.AWSECommerceServicePortType;
import com.amazon.soap.ecs.BrowseNode;
import com.amazon.soap.ecs.BrowseNodes;
import com.amazon.soap.ecs.Errors;
import com.amazon.soap.ecs.Item;
import com.amazon.soap.ecs.ItemLookup;
import com.amazon.soap.ecs.ItemLookupRequest;
import com.amazon.soap.ecs.ItemLookupResponse;
import com.amazon.soap.ecs.Items;
import com.amazon.soap.ecs.Offer;
import com.amazon.soap.ecs.Request;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;

import de.dvdb.application.ApplicationSettings;
import de.dvdb.domain.model.item.type.AmazonDVDItem;
import de.dvdb.domain.model.pricing.Price;
import de.dvdb.domain.service.AmazonService;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Name("amazonBridge")
@AutoCreate
public class AmazonServiceImpl implements AmazonService, Serializable {

	private static final long serialVersionUID = 965755125849259771L;

	@In
	ApplicationSettings applicationSettings;

	@PersistenceContext(unitName = "dvdb")
	EntityManager entityManager;

	private static Log log = LogFactory.getLog(AmazonServiceImpl.class
			.getName());

	/**
	 * Gets the current Amazon Price for an Amazon item.
	 */
	public Price getCurrentPriceForItem(
			de.dvdb.domain.model.item.type.Item amazonItem) {
		try {
			SignatureHelper sigo = new SignatureHelper(applicationSettings
					.getAmazonSecret());
			String time = sigo.getTimestamp();
			String sig = sigo.sign("ItemLookup" + time);

			AWSECommerceService service = new AWSECommerceService();
			AWSECommerceServicePortType port = service
					.getAWSECommerceServicePort();

			ItemLookup lookup = new ItemLookup();
			ItemLookupRequest request = new ItemLookupRequest();
			request.getResponseGroup().add("Offers");
			request.getResponseGroup().add("SalesRank");
			request.getResponseGroup().add("ItemIds");
			request.getItemId().add(amazonItem.getAsin());
			request.setIdType("ASIN");
			lookup.getRequest().add(request);

			WSBindingProvider bp = (WSBindingProvider) port;
			Header h1 = Headers
					.create(new QName(
							"http://security.amazonaws.com/doc/2007-01-01/",
							"AWSAccessKeyId"), applicationSettings
							.getAmazonAccessKey());
			Header h2 = Headers.create(new QName(
					"http://security.amazonaws.com/doc/2007-01-01/",
					"Timestamp"), time);
			Header h3 = Headers.create(new QName(
					"http://security.amazonaws.com/doc/2007-01-01/",
					"Signature"), sig);
			bp.setOutboundHeaders(h1, h2, h3);

			ItemLookupResponse response = port.itemLookup(lookup);
			List<Items> itemsArray = response.getItems();

			handleErrors(itemsArray.get(0).getRequest());

			List<Item> items = itemsArray.get(0).getItem();

			if (items == null || items.size() != 1 || items.get(0) == null)
				return null;

			Item item = items.get(0);

			if (item.getOffers() == null)
				return null; // no offers for item

			List<Offer> offers = item.getOffers().getOffer(); // get offers
			Offer amaOff = getAmazonItem(offers); // what offers amazon?
			if (amaOff == null
					|| amaOff.getOfferListing() == null
					|| amaOff.getOfferListing().get(0) == null
					|| amaOff.getOfferListing().get(0).getPrice() == null
					|| amaOff.getOfferListing().get(0).getPrice().getAmount() == null)
				return null;

			int priceInCent = amaOff.getOfferListing().get(0).getPrice()
					.getAmount().intValue();

			Double price = new Double(priceInCent) / 100;
			String availability = amaOff.getOfferListing().get(0)
					.getAvailability();
			String productUrl = applicationSettings.getAmazonProduktUrl()
					.replaceAll("#ASIN#", item.getASIN());

			Price p = new Price();
			p.setItem(amazonItem);
			p.setAvailability(availability);
			p.setPrice(price);
			p.setUrl(productUrl);
			return p;

		} catch (Exception e) {
			log.error("Error accessing Amazon Webservice for fetching a price "
					+ e);
		}
		return null;
	}

	/**
	 * Creates a new AmazonItem based on the retrieved item attributes for an
	 * ASIN.
	 */
	public AmazonDVDItem getAmazonDVDItemFull(String asin) {

		try {
			SignatureHelper sigo = new SignatureHelper(applicationSettings
					.getAmazonSecret());
			String time = sigo.getTimestamp();
			String sig = sigo.sign("ItemLookup" + time);
<commit_msg>more fixes<commit_after>package de.dvdb.infrastructure.amazon;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.namespace.QName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import com.amazon.soap.ecs.AWSECommerceService;
import com.amazon.soap.ecs.AWSECommerceServicePortType;
import com.amazon.soap.ecs.BrowseNode;
import com.amazon.soap.ecs.BrowseNodes;
import com.amazon.soap.ecs.Errors;
import com.amazon.soap.ecs.Item;
import com.amazon.soap.ecs.ItemLookup;
import com.amazon.soap.ecs.ItemLookupRequest;
import com.amazon.soap.ecs.ItemLookupResponse;
import com.amazon.soap.ecs.Items;
import com.amazon.soap.ecs.Offer;
import com.amazon.soap.ecs.Request;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;

import de.dvdb.application.ApplicationSettings;
import de.dvdb.domain.model.item.type.AmazonDVDItem;
import de.dvdb.domain.model.pricing.Price;
import de.dvdb.domain.service.AmazonService;

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Name("amazonBridge")
@AutoCreate
public class AmazonServiceImpl implements AmazonService, Serializable {

	private static final long serialVersionUID = 965755125849259771L;

	@In
	ApplicationSettings applicationSettings;

	@PersistenceContext(unitName = "dvdb")
	EntityManager entityManager;

	private static Log log = LogFactory.getLog(AmazonServiceImpl.class
			.getName());

	/**
	 * Gets the current Amazon Price for an Amazon item.
	 */
	public Price getCurrentPriceForItem(
			de.dvdb.domain.model.item.type.Item amazonItem) {
		try {
			SignatureHelper sigo = new SignatureHelper(applicationSettings
					.getAmazonSecret());
			String time = sigo.getTimestamp();
			String sig = sigo.sign("ItemLookup" + time);

			AWSECommerceService service = new AWSECommerceService();
			AWSECommerceServicePortType port = service
					.getAWSECommerceServicePort();

			ItemLookup lookup = new ItemLookup();
			ItemLookupRequest request = new ItemLookupRequest();
			request.getResponseGroup().add("Offers");
			request.getResponseGroup().add("SalesRank");
			request.getResponseGroup().add("ItemIds");
			request.getItemId().add(amazonItem.getAsin());
			request.setIdType("ASIN");
			lookup.getRequest().add(request);

			WSBindingProvider bp = (WSBindingProvider) port;
			Header h1 = Headers
					.create(new QName(
							"http://security.amazonaws.com/doc/2007-01-01/",
							"AWSAccessKeyId"), applicationSettings
							.getAmazonAccessKey());
			Header h2 = Headers.create(new QName(
					"http://security.amazonaws.com/doc/2007-01-01/",
					"Timestamp"), time);
			Header h3 = Headers.create(new QName(
					"http://security.amazonaws.com/doc/2007-01-01/",
					"Signature"), sig);
			bp.setOutboundHeaders(h1, h2, h3);

			ItemLookupResponse response = port.itemLookup(lookup);
			List<Items> itemsArray = response.getItems();

			handleErrors(itemsArray.get(0).getRequest());

			List<Item> items = itemsArray.get(0).getItem();

			if (items == null || items.size() != 1 || items.get(0) == null)
				return null;

			Item item = items.get(0);

			if (item.getOffers() == null)
				return null; // no offers for item

			List<Offer> offers = item.getOffers().getOffer(); // get offers
			Offer amaOff = getAmazonItem(offers); // what offers amazon?
			if (amaOff == null
					|| amaOff.getOfferListing() == null
					|| amaOff.getOfferListing().get(0) == null
					|| amaOff.getOfferListing().get(0).getPrice() == null
					|| amaOff.getOfferListing().get(0).getPrice().getAmount() == null)
				return null;

			int priceInCent = amaOff.getOfferListing().get(0).getPrice()
					.getAmount().intValue();

			Double price = new Double(priceInCent) / 100;
			String availability = amaOff.getOfferListing().get(0)
					.getAvailability();
			String productUrl = applicationSettings.getAmazonProduktUrl()
					.replaceAll("#ASIN#", item.getASIN());

			Price p = new Price();
			p.setItem(amazonItem);
			p.setAvailability(availability);
			p.setPrice(price);
			p.setUrl(productUrl);
			return p;

		} catch (Exception e) {
			log.error("Error accessing Amazon Webservice for fetching a price "
					+ e);
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Creates a new AmazonItem based on the retrieved item attributes for an
	 * ASIN.
	 */
	public AmazonDVDItem getAmazonDVDItemFull(String asin) {

		try {
			SignatureHelper sigo = new SignatureHelper(applicationSettings
					.getAmazonSecret());
			String time = sigo.getTimestamp();
			String sig = sigo.sign("ItemLookup" + time);

{'fraction_non_alphanumeric': 0.1277181934468169, 'fraction_numerical': 0.012004329430286332, 'mean_word_length': 4.444027852169255, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4742783', 'n_tokens_mistral': 3715, 'n_tokens_neox': 3203, 'n_words': 628}