private Participant copyFrom(Participant participant) { final Participant p = ParticipantFactory.getInstance().createObject(); p.setId(participant.getId()); p.setActivityStatus(participant.getActivityStatus()); p.setBirthDate(participant.getBirthDate()); p.setEthnicity(participant.getEthnicity()); p.setFirstName(participant.getFirstName()); p.setLastName(participant.getLastName()); p.setGender(participant.getGender()); p.setMetaPhoneCode(participant.getMetaPhoneCode()); p.setSocialSecurityNumber(participant.getSocialSecurityNumber()); p.setVitalStatus(participant.getVitalStatus()); final Iterator<Race> iter = participant.getRaceCollection().iterator(); while (iter.hasNext()) { final Race race = (Race) iter.next(); final Race r = RaceFactory.getInstance().createObject(); r.setParticipant(p); r.setRaceName(race.getRaceName()); p.getRaceCollection().add(r); } final Iterator<CollectionProtocolRegistration> cprIter = participant.getCollectionProtocolRegistrationCollection().iterator(); while (cprIter.hasNext()) { final CollectionProtocolRegistration collectionProtocolRegistration = (CollectionProtocolRegistration) cprIter.next(); final CollectionProtocol collectionProtocol = collectionProtocolRegistration.getCollectionProtocol(); final CollectionProtocol cp = CollectionProtocolFactory.getInstance().createObject(); cp.setActivityStatus(collectionProtocol.getActivityStatus()); cp.setTitle(collectionProtocol.getTitle()); cp.setShortTitle(collectionProtocol.getShortTitle()); final CollectionProtocolRegistration cpr = CollectionProtocolRegistrationFactory.getInstance().createObject(); cpr.setParticipant(p); cpr.setConsentSignatureDate(collectionProtocolRegistration.getConsentSignatureDate()); cpr.setRegistrationDate(collectionProtocolRegistration.getRegistrationDate()); cpr.setProtocolParticipantIdentifier( collectionProtocolRegistration.getProtocolParticipantIdentifier()); // setting empty consenttier status collection, as update flow will not update consent tier cpr.setConsentTierResponseCollection( (Collection<ConsentTierResponse>) new LinkedHashSet<ConsentTierResponse>()); cpr.setCollectionProtocol(cp); p.getCollectionProtocolRegistrationCollection().add(cpr); } return p; }
/** * This method is used to update the Race collection. It will replace the existing Race with the * current Race and then if the current race is MORE, then it will add remaining new race inside * the collection. If the new Race is LESSER than existing then it will set the remaining * existing(after replacing with current race) to null. This logic is implemented to avoid * unnecessary null records in the database. * * @return */ private Participant updateParticipantRaceCollection( Participant existingParticipant, Participant participant) { final Race[] existRaceArray = (Race[]) existingParticipant .getRaceCollection() .toArray(new Race[existingParticipant.getRaceCollection().size()]); final Race[] newRaceArray = (Race[]) participant .getRaceCollection() .toArray(new Race[participant.getRaceCollection().size()]); final int existRaceCount = existRaceArray.length; final int newRaceCount = newRaceArray.length; // if the existing Race are more than the new/incoming Race if (existRaceCount >= newRaceCount) { int i = 0; for (; i < newRaceCount; i++) { // Iterate(till newRaceCount) & Replace the existing Race with the new/incoming Race existRaceArray[i].setRaceName(newRaceArray[i].getRaceName()); existRaceArray[i].setParticipant(newRaceArray[i].getParticipant()); } for (; i < existRaceCount; i++) { // set the remaining(more) existing Race to NULL existRaceArray[i].setRaceName(null); existRaceArray[i].setParticipant(null); } final Set<Race> mySet = new HashSet<Race>(); Collections.addAll(mySet, existRaceArray); participant.setRaceCollection(mySet); } else { // if the existing Race are LESS than the new/incoming Race int i = 0; for (; i < existRaceCount; i++) { // Iterate(till existRaceCount) & Replace the existing Race with the new/incoming Race existRaceArray[i].setRaceName(newRaceArray[i].getRaceName()); existRaceArray[i].setParticipant(newRaceArray[i].getParticipant()); } final Set<Race> mySet = new HashSet<Race>(); Collections.addAll(mySet, existRaceArray); participant.setRaceCollection(mySet); for (; i < newRaceCount; i++) { // add the remaining left new/incoming Race in the collection participant.getRaceCollection().add(newRaceArray[i]); } } return participant; }