Skip to content

Commit

Permalink
Merge branch 'batchhandler-autoclose' of github.com:dhis2/dhis2-core …
Browse files Browse the repository at this point in the history
…into batchhandler-autoclose
  • Loading branch information
jason-p-pickering committed Nov 15, 2023
2 parents 6a7e57b + d4f97de commit df4ee6c
Show file tree
Hide file tree
Showing 128 changed files with 1,410 additions and 1,874 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,33 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.hisp.dhis.feedback.ConflictException;
import org.hisp.dhis.feedback.NotFoundException;

/**
* @author Halvdan Hoem Grelland
*/
public interface FileResourceService {

@CheckForNull
FileResource getFileResource(String uid);

/**
* Get the {@link FileResource} with the given ID
*
* @param uid the resource to fetch
* @return the file resource
* @throws NotFoundException when no such file resource exits
*/
@Nonnull
FileResource getExistingFileResource(String uid) throws NotFoundException;

/**
* Lookup a {@link FileResource} by uid and {@link FileResourceDomain}.
*
Expand Down Expand Up @@ -83,7 +98,12 @@ public interface FileResourceService {

void deleteFileResource(FileResource fileResource);

InputStream getFileResourceContent(FileResource fileResource);
@Nonnull
InputStream getFileResourceContent(FileResource fileResource) throws ConflictException;

@Nonnull
InputStream getFileResourceContent(FileResource fileResource, Duration timeout)
throws ConflictException;

/** Copy fileResource content to outputStream and Return File content length */
void copyFileResourceContent(FileResource fileResource, OutputStream outputStream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,5 @@
public enum FileResourceStorageStatus {
NONE, // No content stored
PENDING, // In transit to store, not available
FAILED, // Storing the resource failed
STORED // Is available from store
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public boolean isUsingNotifications() {
|| this == DATAVALUE_IMPORT
|| this == COMPLETE_DATA_SET_REGISTRATION_IMPORT
|| this == METADATA_IMPORT
|| this == TRACKER_IMPORT_JOB
|| this == GEOJSON_IMPORT;
}

Expand All @@ -231,7 +232,7 @@ public boolean isUsingErrorNotification() {
* the ready jobs per type is attempted to start in a single loop cycle
*/
public boolean isUsingContinuousExecution() {
return this == METADATA_IMPORT;
return this == METADATA_IMPORT || this == TRACKER_IMPORT_JOB;
}

public boolean hasJobParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
*/
package org.hisp.dhis.fileresource;

import static java.lang.System.currentTimeMillis;
import static java.time.Duration.ofMillis;
import static java.time.Duration.ofSeconds;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -43,7 +47,9 @@
import lombok.RequiredArgsConstructor;
import org.hibernate.SessionFactory;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.feedback.ConflictException;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.fileresource.events.BinaryFileSavedEvent;
import org.hisp.dhis.fileresource.events.FileDeletedEvent;
import org.hisp.dhis.fileresource.events.FileSavedEvent;
Expand Down Expand Up @@ -86,6 +92,15 @@ public class DefaultFileResourceService implements FileResourceService {
// FileResourceService implementation
// -------------------------------------------------------------------------

@Nonnull
@Override
@Transactional(readOnly = true)
public FileResource getExistingFileResource(String uid) throws NotFoundException {
FileResource fr = fileResourceStore.getByUid(uid);
if (fr == null) throw new NotFoundException(FileResource.class, uid);
return fr;
}

@Override
@Transactional(readOnly = true)
public FileResource getFileResource(String uid) {
Expand Down Expand Up @@ -228,26 +243,43 @@ public void deleteFileResource(FileResource fileResource) {
}

@Override
@Transactional(readOnly = true)
public InputStream getFileResourceContent(FileResource fileResource) {
return fileResourceContentStore.getFileResourceContent(fileResource.getStorageKey());
@Nonnull
public InputStream getFileResourceContent(FileResource fileResource) throws ConflictException {
return getFileResourceContent(fileResource, ofSeconds(10));
}

@Nonnull
@Override
public InputStream getFileResourceContent(FileResource fileResource, java.time.Duration timeout)
throws ConflictException {
String key = fileResource.getStorageKey();
InputStream content = fileResourceContentStore.getFileResourceContent(key);
long since = currentTimeMillis();
while (content == null && !timeout.minus(ofMillis(currentTimeMillis() - since)).isNegative()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
content = fileResourceContentStore.getFileResourceContent(key);
}
if (content == null)
throw new ConflictException("File resource exists but content input stream was null");
return content;
}

@Override
@Transactional(readOnly = true)
public long getFileResourceContentLength(FileResource fileResource) {
return fileResourceContentStore.getFileResourceContentLength(fileResource.getStorageKey());
}

@Override
@Transactional(readOnly = true)
public void copyFileResourceContent(FileResource fileResource, OutputStream outputStream)
throws IOException, NoSuchElementException {
fileResourceContentStore.copyContent(fileResource.getStorageKey(), outputStream);
}

@Override
@Transactional(readOnly = true)
public byte[] copyFileResourceContent(FileResource fileResource)
throws IOException, NoSuchElementException {
return fileResourceContentStore.copyContent(fileResource.getStorageKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public InputStream getFileResourceContent(String key) {
final Blob blob = getBlob(key);

if (blob == null) {

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static java.lang.Math.max;
import static java.util.stream.Collectors.toSet;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

import java.util.List;
import java.util.Set;
Expand All @@ -45,6 +46,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Jan Bernitt
Expand Down Expand Up @@ -242,6 +244,7 @@ public Stream<JobConfiguration> getDueJobConfigurations(boolean includeWaiting)
}

@Override
@Transactional(propagation = REQUIRES_NEW)
public boolean tryExecuteNow(@Nonnull String jobId) {
// language=SQL
String sql =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.security.acl.AclService;
Expand All @@ -78,8 +77,6 @@ public class DataValueSetImportValidator {

private final DataValueService dataValueService;

private final OrganisationUnitService organisationUnitService;

/** Validation on the {@link DataSet} level */
interface DataSetValidation {
void validate(DataValueSet dataValueSet, ImportContext context, DataSetContext dataSetContext);
Expand Down Expand Up @@ -645,8 +642,7 @@ private void checkDataValueOrgUnitValidForAttrOptionCombo(

private boolean isOrgUnitValidForAttrOptionCombo(DataValueContext valueContext) {
Set<OrganisationUnit> aocOrgUnits = valueContext.getAttrOptionCombo().getOrganisationUnits();
return aocOrgUnits == null
|| organisationUnitService.isDescendant(valueContext.getOrgUnit(), aocOrgUnits);
return aocOrgUnits == null || valueContext.getOrgUnit().isDescendant(aocOrgUnits);
}

private static void checkDataValueTargetDataSets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
import org.hisp.dhis.importexport.ImportStrategy;
import org.hisp.dhis.option.OptionSet;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.period.PeriodTypeEnum;
Expand All @@ -99,8 +98,6 @@ class DataValueSetImportValidatorTest {

private DataValueSetImportValidator validator;

private OrganisationUnitService organisationUnitService;

private final CategoryCombo defaultCombo = new CategoryCombo();

@BeforeEach
Expand All @@ -109,16 +106,11 @@ void setUp() {
lockExceptionStore = mock(LockExceptionStore.class);
approvalService = mock(DataApprovalService.class);
dataValueService = mock(DataValueService.class);
organisationUnitService = mock(OrganisationUnitService.class);

i18n = mock(I18n.class);
validator =
new DataValueSetImportValidator(
aclService,
lockExceptionStore,
approvalService,
dataValueService,
organisationUnitService);
aclService, lockExceptionStore, approvalService, dataValueService);
validator.init();
setupUserCanWriteCategoryOptions(true);
when(i18n.getString(anyString()))
Expand Down Expand Up @@ -298,12 +290,6 @@ void testValidateDataValueAttrOptionComboAccess() {

@Test
void testValidateDataValueOrgUnitInUserHierarchy() {
when(organisationUnitService.isDescendant(any(OrganisationUnit.class), any(Set.class)))
.thenReturn(false);
when(organisationUnitService.isDescendant(
any(OrganisationUnit.class), any(OrganisationUnit.class)))
.thenReturn(false);

DataValue dataValue = createRandomDataValue();
DataValueContext valueContext = createDataValueContext(dataValue).build();
DataSetContext dataSetContext = createMinimalDataSetContext().build();
Expand Down
8 changes: 0 additions & 8 deletions dhis-2/dhis-services/dhis-service-tracker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
<groupId>org.hisp.dhis.rules</groupId>
<artifactId>rule-engine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
Expand All @@ -89,10 +85,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
Loading

0 comments on commit df4ee6c

Please sign in to comment.