summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/tools/AttributeTool.java
blob: 8e0540a36aadd6a76f99680819fe39c387ba7b96 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright 2014 Jesse Morgan
 */

package com.p4square.grow.tools;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.restlet.Client;
import org.restlet.Context;
import org.restlet.data.Protocol;

import com.p4square.grow.config.Config;
import com.p4square.f1oauth.Attribute;
import com.p4square.f1oauth.F1Access;
import com.p4square.f1oauth.F1API;
import com.p4square.f1oauth.F1Exception;
import com.p4square.restlet.oauth.OAuthUser;

/**
 * Tool for manipulating F1 Attributes.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class AttributeTool {

    private static Config mConfig;
    private static F1API mF1API;

    public static void usage() {
        System.out.println("java com.p4square.grow.tools.AttributeTool <command>...\n");
        System.out.println("Commands:");
        System.out.println("\t--domain <domain>                          Set config domain");
        System.out.println("\t--dev                                      Set config domain to dev");
        System.out.println("\t--config <file>                            Merge in config file");
        System.out.println("\t--list                                     List all attributes");
        System.out.println("\t--assign <userId> <attribute> <comment>    Assign an attribute");
        System.out.println("\t--getall <userId>                          Get an attribute");
        System.out.println("\t--get <userId> <attribute>                 Get an attribute");
    }

    public static void main(String... args) {
        if (args.length == 0) {
            usage();
            System.exit(1);
        }

        mConfig = new Config();

        try {
            mConfig.updateConfig(AttributeTool.class.getResourceAsStream("/grow.properties"));

            int offset = 0;
            while (offset < args.length) {
                if ("--domain".equals(args[offset])) {
                    mConfig.setDomain(args[offset + 1]);
                    mF1API = null;
                    offset += 2;

                } else if ("--dev".equals(args[offset])) {
                    mConfig.setDomain("dev");
                    mF1API = null;
                    offset += 1;

                } else if ("--config".equals(args[offset])) {
                    mConfig.updateConfig(args[offset + 1]);
                    mF1API = null;
                    offset += 2;

                } else if ("--list".equals(args[offset])) {
                    offset = list(args, ++offset);

                } else if ("--assign".equals(args[offset])) {
                    offset = assign(args, ++offset);

                } else if ("--getall".equals(args[offset])) {
                    offset = getall(args, ++offset);

                } else if ("--get".equals(args[offset])) {
                    offset = get(args, ++offset);

                } else {
                    throw new IllegalArgumentException("Unknown command " + args[offset]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
    }

    private static F1API getF1API() throws Exception {
        if (mF1API == null) {
            Context context = new Context();
            Client client = new Client(context, Arrays.asList(Protocol.HTTP, Protocol.HTTPS));
            context.setClientDispatcher(client);

            F1Access f1Access = new F1Access(context,
                    mConfig.getString("f1ConsumerKey"),
                    mConfig.getString("f1ConsumerSecret"),
                    mConfig.getString("f1BaseUrl"),
                    mConfig.getString("f1ChurchCode"),
                    F1Access.UserType.WEBLINK);

            // Gather Username and Password
            String username = System.console().readLine("F1 Username: ");
            char[] password = System.console().readPassword("F1 Password: ");

            OAuthUser user = f1Access.getAccessToken(username, new String(password));
            Arrays.fill(password, ' '); // Lost cause, but I'll still try.

            mF1API = f1Access.getAuthenticatedApi(user);
        }

        return mF1API;
    }

    private static int list(String[] args, int offset) throws Exception {
        final F1API f1 = getF1API();

        final Map<String, String> attributes = f1.getAttributeList();
        System.out.printf("%7s %s\n", "ID", "Name");
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            System.out.printf("%7s %s\n", entry.getValue(), entry.getKey());
        }

        return offset;
    }

    private static int assign(String[] args, int offset) throws Exception {
        final String userId = args[offset++];
        final String attributeName = args[offset++];
        final String comment = args[offset++];

        final F1API f1 = getF1API();

        Attribute attribute = new Attribute(attributeName);
        attribute.setStartDate(new Date());
        attribute.setComment(comment);

        if (f1.addAttribute(userId, attribute)) {
            System.out.println("Added attribute " + attributeName + " for " + userId);
        } else {
            System.out.println("Failed to add attribute " + attributeName + " for " + userId);
        }

        return offset;
    }

    private static int getall(String[] args, int offset) throws Exception {
        final String userId = args[offset++];

        doGet(userId, null);

        return offset;
    }

    private static int get(String[] args, int offset) throws Exception {
        final String userId = args[offset++];
        final String attributeName = args[offset++];

        doGet(userId, attributeName);

        return offset;
    }

    private static void doGet(final String userId, final String attributeName) throws Exception {
        final F1API f1 = getF1API();

        List<Attribute> attributes = f1.getAttribute(userId, attributeName);
        for (Attribute attribute : attributes) {
            System.out.printf("%s %s %s %s %s\n%s\n\n",
                    userId,
                    attribute.getAttributeName(),
                    attribute.getId(),
                    attribute.getStartDate(),
                    attribute.getEndDate(),
                    attribute.getComment());
        }
    }
}