com.sinet.gage.delta.DomainUpdatesImporter.java Source code

Java tutorial

Introduction

Here is the source code for com.sinet.gage.delta.DomainUpdatesImporter.java

Source

package com.sinet.gage.delta;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.sinet.gage.dao.DomainsRepository;
import com.sinet.gage.dlap.clients.DlapDomainClient;
import com.sinet.gage.dlap.config.ServerSettings;
import com.sinet.gage.dlap.entities.DomainResponse;
import com.sinet.gage.entities.Domain;

/**
 * Performs delta updates for Domains
 * 
 * @author Team.Gage
 *
 */
@Component
public class DomainUpdatesImporter {

    Integer parentDomainId = 34863804;

    @Autowired
    ServerSettings serverSettings;

    @Autowired
    private DlapDomainClient dlapDomainClient;

    @Autowired
    private DomainsRepository domainsRepository;

    @Autowired
    SignalManager signalManager;

    private final Logger log = LoggerFactory.getLogger(DomainUpdatesImporter.class);

    DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTimeParser();
    DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    /**
     * Performs the delta imports for District & School Domain
     * 
     * @param domainIdsToProcessInsert
     */
    public void processInsertsForDomains(Set<Integer> domainIdsToProcessInsert) {
        Set<Integer> domainIdsForInsert;
        List<DomainResponse> domainResponseList;
        List<Domain> domainList = new ArrayList<>();

        for (Integer domainId : domainIdsToProcessInsert) {

            domainIdsForInsert = signalManager.getDomainIdsForDeltaImportsForADomain(domainId,
                    serverSettings.getEntityCreatedSignalTypeCode());

            if (!CollectionUtils.isEmpty(domainIdsForInsert)) {
                log.debug("Domain Ids to be inserted: " + domainIdsForInsert);
                String idsString = domainIdsForInsert.stream().map(Object::toString)
                        .collect(Collectors.joining(", "));
                domainResponseList = getDomainsWithData(domainId, idsString);

                for (DomainResponse domainResponse : domainResponseList) {
                    DomainResponse parentDomainResponse = dlapDomainClient.getDomain(null,
                            Long.parseLong(domainId.toString()));
                    Domain domain = mapToDomain(domainResponse);
                    if (domainResponse.getData() != null && domainResponse.getData().getCustomization() != null
                            && domainResponse.getData().getCustomization().getEdivatelearn() != null) {
                        if ("DISTRICT".equalsIgnoreCase(
                                domainResponse.getData().getCustomization().getEdivatelearn().getDomaintype())) {
                            domain.setStateDomainId(Long.parseLong(parentDomainResponse.getId()));
                            domain.setStateDomainName(parentDomainResponse.getName());
                            domain.setParentDomainName(parentDomainResponse.getName());
                        } else if ("SCHOOL".equalsIgnoreCase(
                                domainResponse.getData().getCustomization().getEdivatelearn().getDomaintype())) {
                            DomainResponse stateDomainResponse = dlapDomainClient.getDomain(null,
                                    Long.parseLong(parentDomainResponse.getParentid()));
                            domain.setStateDomainId(Long.parseLong(stateDomainResponse.getId()));
                            domain.setStateDomainName(stateDomainResponse.getName());
                            domain.setParentDomainName(parentDomainResponse.getName());
                        }
                    } else {
                        domain.setStateDomainId(Long.parseLong(parentDomainResponse.getId()));
                        domain.setStateDomainName(parentDomainResponse.getName());
                        domain.setParentDomainName(parentDomainResponse.getName());
                    }
                    domainList.add(domain);
                }
                log.debug("Inserting domain ids: " + domainId);
                if (!CollectionUtils.isEmpty(domainList)) {
                    domainsRepository.insertDomains(domainList);
                }
            }
        }
    }

    /**
     * @param domainId
     * @param idsString
     * @return
     */
    private List<DomainResponse> getDomainsWithData(Integer domainId, String idsString) {
        try {
            return dlapDomainClient.getDomainReponseListWithDomainData(null, domainId,
                    "/id IN ( " + idsString + " )", "", 10000);
        } catch (Exception e) {
            log.error("Error reading child domains of domain  " + domainId, e);
        }
        return new ArrayList<>();
    }

    /**
     * Performs the delta updates for Domains
     * 
     * @param domainIdsToProcessUpdates
     */
    public void processUpdatesForDomains(Set<Integer> domainIdsToProcessUpdates) {
        Set<Integer> domainIdsForUpdate;
        List<DomainResponse> domainResponseList;
        List<Domain> domainList = new ArrayList<>();

        for (Integer domainId : domainIdsToProcessUpdates) {

            domainIdsForUpdate = signalManager.getDomainIdsForDeltaImportsForADomain(domainId,
                    serverSettings.getEntityChangedSignalTypeCode());

            if (!CollectionUtils.isEmpty(domainIdsForUpdate)) {
                log.debug("Domain Ids to be updated: " + domainIdsForUpdate);
                String idsString = domainIdsForUpdate.stream().map(Object::toString)
                        .collect(Collectors.joining(", "));
                domainResponseList = getDomainsWithData(domainId, idsString);

                for (DomainResponse domainResponse : domainResponseList) {

                    DomainResponse parentDomainResponse = dlapDomainClient.getDomain(null,
                            Long.parseLong(domainId.toString()));

                    Domain domain = mapToDomain(domainResponse);
                    if (domainResponse.getData() != null && domainResponse.getData().getCustomization() != null
                            && domainResponse.getData().getCustomization().getEdivatelearn() != null) {
                        if ("DISTRICT".equalsIgnoreCase(
                                domainResponse.getData().getCustomization().getEdivatelearn().getDomaintype())) {
                            domain.setStateDomainId(Long.parseLong(parentDomainResponse.getId()));
                            domain.setStateDomainName(parentDomainResponse.getName());
                            domain.setParentDomainName(parentDomainResponse.getName());
                        } else if ("SCHOOL".equalsIgnoreCase(
                                domainResponse.getData().getCustomization().getEdivatelearn().getDomaintype())) {
                            DomainResponse stateDomainResponse = dlapDomainClient.getDomain(null,
                                    Long.parseLong(parentDomainResponse.getParentid()));
                            domain.setStateDomainId(Long.parseLong(stateDomainResponse.getId()));
                            domain.setStateDomainName(stateDomainResponse.getName());
                            domain.setParentDomainName(parentDomainResponse.getName());
                        }
                    } else {
                        domain.setStateDomainId(Long.parseLong(parentDomainResponse.getId()));
                        domain.setStateDomainName(parentDomainResponse.getName());
                        domain.setParentDomainName(parentDomainResponse.getName());
                    }
                    domainList.add(domain);
                }
                log.debug("Updating domain ids: " + domainId);
                if (!CollectionUtils.isEmpty(domainList)) {
                    domainsRepository.updateDomains(domainList);
                }
            }
        }
    }

    /**
     * @param domainIdsToProcessDeletes
     */
    public void processDeletesForDomains(Set<Integer> domainIdsToProcessDeletes) {
        Set<Integer> domainIdsForDeletes;

        for (Integer domainId : domainIdsToProcessDeletes) {

            domainIdsForDeletes = signalManager.getDomainIdsForDeltaImportsForADomain(domainId,
                    serverSettings.getEntityDeletedSignalTypeCode());

            log.debug("Domain Ids to be deleted: " + domainIdsForDeletes);

            for (Integer deletedDomainId : domainIdsForDeletes) {
                log.debug("Deleting domain id: " + deletedDomainId);
                int status = domainsRepository.updateDomains(deletedDomainId);
                if (status == 0) {
                    log.error("Error in updating domain with id: " + deletedDomainId);
                } else {
                    log.debug("Domain updated successfully.");
                }
            }

        }
    }

    private Domain mapToDomain(DomainResponse response) {
        Domain domain = new Domain();
        domain.setDomainId(Long.parseLong(response.getId()));
        domain.setGuid(UUID.fromString(response.getGuid()));
        domain.setDomainName(response.getName());
        domain.setLoginPrefix(response.getUserspace());
        domain.setFlag(response.getFlags());
        domain.setParentDomainId(Long.parseLong(response.getParentid()));

        if (response.getData() != null && response.getData().getCustomization() != null
                && response.getData().getCustomization().getEdivatelearn() != null) {
            domain.setDomainType(response.getData().getCustomization().getEdivatelearn().getDomaintype());

            domain.setLicenseType(response.getData().getCustomization().getEdivatelearn().getLicensetype());
            domain.setLicensePoolType(response.getData().getCustomization().getEdivatelearn().getPooltype());
            domain.setNoOfLicense(Integer.parseInt(
                    response.getData().getCustomization().getEdivatelearn().getNooflicense().trim().equals("") ? "0"
                            : response.getData().getCustomization().getEdivatelearn().getNooflicense()));
            domain.setPilot(response.getData().getCustomization().getEdivatelearn().isPilot());
            domain.setPilotStartDate(
                    getDateFormat(response.getData().getCustomization().getEdivatelearn().getPilotstartdate()));
            domain.setPilotEndDate(
                    getDateFormat(response.getData().getCustomization().getEdivatelearn().getPilotenddate()));
            domain.setFullSubscription(
                    response.getData().getCustomization().getEdivatelearn().isFullsubscription());
            domain.setSubscriptionStartDate(getDateFormat(
                    response.getData().getCustomization().getEdivatelearn().getSubscriptionstartdate()));
            domain.setSubscriptionEndDate(getDateFormat(
                    response.getData().getCustomization().getEdivatelearn().getSubscriptionenddate()));
        }

        domain.setCreatorUserId(Long.parseLong(response.getCreationby()));
        domain.setCreationDate(getDateWithISOFormat(response.getCreationdate()));
        domain.setModifierUserId(Long.parseLong(response.getModifiedby()));
        domain.setModifiedDate(getDateWithISOFormat(response.getModifieddate()));

        return domain;
    }

    private Timestamp getDateWithISOFormat(String date) {
        Timestamp formattedDate = null;
        try {
            if (date != null) {
                formattedDate = new Timestamp(dateTimeFormatter.withOffsetParsed().parseDateTime(date).getMillis());
            }
        } catch (Exception e) {
            log.error("Invalid date format: " + e.getMessage());
        }
        return formattedDate;
    }

    private Date getDateFormat(String date) {
        Date formattedDate = null;
        try {
            if (date != null) {
                formattedDate = new Date(dateFormat.parse(date).getTime());
            }
        } catch (ParseException e) {
            log.error("Invalid date format: " + e.getMessage());
        }
        return formattedDate;
    }
}