diff options
Diffstat (limited to 'service/src/main/java')
2 files changed, 140 insertions, 0 deletions
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/mapper/v3/EventMapperV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/mapper/v3/EventMapperV3.java new file mode 100644 index 0000000..7fd8112 --- /dev/null +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/mapper/v3/EventMapperV3.java | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | package market.guess.service.catalog.infrastructure.mapper.v3; | ||
| 2 | |||
| 3 | import java.util.ArrayList; | ||
| 4 | import java.util.List; | ||
| 5 | import market.guess.model.event.CommissionTiming; | ||
| 6 | import market.guess.model.event.EventStatus; | ||
| 7 | import market.guess.service.catalog.model.v3.GMEvent; | ||
| 8 | import market.guess.service.catalog.model.v3.GMLMSR; | ||
| 9 | import market.guess.service.catalog.model.v3.GMOrderBook; | ||
| 10 | import market.guess.service.domain.Event; | ||
| 11 | import market.guess.service.domain.Market; | ||
| 12 | import market.guess.service.mechanism.LmsrTradingMechanism; | ||
| 13 | import market.guess.service.mechanism.OrderBookTradingMechanism; | ||
| 14 | import market.guess.service.mechanism.TradingMechanism; | ||
| 15 | |||
| 16 | public final class EventMapperV3 { | ||
| 17 | |||
| 18 | public Event toDomain(GMEvent source, int displayId, String marketMaker) { | ||
| 19 | var key = source.getName().trim(); | ||
| 20 | return new Event( | ||
| 21 | key, | ||
| 22 | displayId, | ||
| 23 | key, | ||
| 24 | source.getDescription(), | ||
| 25 | source.getCommission().getValue(), | ||
| 26 | getTiming(source.getCommission().getType()), | ||
| 27 | getMechanism(source, source.getGMOptions().getGMOption().size()), | ||
| 28 | EventStatus.DRAFT, | ||
| 29 | getOptions(key, source.getGMOptions().getGMOption()), | ||
| 30 | marketMaker); | ||
| 31 | } | ||
| 32 | |||
| 33 | private static CommissionTiming getTiming(String type) { | ||
| 34 | return switch (type.toLowerCase().trim()) { | ||
| 35 | case "on-close" -> CommissionTiming.ON_CLOSE; | ||
| 36 | case "on-purchase" -> CommissionTiming.ON_PURCHASE; | ||
| 37 | default -> throw new IllegalArgumentException("Unknown commission type: " + type); | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | private static TradingMechanism getMechanism(GMEvent source, int optionCount) { | ||
| 42 | if (source.getGMMethod().getGMLMSR() instanceof GMLMSR lmsr) { | ||
| 43 | return new LmsrTradingMechanism(lmsr.getB(), optionCount); | ||
| 44 | } | ||
| 45 | if (source.getGMMethod().getGMOrderBook() instanceof GMOrderBook orderBook) { | ||
| 46 | return new OrderBookTradingMechanism( | ||
| 47 | "true".equalsIgnoreCase(orderBook.getAllowMint()), | ||
| 48 | orderBook.getInitial(), | ||
| 49 | orderBook.getD(), | ||
| 50 | optionCount); | ||
| 51 | } | ||
| 52 | throw new IllegalArgumentException("Unknown trading method for event: " + source.getName()); | ||
| 53 | } | ||
| 54 | |||
| 55 | private static List<Market> getOptions(String key, List<String> raw) { | ||
| 56 | var ret = new ArrayList<Market>(raw.size()); | ||
| 57 | for (var i = 0; i < raw.size(); i++) { | ||
| 58 | ret.add(new Market(key + ":" + i, raw.get(i).trim())); | ||
| 59 | } | ||
| 60 | return ret; | ||
| 61 | } | ||
| 62 | } | ||
diff --git a/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java new file mode 100644 index 0000000..59aa3ad --- /dev/null +++ b/service/src/main/java/market/guess/service/catalog/infrastructure/provider/v3/XMLLoaderV3.java | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | package market.guess.service.catalog.infrastructure.provider.v3; | ||
| 2 | |||
| 3 | import jakarta.xml.bind.JAXBContext; | ||
| 4 | import jakarta.xml.bind.JAXBException; | ||
| 5 | import java.io.InputStream; | ||
| 6 | import java.util.ArrayList; | ||
| 7 | import javax.xml.stream.XMLInputFactory; | ||
| 8 | import javax.xml.stream.XMLStreamException; | ||
| 9 | import market.guess.api.LoadResult; | ||
| 10 | import market.guess.api.Result; | ||
| 11 | import market.guess.service.catalog.infrastructure.mapper.v3.EventMapperV3; | ||
| 12 | import market.guess.service.catalog.infrastructure.repository.EventRepository; | ||
| 13 | import market.guess.service.catalog.model.v3.GMEvent; | ||
| 14 | import market.guess.service.catalog.model.v3.GuessMarket; | ||
| 15 | |||
| 16 | public final class XMLLoaderV3 { | ||
| 17 | private final JAXBContext contextV3; | ||
| 18 | private final XMLInputFactory xmlInput; | ||
| 19 | private final EventRepository eventRepository; | ||
| 20 | private final EventMapperV3 mapperV3; | ||
| 21 | private final XMLValidatorV3 validatorV3; | ||
| 22 | |||
| 23 | public XMLLoaderV3( | ||
| 24 | EventRepository eventRepository, EventMapperV3 mapperV3, XMLValidatorV3 validatorV3) { | ||
| 25 | this.eventRepository = eventRepository; | ||
| 26 | this.mapperV3 = mapperV3; | ||
| 27 | this.validatorV3 = validatorV3; | ||
| 28 | try { | ||
| 29 | this.contextV3 = JAXBContext.newInstance(GuessMarket.class); | ||
| 30 | } catch (JAXBException e) { | ||
| 31 | throw new IllegalStateException("Failed to initialize JAXB context: " + e.getMessage(), e); | ||
| 32 | } | ||
| 33 | this.xmlInput = XMLInputFactory.newFactory(); | ||
| 34 | xmlInput.setProperty(XMLInputFactory.SUPPORT_DTD, false); | ||
| 35 | xmlInput.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); | ||
| 36 | } | ||
| 37 | |||
| 38 | public synchronized Result<LoadResult> load(InputStream in, String source, String uploader) { | ||
| 39 | if (in == null) { | ||
| 40 | return Result.error("INVALID_INPUT", "No file content was provided."); | ||
| 41 | } | ||
| 42 | if (uploader == null || uploader.isBlank()) { | ||
| 43 | return Result.error("INVALID_UPLOADER", "An upload must come from a logged-in user."); | ||
| 44 | } | ||
| 45 | |||
| 46 | GuessMarket seed; | ||
| 47 | try { | ||
| 48 | var reader = xmlInput.createXMLStreamReader(in); | ||
| 49 | try { | ||
| 50 | seed = (GuessMarket) contextV3.createUnmarshaller().unmarshal(reader); | ||
| 51 | } finally { | ||
| 52 | reader.close(); | ||
| 53 | } | ||
| 54 | } catch (JAXBException e) { | ||
| 55 | var cause = | ||
| 56 | e.getLinkedException() != null ? e.getLinkedException().getMessage() : e.getMessage(); | ||
| 57 | return Result.error( | ||
| 58 | "XML_PARSE_ERROR", | ||
| 59 | "The XML file is malformed or invalid: " + (cause != null ? cause : e.getMessage())); | ||
| 60 | } catch (XMLStreamException e) { | ||
| 61 | return Result.error("XML_PARSE_ERROR", "The XML file is malformed: " + e.getMessage()); | ||
| 62 | } | ||
| 63 | |||
| 64 | var validation = validatorV3.validate(seed, eventRepository); | ||
| 65 | if (!validation.isSuccess()) { | ||
| 66 | return Result.error(validation.getMessage(), validation.getDetails()); | ||
| 67 | } | ||
| 68 | |||
| 69 | var nextId = eventRepository.getAll().size() + 1; | ||
| 70 | var mapped = new ArrayList<market.guess.service.domain.Event>(); | ||
| 71 | for (GMEvent e : seed.getGMEvents().getGMEvent()) { | ||
| 72 | mapped.add(mapperV3.toDomain(e, nextId++, uploader.trim())); | ||
| 73 | } | ||
| 74 | mapped.forEach(eventRepository::add); | ||
| 75 | |||
| 76 | return Result.ok(new LoadResult(source, mapped.size())); | ||
| 77 | } | ||
| 78 | } | ||