summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java
blob: dd62401f09a3ea51fe6bf20aac478fbc10f4c695 (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
package com.p4square.grow.ccb;

import com.codahale.metrics.MetricRegistry;
import com.p4square.ccbapi.CCBAPI;
import com.p4square.ccbapi.CCBAPIClient;
import com.p4square.grow.config.Config;
import com.p4square.grow.frontend.IntegrationDriver;
import com.p4square.grow.frontend.ProgressReporter;
import org.apache.log4j.Logger;
import org.restlet.Context;
import org.restlet.security.Verifier;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * The ChurchCommunityBuilderIntegrationDriver is used to integrate Grow with Church Community Builder.
 */
public class ChurchCommunityBuilderIntegrationDriver implements IntegrationDriver {

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

    private final Context mContext;
    private final MetricRegistry mMetricRegistry;
    private final Config mConfig;

    private final CCBAPI mAPI;

    private final CCBProgressReporter mProgressReporter;

    public ChurchCommunityBuilderIntegrationDriver(final Context context) {
        mContext = context;
        mConfig = (Config) context.getAttributes().get("com.p4square.grow.config");
        mMetricRegistry = (MetricRegistry) context.getAttributes().get("com.p4square.grow.metrics");

        try {
            CCBAPI api = new CCBAPIClient(new URI(mConfig.getString("CCBAPIURL", "")),
                                          mConfig.getString("CCBAPIUser", ""),
                                          mConfig.getString("CCBAPIPassword", ""));

            if (mMetricRegistry != null) {
                api = new MonitoredCCBAPI(api, mMetricRegistry);
            }

            mAPI = api;

            final CustomFieldCache cache = new CustomFieldCache(mAPI);
            mProgressReporter = new CCBProgressReporter(mAPI, cache);

        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @return A CCB API client.
     */
    public CCBAPI getAPI() {
        return mAPI;
    }

    @Override
    public Verifier newUserAuthenticationVerifier() {
        return new CCBUserVerifier(mAPI);
    }

    @Override
    public ProgressReporter getProgressReporter() {
        return mProgressReporter;
    }

    @Override
    public boolean doHealthCheck() {
        try {
            // Try something benign, like getting the custom field labels,
            // to verify that we can talk to CCB.
            mAPI.getCustomFieldLabels();
            return true;

        } catch (IOException e) {
            LOG.warn("CCB Health Check Failed: " + e.getMessage(), e);
            return false;
        }
    }
}