<commit_before>package uk.gov.dvla.services.mib;

import uk.gov.dvla.domain.*;
import uk.gov.dvla.domain.mib.EntitlementType;
import uk.gov.dvla.domain.mib.MibDTO;
import uk.gov.dvla.services.transform.TransformService;

import java.util.*;

public class MibDriverTransformService implements TransformService<RulesDriver, MibDTO> {

    private static final String MIB_CURRENT_FULL_LICENCE = "FC";
    private static final String MIB_CURRENT_PROV_LICENCE = "PC";
    private static final String MIB_EXPIRED_FULL_LICENCE = "FE";
    private static final String MIB_EXPIRED_PROV_LICENCE = "PE";
    private static final String MIB_DISQUALIFIED_LICENCE = "DQ";
    private static final String MIB_SURRENDERED_PROV_LICENCE = "PS";
    private static final String MIB_SURRENDERED_FULL_LICENCE = "FS";
    private static final String MIB_DISQUALIFIED_UNTIL_GIVEN_DATE = "DD";
    private static final String MIB_DISQUALIFIED_UNTIL_TEST_PASS = "DT";
    private static final String MIB_DISQUALIFIED_UNTIL_GIVEN_DATE_AND_TEST_PASS = "DP";
    private static final String MIB_DISQUALIFIED_UNTIL_SENTENCED = "DS";
    private static final String MIB_DISQUALIFIED_FOR_LIFE = "DX";
    private static final String MIB_REVOKED_UNTIL_TEST_PASS = "RV";
    private static final String MIB_NO_CURRENT_GB_LICENCE_HELP = "NE";

    private static final String LICENCE_STATUS_A = "A";
    private static final String LICENCE_STATUS_B = "B";
    private static final String LICENCE_STATUS_E = "E";
    private static final String LICENCE_STATUS_F = "F";
    private static final String LICENCE_STATUS_G = "G";
    private static final String LICENCE_STATUS_M = "M";
    private static final String LICENCE_STATUS_O = "O";
    private static final String LICENCE_STATUS_Q = "Q";
    private static final String LICENCE_STATUS_S = "S";

    private static final String DISQUALIFIED_FOR_LIFE = "licence.status.disqualified.for.life";
    private static final String DISQUALIFIED_REAPPLY_WITH_DATE = "licence.status.disqualified.reapply.with.date";
    private static final String DISQUALIFICATION_EXPIRED_REAPPLY_WITH_DATE =
            "licence.status.not.disqualified.reapply.with.date";
    private static final String DISQUALIFIED_UNTIL_DATE = "licence.status.disqualified.until.date";
    private static final String REVOKED_REAPPLY = "licence.status.revoked.reapply";
    private static final String REVOKED = "licence.status.revoked";
    private static final String DISQUALIFIED_UNTIL_SENTENCING = "licence.status.disqualified.until.sentencing";
    private static final String DISQUALIFIED = "licence.status.disqualified";
    private static final String DISQUALIFIED_REAPPLY = "licence.status.disqualified.reapply";


    @Override
    public MibDTO transform(RulesDriver result) {
        MibDTO mibDTO = new MibDTO();

        Driver driver = result.getDriver();
        Licence licence = driver.getLicence();

        if (null != licence) {
            mibDTO.setLicence(getLicence(licence));
            MibDTO.Licence mibLicence = mibDTO.getLicence();
            mibLicence.setStatus(getStatus(result));
            mibLicence.setEntitlements(getEntitlements(driver));
            mibLicence.setEndorsements(getEndorsements(driver));
        }
        return mibDTO;
    }

    private MibDTO.Licence getLicence(Licence licence) {

        MibDTO.Licence mibLicence = new MibDTO.Licence();
        mibLicence.setValidFrom(licence.getValidFrom());
        mibLicence.setValidTo(licence.getValidTo());
        mibLicence.setDirectiveIndicator(licence.getDirectiveStatus());
        return mibLicence;
    }

    private List<MibDTO.Entitlement> getEntitlements(Driver driver) {
        Licence licence = driver.getLicence();
        List<MibDTO.Entitlement> entitlements = new ArrayList<MibDTO.Entitlement>();
        if (licence.getEntitlements() == null) return entitlements;

        for (Entitlement ent : licence.getEntitlements()) {
            MibDTO.Entitlement mibEntitlement = new MibDTO.Entitlement();
            TestPass testPass = driver.getTestPassForEntitlement(ent);
            mibEntitlement.setCode(ent.getCode());
            mibEntitlement.setValidFrom(getValidFrom(ent, testPass));
            mibEntitlement.setValidTo(getValidTo(ent, testPass));
            mibEntitlement.setPriorTo(ent.getPriorTo());
            mibEntitlement.setType(getEntitlementType(ent, testPass));
            mibEntitlement.setRestrictions(getEntitlementRestrictions(ent));

            entitlements.add(mibEntitlement);
        }
        return entitlements;
    }

    private List<MibDTO.Endorsement> getEndorsements(Driver driver) {
        List<MibDTO.Endorsement> endorsements = new ArrayList<MibDTO.Endorsement>();
        if (driver.getLicence().getEndorsements() == null) return endorsements;

        for (Endorsement end : driver.getLicence().getEndorsements()) {
            MibDTO.Endorsement mibEndorsement = new MibDTO.Endorsement();
            mibEndorsement.setCode(end.getCode());
            mibEndorsement.setOffenceDate(end.getOffence());
            mibEndorsement.setConvictionDate(end.getConviction());
            mibEndorsement.setFine(end.getFine());
            mibEndorsement.setNoOfPoints(end.getNoPoints());
            mibEndorsement.setIsDisqual(end.getDisqual());
            mibEndorsement.setDisqualPeriod(end.getDuration());

            mibEndorsement.setCustodialPeriod(end.getCustodialPeriod());
            mibEndorsement.setIndicativeRehabSpentDate(end.getIndicativeRehabSpentDate());
            // TODO: this logic needs to be updated once we confirm what disqual should be returned
            // TODO: check if this logic shouldn't be in Drools rules.
            if (driver.getDisqualifications() != null) {
                for (Disqualification disq : driver.getDisqualifications()) {
                    if (end.getId().equals(disq.getEndorsementID())) {
                        mibEndorsement.setDisqualStartDate(disq.getDisqFromDate());
                        mibEndorsement.setDisqualEndDate(disq.getDisqToDate());
                    }
                }
            }
            endorsements.add(mibEndorsement);
        }

        return endorsements;<commit_msg>defect fixed<commit_after>package uk.gov.dvla.services.mib;

import uk.gov.dvla.domain.*;
import uk.gov.dvla.domain.mib.EntitlementType;
import uk.gov.dvla.domain.mib.MibDTO;
import uk.gov.dvla.services.transform.TransformService;

import java.util.*;

public class MibDriverTransformService implements TransformService<RulesDriver, MibDTO> {

    private static final String MIB_CURRENT_FULL_LICENCE = "FC";
    private static final String MIB_CURRENT_PROV_LICENCE = "PC";
    private static final String MIB_EXPIRED_FULL_LICENCE = "FE";
    private static final String MIB_EXPIRED_PROV_LICENCE = "PE";
    private static final String MIB_DISQUALIFIED_LICENCE = "DQ";
    private static final String MIB_SURRENDERED_PROV_LICENCE = "PS";
    private static final String MIB_SURRENDERED_FULL_LICENCE = "FS";
    private static final String MIB_DISQUALIFIED_UNTIL_GIVEN_DATE = "DD";
    private static final String MIB_DISQUALIFIED_UNTIL_TEST_PASS = "DT";
    private static final String MIB_DISQUALIFIED_UNTIL_GIVEN_DATE_AND_TEST_PASS = "DP";
    private static final String MIB_DISQUALIFIED_UNTIL_SENTENCED = "DS";
    private static final String MIB_DISQUALIFIED_FOR_LIFE = "DX";
    private static final String MIB_REVOKED_UNTIL_TEST_PASS = "RV";
    private static final String MIB_NO_CURRENT_GB_LICENCE_HELP = "NE";

    private static final String LICENCE_STATUS_A = "A";
    private static final String LICENCE_STATUS_B = "B";
    private static final String LICENCE_STATUS_E = "E";
    private static final String LICENCE_STATUS_F = "F";
    private static final String LICENCE_STATUS_G = "G";
    private static final String LICENCE_STATUS_M = "M";
    private static final String LICENCE_STATUS_O = "O";
    private static final String LICENCE_STATUS_Q = "Q";
    private static final String LICENCE_STATUS_S = "S";

    private static final String DISQUALIFIED_FOR_LIFE = "licence.status.disqualified.for.life";
    private static final String DISQUALIFIED_REAPPLY_WITH_DATE = "licence.status.disqualified.reapply.with.date";
    private static final String DISQUALIFICATION_EXPIRED_REAPPLY_WITH_DATE =
            "licence.status.not.disqualified.reapply.with.date";
    private static final String DISQUALIFIED_UNTIL_DATE = "licence.status.disqualified.until.date";
    private static final String REVOKED_REAPPLY = "licence.status.revoked.reapply";
    private static final String REVOKED = "licence.status.revoked";
    private static final String DISQUALIFIED_UNTIL_SENTENCING = "licence.status.disqualified.until.sentencing";
    private static final String DISQUALIFIED = "licence.status.disqualified";
    private static final String DISQUALIFIED_REAPPLY = "licence.status.disqualified.reapply";


    @Override
    public MibDTO transform(RulesDriver result) {
        MibDTO mibDTO = new MibDTO();

        Driver driver = result.getDriver();
        Licence licence = driver.getLicence();

        if (null != licence) {
            mibDTO.setLicence(getLicence(licence));
            MibDTO.Licence mibLicence = mibDTO.getLicence();
            mibLicence.setStatus(getStatus(result));
            mibLicence.setEntitlements(getEntitlements(driver));
            mibLicence.setEndorsements(getEndorsements(driver));
        }
        return mibDTO;
    }

    private MibDTO.Licence getLicence(Licence licence) {

        MibDTO.Licence mibLicence = new MibDTO.Licence();
        mibLicence.setValidFrom(licence.getValidFrom());
        mibLicence.setValidTo(licence.getValidTo());
        mibLicence.setDirectiveIndicator(licence.getDirectiveStatus());
        return mibLicence;
    }

    private List<MibDTO.Entitlement> getEntitlements(Driver driver) {
        Licence licence = driver.getLicence();
        List<MibDTO.Entitlement> entitlements = new ArrayList<MibDTO.Entitlement>();
        if (licence.getEntitlements() == null) return entitlements;

        for (Entitlement ent : licence.getEntitlements()) {
            MibDTO.Entitlement mibEntitlement = new MibDTO.Entitlement();
            TestPass testPass = driver.getTestPassForEntitlement(ent);
            mibEntitlement.setCode(ent.getCode());
            mibEntitlement.setValidFrom(getValidFrom(ent, testPass));
            mibEntitlement.setValidTo(getValidTo(ent, testPass));
            mibEntitlement.setPriorTo(ent.getPriorTo());
            mibEntitlement.setType(getEntitlementType(ent, testPass));
            mibEntitlement.setRestrictions(getEntitlementRestrictions(ent));

            entitlements.add(mibEntitlement);
        }
        return entitlements;
    }

    private List<MibDTO.Endorsement> getEndorsements(Driver driver) {
        List<MibDTO.Endorsement> endorsements = new ArrayList<MibDTO.Endorsement>();
        if (driver.getLicence().getEndorsements() == null) return endorsements;

        for (Endorsement end : driver.getLicence().getEndorsements()) {
            MibDTO.Endorsement mibEndorsement = new MibDTO.Endorsement();
            mibEndorsement.setCode(end.getCode());
            mibEndorsement.setOffenceDate(end.getOffence());
            mibEndorsement.setConvictionDate(end.getConviction());
            mibEndorsement.setFine(end.getFine());
            mibEndorsement.setNoOfPoints(end.getNoPoints());
            mibEndorsement.setIsDisqual(end.getDisqual());
            mibEndorsement.setDisqualPeriod(end.getDuration());

            mibEndorsement.setCustodialPeriod(end.getCustodialPeriod());
            mibEndorsement.setIndicativeRehabilitationSpentDate(end.getIndicativeRehabSpentDate());
            // TODO: this logic needs to be updated once we confirm what disqual should be returned
            // TODO: check if this logic shouldn't be in Drools rules.
            if (driver.getDisqualifications() != null) {
                for (Disqualification disq : driver.getDisqualifications()) {
                    if (end.getId().equals(disq.getEndorsementID())) {
                        mibEndorsement.setDisqualStartDate(disq.getDisqFromDate());
                        mibEndorsement.setDisqualEndDate(disq.getDisqToDate());
                    }
                }
            }
            endorsements.add(mibEndorsement);
        }

        return endorsements;
{'fraction_non_alphanumeric': 0.08830625703715618, 'fraction_numerical': 0.0, 'mean_word_length': 3.4410714285714286, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5538294', 'n_tokens_mistral': 3942, 'n_tokens_neox': 3845, 'n_words': 716}