/*-
 * ~~~~~~licensing~~~~~~
 * permission-manager
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.management.permission.manager;

import ru.entaxy.esb.system.core.permission.jpa.PermissionService;
import ru.entaxy.esb.system.core.permission.jpa.entity.Permission;
import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.jpa.entity.System;
import ru.entaxy.esb.system.management.permission.manager.dto.PermissionDto;
import ru.entaxy.esb.system.management.permission.manager.mapper.PermissionMapper;

import static ru.entaxy.esb.system.core.permission.common.PermissionConstants.TYPE_SYSTEM;

public class PermissionManagerImpl implements PermissionManager {

    private PermissionService permissionService;
    private SystemService systemService;
    private PermissionMapper permissionMapper;

    @Override
    public PermissionDto createPermission(PermissionDto permissionDto) {
        System object = systemService.getByUuid(permissionDto.getObjectId());
        System subject = systemService.getByUuid(permissionDto.getSubjectId());

        Permission permission = permissionService.addIfNotExist(object.getId(),
                TYPE_SYSTEM, String.valueOf(subject.getId()), TYPE_SYSTEM, null);
        return permissionMapper.toPermissionDto(permission);
    }

    @Override
    public void removePermission(PermissionDto permissionDto) {
        System object = systemService.getByUuid(permissionDto.getObjectId());
        System subject = systemService.getByUuid(permissionDto.getSubjectId());

        permissionService.remove(object.getId(), TYPE_SYSTEM, String.valueOf(subject.getId()),
                TYPE_SYSTEM, null);
    }

    @Override
    public void removeAllPermission(long id) {
        permissionService.removeAll(id, TYPE_SYSTEM);
    }

    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    public void setSystemService(SystemService systemService) {
        this.systemService = systemService;
    }
}
