org.opentestsystem.shared.monitoringalerting.service.impl.DiscreteIntakeServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.monitoringalerting.service.impl.DiscreteIntakeServiceImpl.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.shared.monitoringalerting.service.impl;

import org.opentestsystem.shared.monitoringalerting.domain.DiscreteIntake;
import org.opentestsystem.shared.monitoringalerting.domain.DiscreteIntake.TYPE;
import org.opentestsystem.shared.monitoringalerting.gateway.DiscreteIntakeService;
import org.opentestsystem.shared.monitoringalerting.persistence.DiscreteIntakeRepository;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class DiscreteIntakeServiceImpl implements DiscreteIntakeService {

    private static final Function<DiscreteIntake, String> VALUE_TRANSFORMER = new Function<DiscreteIntake, String>() {

        @Override
        public String apply(final DiscreteIntake discreteIntake) {
            return discreteIntake.getValue();
        }
    };

    @Autowired
    private DiscreteIntakeRepository discreteIntakeRepository;

    @Override
    public String[] getDistinctAlertTypes() {
        return Iterables.toArray(
                Lists.transform(this.discreteIntakeRepository.findByType(TYPE.ALERT), VALUE_TRANSFORMER),
                String.class);
    }

    @Override
    public String[] getDistinctMetricTypes() {
        return Iterables.toArray(
                Lists.transform(this.discreteIntakeRepository.findByType(TYPE.METRIC), VALUE_TRANSFORMER),
                String.class);
    }

    @Override
    public String[] getDistinctComponents() {
        return Iterables.toArray(
                Lists.transform(this.discreteIntakeRepository.findByType(TYPE.COMPONENT), VALUE_TRANSFORMER),
                String.class);
    }

    @Override
    public String[] getDistinctServers() {
        return Iterables.toArray(
                Lists.transform(this.discreteIntakeRepository.findByType(TYPE.SERVER), VALUE_TRANSFORMER),
                String.class);
    }

    @Override
    public DiscreteIntake save(final DiscreteIntake discreteIntake) {
        if (!Lists.transform(this.discreteIntakeRepository.findByType(discreteIntake.getType()), VALUE_TRANSFORMER)
                .contains(discreteIntake.getValue())) {
            return this.discreteIntakeRepository.save(discreteIntake);
        }
        return null;
    }
}