|
| 1 | +package fr.sandro642.github; |
| 2 | + |
| 3 | +import fr.sandro642.github.hook.MCSupport; |
| 4 | +import fr.sandro642.github.utils.*; |
| 5 | +import fr.sandro642.github.jobs.JobGetInfos; |
| 6 | +import fr.sandro642.github.jobs.misc.ResourceType; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * ConnectLib is the main class of the ConnectorAPI library for Standard API. |
| 13 | + * It provides methods to initialize the library with resource types, routes, and schemas, |
| 14 | + * @author Sandro642 |
| 15 | + * @version 1.0 |
| 16 | + * @see ConnectLib#JobGetInfos() |
| 17 | + * @see ConnectLib#Logger() |
| 18 | + * @see ConnectLib#StoreAndRetrieve() |
| 19 | + * @see ConnectLib#YamlUtils() |
| 20 | + * @see ConnectLib#MCSupport() |
| 21 | + */ |
| 22 | + |
| 23 | +public class ConnectLib { |
| 24 | + |
| 25 | + private static Logger logger = new Logger(); |
| 26 | + private static StoreAndRetrieve storeAndRetrieve = new StoreAndRetrieve(); |
| 27 | + private static YamlUtils yamlUtils = new YamlUtils(); |
| 28 | + private static final Map<String,String> routes = new HashMap<>(); |
| 29 | + private static Logs logs = new Logs(); |
| 30 | + |
| 31 | + /** |
| 32 | + * Init the ConnectLib with the specified resource type and routes. |
| 33 | + * @param resourceType the type of resource to initialize |
| 34 | + * @param routes the routes to be used in the ConnectLib |
| 35 | + */ |
| 36 | + public static void initialize(ResourceType resourceType, Class<? extends Enum<?>>... routes) { |
| 37 | + try { |
| 38 | + Map<Enum<?>, String> routesEnums = new HashMap<>(); |
| 39 | + for (Class<? extends Enum<?>> route : routes) { |
| 40 | + if (route == null) { |
| 41 | + ConnectLib.Logger().ERROR("Route class cannot be null."); |
| 42 | + } |
| 43 | + |
| 44 | + routesEnums.putAll(ConvertEnum.convertRouteImport(route)); |
| 45 | + } |
| 46 | + |
| 47 | + logger = new Logger(); |
| 48 | + storeAndRetrieve = new StoreAndRetrieve(); |
| 49 | + yamlUtils = new YamlUtils(); |
| 50 | + logs = new Logs(); |
| 51 | + |
| 52 | + yamlUtils.generateTemplateIfNotExists(resourceType, routesEnums); |
| 53 | + logs.setPathFile(resourceType); |
| 54 | + |
| 55 | + if (resourceType == ResourceType.MC_RESOURCES) { |
| 56 | + storeAndRetrieve.store.put(storeAndRetrieve.FILE_LOCATION_KEY, MCSupport().getPluginPath()); |
| 57 | + } else { |
| 58 | + storeAndRetrieve.store.put(storeAndRetrieve.FILE_LOCATION_KEY, resourceType.getPath()); |
| 59 | + } |
| 60 | + |
| 61 | + String baseUrl = yamlUtils.getURL(); |
| 62 | + if (baseUrl != null) { |
| 63 | + storeAndRetrieve.store.put(storeAndRetrieve.URL_KEY, baseUrl); |
| 64 | + } |
| 65 | + |
| 66 | + Map<String, String> yamlRoutes = yamlUtils.getRoutes(); |
| 67 | + if (yamlRoutes != null) { |
| 68 | + ConnectLib.routes.putAll(yamlRoutes); |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + ConnectLib.Logger().ERROR("Error while initializing ConnectLib: " + e.getMessage()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Return the route associated with the given route name. |
| 77 | + * @param routeName the name of the route to retrieve |
| 78 | + * @return the route as a String |
| 79 | + */ |
| 80 | + public static String getRoute(String routeName) { |
| 81 | + if (routes.containsKey(routeName)) { |
| 82 | + return routes.get(routeName); |
| 83 | + } else { |
| 84 | + ConnectLib.Logger().ERROR("Route '" + routeName + "' not found in the ConnectorAPI routes."); |
| 85 | + return null; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Return the route associated with the given Enum route. |
| 91 | + * The Enum name is converted to lowercase to match the route names. |
| 92 | + * @param routeEnum the Enum representing the route |
| 93 | + * @return the route as a String |
| 94 | + */ |
| 95 | + public static String getRoute(Enum<?> routeEnum) { |
| 96 | + return getRoute(routeEnum.name().toLowerCase()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return an instance of JobGetInfos. |
| 101 | + * @return JobGetInfos instance |
| 102 | + */ |
| 103 | + public static JobGetInfos JobGetInfos() { |
| 104 | + return new JobGetInfos(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Return the instance of Logger. |
| 109 | + * @return Logger instance |
| 110 | + */ |
| 111 | + public static Logger Logger() { |
| 112 | + if (logger == null) { |
| 113 | + logger = new Logger(); |
| 114 | + } |
| 115 | + return logger; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Return the instance of StoreAndRetrieve. |
| 120 | + * @return StoreAndRetrieve instance |
| 121 | + */ |
| 122 | + public static StoreAndRetrieve StoreAndRetrieve() { |
| 123 | + if (storeAndRetrieve == null) { |
| 124 | + storeAndRetrieve = new StoreAndRetrieve(); |
| 125 | + } |
| 126 | + return storeAndRetrieve; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Return the instance of YamlUtils. |
| 131 | + * @return YamlUtils instance |
| 132 | + */ |
| 133 | + public static YamlUtils YamlUtils() { |
| 134 | + if (yamlUtils == null) { |
| 135 | + yamlUtils = new YamlUtils(); |
| 136 | + } |
| 137 | + return yamlUtils; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return the instance of Logs. |
| 142 | + * @return Logs instance |
| 143 | + */ |
| 144 | + public static MCSupport MCSupport() { |
| 145 | + return MCSupport.getInstance(); |
| 146 | + } |
| 147 | +} |
0 commit comments