org.apigw.appmanagement.ApplicationManagementAdminServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.appmanagement.ApplicationManagementAdminServiceImpl.java

Source

/**
 *   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.appmanagement;

import org.apache.commons.beanutils.PropertyUtils;
import org.apigw.appmanagement.domain.ApplicationManagementRevision;
import org.apigw.appmanagement.domain.Application;
import org.apigw.appmanagement.domain.Certificate;
import org.apigw.appmanagement.domain.Developer;
import org.apigw.appmanagement.dto.*;
import org.apigw.appmanagement.repository.ApplicationRepository;
import org.apigw.appmanagement.repository.AppmanRevisionRepository;
import org.apigw.appmanagement.repository.DeveloperRepository;
import org.javers.core.Javers;
import org.javers.core.JaversBuilder;
import org.javers.core.diff.Diff;
import org.javers.core.diff.changetype.NewObject;
import org.javers.core.diff.changetype.ObjectRemoved;
import org.javers.core.diff.changetype.PropertyChange;
import org.javers.core.metamodel.object.GlobalId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.history.Revision;
import org.springframework.data.history.Revisions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * Created by martin on 19/03/15.
 */
@Service
@Transactional
public class ApplicationManagementAdminServiceImpl implements ApplicationManagementAdminService {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationManagementAdminServiceImpl.class);
    private final Javers javers = JaversBuilder.javers().build();

    @Autowired
    private ApplicationRepository applicationRepository;

    @Autowired
    private DeveloperRepository developerRepository;

    @Autowired
    private AppmanRevisionRepository appmanRevisionRepository;

    @Autowired
    private ApplicationManagementConverter converter;

    @Override
    public List<ApplicationDTO> getAllApplications() {
        List<ApplicationDTO> applicationDTOs = new ArrayList<>();
        List<Application> applications = applicationRepository.findAll();
        for (Application application : applications) {
            applicationDTOs.add(converter.toDTO(application));
        }
        return applicationDTOs;
    }

    @Override
    public ApplicationDTO getApplication(Long id) {
        Application application = applicationRepository.findOne(id);
        logger.debug("found application: {}, number of certificates: {}", application,
                application.getCertificates().size());
        return converter.toDTO(application);
    }

    @Override
    public ApplicationDTO updateApplication(Long id, ApplicationDTO applicationDTO) {
        Application application = converter.fromDTO(applicationDTO);
        Application savedApplication = applicationRepository.save(application);
        return converter.toDTO(savedApplication);
    }

    @Override
    public List<RevisionDTO<ApplicationDTO>> getApplicationRevisions(Long id) {
        List<RevisionDTO<ApplicationDTO>> applicationRevisions = new ArrayList<>();
        Revisions<Integer, Application> revisions = applicationRepository.findRevisions(id);
        ApplicationDTO prev = null;
        for (Revision<Integer, Application> revision : revisions) {
            ApplicationManagementRevision applicationManagementRevision = appmanRevisionRepository
                    .findOne(revision.getRevisionNumber());
            List<ChangeDTO> changeDTOs = null;
            ApplicationDTO applicationDTO = converter.toDTO(revision.getEntity());
            if (prev != null) {
                changeDTOs = createListOfChangeDTOs(prev, applicationDTO);
            }
            applicationRevisions.add(new RevisionDTO<>(revision.getRevisionNumber(),
                    revision.getRevisionDate().toDate(), applicationDTO, changeDTOs,
                    applicationManagementRevision.getEditor(), applicationManagementRevision.isEditorAdmin()));
            prev = applicationDTO;
        }
        return applicationRevisions;
    }

    @Override
    public Set<CertificateDTO> getApplicationCertificates(Long applicationId) {
        Application application = applicationRepository.findOne(applicationId);
        Set<Certificate> certificates = application.getCertificates();
        Set<CertificateDTO> certificateDTOs = new HashSet<>();
        for (Certificate certificate : certificates) {
            certificateDTOs.add(converter.toDTO(certificate));
        }
        return certificateDTOs;
    }

    @Override
    public CertificateDTO addApplicationCertificate(Long applicationId, CertificateDTO certificateDTO) {
        Application application = applicationRepository.findOne(applicationId);
        Certificate certificate = converter.fromDTO(certificateDTO);
        certificate.setApplication(application);
        application.getCertificates().add(certificate);
        applicationRepository.save(application);
        return converter.toDTO(certificate);
    }

    @Override
    public void removeApplicationCertificate(Long applicationId, Long certificateId) {
        Application application = applicationRepository.findOne(applicationId);
        Set<Certificate> certificates = application.getCertificates();
        Iterator<Certificate> iterator = certificates.iterator();
        while (iterator.hasNext()) {
            Certificate certificate = iterator.next();
            if (certificate.getId().equals(certificateId)) {
                iterator.remove();
                applicationRepository.save(application);
                break;
            }
        }
    }

    @Override
    public DeveloperDTO getApplicationDeveloper(Long applicationId) {
        Application application = applicationRepository.getOne(applicationId);
        Developer developer = application.getDeveloper();
        return converter.toDTO(developer);
    }

    @Override
    public CertificateDTO updateApplicationCertificate(Long applicationId, Long certificateId,
            CertificateDTO certificateDTO) {
        return null;
    }

    @Override
    public List<DeveloperDTO> getAllDevelopers() {
        List<DeveloperDTO> developerDTOs = new ArrayList<>();
        List<Developer> developers = developerRepository.findAll();
        for (Developer developer : developers) {
            developerDTOs.add(converter.toDTO(developer));
        }
        return developerDTOs;
    }

    @Override
    public DeveloperDTO getDeveloper(String id) {
        return converter.toDTO(developerRepository.findByResidentIdentificationNumber(id));
    }

    @Override
    public List<RevisionDTO<DeveloperDTO>> getDeveloperRevisions(String id) {
        List<RevisionDTO<DeveloperDTO>> developerRevisions = new ArrayList<>();
        Revisions<Integer, Developer> revisions = developerRepository.findRevisions(id);
        DeveloperDTO prev = null;
        for (Revision<Integer, Developer> revision : revisions.getContent()) {
            ApplicationManagementRevision applicationManagementRevision = appmanRevisionRepository
                    .findOne(revision.getRevisionNumber());
            List<ChangeDTO> changeDTOs = null;
            DeveloperDTO developerDTO = converter.toDTO(revision.getEntity());
            if (prev != null) {
                changeDTOs = createListOfChangeDTOs(prev, developerDTO);
            }
            developerRevisions.add(new RevisionDTO<>(revision.getRevisionNumber(),
                    revision.getRevisionDate().toDate(), developerDTO, changeDTOs,
                    applicationManagementRevision.getEditor(), applicationManagementRevision.isEditorAdmin()));
            prev = developerDTO;
        }
        return developerRevisions;
    }

    @Override
    public List<ApplicationDTO> getDeveloperApplications(String id) {
        Developer developer = developerRepository.findByResidentIdentificationNumber(id);
        List<Application> applications = developer.getApplications();
        List<ApplicationDTO> applicationDTOs = new ArrayList<>();
        for (Application application : applications) {
            applicationDTOs.add(converter.toDTO(application));
        }
        return applicationDTOs;
    }

    private List<ChangeDTO> createListOfChangeDTOs(Object oldObj, Object newObj) {
        List<ChangeDTO> changeDTOs = new ArrayList<>();
        Diff diff = javers.compare(oldObj, newObj);
        List<PropertyChange> propertyChanges = diff.getChangesByType(PropertyChange.class);
        for (PropertyChange propertyChange : propertyChanges) {
            logger.debug("propertyChange: {}", propertyChange);
            if (propertyChange.getAffectedLocalId() != null) {
                ChangeDTO changeDTO = createChangeDTO(propertyChange, oldObj, newObj);
                changeDTOs.add(changeDTO);
            }
        }
        //TODO: NewObject (Change)
        List<NewObject> newObjects = diff.getChangesByType(NewObject.class);
        for (NewObject newObject : newObjects) {
            logger.debug("newObject: {}", newObject);
            Object affectedLocalId = newObject.getAffectedLocalId();
            logger.debug("affectedLocalId: {}", affectedLocalId);
            GlobalId affectedGlobalId = newObject.getAffectedGlobalId();
            logger.debug("affectedGlobalId: {}", affectedGlobalId);
        }

        //TODO: ObjectRemoved (Change)
        List<ObjectRemoved> objectRemoveds = diff.getChangesByType(ObjectRemoved.class);
        for (ObjectRemoved objectRemoved : objectRemoveds) {
            logger.debug("objectRemoved: {}", objectRemoved);
            Object affectedLocalId = objectRemoved.getAffectedLocalId();
            logger.debug("affectedLocalId: {}", affectedLocalId);
            GlobalId affectedGlobalId = objectRemoved.getAffectedGlobalId();
            logger.debug("affectedGlobalId: {}", affectedGlobalId);
        }
        return changeDTOs;
    }

    private ChangeDTO createChangeDTO(PropertyChange propertyChange, Object oldObj, Object newObj) {
        ChangeDTO dto = null;
        String propertyName = propertyChange.getProperty().getName();
        try {
            Object oldVal = PropertyUtils.getProperty(oldObj, propertyName);
            Object newVal = PropertyUtils.getProperty(newObj, propertyName);
            dto = new ChangeDTO(propertyName, oldVal, newVal);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            logger.error("Unable to create ChangeDTO", e);
        }
        return dto;
    }

}