package org.xcolab.portlets.admintasks;

import java.io.IOException;
import java.io.InputStreamReader;

import au.com.bytecode.opencsv.CSVReader;

import com.ext.portlet.model.ContestPhase;
import com.ext.portlet.service.ContestLocalServiceUtil;
import com.ext.portlet.service.ContestPhaseLocalServiceUtil;
import com.ext.portlet.service.Proposal2PhaseLocalServiceUtil;
import com.ext.portlet.service.ProposalLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

public class FixForRemovedProposalsTeamMembers {
	
	private final static String removedMembersList =  "removedTeamMembers.csv";
	
	public String processRemovedMembers() throws IOException, PortalException, SystemException {
		CSVReader reader = new CSVReader(new InputStreamReader(FixForRemovedProposalsTeamMembers.class.getClassLoader().getResourceAsStream(removedMembersList)));
		
		String[] line = null;
		
		while ( ( line = reader.readNext()) != null) {
			Long proposalId = Long.parseLong(line[0]);
			Long memberId = Long.parseLong(line[1]);
			
			boolean isInActiveContest = false;
			
			for (Long contestPhasePK: Proposal2PhaseLocalServiceUtil.getContestPhasesForProposal(proposalId)) {
				ContestPhase cp = ContestPhaseLocalServiceUtil.getContestPhase(contestPhasePK);
				isInActiveContest |= ContestLocalServiceUtil.isActive(ContestLocalServiceUtil.getContest(cp.getContestPK()));
			}
			
			User user = UserLocalServiceUtil.getUser(memberId);
			
			if (! ProposalLocalServiceUtil.isUserAMember(proposalId, memberId)) {
				System.out.println(proposalId + "," + memberId + ",\"" + user.getScreenName() + "\",\"" + user.getFullName() + "\",\"" + user.getEmailAddress() + "\"," + isInActiveContest);
			}
		}
		
		
		return null;
	}

}

@@ -183,16 +183,55 @@
 tPhase;%0A
+import com.ext.portlet.model.Proposal;%0A
 import c
@@ -617,16 +617,73 @@
 l.User;%0A
+import com.liferay.portal.service.GroupLocalServiceUtil;%0A
 import c
@@ -1699,17 +1699,27 @@
 %0A%09%09%09
-%0A%09%09%09if (!
+Proposal proposal =
  Pro
@@ -1744,222 +1744,239 @@
 til.
-isUserAMember(proposalId, memberId)) %7B%0A%09%09%09%09System.out.println(proposalId + %22,%22 + memberId + %22,%5C%22%22 + user.getScreenName() + %22%5C%22,%5C%22%22 + user.getFullName() + %22%5C%22,%5C%22%22 + user.getEmailAddress() + %22%5C%22,%22 + isInActiveContest
+getProposal(proposalId);%0A%09%09%09%0A%09%09%09if (memberId != 10144) %7B%0A%09%09%09%09System.out.println(%22adding user to proposal: %22 + proposalId + %22, user: %22 + memberId);%0A%09%09%09%09GroupLocalServiceUtil.setUserGroups(memberId, new long%5B%5D %7Bproposal.getGroupId()%7D
 );%0A%09

Fix for removed proposal team members (readding them)