TicketSystem.java 5.03 KB
Newer Older
1
package de.hftstuttgart.unifiedticketing.core;
2

3
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
4
import de.hftstuttgart.unifiedticketing.systems.github.GithubTicketSystem;
5
import de.hftstuttgart.unifiedticketing.systems.gitlab.GitlabTicketSystem;
6
7

import java.util.HashMap;
8
import java.util.Map;
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
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Defines the representation of a ticket system connection<br/>
 * <br/>
 *
 * @param <T> ticket implementation this ticket system uses
 * @param <TS> implementation of this class an instance belongs to
 * @param <TB> ticket builder this ticket system uses
 * @param <F> filter implementation this ticket system uses
 */
public abstract class TicketSystem<T extends Ticket, TS extends TicketSystem, TB extends TicketBuilder, F extends Filter>
{
    private static Logger logger = Logging.getLogger(TicketSystem.class.getName());

    /**
     * Global configuration options for ticket system instances, settable with the method config
     */
    public enum ConfigurationOptions
    {
        RETURN_NULL_ON_ERROR
    }

    protected TicketSystem() { }

    /**
     * creates a ticketsystem from an uri<br/>
     * <br/>
     * a valid uri for this library always starts with "unifiedticketing" followed by a colon.
     * Next is the identifier for the desired ticket system.
     * The available systems and what identifier they need, can be found in the documentation.
     * <br/>
     * example: {@code "unifiedticketing:<systemidentifier>:<system specific things>"}
     *
     * @return ticket system instance
     */
    public static TicketSystem fromUri(String uri)
    {
        Pattern pattern = Pattern.compile("^(unifiedticketing):([a-zA-Z0-9_]+):(.*)$");
        Matcher matcher = pattern.matcher(uri);

        if (!matcher.matches())
        {
            logger.log(Level.SEVERE, "given URI not for unifiedticketing!");
            throw new AssertionException(String.format("URI %s not for unifiedticketing", uri));
        }

        switch (matcher.group(2))
        {
61
62
63
            case "github":
                return GithubTicketSystem.fromUri(matcher.group(3));

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
            case "gitlab":
                return GitlabTicketSystem.fromUri(matcher.group(3));

            default:
                logger.log(Level.SEVERE, String.format(
                    "Unknown system identifier: %s",
                    matcher.group(2)
                ));
                throw new AssertionException(String.format(
                    "no supported system implementation found for %s",
                    matcher.group(2)
                ));

        }

    }

    /**
     * @return builder selection
     */
    public static RegisteredSystems fromBuilder()
    {
        return RegisteredSystems.getInstance();
    }

    // Instance definition =====================================================================//

    public String apiKey;
    public String baseUrl;
    public String password;
    public String username;

96
97
    protected Map<String, String> configuration = new HashMap<>();

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
    /**
     * sets a value configuration
     * @param option configuration to set
     * @param value value to be stored
     * @return this ticket system
     */
    public TS config(ConfigurationOptions option, String value)
    {
        this.configuration.put(option.name(), value);
        return (TS) this;
    }

    /**
     * enables/disables a configuration
     * @param option configuration to set
     * @param value true to enable the setting
     * @return this ticket system
     */
    public TS config(ConfigurationOptions option, boolean value)
    {
        return config(option, String.valueOf(value));
    }

    /**
     * @param option configuration to check
     * @return true if configuration has stored "true"
     */
    public boolean getConfigTrue(ConfigurationOptions option)
    {
        return Boolean.parseBoolean(this.configuration.get(option.name()));
    }

    /**
     * @param option configuration to get stored value
     * @return stored value of given config
     */
    public Object getConfigValue(ConfigurationOptions option)
    {
        return this.configuration.get(option.name());
    }

    /**
     * starts ticket builder for this ticket system
     */
    public abstract TB createTicket();

    /**
     * starts ticket filter for this ticket system
     */
    public abstract F find();

//    public abstract List<T> getAllTickets();

    /**
     * requests a single ticket by it's id
     */
    public abstract T getTicketById(String id);

    public T getTicketById(int id)
    {
        logger.log(Level.FINEST, "transforming id to String");
        return this.getTicketById(String.valueOf(id));
    }

    public abstract boolean hasAssigneeSupport();
    public abstract boolean hasDefaultPagination();
    public abstract boolean hasLabelSupport();
    public abstract boolean hasPaginationSupport();
    public abstract boolean hasReturnNullOnErrorSupport();
//    public static boolean hasUriBuildSupport();
}