summaryrefslogtreecommitdiff
path: root/src/com/p4square/f1oauth/F1ProgressReporter.java
blob: 8382020b8ed6273e9e23fb94eacabeac0d5cd1aa (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
package com.p4square.f1oauth;

import com.p4square.grow.frontend.ProgressReporter;
import org.apache.log4j.Logger;
import org.restlet.security.User;

import java.util.Date;

/**
 * A ProgressReporter implementation to record progress in F1.
 */
public class F1ProgressReporter implements ProgressReporter {

    private static final Logger LOG = Logger.getLogger(F1ProgressReporter.class);

    private F1Access mF1Access;

    public F1ProgressReporter(final F1Access f1access) {
        mF1Access = f1access;
    }

    @Override
    public void reportAssessmentComplete(final User user, final String level, final Date date, final String results) {
        String attributeName = "Assessment Complete - " + level;
        Attribute attribute = new Attribute(attributeName);
        attribute.setStartDate(date);
        attribute.setComment(results);
        addAttribute(user, attribute);
    }

    @Override
    public void reportChapterComplete(final User user, final String chapter, final Date date) {
        final String attributeName = "Training Complete - " + chapter;
        final Attribute attribute = new Attribute(attributeName);
        attribute.setStartDate(date);
        addAttribute(user, attribute);
    }

    private void addAttribute(final User user, final Attribute attribute) {
        if (!(user instanceof F1User)) {
            throw new IllegalArgumentException("User must be an F1User, but got " + user.getClass().getName());
        }

        try {
            final F1User f1User = (F1User) user;
            final F1API f1 = mF1Access.getAuthenticatedApi(f1User);

            if (!f1.addAttribute(user.getIdentifier(), attribute)) {
                LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute "
                          + attribute.getAttributeName());
            }
        } catch (Exception e) {
            LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute "
                      + attribute.getAttributeName(), e);
        }
    }
}