/* * This file is a part of the SteamWar software. * * Copyright (C) 2020 SteamWar.de-Serverteam * * 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 de.steamwar.sql; import de.steamwar.sql.internal.Field; import de.steamwar.sql.internal.SelectStatement; import de.steamwar.sql.internal.Statement; import de.steamwar.sql.internal.Table; import lombok.AllArgsConstructor; import java.util.HashSet; import java.util.Optional; import java.util.Set; @AllArgsConstructor public class NodeMember { public static void init() { // enforce class initialization } private static final Table table = new Table<>(NodeMember.class); private static final SelectStatement getNodeMember = table.select(Table.PRIMARY); private static final SelectStatement getNodeMembers = table.selectFields("NodeId"); private static final SelectStatement getSchematics = table.selectFields("UserId"); private static final Statement create = table.insert(Table.PRIMARY); private static final Statement delete = table.delete(Table.PRIMARY); private static final Statement updateParent = table.update(Table.PRIMARY, "ParentId"); @Field(keys = {Table.PRIMARY}) private final int nodeId; @Field(keys = {Table.PRIMARY}) private final int userId; @Field(nullable = true, def = "null") private Integer parentId; public int getNode() { return nodeId; } public int getMember() { return userId; } public Optional getParent() { return Optional.ofNullable(parentId); } public void delete() { delete.update(nodeId, userId); } public static NodeMember createNodeMember(int node, int member) { create.update(node, member); return new NodeMember(node, member, null); } public static NodeMember getNodeMember(int node, int member) { return getNodeMember.select(node, member); } public static Set getNodeMembers(int node) { return new HashSet<>(getNodeMembers.listSelect(node)); } public static Set getSchematics(int member) { return new HashSet<>(getSchematics.listSelect(member)); } public void setParentId(Integer parentId) { this.parentId = parentId; updateParent.update(this.parentId, nodeId, userId); } }