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

org.openremote.manager.alarm.AlarmResourceImpl Maven / Gradle / Ivy

/*
 * Copyright 2024, OpenRemote Inc.
 *
 * See the CONTRIBUTORS.txt file in the distribution for a
 * full listing of individual contributors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 */
package org.openremote.manager.alarm;

import jakarta.persistence.EntityNotFoundException;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.NotFoundException;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebResource;
import org.openremote.model.alarm.Alarm;
import org.openremote.model.alarm.AlarmResource;
import org.openremote.model.alarm.SentAlarm;
import org.openremote.model.alarm.AlarmAssetLink;
import org.openremote.model.http.RequestParams;

import org.openremote.model.util.TextUtil;

import java.util.List;
import java.util.function.Supplier;

import static org.openremote.model.alarm.Alarm.Source.CLIENT;
import static org.openremote.model.alarm.Alarm.Source.MANUAL;

public class AlarmResourceImpl extends ManagerWebResource implements AlarmResource {

    private final AlarmService alarmService;

    public AlarmResourceImpl(TimerService timerService,
                             ManagerIdentityService identityService,
                             AlarmService alarmService) {
        super(timerService, identityService);
        this.alarmService = alarmService;
    }

    private  T mapExceptions(Supplier supplier) {
        try {
            return supplier.get();
        } catch (IllegalArgumentException | NullPointerException e) {
            throw new BadRequestException(e.getMessage(), e);
        } catch (EntityNotFoundException e) {
            throw new NotFoundException(e.getMessage(), e);
        } catch (SecurityException e) {
            throw new ForbiddenException(e.getMessage(), e);
        }
    }

    private void mapExceptions(Runnable runnable) {
        try {
            runnable.run();
        } catch (IllegalArgumentException | NullPointerException e) {
            throw new BadRequestException(e.getMessage(), e);
        } catch (EntityNotFoundException e) {
            throw new NotFoundException(e.getMessage(), e);
        } catch (SecurityException e) {
            throw new ForbiddenException(e.getMessage(), e);
        }
    }

    @Override
    public SentAlarm[] getAlarms(RequestParams requestParams, String realm, Alarm.Status status, String assetId, String assigneeId) {
        String filterRealm = TextUtil.isNullOrEmpty(realm) ? getAuthenticatedRealm().getName() : realm;
        if (!isRealmActiveAndAccessible(filterRealm)) {
            throw new ForbiddenException("Realm '" + filterRealm + "' is not active or inaccessible");
        }
        return mapExceptions(() -> alarmService.getAlarms(filterRealm, status, assetId, assigneeId).toArray(new SentAlarm[0]));
    }

    @Override
    public void removeAlarms(RequestParams requestParams, List alarmIds) {
        mapExceptions(() -> alarmService.removeAlarms(alarmIds, getUserId()));
    }

    @Override
    public SentAlarm getAlarm(RequestParams requestParams, Long alarmId) {
        return mapExceptions(() -> alarmService.getAlarm(alarmId, getUserId()));
    }

    @Override
    public void removeAlarm(RequestParams requestParams, Long alarmId) {
        mapExceptions(() -> alarmService.removeAlarm(alarmId, getUserId()));
    }

    @Override
    public SentAlarm createAlarm(RequestParams requestParams, Alarm alarm, List assetIds) {
        if (getUserId() != null) {
            alarm.setSource(MANUAL);
            alarm.setSourceId(getUserId());
        } else if (getClientId() != null) {
            alarm.setSource(CLIENT);
            alarm.setSourceId(getClientId());
        }
        return mapExceptions(() -> alarmService.sendAlarm(alarm, assetIds, getUserId()));
    }

    @Override
    public void updateAlarm(RequestParams requestParams, Long alarmId, SentAlarm alarm) {
        mapExceptions(() -> alarmService.updateAlarm(alarmId, getUserId(), alarm));
    }

    @Override
    public List getAssetLinks(RequestParams requestParams, Long alarmId, String realm) {
        return mapExceptions(() -> alarmService.getAssetLinks(alarmId, getUserId(), realm));
    }

    @Override
    public void setAssetLinks(RequestParams requestParams, List links) {
        mapExceptions(() -> alarmService.linkAssets(links, getUserId()));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy