001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2014 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.async.thing;
020
021import org.shredzone.flattr4j.FlattrService;
022import org.shredzone.flattr4j.async.AbstractFlattrCallable;
023import org.shredzone.flattr4j.async.FlattrCallable;
024import org.shredzone.flattr4j.model.AutoSubmission;
025import org.shredzone.flattr4j.model.MiniThing;
026import org.shredzone.flattr4j.model.ThingId;
027
028/**
029 * {@link FlattrCallable} for invoking {@link FlattrService#flattr(ThingId)},
030 * {@link FlattrService#flattr(String)} or {@link FlattrService#flattr(AutoSubmission)}
031 *
032 * @author Iulius Gutberlet
033 * @author Richard "Shred" Körber
034 */
035public class FlattrMethod extends AbstractFlattrCallable<MiniThing> {
036
037    private ThingId thingId;
038    private String url;
039    private AutoSubmission submission;
040
041    public FlattrMethod() {
042        // default constructor
043    }
044
045    public FlattrMethod(ThingId thingId) {
046        setThingId(thingId);
047    }
048
049    public FlattrMethod(String url) {
050        setUrl(url);
051    }
052
053    public FlattrMethod(AutoSubmission submission) {
054        setAutoSubmission(submission);
055    }
056
057    public ThingId getThingId()                 { return thingId; }
058    public void setThingId(ThingId thingId) {
059        this.thingId = thingId;
060        this.url = null;
061        this.submission = null;
062    }
063
064    public String getUrl()                      { return url; }
065    public void setUrl(String url) {
066        this.url = url;
067        this.submission = null;
068        this.thingId = null;
069    }
070
071    public AutoSubmission getAutoSubmission()   { return submission; }
072    public void setAutoSubmission(AutoSubmission submission) {
073        this.submission = submission;
074        this.thingId = null;
075        this.url = null;
076    }
077
078    @Override
079    public MiniThing call(FlattrService service) throws Exception {
080        if (thingId != null) {
081            return service.flattr(thingId);
082        } else if (url != null) {
083            return service.flattr(url);
084        } else {
085            return service.flattr(submission);
086        }
087    }
088
089}