summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/frontend/FeedResource.java
blob: 13d0fa05a4a99cc1e1ca4856efd5cfa2bf49b92a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2014 Jesse Morgan
 */

package com.p4square.grow.frontend;

import java.io.IOException;

import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.resource.ServerResource;

import org.apache.log4j.Logger;

import com.p4square.grow.config.Config;
import com.p4square.grow.model.Message;
import com.p4square.grow.model.UserRecord;

/**
 * This resource handles user interactions with the feed.
 */
public class FeedResource extends ServerResource {
    private static final Logger LOG = Logger.getLogger(FeedResource.class);

    private Config mConfig;

    private FeedData mFeedData;

    // Fields pertaining to this request.
    protected String mTopic;
    protected String mThread;

    @Override
    public void doInit() {
        super.doInit();

        GrowFrontend growFrontend = (GrowFrontend) getApplication();
        mConfig = growFrontend.getConfig();

        mFeedData = new FeedData(getContext(), mConfig);

        mTopic = getAttribute("topic");
        if (mTopic != null) {
            mTopic = mTopic.trim();
        }

        mThread = getAttribute("thread");
        if (mThread != null) {
            mThread = mThread.trim();
        }
    }

    /**
     * Create a new MessageThread.
     */
    @Override
    protected Representation post(Representation entity) {
        try {
            if (mTopic == null || mTopic.length() == 0 || !FeedData.TOPICS.contains(mTopic)) {
                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
                return ErrorPage.NOT_FOUND;
            }

            Form form = new Form(entity);

            String question = form.getFirstValue("question");

            Message message = new Message();
            message.setMessage(question);

            UserRecord user = new UserRecord(getRequest().getClientInfo().getUser());
            message.setAuthor(user);

            if (mThread != null && mThread.length() != 0) {
                // Post a response
                mFeedData.createResponse(mTopic, mThread, message);

            } else {
                // Post a new thread
                mFeedData.createThread(mTopic, message);
            }

            /*
             * Can't trust the referrer, so we'll send them to the
             * appropriate part of the training page
             * TODO: This could be better done.
             */
            String nextPage = mConfig.getString("dynamicRoot", "");
            nextPage += "/account/training/" + mTopic;
            getResponse().redirectSeeOther(nextPage);
            return null;

        } catch (IOException e) {
            LOG.fatal("Could not save message: " + e.getMessage(), e);
            setStatus(Status.SERVER_ERROR_INTERNAL);
            return ErrorPage.BACKEND_ERROR;

        }
    }
}