1212import org .joda .time .DateTime ;
1313import org .slf4j .LoggerFactory ;
1414import org .springframework .beans .factory .annotation .Autowired ;
15+ import org .springframework .data .domain .Page ;
16+ import org .springframework .data .domain .PageRequest ;
1517import org .springframework .stereotype .Service ;
1618
1719import javax .transaction .Transactional ;
@@ -36,6 +38,7 @@ public class SubjectMigrationService implements ScopeAwareService<SubjectMigrati
3638 private final LocationRepository locationRepository ;
3739 private final ConceptRepository conceptRepository ;
3840 private final IndividualService individualService ;
41+ private final AvniJobRepository avniJobRepository ;
3942
4043 public enum BulkSubjectMigrationModes {
4144 byAddress ,
@@ -54,7 +57,7 @@ public SubjectMigrationService(EntityApprovalStatusRepository entityApprovalStat
5457 GroupSubjectRepository groupSubjectRepository , AddressLevelService addressLevelService ,
5558 ChecklistRepository checklistRepository ,
5659 ChecklistItemRepository checklistItemRepository ,
57- IndividualRelationshipRepository individualRelationshipRepository , AccessControlService accessControlService , LocationRepository locationRepository , ConceptRepository conceptRepository , IndividualService individualService ) {
60+ IndividualRelationshipRepository individualRelationshipRepository , AccessControlService accessControlService , LocationRepository locationRepository , ConceptRepository conceptRepository , IndividualService individualService , AvniJobRepository avniJobRepository ) {
5861 this .entityApprovalStatusRepository = entityApprovalStatusRepository ;
5962 this .subjectMigrationRepository = subjectMigrationRepository ;
6063 this .subjectTypeRepository = subjectTypeRepository ;
@@ -71,6 +74,7 @@ public SubjectMigrationService(EntityApprovalStatusRepository entityApprovalStat
7174 this .locationRepository = locationRepository ;
7275 this .conceptRepository = conceptRepository ;
7376 this .individualService = individualService ;
77+ this .avniJobRepository = avniJobRepository ;
7478 }
7579
7680 @ Override
@@ -155,6 +159,7 @@ public void changeSubjectSyncConceptValues(Individual subject, String destinatio
155159 individualService .save (subject );
156160 }
157161
162+ @ Transactional
158163 public Map <String , String > bulkMigrate (BulkSubjectMigrationModes mode , BulkSubjectMigrationRequest bulkSubjectMigrationRequest ) {
159164 if (mode == BulkSubjectMigrationModes .byAddress ) {
160165 return bulkMigrateByAddress (bulkSubjectMigrationRequest .getSubjectIds (), bulkSubjectMigrationRequest .getDestinationAddresses ());
@@ -163,6 +168,7 @@ public Map<String, String> bulkMigrate(BulkSubjectMigrationModes mode, BulkSubje
163168 }
164169 }
165170
171+ @ Transactional
166172 public Map <String , String > bulkMigrateByAddress (List <Long > subjectIds , Map <String , String > destinationAddresses ) {
167173 Map <String , String > migrationFailures = new HashMap <>();
168174 Map <AddressLevel , AddressLevel > addressLevelMap = new HashMap <>();
@@ -197,6 +203,7 @@ public Map<String, String> bulkMigrateByAddress(List<Long> subjectIds, Map<Strin
197203 return migrationFailures ;
198204 }
199205
206+ @ Transactional
200207 public Map <String , String > bulkMigrateBySyncConcept (List <Long > subjectIds , Map <String , String > destinationSyncConcepts ) {
201208 Map <String , String > migrationFailures = new HashMap <>();
202209 subjectIds .forEach (subjectId -> {
@@ -219,16 +226,18 @@ public Map<String, String> bulkMigrateBySyncConcept(List<Long> subjectIds, Map<S
219226 }
220227
221228 private String validateSyncConcept (String subjectTypeSyncConceptUuid , String currentValue , Map <String , String > destinationSyncConcepts ) {
222- String destinationSyncConceptValue = destinationSyncConcepts .get (subjectTypeSyncConceptUuid );
223- if (subjectTypeSyncConceptUuid != null && destinationSyncConceptValue == null ) {
224- return null ; // No migration required for this sync concept.
229+ if (subjectTypeSyncConceptUuid == null || //sync concept not configured for subject type
230+ !destinationSyncConcepts .containsKey (subjectTypeSyncConceptUuid ) //sync concept not included in migration
231+ ) {
232+ return null ;
225233 }
226- if (subjectTypeSyncConceptUuid == null ) {
227- throw new RuntimeException ("No sync concept configured for subject type" );
234+ String destinationSyncConceptValue = destinationSyncConcepts .get (subjectTypeSyncConceptUuid );
235+ if (destinationSyncConceptValue == null ) {
236+ return null ;
228237 }
229238 Concept syncConcept = conceptRepository .findByUuid (subjectTypeSyncConceptUuid );
230239
231- if (Objects .equals (currentValue , destinationSyncConceptValue )) {
240+ if (currentValue != null && Objects .equals (currentValue . trim () , destinationSyncConceptValue . trim () )) {
232241 throw new RuntimeException ("Source value and Destination value are the same" );
233242 }
234243
@@ -243,12 +252,19 @@ private String validateSyncConcept(String subjectTypeSyncConceptUuid, String cur
243252
244253 private static ObservationCollection buildSyncConceptValueObservations (Individual subject , String destinationSyncConcept1Value , String destinationSyncConcept2Value ) {
245254 ObservationCollection newObservations = new ObservationCollection ();
246- if (destinationSyncConcept1Value != null ) {
247- newObservations .put (subject .getSubjectType ().getSyncRegistrationConcept1 (), destinationSyncConcept1Value .trim ());
255+ //set observation for unchanged values if sync concept exists so unchanged sync concept values are not overwritten
256+ if (subject .getSubjectType ().getSyncRegistrationConcept1 () != null ) {
257+ newObservations .put (subject .getSubjectType ().getSyncRegistrationConcept1 (), destinationSyncConcept1Value != null ? destinationSyncConcept1Value .trim () : subject .getSyncConcept1Value ());
248258 }
249- if (destinationSyncConcept2Value != null ) {
250- newObservations .put (subject .getSubjectType ().getSyncRegistrationConcept2 (), destinationSyncConcept2Value .trim ());
259+ if (subject . getSubjectType (). getSyncRegistrationConcept2 () != null ) {
260+ newObservations .put (subject .getSubjectType ().getSyncRegistrationConcept2 (), destinationSyncConcept2Value != null ? destinationSyncConcept2Value .trim () : subject . getSyncConcept2Value ());
251261 }
252262 return newObservations ;
253263 }
264+
265+ public JobStatus getBulkSubjectMigrationJobStatus (String jobUuid ) {
266+ String jobFilterCondition = " and uuid = '" + jobUuid + "'" ;
267+ Page <JobStatus > jobStatuses = avniJobRepository .getJobStatuses (UserContextHolder .getUser (), jobFilterCondition , PageRequest .of (0 , 1 ));
268+ return (jobStatuses != null && !jobStatuses .getContent ().isEmpty ()) ? jobStatuses .getContent ().get (0 ) : null ;
269+ }
254270}
0 commit comments