001package org.kuali.coeus.s2sgen.impl.person;
002
003
004import org.kuali.coeus.propdev.api.core.ProposalDevelopmentDocumentContract;
005import org.kuali.coeus.propdev.api.person.ProposalPersonContract;
006import org.kuali.coeus.propdev.api.s2s.S2SConfigurationService;
007
008import org.kuali.coeus.s2sgen.impl.citizenship.CitizenshipType;
009import org.kuali.coeus.s2sgen.impl.citizenship.CitizenshipTypeService;
010import org.kuali.coeus.s2sgen.api.core.ConfigurationConstants;
011import org.springframework.beans.factory.annotation.Autowired;
012import org.springframework.beans.factory.annotation.Qualifier;
013import org.springframework.stereotype.Component;
014
015import java.util.ArrayList;
016import java.util.List;
017
018@Component("s2SProposalPersonService")
019public class S2SProposalPersonServiceImpl implements S2SProposalPersonService {
020
021    @Autowired
022    @Qualifier("s2SConfigurationService")
023    private S2SConfigurationService s2SConfigurationService;
024
025    @Autowired
026    @Qualifier("citizenshipTypeService")
027    private CitizenshipTypeService citizenshipTypeService;
028
029    /**
030     * This method limits the number of key persons to n, returns list of key persons, first n in case firstN is true, or all other
031     * than first n, in case of firstN being false
032     *
033     * @param proposalPersons list of {@link org.kuali.coeus.propdev.api.person.ProposalPersonContract}
034     * @param firstN value that determines whether the returned list should contain first n persons or the rest of persons
035     * @param n number of key persons that are considered as not extra persons
036     * @return list of {@link org.kuali.coeus.propdev.api.person.ProposalPersonContract}
037     */
038    @Override
039    public List<ProposalPersonContract> getNKeyPersons(List<? extends ProposalPersonContract> proposalPersons, boolean firstN, int n) {
040        ProposalPersonContract proposalPerson, previousProposalPerson;
041        int size = proposalPersons.size();
042
043        for (int i = size - 1; i > 0; i--) {
044            proposalPerson = proposalPersons.get(i);
045            previousProposalPerson = proposalPersons.get(i - 1);
046            if (proposalPerson.getPersonId() != null && previousProposalPerson.getPersonId() != null
047                    && proposalPerson.getPersonId().equals(previousProposalPerson.getPersonId())) {
048                proposalPersons.remove(i);
049            }
050            else if (proposalPerson.getRolodexId() != null && previousProposalPerson.getRolodexId() != null
051                    && proposalPerson.getRolodexId().equals(previousProposalPerson.getRolodexId())) {
052                proposalPersons.remove(i);
053            }
054        }
055
056        size = proposalPersons.size();
057        if (firstN) {
058            List<ProposalPersonContract> firstNPersons = new ArrayList<ProposalPersonContract>();
059
060            // Make sure we don't exceed the size of the list.
061            if (size > n) {
062                size = n;
063            }
064            // remove extras
065            for (int i = 0; i < size; i++) {
066                firstNPersons.add(proposalPersons.get(i));
067            }
068            return firstNPersons;
069        }
070        else {
071            // return extra people
072            List<ProposalPersonContract> extraPersons = new ArrayList<ProposalPersonContract>();
073            for (int i = n; i < size; i++) {
074                extraPersons.add(proposalPersons.get(i));
075            }
076            return extraPersons;
077        }
078    }
079
080    /**
081     * This method is to get PrincipalInvestigator from person list
082     *
083     * @param pdDoc Proposal development document.
084     * @return ProposalPerson PrincipalInvestigator for the proposal.
085     */
086    @Override
087    public ProposalPersonContract getPrincipalInvestigator(ProposalDevelopmentDocumentContract pdDoc) {
088        ProposalPersonContract proposalPerson = null;
089        if (pdDoc != null) {
090            for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
091                if (person.isPrincipalInvestigator()) {
092                    proposalPerson = person;
093                }
094            }
095        }
096        return proposalPerson;
097    }
098
099    /**
100     * Finds all the Investigators associated with the provided pdDoc.
101     */
102    @Override
103    public List<ProposalPersonContract> getCoInvestigators(ProposalDevelopmentDocumentContract pdDoc) {
104        List<ProposalPersonContract> investigators = new ArrayList<ProposalPersonContract>();
105        if (pdDoc != null) {
106            for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
107                //multi-pis are still considered co-i within S2S.
108                if (person.isCoInvestigator() || person.isMultiplePi()) {
109                    investigators.add(person);
110                }
111            }
112        }
113        return investigators;
114    }
115
116    /**
117     * Finds all the key Person associated with the provided pdDoc.
118     */
119    @Override
120    public List<ProposalPersonContract> getKeyPersons(ProposalDevelopmentDocumentContract pdDoc) {
121        List<ProposalPersonContract> keyPersons = new ArrayList<ProposalPersonContract>();
122        if (pdDoc != null) {
123            for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
124                if (person.isKeyPerson()) {
125                    keyPersons.add(person);
126                }
127            }
128        }
129        return keyPersons;
130    }
131
132    /**
133     * Implementation should return one of the enums defined in PHS398CareerDevelopmentAwardSup11V11 form schema. For now, it
134     * returns US RESIDENT as default
135     */
136    @Override
137    public CitizenshipType getCitizenship(ProposalPersonContract proposalPerson) {
138        String citizenSource = "1";
139        String piCitizenShipValue = s2SConfigurationService.getValueAsString(ConfigurationConstants.PI_CUSTOM_DATA);
140        if (piCitizenShipValue != null) {
141            citizenSource = piCitizenShipValue;
142        }
143        if (citizenSource.equals("0")) {
144            CitizenshipType citizenShipType = citizenshipTypeService.getCitizenshipDataFromExternalSource();
145            return citizenShipType;
146        }
147        else {
148            Integer citizenShip;
149            Boolean allowOverride = s2SConfigurationService.getValueAsBoolean(
150                    ConfigurationConstants.ALLOW_PROPOSAL_PERSON_TO_OVERRIDE_KC_PERSON_EXTENDED_ATTRIBUTES);
151            if (allowOverride) {
152                citizenShip = proposalPerson.getCitizenshipType().getCode();
153            }
154            else {
155                citizenShip = proposalPerson.getPerson().getCitizenshipTypeCode();
156            }
157            CitizenshipType retVal = null;
158            String citizenShipCode = String.valueOf(citizenShip);
159            if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
160                    ConfigurationConstants.NON_US_CITIZEN_WITH_TEMPORARY_VISA_TYPE_CODE))) {
161                return CitizenshipType.NON_US_CITIZEN_WITH_TEMPORARY_VISA;
162            }
163            else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
164                    ConfigurationConstants.PERMANENT_RESIDENT_OF_US_TYPE_CODE))) {
165                return CitizenshipType.PERMANENT_RESIDENT_OF_US;
166            }
167            else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
168                    ConfigurationConstants.US_CITIZEN_OR_NONCITIZEN_NATIONAL_TYPE_CODE))) {
169                return CitizenshipType.US_CITIZEN_OR_NONCITIZEN_NATIONAL;
170            }
171            else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
172                    ConfigurationConstants.PERMANENT_RESIDENT_OF_US_PENDING))) {
173                return CitizenshipType.PERMANENT_RESIDENT_OF_US_PENDING;
174            }
175            else {
176                throw new IllegalArgumentException("Invalid citizenship type provided");
177            }
178
179        }
180    }
181}