void
TestAmazonMetaFactory::testCreateTrack()
{
    QStringList list, list2;
    list << "id" << "name" << "trackNumber" << "length" << "playableUrl" << "albumId" << "artistId" << "price" << "ASIN";
    list2 << "23" << "name" << "5" << "300" << "http://www.amazon.de/gp/dmusic/get_sample_url.html?ASIN=B007NV28OK" << "42" << "12" << "99" << "B007NV28OK";

    AmazonMetaFactory factory( "Amazontest" );
    Meta::TrackPtr trackPtr = factory.createTrack( list );
    Meta::TrackPtr trackPtr2 = factory.createTrack( list2 );

    QVERIFY( trackPtr );
    QVERIFY( trackPtr2 );

    Meta::AmazonTrack* amazonTrack = dynamic_cast<Meta::AmazonTrack*>( trackPtr.data() );
    Meta::AmazonTrack* amazonTrack2 = dynamic_cast<Meta::AmazonTrack*>( trackPtr2.data() );

    QVERIFY( amazonTrack );
    QVERIFY( amazonTrack2 );

    QCOMPARE( amazonTrack->id(), 0 );
    QCOMPARE( amazonTrack->name(), QString( "name" ) );
    QCOMPARE( amazonTrack->trackNumber(), 0 );
    QCOMPARE( amazonTrack->length(), (qint64)0 );
    QCOMPARE( amazonTrack->playableUrl(), KUrl( "playableUrl" ) );
    QCOMPARE( amazonTrack->albumId(), 0 );
    QCOMPARE( amazonTrack->artistId(), 0 );
    QCOMPARE( amazonTrack->price(), QString( "price" ) );
    QCOMPARE( amazonTrack->asin(), QString( "ASIN" ) );

    QCOMPARE( amazonTrack2->id(), 23 );
    QCOMPARE( amazonTrack2->trackNumber(), 5 );
    QCOMPARE( amazonTrack2->length(), (qint64)300 );
    QCOMPARE( amazonTrack2->playableUrl(), KUrl( "http://www.amazon.de/gp/dmusic/get_sample_url.html?ASIN=B007NV28OK" ) );
    QCOMPARE( amazonTrack2->albumId(), 42 );
    QCOMPARE( amazonTrack2->artistId(), 12 );
    QCOMPARE( amazonTrack2->price(), QString( "99" ) );
    QCOMPARE( amazonTrack2->asin(), QString( "B007NV28OK" ) );
}
void
AmazonParser::run()
{
    DEBUG_BLOCK
    debug() << "AmazonParser::run: " << m_tempFileName;

    m_responseDocument = new QDomDocument;

    QFile responseFile( m_tempFileName );
    if( !responseFile.open( QIODevice::ReadOnly ) )
    {
        debug() << "ERROR opening temp file";
        emit( failed( this ) );
        QFile::remove( m_tempFileName );
        return;
    }

    QString errorMsg;
    int errorLine;
    int errorColumn;

    if( !m_responseDocument->setContent( &responseFile, false, &errorMsg, &errorLine, &errorColumn ) ) // parse error
    {
        debug() << m_responseDocument->toString();
        debug() << "Parse ERROR";
        debug() << "Message:" << errorMsg;
        debug() << "Line:" << errorLine;
        debug() << "Column:" << errorColumn;
        m_success = false;
        // let's keep the temp file in case of an error
        //QFile::remove( m_tempFileName );
        return;
    }

    QString compilation;
    QString trackAsin, albumAsin, albumPrice;
    QString artist, albumTitle, songTitle;
    QString price, imgUrl, playableUrl;
    QString artistID, albumID, trackID;
    QStringList results;
    QString description( "TODO: I am a description. Where do I show up?" );

    QDomNodeList albumItemsList = m_responseDocument->documentElement().firstChildElement( QLatin1String( "albums" ) ).elementsByTagName( QString( "item" ) );
    QDomNodeList trackItemsList = m_responseDocument->documentElement().firstChildElement( QLatin1String( "tracks" ) ).elementsByTagName( QString( "item" ) );

    m_collection->acquireWriteLock(); // locks for reading AND writing
    // we have a new results page, so we can clear the old one
    m_collection->clear();

    // album parsing
    for( int i = 0; i < albumItemsList.size(); i++ )
    {
        albumAsin   = albumItemsList.at( i ).firstChildElement( QLatin1String( "asin" ) ).firstChild().nodeValue();
        artist      = albumItemsList.at( i ).firstChildElement( QLatin1String( "artist" ) ).firstChild().nodeValue();
        albumTitle  = albumItemsList.at( i ).firstChildElement( QLatin1String( "album" ) ).firstChild().nodeValue();
        albumPrice  = albumItemsList.at( i ).firstChildElement( QLatin1String( "price" ) ).firstChild().nodeValue();
        compilation = albumItemsList.at( i ).firstChildElement( QLatin1String( "iscompilation" ) ).firstChild().nodeValue();
        imgUrl      = albumItemsList.at( i ).firstChildElement( QLatin1String( "img" ) ).firstChild().nodeValue();

        debug() << albumAsin << artist << albumTitle << albumPrice << imgUrl;

        artistID.setNum( addArtistToCollection( artist, description ) );
        addAlbumToCollection( albumTitle, description, artistID, albumPrice, imgUrl, albumAsin, compilation == QLatin1String( "true" ) );
    }

    // track parsing
    albumPrice.clear(); // maybe we get that with tracks in future API revisions, but not now

    for( int i = 0; i < trackItemsList.size(); i++ )
    {
        albumAsin   = trackItemsList.at( i ).firstChildElement( QLatin1String( "albumasin" ) ).firstChild().nodeValue();
        artist      = trackItemsList.at( i ).firstChildElement( QLatin1String( "artist" ) ).firstChild().nodeValue();
        albumTitle  = trackItemsList.at( i ).firstChildElement( QLatin1String( "album" ) ).firstChild().nodeValue();
        compilation = trackItemsList.at( i ).firstChildElement( QLatin1String( "iscompilation" ) ).firstChild().nodeValue();
        imgUrl      = trackItemsList.at( i ).firstChildElement( QLatin1String( "img" ) ).firstChild().nodeValue();
        price       = trackItemsList.at( i ).firstChildElement( QLatin1String( "price" ) ).firstChild().nodeValue();
        songTitle   = trackItemsList.at( i ).firstChildElement( QLatin1String( "title" ) ).firstChild().nodeValue();
        trackAsin   = trackItemsList.at( i ).firstChildElement( QLatin1String( "asin" ) ).firstChild().nodeValue();

        // first we make sure the artist is in the collection and get its id
        artistID.setNum( addArtistToCollection( artist, description ) );

        // same for the album
        albumID.setNum( addAlbumToCollection( albumTitle, description, artistID, albumPrice, imgUrl, albumAsin, compilation == QLatin1String( "true" ) ) );

        // now we can be sure that artist and album of this track are in the collection and we have their IDs
        // id, name, tracknumber, length, Url, albumId, artistID
        trackID.setNum( m_collection->trackIDMap().size() + 1 );
        playableUrl = "http://www.amazon." + AmazonConfig::instance()->country() + "/gp/dmusic/get_sample_url.html?ASIN=" + trackAsin;
        results << trackID << songTitle << "1" << "30000" << playableUrl << albumID << artistID << price << trackAsin;

        Meta::TrackPtr trackPtr = m_factory->createTrack( results );

        if( trackPtr )
        {
            dynamic_cast<Meta::AmazonTrack*>( trackPtr.data() )->setAlbumPtr( m_collection->albumById( albumID.toInt() ) );
            dynamic_cast<Meta::AmazonTrack*>( trackPtr.data() )->setArtist( m_collection->artistById( artistID.toInt() ) );
        }

        m_collection->addTrack( trackPtr );
        m_collection->trackIDMap().insert( trackAsin, trackID.toInt() );
        results.clear();
    }

    m_collection->releaseLock();
    m_collection->emitUpdated();
    QFile::remove( m_tempFileName );
    responseFile.close();
    // ThreadWeaver::Job automatically emits the done( this ) signal
}