5252public class AutoLockSpamRecords {
5353
5454 private static final Logger LOG = LoggerFactory .getLogger (AutoLockSpamRecords .class );
55-
55+
5656 @ Resource
5757 private SlackManager slackManager ;
5858
@@ -61,48 +61,49 @@ public class AutoLockSpamRecords {
6161
6262 @ Value ("${org.orcid.core.orgs.load.slackUser}" )
6363 private String slackUser ;
64-
64+
6565 @ Value ("${org.orcid.message-listener.s3.accessKey}" )
6666 private String S3_ACCESS_KEY ;
67-
67+
6868 @ Value ("${org.orcid.message-listener.s3.secretKey}" )
6969 private String S3_SECRET_KEY ;
70-
70+
7171 @ Value ("${org.orcid.scheduler.aws.bucket:auto-spam-folder}" )
7272 private String SPAM_BUCKET ;
73-
73+
7474 @ Value ("${org.orcid.scheduler.aws.file:orcidspam.csv}" )
7575 private String ORCID_S3_SPAM_FILE ;
76-
76+
7777 @ Value ("${org.orcid.scheduler.autospam.enabled:false}" )
7878 private boolean AUTOSPAM_ENABLED ;
79-
79+
8080 @ Value ("${org.orcid.scheduler.autospam.file:orcidspam.csv}" )
8181 private String ORCID_SPAM_FILE ;
82-
82+
8383 @ Value ("${org.orcid.scheduler.autospam.daily.batch:20000}" )
8484 private int DAILY_BATCH_SIZE ;
85-
85+
8686 @ Resource (name = "notificationManagerV3" )
8787 private NotificationManager notificationManager ;
88-
89- @ Resource (name = "orcidOauth2TokenDetailDao" )
88+
89+ @ Resource (name = "orcidOauth2TokenDetailDao" )
9090 private OrcidOauth2TokenDetailDao orcidOauthDao ;
9191
9292 private static int ONE_DAY = 86400000 ;
9393
94+ @ Resource (name = "profileEntityManagerV3" )
9495 private ProfileEntityManager profileEntityManager ;
95-
96+
9697 @ Resource
9798 private ProfileEntityCacheManager profileEntityCacheManager ;
98-
99- @ Resource
99+
100+ @ Resource
100101 private AutospamEmailSender autospamEmailSender ;
101-
102+
102103 @ Resource
103104 private AffiliationsManager affiliationsManager ;
104105
105- //for running spam manually
106+ // for running spam manually
106107 public static void main (String [] args ) {
107108 AutoLockSpamRecords autolockSpamRecords = new AutoLockSpamRecords ();
108109 try {
@@ -125,16 +126,17 @@ private void autolockRecords(List<String> toLock) {
125126 for (String orcidId : toLock ) {
126127 try {
127128 LOG .info ("Processing orcidId: " + orcidId );
128- if (OrcidStringUtils .isValidOrcid (orcidId )) {
129+ if (OrcidStringUtils .isValidOrcid (orcidId )) {
129130 ProfileEntity profileEntity = profileEntityManager .findByOrcid (orcidId );
130- //only lock account was not reviewed and not already locked and not have an auth token
131-
132- if (!profileEntity .isReviewed () && profileEntity .isAccountNonLocked () && !orcidOauthDao .hasToken (orcidId )) {
131+ // only lock account was not reviewed and not already locked
132+ // and not have an auth token
133+
134+ if (profileEntity != null && !profileEntity .isReviewed () && profileEntity .isAccountNonLocked () && !orcidOauthDao .hasToken (orcidId )) {
133135 List <Affiliation > affiliations = affiliationsManager .getAffiliations (orcidId );
134- //Lock only if doesn't have any affiliations
135- if (affiliations == null || affiliations .size () < 1 ) {
136+ // Lock only if doesn't have any affiliations
137+ if (affiliations == null || affiliations .size () < 1 ) {
136138 boolean wasLocked = profileEntityManager .lockProfile (orcidId , LockReason .SPAM_AUTO .getLabel (), "ML Detected" , "" );
137- if (wasLocked ) {
139+ if (wasLocked ) {
138140 autospamEmailSender .sendOrcidLockedEmail (orcidId );
139141 accountsLocked ++;
140142 }
@@ -144,45 +146,46 @@ private void autolockRecords(List<String> toLock) {
144146 }
145147 } catch (Exception e ) {
146148 LOG .error ("Exception when locking spam record " + orcidId , e );
147- slackManager .sendAlert ("Exception when locking spam record " + orcidId + ". LastOrcid processed is: " + lastOrcidProcessed , slackChannel , slackUser );
149+ slackManager .sendAlert ("Exception when locking spam record " + orcidId + ". LastOrcid processed is: " + lastOrcidProcessed , slackChannel , slackUser );
148150 LOG .info ("LastOrcid processed is: " + lastOrcidProcessed );
149151 e .printStackTrace ();
150152 }
151153 }
152- System .out .println ("Spam locking for the batch processed on the day: " + System .currentTimeMillis () + " lastOrcid processed is: " + lastOrcidProcessed + " acccounts locked in DB: " + accountsLocked );
153- LOG .info ("Spam locking for the batch processed on the day: " + System .currentTimeMillis () + " lastOrcid processed is: " + lastOrcidProcessed + " acccounts locked in DB: " + accountsLocked );
154- slackManager .sendAlert ("Spam locking for the batch processed on the day ended. LastOrcid processed is: " + lastOrcidProcessed + " acccounts locked in DB: " + accountsLocked , slackChannel , slackUser );
155- }
156-
157-
154+ System .out .println ("Spam locking for the batch processed on the day: " + System .currentTimeMillis () + " lastOrcid processed is: " + lastOrcidProcessed
155+ + " acccounts locked in DB: " + accountsLocked );
156+ LOG .info ("Spam locking for the batch processed on the day: " + System .currentTimeMillis () + " lastOrcid processed is: " + lastOrcidProcessed
157+ + " acccounts locked in DB: " + accountsLocked );
158+ slackManager .sendAlert (
159+ "Spam locking for the batch processed on the day ended. LastOrcid processed is: " + lastOrcidProcessed + " acccounts locked in DB: " + accountsLocked ,
160+ slackChannel , slackUser );
161+ }
162+
158163 public void scheduledProcess () throws InterruptedException , IOException {
159- if (AUTOSPAM_ENABLED ) {
164+ if (AUTOSPAM_ENABLED ) {
160165 process (true );
161- }
166+ }
162167 }
163168
164-
165169 public void process (boolean fromS3 ) throws InterruptedException , IOException {
166170 List <String > allIDs = getAllSpamIDs (fromS3 );
167171 System .out .println ("Found " + allIDs .size () + " profiles for autolocking. Starting the autolocking process" );
168172 slackManager .sendAlert ("Found " + allIDs .size () + " profiles for autolocking." , slackChannel , slackUser );
169173 LOG .info ("Found {} profiles for autolocking" , allIDs .size ());
170-
174+
171175 List <String > toLock = getNextIdSubset (allIDs );
172176 while (toLock != null && !toLock .isEmpty ()) {
173177 autolockRecords (toLock );
174178 LOG .info ("Locked {} profiles, {} remaining to lock" , new Object [] { toLock .size (), allIDs .size () });
175179 LOG .info ("Profiles autolocked" );
176180 Thread .sleep (ONE_DAY );
177- if (allIDs .size () - toLock .size () <=0 ) {
181+ if (allIDs .size () - toLock .size () <= 0 ) {
178182 break ;
179- }
180- else {
183+ } else {
181184 toLock = getNextIdSubset (allIDs );
182185 }
183- }
186+ }
184187 }
185-
188+
186189 @ SuppressWarnings ("resource" )
187190 private void init () {
188191 ApplicationContext context = new ClassPathXmlApplicationContext ("orcid-scheduler-context.xml" );
@@ -205,17 +208,14 @@ private List<String> getNextIdSubset(List<String> ids) {
205208
206209 private ArrayList <String > getAllSpamIDs (boolean fromS3 ) throws IOException {
207210 Reader reader ;
208- if (fromS3 ) {
209- BasicAWSCredentials creds = new BasicAWSCredentials (S3_SECRET_KEY , S3_ACCESS_KEY );
210- AmazonS3 s3 = AmazonS3Client .builder ()
211- .withRegion (Regions .US_EAST_2 )
212- .withCredentials (new AWSStaticCredentialsProvider (creds ))
213- .build ();
214-
211+ if (fromS3 ) {
212+ BasicAWSCredentials creds = new BasicAWSCredentials (S3_ACCESS_KEY , S3_SECRET_KEY );
213+ AmazonS3 s3 = AmazonS3Client .builder ().withRegion (Regions .US_EAST_2 ).withCredentials (new AWSStaticCredentialsProvider (creds )).build ();
214+
215215 S3Object response = s3 .getObject (new GetObjectRequest (SPAM_BUCKET , ORCID_S3_SPAM_FILE ));
216216 byte [] byteArray = IOUtils .toByteArray (response .getObjectContent ());
217217 reader = new InputStreamReader (new ByteArrayInputStream (byteArray ));
218-
218+
219219 } else {
220220 reader = new FileReader (ORCID_SPAM_FILE );
221221 }
@@ -232,10 +232,9 @@ private ArrayList<String> getAllSpamIDs(boolean fromS3) throws IOException {
232232 return spamList ;
233233 }
234234
235-
236235 private static void bootstrapTogglz (OrcidTogglzConfiguration togglzConfig ) {
237236 FeatureManager featureManager = new FeatureManagerBuilder ().togglzConfig (togglzConfig ).build ();
238- ContextClassLoaderFeatureManagerProvider .bind (featureManager );
239- }
237+ ContextClassLoaderFeatureManagerProvider .bind (featureManager );
238+ }
240239
241240}
0 commit comments