diff --git a/src/main/java/edu/group5/app/model/organization/OrganizationService.java b/src/main/java/edu/group5/app/model/organization/OrganizationService.java index c5979f5..9785040 100644 --- a/src/main/java/edu/group5/app/model/organization/OrganizationService.java +++ b/src/main/java/edu/group5/app/model/organization/OrganizationService.java @@ -1,15 +1,28 @@ package edu.group5.app.model.organization; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; + +import java.util.HashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; /** * Service class for managing organization-related operations. * It interacts with the OrganizationRepository to retrieve organization information * and contains business logic associated with organization management. + * + *
It provides fetching logo URLs by web scraping each organization's page on + * Innsamlingskontrollen.
+ * + * Fetched logo URLs are cached to avoid redundant network requests. */ public class OrganizationService { private OrganizationRepository organizationRepository; + private final Map+ * Using Jsoup to web scrape through the URLs in the API. + *
+ * @param pageUrl the URL for the organization's page; may be null or blank + * @return the absolute logo URL, or null if not found or pageUrl is invalid + */ + + public String fetchLogoUrl(String pageUrl) { + if (pageUrl == null || pageUrl.isBlank()) { + return null; + } + + if (logoCache.containsKey(pageUrl)) { + return logoCache.get(pageUrl); + } + + try { + Document doc = Jsoup.connect(pageUrl).get(); + Element img = doc.selectFirst("div.logo img"); + + if (img != null) { + String logoUrl = img.absUrl("src"); + logoCache.put(pageUrl, logoUrl); + return logoUrl; + } + } catch (Exception e) { + System.out.println("Could not get logo for: " + pageUrl); + } + return null; + } + + /** + * Fetches all trusted organizations with their logo URLs. + * + *+ * For each trusted organization, attempts to get its logo using + * {@link #fetchLogoUrl(String)}. Creates a new Organization + * object including the logo URL. + *
+ * @return a map of trusted organizations keyed by organization number, with logos included + */ + public MapRuns in the background so the UI thread is no blocked. + * Returns a CompletableFuture that completes when all logos are loaded.
+ * + * @return a CompletableFuture containing a map of organizations with logos + */ + public CompletableFuture