Skip to content

Commit

Permalink
feat: repair default icons job [DHIS2-18271] (#18890)
Browse files Browse the repository at this point in the history
* feat: repair default icons job [DHIS2-18271]

* test: adds a controller test for the new endpoint

* refactor: remove phantom icon repair job
  • Loading branch information
jbee authored Oct 23, 2024
1 parent 3edb93a commit e1ef244
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/icon/IconService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public interface IconService {
String addDefaultIconImage(@Nonnull String key, @Nonnull DefaultIcon origin)
throws ConflictException;

/**
* A phantom default icon is an icon that exists as {@link
* org.hisp.dhis.fileresource.FileResource} but for some reason has lost its file in the store. To
* repair the icon the file is re-uploaded.
*
* @return the number of {@link DefaultIcon} {@link org.hisp.dhis.fileresource.FileResource}s that
* were repaired.
* @throws ConflictException when an exception occurred during repair
*/
int repairPhantomDefaultIcons() throws ConflictException;

/**
* Get the count of Icons based on filters provided in {@link IconQueryParams}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -124,6 +125,34 @@ public String addDefaultIconImage(@Nonnull String key, @Nonnull DefaultIcon orig
}
}

@Override
@Transactional
public int repairPhantomDefaultIcons() throws ConflictException {
int c = 0;
List<String> keys =
Stream.of(DefaultIcon.values()).flatMap(i -> i.getVariantKeys().stream()).toList();
IconQueryParams params = new IconQueryParams();
params.setPaging(false);
params.setKeys(keys);
try {
List<Icon> icons = getIcons(params);
for (Icon i : icons) {
if (!fileResourceContentStore.fileResourceContentExists(
i.getFileResource().getStorageKey())) {
try (InputStream image = getDefaultIconResource(i.getKey()).getInputStream()) {
fileResourceService.syncSaveFileResource(i.getFileResource(), image);
}
c++;
}
}
} catch (Exception ex) {
ConflictException e = new ConflictException("Repair failed");
e.initCause(ex);
throw e;
}
return c;
}

private static Resource getDefaultIconResource(String key) {
return new ClassPathResource(String.format("%s/%s.%s", ICON_PATH, key, DefaultIcon.SUFFIX));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ void shouldGetIconsWithDefaultPager() throws ConflictException {
() -> String.format("mismatch in number of expected Icon(s), fetched %s", icons));
}

@Test
void testRepairPhantomIcons() {
JsonWebMessage msg = PATCH("/icons").content().as(JsonWebMessage.class);
assertEquals("0 icons repaired", msg.getMessage());
}

private JsonWebMessage createIcon(Set<String> keywords, String key) throws ConflictException {
return createIcon(createFileResource(), keywords, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.Maturity;
import org.hisp.dhis.common.OpenApi;
import org.hisp.dhis.common.Pager;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
Expand Down Expand Up @@ -70,6 +71,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
Expand Down Expand Up @@ -155,6 +157,13 @@ public WebMessage addIcon(@RequestBody AddIconRequest request)
return created(format("Icon created with key %s", icon.getKey()));
}

@Maturity.Alpha
@PatchMapping
public WebMessage repairPhantomIcons() throws ConflictException {
int repaired = iconService.repairPhantomDefaultIcons();
return ok(format("%d icons repaired", repaired));
}

@PutMapping(value = "/{key}")
public WebMessage updateIcon(@PathVariable String key, @RequestBody UpdateIconRequest request)
throws NotFoundException, BadRequestException {
Expand Down

0 comments on commit e1ef244

Please sign in to comment.