View Javadoc
1   /*
2    * Kuali Coeus, a comprehensive research administration system for higher education.
3    * 
4    * Copyright 2005-2015 Kuali, Inc.
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.coeus.s2sgen.impl.person;
20  
21  
22  import org.kuali.coeus.propdev.api.core.ProposalDevelopmentDocumentContract;
23  import org.kuali.coeus.propdev.api.person.ProposalPersonContract;
24  import org.kuali.coeus.propdev.api.s2s.S2SConfigurationService;
25  
26  import org.kuali.coeus.s2sgen.impl.citizenship.CitizenshipType;
27  import org.kuali.coeus.s2sgen.impl.citizenship.CitizenshipTypeService;
28  import org.kuali.coeus.s2sgen.api.core.ConfigurationConstants;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.beans.factory.annotation.Qualifier;
31  import org.springframework.stereotype.Component;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  @Component("s2SProposalPersonService")
37  public class S2SProposalPersonServiceImpl implements S2SProposalPersonService {
38  
39      @Autowired
40      @Qualifier("s2SConfigurationService")
41      private S2SConfigurationService s2SConfigurationService;
42  
43      @Autowired
44      @Qualifier("citizenshipTypeService")
45      private CitizenshipTypeService citizenshipTypeService;
46  
47      /**
48       * 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
49       * than first n, in case of firstN being false
50       *
51       * @param proposalPersons list of {@link org.kuali.coeus.propdev.api.person.ProposalPersonContract}
52       * @param firstN value that determines whether the returned list should contain first n persons or the rest of persons
53       * @param n number of key persons that are considered as not extra persons
54       * @return list of {@link org.kuali.coeus.propdev.api.person.ProposalPersonContract}
55       */
56      @Override
57      public List<ProposalPersonContract> getNKeyPersons(List<? extends ProposalPersonContract> proposalPersons, boolean firstN, int n) {
58          ProposalPersonContract proposalPerson, previousProposalPerson;
59          int size = proposalPersons.size();
60  
61          for (int i = size - 1; i > 0; i--) {
62              proposalPerson = proposalPersons.get(i);
63              previousProposalPerson = proposalPersons.get(i - 1);
64              if (proposalPerson.getPersonId() != null && previousProposalPerson.getPersonId() != null
65                      && proposalPerson.getPersonId().equals(previousProposalPerson.getPersonId())) {
66                  proposalPersons.remove(i);
67              }
68              else if (proposalPerson.getRolodexId() != null && previousProposalPerson.getRolodexId() != null
69                      && proposalPerson.getRolodexId().equals(previousProposalPerson.getRolodexId())) {
70                  proposalPersons.remove(i);
71              }
72          }
73  
74          size = proposalPersons.size();
75          if (firstN) {
76              List<ProposalPersonContract> firstNPersons = new ArrayList<ProposalPersonContract>();
77  
78              // Make sure we don't exceed the size of the list.
79              if (size > n) {
80                  size = n;
81              }
82              // remove extras
83              for (int i = 0; i < size; i++) {
84                  firstNPersons.add(proposalPersons.get(i));
85              }
86              return firstNPersons;
87          }
88          else {
89              // return extra people
90              List<ProposalPersonContract> extraPersons = new ArrayList<ProposalPersonContract>();
91              for (int i = n; i < size; i++) {
92                  extraPersons.add(proposalPersons.get(i));
93              }
94              return extraPersons;
95          }
96      }
97  
98      /**
99       * This method is to get PrincipalInvestigator from person list
100      *
101      * @param pdDoc Proposal development document.
102      * @return ProposalPerson PrincipalInvestigator for the proposal.
103      */
104     @Override
105     public ProposalPersonContract getPrincipalInvestigator(ProposalDevelopmentDocumentContract pdDoc) {
106         ProposalPersonContract proposalPerson = null;
107         if (pdDoc != null) {
108             for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
109                 if (person.isPrincipalInvestigator()) {
110                     proposalPerson = person;
111                 }
112             }
113         }
114         return proposalPerson;
115     }
116 
117     /**
118      * Finds all the Investigators associated with the provided pdDoc.
119      */
120     @Override
121     public List<ProposalPersonContract> getCoInvestigators(ProposalDevelopmentDocumentContract pdDoc) {
122         List<ProposalPersonContract> investigators = new ArrayList<ProposalPersonContract>();
123         if (pdDoc != null) {
124             for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
125                 //multi-pis are still considered co-i within S2S.
126                 if (person.isCoInvestigator() || person.isMultiplePi()) {
127                     investigators.add(person);
128                 }
129             }
130         }
131         return investigators;
132     }
133 
134     /**
135      * Finds all the key Person associated with the provided pdDoc.
136      */
137     @Override
138     public List<ProposalPersonContract> getKeyPersons(ProposalDevelopmentDocumentContract pdDoc) {
139         List<ProposalPersonContract> keyPersons = new ArrayList<ProposalPersonContract>();
140         if (pdDoc != null) {
141             for (ProposalPersonContract person : pdDoc.getDevelopmentProposal().getProposalPersons()) {
142                 if (person.isKeyPerson()) {
143                     keyPersons.add(person);
144                 }
145             }
146         }
147         return keyPersons;
148     }
149 
150     /**
151      * Implementation should return one of the enums defined in PHS398CareerDevelopmentAwardSup11V11 form schema. For now, it
152      * returns US RESIDENT as default
153      */
154     @Override
155     public CitizenshipType getCitizenship(ProposalPersonContract proposalPerson) {
156         String citizenSource = "1";
157         String piCitizenShipValue = s2SConfigurationService.getValueAsString(ConfigurationConstants.PI_CUSTOM_DATA);
158         if (piCitizenShipValue != null) {
159             citizenSource = piCitizenShipValue;
160         }
161         if (citizenSource.equals("0")) {
162             CitizenshipType citizenShipType = citizenshipTypeService.getCitizenshipDataFromExternalSource();
163             return citizenShipType;
164         }
165         else {
166             Integer citizenShip;
167             Boolean allowOverride = s2SConfigurationService.getValueAsBoolean(
168                     ConfigurationConstants.ALLOW_PROPOSAL_PERSON_TO_OVERRIDE_KC_PERSON_EXTENDED_ATTRIBUTES);
169             if (allowOverride) {
170                 citizenShip = proposalPerson.getCitizenshipType().getCode();
171             }
172             else {
173                 citizenShip = proposalPerson.getPerson().getCitizenshipTypeCode();
174             }
175             CitizenshipType retVal = null;
176             String citizenShipCode = String.valueOf(citizenShip);
177             if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
178                     ConfigurationConstants.NON_US_CITIZEN_WITH_TEMPORARY_VISA_TYPE_CODE))) {
179                 return CitizenshipType.NON_US_CITIZEN_WITH_TEMPORARY_VISA;
180             }
181             else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
182                     ConfigurationConstants.PERMANENT_RESIDENT_OF_US_TYPE_CODE))) {
183                 return CitizenshipType.PERMANENT_RESIDENT_OF_US;
184             }
185             else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
186                     ConfigurationConstants.US_CITIZEN_OR_NONCITIZEN_NATIONAL_TYPE_CODE))) {
187                 return CitizenshipType.US_CITIZEN_OR_NONCITIZEN_NATIONAL;
188             }
189             else if (citizenShipCode.equals(s2SConfigurationService.getValueAsString(
190                     ConfigurationConstants.PERMANENT_RESIDENT_OF_US_PENDING))) {
191                 return CitizenshipType.PERMANENT_RESIDENT_OF_US_PENDING;
192             }
193             else {
194                 throw new IllegalArgumentException("Invalid citizenship type provided");
195             }
196 
197         }
198     }
199 }