<commit_before>/**
 * 
 */
package org.sagebionetworks.repo.model.dbo.dao;

import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_GROUP_MEMBERS_GROUP_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_GROUP_MEMBERS_MEMBER_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_PROPERTIES;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITEE_INVITATION_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITEE_INVITEE_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_TEAM_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.LIMIT_PARAM_NAME;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.OFFSET_PARAM_NAME;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_GROUP_MEMBERS;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_MEMBERSHIP_INVITATION_SUBMISSION;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_MEMBERSHIP_INVITEE;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_TEAM;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.sagebionetworks.ids.ETagGenerator;
import org.sagebionetworks.ids.IdGenerator;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.GroupMembersDAO;
import org.sagebionetworks.repo.model.InvalidModelException;
import org.sagebionetworks.repo.model.MembershipInvitation;
import org.sagebionetworks.repo.model.MembershipInvtnSubmission;
import org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.repo.model.dbo.DBOBasicDao;
import org.sagebionetworks.repo.model.dbo.persistence.DBOMembershipInvitee;
import org.sagebionetworks.repo.model.dbo.persistence.DBOMembershipInvtnSubmission;
import org.sagebionetworks.repo.model.dbo.persistence.DBOTeam;
import org.sagebionetworks.repo.web.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author brucehoff
 *
 */
public class DBOMembershipInvtnSubmissionDAOImpl implements MembershipInvtnSubmissionDAO {

	@Autowired
	private DBOBasicDao basicDao;	
	@Autowired
	private IdGenerator idGenerator;
	@Autowired
	private ETagGenerator eTagGenerator;
	@Autowired
	private SimpleJdbcTemplate simpleJdbcTemplate;
	@Autowired
	GroupMembersDAO groupMembersDAO;
	
	private static final String INVITEE_ID_COLUMN_LABEL = "INVITEE_ID";

	private static final String SELECT_OPEN_INVITATIONS_BY_USER_PAGINATED = 
			"SELECT mis.*, mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" as "+INVITEE_ID_COLUMN_LABEL+" "+" FROM "+
					TABLE_MEMBERSHIP_INVITATION_SUBMISSION+" mis, "+
					TABLE_MEMBERSHIP_INVITEE+" mi "+
			" WHERE mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITATION_ID+
			" AND mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+"=:"+COL_MEMBERSHIP_INVITEE_INVITEE_ID+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+" NOT IN (SELECT "+COL_GROUP_MEMBERS_GROUP_ID+" FROM "+
				TABLE_GROUP_MEMBERS+" WHERE "+COL_GROUP_MEMBERS_MEMBER_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" ) "+
			" AND ( mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" IS NULL OR mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+">:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" ) "+
			" LIMIT :"+LIMIT_PARAM_NAME+" OFFSET :"+OFFSET_PARAM_NAME;
	
	private static final String SELECT_OPEN_INVITATIONS_BY_TEAM_AND_USER_PAGINATED = 
			"SELECT mis.*, mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" as "+INVITEE_ID_COLUMN_LABEL+" "+" FROM "+
					TABLE_MEMBERSHIP_INVITATION_SUBMISSION+" mis, "+
					TABLE_MEMBERSHIP_INVITEE+" mi "+
			" WHERE mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITATION_ID+
			" AND mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+"=:"+COL_MEMBERSHIP_INVITEE_INVITEE_ID+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+" NOT IN (SELECT "+COL_GROUP_MEMBERS_GROUP_ID+" FROM "+
				TABLE_GROUP_MEMBERS+" WHERE "+COL_GROUP_MEMBERS_MEMBER_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" ) "+
				" AND ( mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" IS NULL OR mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+">:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" ) "+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+"=:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+
			" LIMIT :"+LIMIT_PARAM_NAME+" OFFSET :"+OFFSET_PARAM_NAME;
	
	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MemberRqstSubmissionDAO#create(org.sagebionetworks.repo.model.MemberRqstSubmission)
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public MembershipInvtnSubmission create(MembershipInvtnSubmission dto) throws DatastoreException,
	InvalidModelException {
		DBOMembershipInvtnSubmission dbo = new DBOMembershipInvtnSubmission();
		MembershipInvtnSubmissionUtils.copyDtoToDbo(dto, dbo);
		if (dbo.getId()==null) dbo.setId(idGenerator.generateNewId());
		dbo.setEtag(eTagGenerator.generateETag());
		dbo = basicDao.createNew(dbo);
		populateMembershipInvitees(dbo.getId(), dto.getInvitees());
		MembershipInvtnSubmission result = MembershipInvtnSubmissionUtils.copyDboToDto(dbo);
		return result;
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void populateMembershipInvitees(long invitationId, List<String> invitees) throws DatastoreException {
		List<DBOMembershipInvitee> mis = new ArrayList<DBOMembershipInvitee>();
		for (String inviteeId : invitees) {
			DBOMembershipInvitee mi = new DBOMembershipInvitee();
			mi.setInvitationId(invitationId);
			mi.setInviteeId(Long.parseLong(inviteeId));
			mis.add(mi);
		}
		basicDao.createBatch(mis);
	}
	

	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO#get(java.lang.String)
	 */
	@Override
	public MembershipInvtnSubmission get(String id) throws DatastoreException, NotFoundException {
		MapSqlParameterSource param = new MapSqlParameterSource();
		param.addValue(COL_TEAM_ID.toLowerCase(), id);
		DBOMembershipInvtnSubmission dbo = basicDao.getObjectByPrimaryKey(DBOMembershipInvtnSubmission.class, param);
		MembershipInvtnSubmission dto = MembershipInvtnSubmissionUtils.copyDboToDto(dbo);
		// note:  there's no need to retrieve from the MembershipInvitee table, since the invitee list is serialized 
		// into the properties blob
		return dto;
	}

	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO#delete(java.lang.String)
	 */
	@Override
	public void delete(String id) throws DatastoreException, NotFoundException {
		MapSqlParameterSource param = new MapSqlParameterSource();
		param.addValue(COL_TEAM_ID.toLowerCase(), id);
		basicDao.deleteObjectByPrimaryKey(DBOMembershipInvtnSubmission.class, param);
		// note:  there's no need to clean up the membership-invitees table since we have an 'on delete cascade' constraint<commit_msg>Edit method call for the change in the basicDao's methods after merging<commit_after>/**
 * 
 */
package org.sagebionetworks.repo.model.dbo.dao;

import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_GROUP_MEMBERS_GROUP_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_GROUP_MEMBERS_MEMBER_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_PROPERTIES;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITEE_INVITATION_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_MEMBERSHIP_INVITEE_INVITEE_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.COL_TEAM_ID;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.LIMIT_PARAM_NAME;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.OFFSET_PARAM_NAME;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_GROUP_MEMBERS;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_MEMBERSHIP_INVITATION_SUBMISSION;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_MEMBERSHIP_INVITEE;
import static org.sagebionetworks.repo.model.query.jdo.SqlConstants.TABLE_TEAM;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.sagebionetworks.ids.ETagGenerator;
import org.sagebionetworks.ids.IdGenerator;
import org.sagebionetworks.repo.model.DatastoreException;
import org.sagebionetworks.repo.model.GroupMembersDAO;
import org.sagebionetworks.repo.model.InvalidModelException;
import org.sagebionetworks.repo.model.MembershipInvitation;
import org.sagebionetworks.repo.model.MembershipInvtnSubmission;
import org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.repo.model.dbo.DBOBasicDao;
import org.sagebionetworks.repo.model.dbo.persistence.DBOMembershipInvitee;
import org.sagebionetworks.repo.model.dbo.persistence.DBOMembershipInvtnSubmission;
import org.sagebionetworks.repo.model.dbo.persistence.DBOTeam;
import org.sagebionetworks.repo.web.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author brucehoff
 *
 */
public class DBOMembershipInvtnSubmissionDAOImpl implements MembershipInvtnSubmissionDAO {

	@Autowired
	private DBOBasicDao basicDao;	
	@Autowired
	private IdGenerator idGenerator;
	@Autowired
	private ETagGenerator eTagGenerator;
	@Autowired
	private SimpleJdbcTemplate simpleJdbcTemplate;
	@Autowired
	GroupMembersDAO groupMembersDAO;
	
	private static final String INVITEE_ID_COLUMN_LABEL = "INVITEE_ID";

	private static final String SELECT_OPEN_INVITATIONS_BY_USER_PAGINATED = 
			"SELECT mis.*, mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" as "+INVITEE_ID_COLUMN_LABEL+" "+" FROM "+
					TABLE_MEMBERSHIP_INVITATION_SUBMISSION+" mis, "+
					TABLE_MEMBERSHIP_INVITEE+" mi "+
			" WHERE mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITATION_ID+
			" AND mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+"=:"+COL_MEMBERSHIP_INVITEE_INVITEE_ID+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+" NOT IN (SELECT "+COL_GROUP_MEMBERS_GROUP_ID+" FROM "+
				TABLE_GROUP_MEMBERS+" WHERE "+COL_GROUP_MEMBERS_MEMBER_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" ) "+
			" AND ( mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" IS NULL OR mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+">:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" ) "+
			" LIMIT :"+LIMIT_PARAM_NAME+" OFFSET :"+OFFSET_PARAM_NAME;
	
	private static final String SELECT_OPEN_INVITATIONS_BY_TEAM_AND_USER_PAGINATED = 
			"SELECT mis.*, mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" as "+INVITEE_ID_COLUMN_LABEL+" "+" FROM "+
					TABLE_MEMBERSHIP_INVITATION_SUBMISSION+" mis, "+
					TABLE_MEMBERSHIP_INVITEE+" mi "+
			" WHERE mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITATION_ID+
			" AND mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+"=:"+COL_MEMBERSHIP_INVITEE_INVITEE_ID+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+" NOT IN (SELECT "+COL_GROUP_MEMBERS_GROUP_ID+" FROM "+
				TABLE_GROUP_MEMBERS+" WHERE "+COL_GROUP_MEMBERS_MEMBER_ID+"=mi."+COL_MEMBERSHIP_INVITEE_INVITEE_ID+" ) "+
				" AND ( mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" IS NULL OR mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+">:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_EXPIRES_ON+" ) "+
			" AND mis."+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+"=:"+COL_MEMBERSHIP_INVITATION_SUBMISSION_TEAM_ID+
			" LIMIT :"+LIMIT_PARAM_NAME+" OFFSET :"+OFFSET_PARAM_NAME;
	
	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MemberRqstSubmissionDAO#create(org.sagebionetworks.repo.model.MemberRqstSubmission)
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public MembershipInvtnSubmission create(MembershipInvtnSubmission dto) throws DatastoreException,
	InvalidModelException {
		DBOMembershipInvtnSubmission dbo = new DBOMembershipInvtnSubmission();
		MembershipInvtnSubmissionUtils.copyDtoToDbo(dto, dbo);
		if (dbo.getId()==null) dbo.setId(idGenerator.generateNewId());
		dbo.setEtag(eTagGenerator.generateETag());
		dbo = basicDao.createNew(dbo);
		populateMembershipInvitees(dbo.getId(), dto.getInvitees());
		MembershipInvtnSubmission result = MembershipInvtnSubmissionUtils.copyDboToDto(dbo);
		return result;
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void populateMembershipInvitees(long invitationId, List<String> invitees) throws DatastoreException {
		List<DBOMembershipInvitee> mis = new ArrayList<DBOMembershipInvitee>();
		for (String inviteeId : invitees) {
			DBOMembershipInvitee mi = new DBOMembershipInvitee();
			mi.setInvitationId(invitationId);
			mi.setInviteeId(Long.parseLong(inviteeId));
			mis.add(mi);
		}
		basicDao.createBatch(mis, false);
	}
	

	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO#get(java.lang.String)
	 */
	@Override
	public MembershipInvtnSubmission get(String id) throws DatastoreException, NotFoundException {
		MapSqlParameterSource param = new MapSqlParameterSource();
		param.addValue(COL_TEAM_ID.toLowerCase(), id);
		DBOMembershipInvtnSubmission dbo = basicDao.getObjectByPrimaryKey(DBOMembershipInvtnSubmission.class, param);
		MembershipInvtnSubmission dto = MembershipInvtnSubmissionUtils.copyDboToDto(dbo);
		// note:  there's no need to retrieve from the MembershipInvitee table, since the invitee list is serialized 
		// into the properties blob
		return dto;
	}

	/* (non-Javadoc)
	 * @see org.sagebionetworks.repo.model.MembershipInvtnSubmissionDAO#delete(java.lang.String)
	 */
	@Override
	public void delete(String id) throws DatastoreException, NotFoundException {
		MapSqlParameterSource param = new MapSqlParameterSource();
		param.addValue(COL_TEAM_ID.toLowerCase(), id);
		basicDao.deleteObjectByPrimaryKey(DBOMembershipInvtnSubmission.class, param);
		// note:  there's no need to clean up the membership-invitees table since we have an 'on delete cascade' constraint
{'fraction_non_alphanumeric': 0.09523206483885266, 'fraction_numerical': 0.0, 'mean_word_length': 7.477724100912507, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5568996', 'n_tokens_mistral': 5992, 'n_tokens_neox': 5874, 'n_words': 725}