<commit_before>package com.imcode.imcms.domain.service.api;

import com.imcode.db.DatabaseException;
import com.imcode.imcms.api.DatabaseService;
import com.imcode.imcms.api.VersionData;
import com.imcode.imcms.domain.service.VersionDataService;
import com.imcode.imcms.servlet.Version;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import static ucar.httpservices.HTTPAuthStore.log;

/**
 * General service for get data versions
 */
@Service
public class DefaultVersionDataService implements VersionDataService {

    private final DatabaseService databaseService;

    @Value("/WEB-INF/version.txt")
    private Path versionFile;

    @Value("schema.xml")
    private Path schema;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    public DefaultVersionDataService(DatabaseService databaseService) {
        this.databaseService = databaseService;
    }

    @Override
    public VersionData getVersionData() {
        VersionData versionData = new VersionData();
        versionData.setImcmsVersion(getImcmsVersion());
        versionData.setJavaVersion(getJavaVersion());
        versionData.setDbVersion(getDbVersion());
        versionData.setServerInfo(getServerInfo());
        versionData.setDbNameVersion(getProductNameVersionData());

        return versionData;
    }

    private String getImcmsVersion() {
        try {
            return Files.readAllLines(versionFile).toString();
        } catch (IOException e) {
            final String errorMessage = "Error reading imcms version.";
            log.error(errorMessage);
            return null;
        }
    }

    private String getJavaVersion() {
        return System.getProperty("java.vm.vendor") +
                " " + System.getProperty("java.vm.name") +
                " " + System.getProperty("java.vm.version") +
                "Java version: " + System.getProperty("java.specification.version");
    }

    private String getDbVersion() { //todo: rewrite get required db version!
        return "Required DB schema version: " + StringUtils.defaultString(Version.getRequiredDbVersion(), "Not Found");
    }


    private String getServerInfo() {
        return servletContext.getServerInfo();
    }

    private String getProductNameVersionData() {
        try {
            DatabaseMetaData metaData = databaseService.getConnection().getMetaData();
            return metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion();
        } catch (SQLException e) {
            final String errorMessage = "Failed get database version and product name !";
            log.error(errorMessage);
            throw new DatabaseException(errorMessage, e);
        }<commit_msg>Issue IMCMS-453: New tab in superadmin - Refactor<commit_after>package com.imcode.imcms.domain.service.api;

import com.imcode.db.DatabaseException;
import com.imcode.imcms.api.DatabaseService;
import com.imcode.imcms.api.VersionData;
import com.imcode.imcms.domain.service.VersionDataService;
import com.imcode.imcms.servlet.Version;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import static ucar.httpservices.HTTPAuthStore.log;

/**
 * General service for get data versions
 */
@Service
public class DefaultVersionDataService implements VersionDataService {

    private final DatabaseService databaseService;

    @Value("/WEB-INF/version.txt")
    private Path versionFile;

    @Value("schema.xml")
    private Path schema;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    public DefaultVersionDataService(DatabaseService databaseService) {
        this.databaseService = databaseService;
    }

    @Override
    public VersionData getVersionData() {
        VersionData versionData = new VersionData();
        versionData.setImcmsVersion(getImcmsVersion());
        versionData.setJavaVersion(getJavaVersion());
        versionData.setDbVersion(getDbVersion());
        versionData.setServerInfo(getServerInfo());
        versionData.setDbNameVersion(getProductNameVersionData());

        return versionData;
    }

    private String getImcmsVersion() {
        try {
            return Files.readAllLines(versionFile).toString();
        } catch (IOException e) {
            final String errorMessage = "Error reading imcms version.";
            log.error(errorMessage);
            return null;
        }
    }

    private String getJavaVersion() {
        return System.getProperty("java.vm.vendor") +
                " " + System.getProperty("java.vm.name") +
                " " + System.getProperty("java.vm.version") +
                " Java version: " + System.getProperty("java.specification.version");
    }

    private String getDbVersion() { //todo: rewrite get required db version!
        return "Required DB schema version: " + StringUtils.defaultString(Version.getRequiredDbVersion(), "Not Found");
    }


    private String getServerInfo() {
        return servletContext.getServerInfo();
    }

    private String getProductNameVersionData() {
        try {
            DatabaseMetaData metaData = databaseService.getConnection().getMetaData();
            return metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion();
        } catch (SQLException e) {
            final String errorMessage = "Failed get database version and product name !";
            log.error(errorMessage);
            throw new DatabaseException(errorMessage, e);
        }
{'fraction_non_alphanumeric': 0.09777127053847405, 'fraction_numerical': 0.0008134049129656743, 'mean_word_length': 3.314385964912281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5455014', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1589, 'n_words': 332}