Skip to content

Commit

Permalink
Merge branch 'master' into 11-tc-1
Browse files Browse the repository at this point in the history
# Conflicts:
#	Makefile
#	avni-server-api/src/main/java/org/avni/server/dao/IdentifierAssignmentRepository.java
#	avni-server-api/src/main/java/org/avni/server/dao/ImplementationRepository.java
#	avni-server-api/src/main/java/org/avni/server/service/IdentifierAssignmentService.java
#	avni-server-api/src/main/java/org/avni/server/web/IdentifierUserAssignmentWebController.java
#	avni-server-api/src/main/resources/application.properties
#	avni-server-api/src/test/resources/application.properties
  • Loading branch information
himeshr committed Dec 13, 2024
2 parents a19fea6 + 856d903 commit dbf917f
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 95 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ start_server_wo_gradle:

# LIVE
log_live:
tail -f /var/log/openchs/openchs.log
tail -f /var/log/avni_server/chs.log
# /LIVE

tail-local-log:
tail -f -n1000 /var/log/openchs/openchs.log
tail -f -n1000 /var/log/avni_server/chs.log

debug_server_live: build_server
OPENCHS_MODE=live OPENCHS_CLIENT_ID=$(STAGING_APP_CLIENT_ID) OPENCHS_USER_POOL=$(STAGING_USER_POOL_ID) java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar avni-server-api/build/libs/avni-server-0.0.1-SNAPSHOT.jar
Expand All @@ -191,7 +191,7 @@ exec-sql: ## Usage: make exec-sql sqlfile=</path/to/sql>

# remote
tail-prod:
ssh avni-server-prod "tail -f /var/log/openchs/openchs.log"
ssh avni-prod "tail -f /var/log/avni_server/chs.log"

show-dependency-graph:
./gradlew avni-server-api:dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
@Repository
@RepositoryRestResource(collectionResourceRel = "identifierAssignment", path = "identifierAssignment")
public interface IdentifierAssignmentRepository extends TransactionalDataRepository<IdentifierAssignment>, FindByLastModifiedDateTime<IdentifierAssignment> {
Page<IdentifierAssignment> findByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullOrderByAssignmentOrderAsc(User currentUser, Instant lastModifiedDateTime, Pageable pageable);
Page<IdentifierAssignment> findByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullAndDeviceIdEqualsOrderByAssignmentOrderAsc(User currentUser, Instant lastModifiedDateTime, String deviceId, Pageable pageable);

Slice<IdentifierAssignment> findSliceByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullOrderByAssignmentOrderAsc(User currentUser, Instant lastModifiedDateTime, Pageable pageable);
Slice<IdentifierAssignment> findSliceByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullAndDeviceIdEqualsOrderByAssignmentOrderAsc(User currentUser, Instant lastModifiedDateTime, String deviceId, Pageable pageable);

Integer countIdentifierAssignmentByIdentifierSourceEqualsAndAndAssignedToEqualsAndIndividualIsNullAndProgramEnrolmentIsNullAndUsedIsFalse(IdentifierSource identifierSource, User assignedTo);
Integer countIdentifierAssignmentByIdentifierSourceEqualsAndAssignedToEqualsAndIndividualIsNullAndProgramEnrolmentIsNullAndUsedIsFalseAndDeviceIdEquals(IdentifierSource identifierSource, User assignedTo, String deviceId);

boolean existsByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNull(User currentUser, Instant lastModifiedDateTime);
boolean existsByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullAndDeviceIdEquals(User currentUser, Instant lastModifiedDateTime, String deviceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public interface ImplementationRepository extends AvniCrudRepository<Organisatio
@Query(value = "select create_db_user(:name, :pass)", nativeQuery = true)
void createDBUser(String name, String pass);

default void createDBUser(Organisation organisation) {
this.createDBUser(organisation.getDbUser(), "password");
}

@Query(value = "select create_implementation_schema(:schemaName, :dbUser)", nativeQuery = true)
void createImplementationSchema(String schemaName, String dbUser);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ public class IdentifierAssignment extends OrganisationAwareEntity {
@NotNull
private boolean used;

private String deviceId;

public IdentifierAssignment() {
super();
}

public IdentifierAssignment(IdentifierSource identifierSource, String identifier, Long assignmentOrder, User assignedTo) {
public IdentifierAssignment(IdentifierSource identifierSource, String identifier, Long assignmentOrder, User assignedTo, String deviceId) {
this();
this.identifierSource = identifierSource;
this.identifier = identifier;
this.assignmentOrder = assignmentOrder;
this.assignedTo = assignedTo;
this.deviceId = deviceId;
}

public IdentifierSource getIdentifierSource() {
Expand Down Expand Up @@ -104,4 +107,12 @@ public boolean isUsed() {
public void setUsed(boolean used) {
this.used = used;
}

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

public enum DatasetColumn {
NAME(1),
TYPE(2);
TYPE(2),
SCHEMA_NAME(3);

private final int index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.avni.server.domain.User;

public interface IdentifierGenerator {
void generateIdentifiers(IdentifierSource identifierSource, User user);
void generateIdentifiers(IdentifierSource identifierSource, User user, String deviceId);
IdentifierAssignment generateSingleIdentifier(IdentifierSource identifierSource, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,40 @@ public PrefixedUserPoolBasedIdentifierGenerator(IdentifierAssignmentRepository i
}

@Transactional
public void generateIdentifiers(IdentifierSource identifierSource, User user, String prefix) {
public void generateIdentifiers(IdentifierSource identifierSource, User user, String prefix, String deviceId) {
List<IdentifierUserAssignment> identifierUserAssignments = identifierUserAssignmentRepository.getAllNonExhaustedUserAssignments(user, identifierSource);
Long batchGenerationSize = identifierSource.getBatchGenerationSize();
NextIdentifierUserAssignment nextIdentifierUserAssignment = new NextIdentifierUserAssignment(identifierUserAssignments, batchGenerationSize);
List<IdentifierAssignment> generatedIdentifiers = new ArrayList<>();

while(nextIdentifierUserAssignment.hasNext()) {
IdentifierUserAssignment identifierUserAssignment = nextIdentifierUserAssignment.next();
generatedIdentifiers.add(assignNextIdentifier(identifierUserAssignment, prefix));
generatedIdentifiers.add(assignNextIdentifier(identifierUserAssignment, prefix, deviceId));
}

identifierUserAssignmentRepository.saveAll(identifierUserAssignments);
identifierAssignmentRepository.saveAll(generatedIdentifiers);
}

@Transactional
public IdentifierAssignment generateSingleIdentifier(IdentifierSource identifierSource, User user, String prefix) {
public IdentifierAssignment generateSingleIdentifier(IdentifierSource identifierSource, User user, String prefix, String deviceId) {
List<IdentifierUserAssignment> identifierUserAssignments = identifierUserAssignmentRepository.getAllNonExhaustedUserAssignments(user, identifierSource);
NextIdentifierUserAssignment nextIdentifierUserAssignment = new NextIdentifierUserAssignment(identifierUserAssignments, 1L);
List<IdentifierAssignment> generatedIdentifiers = new ArrayList<>();

while(nextIdentifierUserAssignment.hasNext()) {
IdentifierUserAssignment identifierUserAssignment = nextIdentifierUserAssignment.next();
generatedIdentifiers.add(assignNextIdentifier(identifierUserAssignment, prefix));
IdentifierAssignment identifierAssignment = assignNextIdentifier(identifierUserAssignment, prefix, deviceId);
identifierAssignment.setUsed(true);
generatedIdentifiers.add(identifierAssignment);
}

identifierUserAssignmentRepository.saveAll(identifierUserAssignments);
identifierAssignmentRepository.saveAll(generatedIdentifiers);
return generatedIdentifiers.stream().findFirst().orElse(null);
}

private IdentifierAssignment assignNextIdentifier(IdentifierUserAssignment identifierUserAssignment, String prefix) {
private IdentifierAssignment assignNextIdentifier(IdentifierUserAssignment identifierUserAssignment, String prefix, String deviceId) {
String lastAssignedIdentifier = identifierUserAssignment.getLastAssignedIdentifier();
IdentifierSource identifierSource = identifierUserAssignment.getIdentifierSource();
long newIdentifierOrder; String newIdentifierStr, newIdentifierStrWithPrefix;
Expand All @@ -92,7 +94,7 @@ private IdentifierAssignment assignNextIdentifier(IdentifierUserAssignment ident

identifierUserAssignment.setLastAssignedIdentifier(newIdentifierStrWithPrefix);

IdentifierAssignment identifierAssignment = new IdentifierAssignment(identifierSource, newIdentifierStrWithPrefix, newIdentifierOrder, identifierUserAssignment.getAssignedTo());
IdentifierAssignment identifierAssignment = new IdentifierAssignment(identifierSource, newIdentifierStrWithPrefix, newIdentifierOrder, identifierUserAssignment.getAssignedTo(), deviceId);
identifierAssignment.assignUUID();
return identifierAssignment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import static org.avni.server.service.DeviceAwareService.WEB_DEVICE_ID;

@Service
@Qualifier("userBasedIdentifierGenerator")
public class UserBasedIdentifierGenerator implements IdentifierGenerator {
Expand All @@ -18,15 +20,15 @@ public UserBasedIdentifierGenerator(PrefixedUserPoolBasedIdentifierGenerator pre
}

@Override
public void generateIdentifiers(IdentifierSource identifierSource, User user) {
public void generateIdentifiers(IdentifierSource identifierSource, User user, String deviceId) {
String idPrefix = getIdPrefix(user);
prefixedUserPoolBasedIdentifierGenerator.generateIdentifiers(identifierSource, user, idPrefix);
prefixedUserPoolBasedIdentifierGenerator.generateIdentifiers(identifierSource, user, idPrefix, deviceId);
}

@Override
public IdentifierAssignment generateSingleIdentifier(IdentifierSource identifierSource, User user) {
String idPrefix = getIdPrefix(user);
IdentifierAssignment identifierAssignment = prefixedUserPoolBasedIdentifierGenerator.generateSingleIdentifier(identifierSource, user, idPrefix);
IdentifierAssignment identifierAssignment = prefixedUserPoolBasedIdentifierGenerator.generateSingleIdentifier(identifierSource, user, idPrefix, WEB_DEVICE_ID);
return identifierAssignment;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import jakarta.transaction.Transactional;

import static org.avni.server.service.DeviceAwareService.WEB_DEVICE_ID;


@Service
@Qualifier("userPoolBasedIdentifierGenerator")
Expand All @@ -23,14 +25,14 @@ public UserPoolBasedIdentifierGenerator(PrefixedUserPoolBasedIdentifierGenerator

@Override
@Transactional
public void generateIdentifiers(IdentifierSource identifierSource, User user) {
public void generateIdentifiers(IdentifierSource identifierSource, User user, String deviceId) {
String prefix = identifierSource.getPrefix();
prefixedUserPoolBasedIdentifierGenerator.generateIdentifiers(identifierSource, user, prefix);
prefixedUserPoolBasedIdentifierGenerator.generateIdentifiers(identifierSource, user, prefix, deviceId);
}

@Override
public IdentifierAssignment generateSingleIdentifier(IdentifierSource identifierSource, User user) {
String prefix = identifierSource.getPrefix();
return prefixedUserPoolBasedIdentifierGenerator.generateSingleIdentifier(identifierSource, user, prefix);
return prefixedUserPoolBasedIdentifierGenerator.generateSingleIdentifier(identifierSource, user, prefix, WEB_DEVICE_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public void addDependentConcepts(ConceptUsageContract conceptUsageContract, Conc
conceptContract.setName(concept.getName());
conceptContract.setId(concept.getId());
conceptContract.setUuid(concept.getUuid());
conceptContract.setVoided(ca.isVoided());
conceptUsageContract.addConcepts(conceptContract);
addDependentFormDetails(conceptUsageContract, concept);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.avni.server.service;

import org.joda.time.DateTime;

public interface DeviceAwareService {
String WEB_DEVICE_ID = "web";

boolean isSyncRequiredForDevice(DateTime lastModifiedDateTime, String deviceId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.stream.Collectors;

@Service
public class IdentifierAssignmentService implements NonScopeAwareService {
public class IdentifierAssignmentService implements DeviceAwareService {

private IdentifierSourceRepository identifierSourceRepository;

Expand All @@ -39,18 +39,18 @@ public IdentifierAssignmentService(IdentifierSourceRepository identifierSourceRe


@Transactional
public void generateIdentifiersIfNecessary(User user) {
public void generateIdentifiersIfNecessary(User user, String deviceId) {
List<IdentifierSource> allAuthorisedIdentifierSources = identifierSourceRepository.getAllAuthorisedIdentifierSources(user.getCatchment());
for (IdentifierSource identifierSource : allAuthorisedIdentifierSources) {
generateIdentifiersIfNecessary(user, identifierSource);
generateIdentifiersIfNecessary(user, identifierSource, deviceId);
}
}

@Transactional
public void generateIdentifiersIfNecessary(User user, IdentifierSource identifierSource) {
if (shouldGenerateIdentifiers(user, identifierSource)) {
public void generateIdentifiersIfNecessary(User user, IdentifierSource identifierSource, String deviceId) {
if (shouldGenerateIdentifiers(user, identifierSource, deviceId)) {
IdentifierGenerator identifierGenerator = context.getBean(identifierSource.getType().name(), IdentifierGenerator.class);
identifierGenerator.generateIdentifiers(identifierSource, user);
identifierGenerator.generateIdentifiers(identifierSource, user, deviceId);
}
}

Expand All @@ -66,15 +66,14 @@ public List<IdentifierAssignment> generateIdentifiersForAForm(Form form, User us
}).collect(Collectors.toList());
}

private boolean shouldGenerateIdentifiers(User user, IdentifierSource identifierSource) {
Integer spareIdentifierAssignments = identifierAssignmentRepository.countIdentifierAssignmentByIdentifierSourceEqualsAndAndAssignedToEqualsAndIndividualIsNullAndProgramEnrolmentIsNullAndUsedIsFalse(identifierSource, user);
private boolean shouldGenerateIdentifiers(User user, IdentifierSource identifierSource, String deviceId) {
Integer spareIdentifierAssignments = identifierAssignmentRepository.countIdentifierAssignmentByIdentifierSourceEqualsAndAssignedToEqualsAndIndividualIsNullAndProgramEnrolmentIsNullAndUsedIsFalseAndDeviceIdEquals(identifierSource, user, deviceId);
return spareIdentifierAssignments < identifierSource.getMinimumBalance();
}

@Override
public boolean isNonScopeEntityChanged(DateTime lastModifiedDateTime) {
public boolean isSyncRequiredForDevice(DateTime lastModifiedDateTime, String deviceId) {
User user = UserContextHolder.getUserContext().getUser();
this.generateIdentifiersIfNecessary(user);
return identifierAssignmentRepository.existsByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNull(user, DateTimeUtil.toInstant(lastModifiedDateTime));
}
this.generateIdentifiersIfNecessary(user, deviceId);
return identifierAssignmentRepository.existsByAssignedToAndLastModifiedDateTimeGreaterThanAndIsVoidedFalseAndIndividualIsNullAndProgramEnrolmentIsNullAndDeviceIdEquals(user, CHSEntity.toDate(lastModifiedDateTime), deviceId); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.avni.server.domain.metabase.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -81,10 +82,12 @@ public List<String> getSubjectTypeNames() {

for (List<String> row : rows) {
String type = row.get(DatasetColumn.TYPE.getIndex());
if (type.equalsIgnoreCase(TableType.INDIVIDUAL.getTypeName()) ||
type.equalsIgnoreCase(TableType.HOUSEHOLD.getTypeName()) ||
type.equalsIgnoreCase(TableType.GROUP.getTypeName()) ||
type.equalsIgnoreCase(TableType.PERSON.getTypeName())) {
String schemaName = row.get(DatasetColumn.SCHEMA_NAME.getIndex());
if (schemaName.equalsIgnoreCase(getGlobalDatabase().getName()) &&
(type.equalsIgnoreCase(TableType.INDIVIDUAL.getTypeName()) ||
type.equalsIgnoreCase(TableType.HOUSEHOLD.getTypeName()) ||
type.equalsIgnoreCase(TableType.GROUP.getTypeName()) ||
type.equalsIgnoreCase(TableType.PERSON.getTypeName()))) {
subjectTypeNames.add(row.get(DatasetColumn.NAME.getIndex()));
}
}
Expand All @@ -98,18 +101,20 @@ public List<String> getProgramAndEncounterNames() {
DatasetResponse datasetResponse = databaseRepository.findAll(fetchedMetadataTable, getGlobalDatabase());
List<List<String>> rows = datasetResponse.getData().getRows();

List<String> programNames = new ArrayList<>();
List<String> programAndEncounterNames = new ArrayList<>();

for (List<String> row : rows) {
String type = row.get(DatasetColumn.TYPE.getIndex());
if (type.equalsIgnoreCase(TableType.PROGRAM_ENCOUNTER.getTypeName()) ||
type.equalsIgnoreCase(TableType.ENCOUNTER.getTypeName()) ||
type.equalsIgnoreCase(TableType.PROGRAM_ENROLMENT.getTypeName())) {
programNames.add(row.get(DatasetColumn.NAME.getIndex()));
String schemaName = row.get(DatasetColumn.SCHEMA_NAME.getIndex());
if (schemaName.equalsIgnoreCase(getGlobalDatabase().getName()) &&
(type.equalsIgnoreCase(TableType.PROGRAM_ENCOUNTER.getTypeName()) ||
type.equalsIgnoreCase(TableType.ENCOUNTER.getTypeName()) ||
type.equalsIgnoreCase(TableType.PROGRAM_ENROLMENT.getTypeName()))) {
programAndEncounterNames.add(row.get(DatasetColumn.NAME.getIndex()));
}
}

return programNames;
return programAndEncounterNames;
}

private void createQuestionsForEntities(List<String> entityNames, FieldDetails addressFieldDetails, FieldDetails entityFieldDetails) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import org.avni.server.domain.metabase.*;
import org.avni.server.service.OrganisationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class MetabaseService {

public static final String ORG_DB_USER_PASSWORD = "password";
@Value("${avni.org.password}")
private String AVNI_DEFAULT_ORG_USER_DB_PASSWORD;
public static final String DB_ENGINE = "postgres";
private final OrganisationService organisationService;
private final AvniDatabase avniDatabase;
Expand Down Expand Up @@ -46,7 +48,7 @@ public void setupMetabase() {

globalDatabase = databaseRepository.getDatabaseByName(new Database(name));
if (globalDatabase == null) {
Database newDatabase = new Database(name, DB_ENGINE, new DatabaseDetails(avniDatabase, dbUser, ORG_DB_USER_PASSWORD));
Database newDatabase = new Database(name, DB_ENGINE, new DatabaseDetails(avniDatabase, dbUser, AVNI_DEFAULT_ORG_USER_DB_PASSWORD));
globalDatabase = databaseRepository.save(newDatabase);
}

Expand Down
Loading

0 comments on commit dbf917f

Please sign in to comment.