001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2012 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 *
019 */
020package org.shredzone.flattr4j.model;
021
022import java.text.ParseException;
023import java.text.SimpleDateFormat;
024import java.util.Date;
025
026import org.shredzone.flattr4j.connector.FlattrObject;
027import org.shredzone.flattr4j.connector.impl.Logger;
028import org.shredzone.flattr4j.exception.MarshalException;
029
030/**
031 * Represents a single activity.
032 * <p>
033 * Handling Activity Streams is beyond the scope of this library. However, this class
034 * gives basic access to the Activity Stream item.
035 *
036 * @see <a href="http://activitystrea.ms/specs/json/1.0/">Activity Streams Specs</a>
037 * @author Richard "Shred" Körber
038 * @since 2.0
039 */
040public class Activity extends Resource {
041    private static final Logger LOG = new Logger("flattr4j", Activity.class.getName());
042    private static final long serialVersionUID = -7610676384296279814L;
043
044    /**
045     * Available activity types.
046     */
047    public static enum Type {
048        OUTGOING, INCOMING;
049    }
050
051    public Activity(FlattrObject data) {
052        super(data);
053    }
054
055    /**
056     * Returns the activity publication date.
057     */
058    public Date getPublished() {
059        try {
060            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
061            String pub = data.get("published");
062            return fmt.parse(pub.replaceAll("(\\d\\d):(\\d\\d)$", "$1$2"));
063        } catch (ParseException e) {
064            return null;
065        }
066    }
067
068    /**
069     * Returns the activity title.
070     */
071    public String getTitle() {
072        return data.get("title");
073    }
074
075    /**
076     * Returns the activity verb.
077     */
078    public String getVerb() {
079        return data.get("verb");
080    }
081
082    /**
083     * Returns a unique activity ID.
084     */
085    public String getActivityId() {
086        return data.get("id");
087    }
088
089    /**
090     * Gets a property of the "Actor" object.
091     *
092     * @param key
093     *            property name (e.g. "objectType", "url", "displayName")
094     * @return Property value, or {@code null} if there is no such property
095     * @see <a href="http://activitystrea.ms/specs/json/1.0/#object">Activity Stream
096     *      Objects</a>
097     */
098    public String getActor(String key) {
099        try {
100            return data.getSubString("actor", key);
101        } catch (MarshalException ex) {
102            LOG.debug("actor", ex);
103            return null;
104        }
105    }
106
107    /**
108     * Gets a property of the "Object" object.
109     *
110     * @param key
111     *            property name (e.g. "objectType", "url", "displayName")
112     * @return Property value, or {@code null} if there is no such property
113     * @see <a href="http://activitystrea.ms/specs/json/1.0/#object">Activity Stream
114     *      Objects</a>
115     */
116    public String getObject(String key) {
117        try {
118            return data.getSubString("object", key);
119        } catch (MarshalException ex) {
120            LOG.debug("object", ex);
121            return null;
122        }
123    }
124
125}