All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.aequologica.neo.buildhub.service.BuildServiceImpl Maven / Gradle / Ivy

package net.aequologica.neo.buildhub.service;

import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;

import net.aequologica.neo.buildhub.core.model.Build;
import net.aequologica.neo.buildhub.neo.document.BuildSerializer;
import net.aequologica.neo.buildhub.persist.BuildDao;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;

@Singleton
public class BuildServiceImpl implements BuildService {

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

    @Inject
    private BuildDao            buildDao;

    private static int          _instanceCount = 0;
    private int                 serialNo;

    public BuildServiceImpl() {
        _instanceCount += 1;
        serialNo = _instanceCount;
        log.info("Instance No: >>> {}",  _instanceCount);
    }

    @PostConstruct
    public void postConstructCallback() {
        log.info("PostConstruct - Serial No: {}", serialNo);
    }

    @PreDestroy
    public void preDestroyCallback() {
        log.info("PreDestory - Serial No: {}", serialNo);
    }

    @Override
    public List all() throws IOException {
        return buildDao.readAll();
    }

    @Override
    public void archive(UUID buildId) throws IOException {
        Build build = BuildSerializer.read(buildId);
        archive(build);
    }

    private void archive(Build build) throws IOException {

        // @formatter:off
        Map properties  = build.getPropertyMap();
        net.aequologica.neo.buildhub.persist.model.Build entity      = new net.aequologica.neo.buildhub.persist.model.Build();

        entity.setBuildId(build.getName());

        try {   entity.setStatus    ( build.getStatus().toString()                                   );  } catch (Exception e) { log.warn("setStatus   exception (ignored)", e); }
        try {   entity.setStart     ( build.getBegin()                                               );  } catch (Exception e) { log.warn("setStart    exception (ignored)", e); }
        try {   entity.setEnd       ( build.getEnd()                                                 );  } catch (Exception e) { log.warn("setEnd      exception (ignored)", e); }
        try {   entity.setDuration  ( build.getEnd().getTime() - build.getBegin().getTime()       );  } catch (Exception e)    { log.warn("setDuration exception (ignored)", e); }
        try {   entity.setWeek      ( new DateTime(build.getBegin().getTime()).getWeekOfWeekyear()   );  } catch (Exception e) { log.warn("setCw       exception (ignored)", e); }
        try {   entity.setGav       ( Joiner.on(":").skipNulls().join(
                                          properties.get("groupId")    ,
                                          properties.get("artifactId") ,
                                          properties.get("version")    )                                );  } catch (Exception e) { log.warn("setGav      exception (ignored)", e); }
        try {   entity.setHost      ( properties.get("hostName")                                        );  } catch (Exception e) { log.warn("setHost     exception (ignored)", e); }
        try {   entity.setGoals     ( properties.get("goals")                                           );  } catch (Exception e) { log.warn("setGoals    exception (ignored)", e); }
        // @formatter:on

        // write it !
        try {
            log.trace("about to write build {}", entity.getId());

            buildDao.write(entity);

            BuildSerializer.delete(build);

            log.trace("build {} written", entity.getId());
        } catch (Exception e) {
            log.warn("ignored exception writing document[" + build.getName() + "]", e);
        }

    }

    @Override
    // @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void archiveAll() throws IOException {
        // get all doc IDs
        Set buildIds = BuildSerializer.getUuids();

        // @formatter:off
        List builds = FluentIterable.from        ( buildIds           )
                                           .filter      ( removeExisting     )
                                           .transform   ( buildId2build      )
                                           .toSortedList( olderBuildFirst    );
        // @formatter:on

        // for each build
        for (Build build : builds) {

            archive(build);

        }

        // reload
    }




    // @formatter:off
    private Predicate removeExisting = new Predicate() {
        @Override
        public boolean apply(UUID id) {
            boolean ret = !buildDao.exists(id.toString());
            log.trace("does build {} exist ? -> {}", id.toString(), ret);
            return ret;
        }
    };

    private Function buildId2build = new Function() {
        @Override
        public Build apply(UUID id) {
            try {
                return BuildSerializer.read(id);
            } catch (Exception e) {
                return null;
            }
        }
    };

    private Comparator olderBuildFirst = new Comparator() {
        @Override
        public int compare(Build o1, Build o2) {
            if (o1.getBegin() == null) {
                if (o2.getBegin() == null) {
                    return 0;
                } else {
                    return -1;
                }
            } else {
                if (o2.getBegin() == null) {
                    return 1;
                } else {
                    return o1.getBegin().compareTo(o2.getBegin());
                }
            }
        }
    };
    // @formatter:on

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy