<commit_before>package org.sagebionetworks.file.worker;

import org.apache.commons.lang.BooleanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sagebionetworks.asynchronous.workers.sqs.MessageUtils;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.Folder;
import org.sagebionetworks.repo.model.NodeDAO;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.ProjectSettingsDAO;
import org.sagebionetworks.repo.model.message.ModificationMessage;
import org.sagebionetworks.repo.model.message.NodeSettingsModificationMessage;
import org.sagebionetworks.repo.model.message.SyncFolderMessage;
import org.sagebionetworks.repo.model.project.ExternalSyncSetting;
import org.sagebionetworks.repo.model.project.ProjectSetting;
import org.sagebionetworks.repo.model.project.ProjectSettingsType;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.sagebionetworks.util.ValidateArgument;
import org.sagebionetworks.workers.util.aws.message.MessageDrivenRunner;
import org.sagebionetworks.workers.util.aws.message.MessageQueue;
import org.sagebionetworks.workers.util.aws.message.RecoverableMessageException;
import org.sagebionetworks.workers.util.progress.ProgressCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.dao.TransientDataAccessException;

import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.SendMessageRequest;

public class SyncSettingsModificationsWorker implements MessageDrivenRunner {

	static private Logger log = LogManager.getLogger(SyncSettingsModificationsWorker.class);

	@Autowired
	private ProjectSettingsDAO projectSettingsDao;
	@Autowired
	private NodeDAO nodeDao;
	@Autowired
	AmazonSQSClient awsSQSClient;

	private MessageQueue messageQueue;

	@Required
	public void setFolderSyncQueue(MessageQueue messageQueue) {
		this.messageQueue = messageQueue;
	}

	/**
	 * This is where the real work happens
	 * 
	 * @param message
	 * @return
	 * @throws Throwable
	 */
	@Override
	public void run(ProgressCallback<Message> progressCallback, Message message)
			throws RecoverableMessageException, Exception {
		try {
			NodeSettingsModificationMessage modificationMessage = extractStatus(message);
			if (modificationMessage != null && modificationMessage.getProjectSettingsType() == ProjectSettingsType.external_sync) {
				EntityHeader entityHeader = nodeDao.getEntityHeader(modificationMessage.getObjectId(), null);
				if (entityHeader.getType().equals(Project.class.getName()) || entityHeader.getType().equals(Folder.class.getName())) {
					ProjectSetting projectSetting = projectSettingsDao.get(modificationMessage.getObjectId(),
							ProjectSettingsType.external_sync);
					if (projectSetting != null) {
						if (projectSetting instanceof ExternalSyncSetting) {
							ExternalSyncSetting externalSyncSetting = (ExternalSyncSetting) projectSetting;
							if (BooleanUtils.isTrue(externalSyncSetting.getAutoSync())) {
								SyncFolderMessage syncFolderMessage = new SyncFolderMessage();
								syncFolderMessage.setEntityId(modificationMessage.getObjectId());
								String bodyJson = EntityFactory.createJSONStringForEntity(syncFolderMessage);
								// publish the message
								awsSQSClient.sendMessage(new SendMessageRequest(messageQueue.getQueueName(), bodyJson));
							}
						}
					}
				}
			}
		} catch (NotFoundException e) {
			// probably means the entity was deleted in the mean time. No biggy
			return;
		} catch (TransientDataAccessException e) {
			throw new RecoverableMessageException();
		}
	}

	/**
	 * Extract the AsynchUploadRequestBody from the message.
	 * <commit_msg>fixed<commit_after>package org.sagebionetworks.file.worker;

import org.apache.commons.lang.BooleanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.sagebionetworks.asynchronous.workers.sqs.MessageUtils;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.Folder;
import org.sagebionetworks.repo.model.NodeDAO;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.ProjectSettingsDAO;
import org.sagebionetworks.repo.model.message.ModificationMessage;
import org.sagebionetworks.repo.model.message.NodeSettingsModificationMessage;
import org.sagebionetworks.repo.model.message.SyncFolderMessage;
import org.sagebionetworks.repo.model.project.ExternalSyncSetting;
import org.sagebionetworks.repo.model.project.ProjectSetting;
import org.sagebionetworks.repo.model.project.ProjectSettingsType;
import org.sagebionetworks.repo.web.NotFoundException;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.EntityFactory;
import org.sagebionetworks.util.ValidateArgument;
import org.sagebionetworks.workers.util.aws.message.HasQueueUrl;
import org.sagebionetworks.workers.util.aws.message.MessageDrivenRunner;
import org.sagebionetworks.workers.util.aws.message.MessageQueue;
import org.sagebionetworks.workers.util.aws.message.RecoverableMessageException;
import org.sagebionetworks.workers.util.progress.ProgressCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.dao.TransientDataAccessException;

import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.Message;
import com.amazonaws.services.sqs.model.SendMessageRequest;

public class SyncSettingsModificationsWorker implements MessageDrivenRunner {

	static private Logger log = LogManager.getLogger(SyncSettingsModificationsWorker.class);

	@Autowired
	private ProjectSettingsDAO projectSettingsDao;
	@Autowired
	private NodeDAO nodeDao;
	@Autowired
	AmazonSQSClient awsSQSClient;

	private HasQueueUrl messageQueue;

	@Required
	public void setFolderSyncQueue(HasQueueUrl messageQueue) {
		this.messageQueue = messageQueue;
	}

	/**
	 * This is where the real work happens
	 * 
	 * @param message
	 * @return
	 * @throws Throwable
	 */
	@Override
	public void run(ProgressCallback<Message> progressCallback, Message message)
			throws RecoverableMessageException, Exception {
		try {
			NodeSettingsModificationMessage modificationMessage = extractStatus(message);
			if (modificationMessage != null && modificationMessage.getProjectSettingsType() == ProjectSettingsType.external_sync) {
				EntityHeader entityHeader = nodeDao.getEntityHeader(modificationMessage.getObjectId(), null);
				if (entityHeader.getType().equals(Project.class.getName()) || entityHeader.getType().equals(Folder.class.getName())) {
					ProjectSetting projectSetting = projectSettingsDao.get(modificationMessage.getObjectId(),
							ProjectSettingsType.external_sync);
					if (projectSetting != null) {
						if (projectSetting instanceof ExternalSyncSetting) {
							ExternalSyncSetting externalSyncSetting = (ExternalSyncSetting) projectSetting;
							if (BooleanUtils.isTrue(externalSyncSetting.getAutoSync())) {
								SyncFolderMessage syncFolderMessage = new SyncFolderMessage();
								syncFolderMessage.setEntityId(modificationMessage.getObjectId());
								String bodyJson = EntityFactory.createJSONStringForEntity(syncFolderMessage);
								// publish the message
								awsSQSClient.sendMessage(new SendMessageRequest(messageQueue.getQueueUrl(), bodyJson));
							}
						}
					}
				}
			}
		} catch (NotFoundException e) {
			// probably means the entity was deleted in the mean time. No biggy
			return;
		} catch (TransientDataAccessException e) {
			throw new RecoverableMessageException();
		}
	}

	/**
	 * Extract the AsynchUploadRequestBody from the message.
	 * 
{'fraction_non_alphanumeric': 0.08825719960278054, 'fraction_numerical': 0.0004965243296921549, 'mean_word_length': 5.833757421543681, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7493030', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2302, 'n_words': 378}