package de.hftstuttgart.unifiedticketing.core;
import de.hftstuttgart.unifiedticketing.exceptions.AssertionException;
import de.hftstuttgart.unifiedticketing.systems.github.GithubTicketSystem;
import de.hftstuttgart.unifiedticketing.systems.gitlab.GitlabTicketSystem;
import java.util.HashMap;
import java.util.Map;
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
*
*
* @param ticket implementation this ticket system uses
* @param implementation of this class an instance belongs to
* @param ticket builder this ticket system uses
* @param filter implementation this ticket system uses
*/
public abstract class TicketSystem
{
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
*
* 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.
*
* example: {@code "unifiedticketing::"}
*
* @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))
{
case "github":
return GithubTicketSystem.fromUri(matcher.group(3));
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;
protected Map configuration = new HashMap<>();
/**
* 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 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();
}