Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.authserver.svc.impl; import static org.joda.time.DateTime.now; import static org.joda.time.Years.years; import static org.joda.time.Years.yearsBetween; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; import org.apigw.authserver.svc.ResidentServices; import org.joda.time.DateTime; import org.joda.time.format.ISODateTimeFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import se.riv.population.residentmaster.lookupresidentforfullprofile.v1.rivtabp21.LookupResidentForFullProfileResponderInterface; import se.riv.population.residentmaster.lookupresidentforfullprofileresponder.v1.LookupResidentForFullProfileResponseType; import se.riv.population.residentmaster.lookupresidentforfullprofileresponder.v1.LookupResidentForFullProfileType; import se.riv.population.residentmaster.v1.RelationStatusTYPE; import se.riv.population.residentmaster.v1.RelationerTYPE.Relation; import se.riv.population.residentmaster.v1.RelationstypTYPE; import se.riv.population.residentmaster.v1.ResidentType; @Service public class ResidentServicesImpl implements ResidentServices { private static final Logger log = LoggerFactory.getLogger(ResidentServicesImpl.class); @Value("${authserver.population.residentmaster.logicalAddress}") private String lookupResidentForFullProfileLogicalAddress; @Value("${authserver.legal.guardian.age.limit}") private int legalGuradianAgeLimit; LookupResidentForFullProfileResponderInterface lookupResidentForFullProfileClient; private static final String RESIDENT_ID_PATTERN_REGEX = "^\\d{12}$"; private static final Pattern RESIDENT_ID_PATTERN = Pattern.compile(RESIDENT_ID_PATTERN_REGEX); @Override public boolean validateLegalGuardian(String legalGuardianResidentIdentificationNumber, String childResidentIdentificationNumber) throws IllegalArgumentException { log.debug("validateLegalGuardian"); if (!RESIDENT_ID_PATTERN.matcher(legalGuardianResidentIdentificationNumber).matches()) { log.debug("legalGuardianResidentIdentificationNumber doesn't match pattern {}", RESIDENT_ID_PATTERN_REGEX); throw new IllegalArgumentException( "legalGuardianResidentIdentificationNumber doesn't match pattern " + RESIDENT_ID_PATTERN_REGEX); } if (!RESIDENT_ID_PATTERN.matcher(childResidentIdentificationNumber).matches()) { log.debug("childResidentIdentificationNumber doesn't match pattern {}", RESIDENT_ID_PATTERN_REGEX); throw new IllegalArgumentException( "childResidentIdentificationNumber doesn't match pattern " + RESIDENT_ID_PATTERN_REGEX); } if (isOverAgeLimit(childResidentIdentificationNumber)) { log.debug("childResidentIdentificationNumber doesn't pass the ageLimit of {}", legalGuradianAgeLimit); return false; } LookupResidentForFullProfileType params = new LookupResidentForFullProfileType(); params.getPersonId().add(legalGuardianResidentIdentificationNumber); // LookUpSpecificationType spec = new LookUpSpecificationType(); // params.setLookUpSpecification(spec); LookupResidentForFullProfileResponseType response = lookupResidentForFullProfileClient .lookupResidentForFullProfile(lookupResidentForFullProfileLogicalAddress, params); if (response.getResident() == null || response.getResident().size() == 0) { log.warn( "A person with residentIdentificationNumber: {} wasn't found in the LookupResidentForFullProfile service."); return false; } boolean legalGuardianIsValid = false; SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); Date now = new Date(); for (ResidentType resident : response.getResident()) { if (resident.getPersonpost() != null && resident.getPersonpost().getRelationer() != null) { for (Relation relation : resident.getPersonpost().getRelationer().getRelation()) { if (relation.getRelationstyp() == RelationstypTYPE.V) { log.debug("Found relation type V"); try { if (relation.getRelationFromdatum() != null) { log.debug("Found relation from date {}", relation.getRelationFromdatum()); Date from = df.parse(relation.getRelationFromdatum()); if (from.after(now)) { log.debug("This relation has not reached the from date, continuing..."); continue; } } if (relation.getRelationTomdatum() != null) { log.debug("Found relation tom date {}", relation.getRelationTomdatum()); Date until = df.parse(relation.getRelationTomdatum()); if (until.before(now)) { log.debug("This relation has passed the until date, continuing..."); continue; } } } catch (ParseException e) { log.error( "ParseException while parsing from {} or until {} date, skipping this relation and continues...", relation.getRelationFromdatum(), relation.getRelationTomdatum()); continue; } if (relation.getAvregistrering() != null) { log.debug("This relation has been unregistered, continuing..."); continue; } if (relation.getStatus() == RelationStatusTYPE.AS || relation.getStatus() == RelationStatusTYPE.AV || relation.getStatus() == RelationStatusTYPE.IV || relation.getStatus() == RelationStatusTYPE.AN) { log.debug("This relation has status {}, continuing...", relation.getStatus()); } if (relation.getRelationId() != null && !StringUtils.isBlank(relation.getRelationId().getPersonNr()) && childResidentIdentificationNumber .equals(relation.getRelationId().getPersonNr())) { log.debug("Found matching relation with relation type V"); legalGuardianIsValid = true; break; } log.debug("This V relation didn't match"); } else { log.debug("Found non V relation type: {}, continuing", relation.getRelationstyp()); continue; } } } if (legalGuardianIsValid) { break; } } log.debug("validateLegalGuardian(legalGuardianResidentIdentificationNumber:NOT_LOGGED, " + "childResidentIdentificationNumber:NOT_LOGGED) returns: {}", legalGuardianIsValid); return legalGuardianIsValid; } private boolean isOverAgeLimit(String childIdNumber) { final String dateOfBirthString = childIdNumber.substring(0, 8); final DateTime dateOfBirth = DateTime.parse(dateOfBirthString, ISODateTimeFormat.basicDate()); return yearsBetween(dateOfBirth, now()).isGreaterThan(years(legalGuradianAgeLimit)); } @Autowired public void setLookupResidentForFullProfileClient( LookupResidentForFullProfileResponderInterface lookupResidentForFullProfileClient) { this.lookupResidentForFullProfileClient = lookupResidentForFullProfileClient; } }