001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.model;
020
021import java.util.Date;
022
023import org.shredzone.flattr4j.connector.FlattrObject;
024
025/**
026 * A Flattr that was made to a thing.
027 * <p>
028 * This class is not threadsafe.
029 *
030 * @author Richard "Shred" Körber
031 * @since 2.0
032 */
033public class Flattr extends Resource implements ThingId, UserId {
034    private static final long serialVersionUID = 8013428651001009374L;
035
036    private transient Thing thing = null;
037    private transient User user = null;
038
039    public Flattr(FlattrObject data) {
040        super(data);
041    }
042
043    /**
044     * The thing that was flattred.
045     * <p>
046     * All properties are only available when the service was set to full mode!
047     */
048    public Thing getThing() {
049        if (thing == null) {
050            thing = new Thing(data.getFlattrObject("thing"));
051        }
052        return thing;
053    }
054
055    public Date getCreated() {
056        return data.getDate("created_at");
057    }
058
059    @Override
060    public String getThingId() {
061        return getThing().getThingId();
062    }
063
064    /**
065     * UserID of the user who flattred the thing.
066     */
067    @Override
068    public String getUserId() {
069        return data.getSubString("owner", "username");
070    }
071
072    /**
073     * User who flattred the thing.
074     * <p>
075     * All properties are only available when the service was set to full mode!
076     *
077     * @since 2.2
078     */
079    public User getUser() {
080        if (user == null) {
081            user = new User(data.getFlattrObject("owner"));
082        }
083        return user;
084    }
085
086}