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.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.shredzone.flattr4j.connector.FlattrObject;
026import org.shredzone.flattr4j.exception.FlattrException;
027import org.shredzone.flattr4j.exception.MarshalException;
028
029/**
030 * A {@link Submission} is used for creating new Things.
031 *
032 * @author Richard "Shred" Körber
033 */
034public class Submission implements Serializable {
035    private static final long serialVersionUID = -6684005944290342599L;
036
037    private String url;
038    private String title;
039    private String description;
040    private List<String> tags = new ArrayList<String>();
041    private String category;
042    private String language;
043    private Boolean hidden;
044
045    /**
046     * URL of the Thing.
047     */
048    public String getUrl()                      { return url; }
049    public void setUrl(String url)              { this.url = url; }
050
051    /**
052     * Title of the Thing.
053     */
054    public String getTitle()                    { return title; }
055    public void setTitle(String title)          { this.title = title; }
056
057    /**
058     * A descriptive text about the Thing.
059     */
060    public String getDescription()              { return description; }
061    public void setDescription(String description) { this.description = description; }
062
063    /**
064     * Tags this Thing is tagged with.
065     */
066    public List<String> getTags()               { return tags; }
067    public void setTags(List<String> tags)      { this.tags = tags; }
068    public void addTag(String tag)              { tags.add(tag); }
069
070    /**
071     * The category this Thing belongs to.
072     */
073    public CategoryId getCategory()             {
074        return category != null ? Category.withId(category) : null;
075    }
076    public void setCategory(CategoryId category) { this.category = category.getCategoryId(); }
077
078    /**
079     * The language this Thing is written in.
080     */
081    public LanguageId getLanguage()             {
082        return language != null ? Language.withId(language) : null;
083    }
084    public void setLanguage(LanguageId language) { this.language = language.getLanguageId(); }
085
086    /**
087     * Is the Thing hidden from the public list of Things at Flattr?
088     */
089    public Boolean isHidden()                   { return hidden; }
090    public void setHidden(Boolean hidden)       { this.hidden = hidden; }
091
092    /**
093     * Returns the set of tags as a comma separated list.
094     *
095     * @return tags
096     * @since 2.0
097     */
098    public String getTagsAsString() {
099        StringBuilder sb = new StringBuilder();
100        for (String tag : tags) {
101            if (tag.indexOf(',') >= 0) {
102                throw new MarshalException("tag '" + tag + "' contains invalid character ','");
103            }
104            sb.append(',').append(tag);
105        }
106        if (sb.length() > 0) {
107            sb.deleteCharAt(0);
108        }
109        return sb.toString();
110    }
111
112    /**
113     * Returns the submission as {@link FlattrObject}.
114     *
115     * @return {@link FlattrObject} of this submission.
116     * @since 2.0
117     */
118    public FlattrObject toFlattrObject() throws FlattrException {
119        FlattrObject result = new FlattrObject();
120        result.put("url", url);
121
122        if (hidden != null) {
123            result.put("hidden", hidden);
124        }
125
126        if (title != null) {
127            result.put("title", title);
128        }
129
130        if (description != null) {
131            result.put("description", description);
132        }
133
134        if (category != null) {
135            result.put("category", category);
136        }
137
138        if (language != null) {
139            result.put("language", language);
140        }
141
142        if (tags != null && !tags.isEmpty()) {
143            result.put("tags", getTagsAsString());
144        }
145
146        return result;
147    }
148
149}