33import java .math .BigInteger ;
44import java .util .ArrayList ;
55import java .util .List ;
6+ import java .util .concurrent .CompletableFuture ;
67
8+ import javax .annotation .Resource ;
79import javax .persistence .Query ;
810import javax .persistence .TypedQuery ;
911
12+ import org .orcid .utils .panoply .PanoplyDeletedItem ;
13+ import org .orcid .utils .panoply .PanoplyRedshiftClient ;
14+ import org .slf4j .Logger ;
15+ import org .slf4j .LoggerFactory ;
16+ import org .apache .commons .logging .Log ;
1017import org .orcid .persistence .aop .UpdateProfileLastModified ;
1118import org .orcid .persistence .aop .UpdateProfileLastModifiedAndIndexingStatus ;
1219import org .orcid .persistence .dao .OrgAffiliationRelationDao ;
1320import org .orcid .persistence .jpa .entities .OrgAffiliationRelationEntity ;
21+ import org .springframework .beans .factory .annotation .Value ;
1422import org .springframework .cache .annotation .Cacheable ;
1523import org .springframework .transaction .annotation .Transactional ;
1624
1725public class OrgAffiliationRelationDaoImpl extends GenericDaoImpl <OrgAffiliationRelationEntity , Long > implements OrgAffiliationRelationDao {
18-
26+
27+ private static final Logger LOG = LoggerFactory .getLogger (OrgAffiliationRelationDaoImpl .class );
28+
29+ @ Value ("${org.orcid.persistence.panoply.cleanup.production:false}" )
30+ private boolean enablePanoplyCleanupInProduction ;
31+
1932 private static final String AFFILIATION_TYPE_DISTINCTION = "DISTINCTION" ;
2033
2134 private static final String AFFILIATION_TYPE_EDUCATION = "EDUCATION" ;
@@ -29,6 +42,11 @@ public class OrgAffiliationRelationDaoImpl extends GenericDaoImpl<OrgAffiliation
2942 private static final String AFFILIATION_TYPE_QUALIFICATION = "QUALIFICATION" ;
3043
3144 private static final String AFFILIATION_TYPE_SERVICE = "SERVICE" ;
45+
46+ private static final String DW_PANOPLY_AFFILIATION_TABLE = "dw_org_affiliation_relation" ;
47+
48+ @ Resource
49+ private PanoplyRedshiftClient panoplyClient ;
3250
3351 public OrgAffiliationRelationDaoImpl () {
3452 super (OrgAffiliationRelationEntity .class );
@@ -51,7 +69,34 @@ public boolean removeOrgAffiliationRelation(String userOrcid, Long orgAffiliatio
5169 Query query = entityManager .createQuery ("delete from OrgAffiliationRelationEntity where orcid=:userOrcid and id=:orgAffiliationRelationId" );
5270 query .setParameter ("userOrcid" , userOrcid );
5371 query .setParameter ("orgAffiliationRelationId" , orgAffiliationRelationId );
54- return query .executeUpdate () > 0 ? true : false ;
72+ if (query .executeUpdate () > 0 ) {
73+ if (enablePanoplyCleanupInProduction ) {
74+ PanoplyDeletedItem item = new PanoplyDeletedItem ();
75+ item .setItemId (orgAffiliationRelationId );
76+ item .setDwTable (DW_PANOPLY_AFFILIATION_TABLE );
77+ storeDeletedItemInPanoply (item );
78+ }
79+ return true ;
80+ }
81+ return false ;
82+ }
83+
84+ private void storeDeletedItemInPanoply (PanoplyDeletedItem item ) {
85+ //Store the deleted item in panoply Db without blocking
86+ CompletableFuture .supplyAsync (() -> {
87+ try {
88+ panoplyClient .addPanoplyDeletedItem (item );
89+ return true ;
90+ } catch (Exception e ) {
91+ LOG .error ("Cannot store deleted affiliation in panoply " , e );
92+ return false ;
93+ }
94+ }).thenAccept (result -> {
95+ if (! result ) {
96+ LOG .error ("Async call to panoply for : " + item .toString () + " Stored: " + result );
97+ }
98+
99+ });
55100 }
56101
57102 /**
@@ -196,6 +241,14 @@ public void removeOrgAffiliationByClientSourceId(String clientSourceId) {
196241 Query query = entityManager .createNativeQuery ("DELETE FROM org_affiliation_relation WHERE client_source_id=:clientSourceId" );
197242 query .setParameter ("clientSourceId" , clientSourceId );
198243 query .executeUpdate ();
244+ if (query .executeUpdate () > 0 ) {
245+ if (enablePanoplyCleanupInProduction ) {
246+ PanoplyDeletedItem item = new PanoplyDeletedItem ();
247+ item .setClientSourceId (clientSourceId );
248+ item .setDwTable (DW_PANOPLY_AFFILIATION_TABLE );
249+ storeDeletedItemInPanoply (item );
250+ }
251+ }
199252 }
200253
201254 @ Override
@@ -281,6 +334,15 @@ public void removeAllAffiliations(String orcid) {
281334 Query query = entityManager .createQuery ("delete from OrgAffiliationRelationEntity where orcid = :orcid" );
282335 query .setParameter ("orcid" , orcid );
283336 query .executeUpdate ();
337+ if (query .executeUpdate () > 0 ) {
338+ if (enablePanoplyCleanupInProduction ) {
339+ PanoplyDeletedItem item = new PanoplyDeletedItem ();
340+ item .setOrcid (orcid );
341+ item .setDwTable (DW_PANOPLY_AFFILIATION_TABLE );
342+ storeDeletedItemInPanoply (item );
343+ }
344+ }
345+
284346 }
285347
286348 @ Override
0 commit comments