<commit_before>/*
 *  Project:    moba-server
 *
 *  Copyright (C) 2018 <NAME> <<EMAIL>>
 *
 *  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 <http://www.gnu.org/licenses/agpl.txt>.
 *
 */

package moba.server.utilities.lock;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;

import moba.server.database.Database;
import moba.server.datatypes.enumerations.ErrorId;
import moba.server.utilities.exceptions.ErrorException;

public final class TracklayoutLock extends AbstractLock {

    protected Database database = null;

    public TracklayoutLock(Database database) {
        this.database = database;
    }

    @Override
    public void resetAll() {
        try {
            Connection con = database.getConnection();

            try(PreparedStatement pstmt = con.prepareStatement("UPDATE `TrackLayouts` SET `Locked` = NULL")) {
                pstmt.executeUpdate();
                getLogger().log(Level.INFO, pstmt.toString());
            }
        } catch(SQLException e) {
            getLogger().log(Level.WARNING, e.toString());
        }
    }

    @Override
    public void resetOwn(long appId) {
        try {
            Connection con = database.getConnection();

            try(PreparedStatement pstmt = con.prepareStatement("UPDATE `TrackLayouts` SET `Locked` = NULL WHERE `Locked` = ?")) {
                pstmt.setLong(1, appId);
                pstmt.executeUpdate();
                getLogger().log(Level.INFO, pstmt.toString());
            }
        } catch(SQLException e) {
            getLogger().log(Level.WARNING, e.toString());
        }
    }

    @Override
    public void tryLock(long appId, Object data) throws ErrorException {
        try {
            long id = (long)data;

            if(isLockedByApp(appId, data)) {
                return;
            }

            Connection con = database.getConnection();
            String q = "UPDATE `TrackLayouts` SET `locked` = ? WHERE `locked` IS NULL AND `id` = ? ";

            try(PreparedStatement pstmt = con.prepareStatement(q)) {
                pstmt.setLong(1, appId);
                pstmt.setLong(2, id);

                getLogger().log(Level.INFO, pstmt.toString());

                if(pstmt.executeUpdate() == 0) {
                    throw new ErrorException(ErrorId.DATASET_MISSING, "no layout found with id <" + Long.toString(id) + ">");
                }
            }
        } catch(SQLException e) {
            throw new ErrorException(ErrorId.DATABASE_ERROR, e.getMessage());
        }
    }

    @Override
    public void unlock(long appId, Object data)
    throws ErrorException {
        try {
            long id = (long)data;

            if(!isLockedByApp(appId, data)) {
                return;
            }

            Connection con = database.getConnection();
            String q = "UPDATE `TrackLayouts` SET `locked` = NULL WHERE `locked` = ? AND `id` = ? ";

            try(PreparedStatement pstmt = con.prepareStatement(q)) {<commit_msg>cleaning up...<commit_after>/*
 *  Project:    moba-server
 *
 *  Copyright (C) 2018 <NAME> <<EMAIL>>
 *
 *  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 <http://www.gnu.org/licenses/agpl.txt>.
 *
 */

package moba.server.utilities.lock;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;

import moba.server.database.Database;
import moba.server.datatypes.enumerations.ErrorId;
import moba.server.utilities.exceptions.ErrorException;

public final class TracklayoutLock extends AbstractLock {

    protected Database database = null;

    public TracklayoutLock(Database database) {
        this.database = database;
    }

    @Override
    public void resetAll() {
        try {
            Connection con = database.getConnection();

            try(PreparedStatement pstmt = con.prepareStatement("UPDATE `TrackLayouts` SET `Locked` = NULL")) {
                pstmt.executeUpdate();
                getLogger().log(Level.INFO, pstmt.toString());
            }
        } catch(SQLException e) {
            getLogger().log(Level.WARNING, e.toString());
        }
    }

    @Override
    public void resetOwn(long appId) {
        try {
            Connection con = database.getConnection();

            try(PreparedStatement pstmt = con.prepareStatement("UPDATE `TrackLayouts` SET `Locked` = NULL WHERE `Locked` = ?")) {
                pstmt.setLong(1, appId);
                pstmt.executeUpdate();
                getLogger().log(Level.INFO, pstmt.toString());
            }
        } catch(SQLException e) {
            getLogger().log(Level.WARNING, e.toString());
        }
    }

    @Override
    public void tryLock(long appId, Object data)
    throws ErrorException {
        try {
            long id = (long)data;

            if(isLockedByApp(appId, data)) {
                return;
            }

            Connection con = database.getConnection();
            String q = "UPDATE `TrackLayouts` SET `locked` = ? WHERE `locked` IS NULL AND `id` = ? ";

            try(PreparedStatement pstmt = con.prepareStatement(q)) {
                pstmt.setLong(1, appId);
                pstmt.setLong(2, id);

                getLogger().log(Level.INFO, pstmt.toString());

                if(pstmt.executeUpdate() == 0) {
                    throw new ErrorException(ErrorId.DATASET_LOCKED, "object is already locked");
                }
            }
        } catch(SQLException e) {
            throw new ErrorException(ErrorId.DATABASE_ERROR, e.getMessage());
        }
    }

    @Override
    public void unlock(long appId, Object data)
    throws ErrorException {
        try {
            long id = (long)data;

            if(!isLockedByApp(appId, data)) {
                return;
            }

            Connection con = database.getConnection();
            String q = "UPDATE `TrackLayouts` SET `locked` = NULL WHERE `locked` = ? AND `id` = ? ";

            try(PreparedStatement pstmt = con.prepareStatement(q)) {
{'fraction_non_alphanumeric': 0.10321697467488022, 'fraction_numerical': 0.0024640657084188913, 'mean_word_length': 2.2557932263814617, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7658812', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1957, 'n_words': 563}