<commit_before>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2021 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.contentviewers.osaccount;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import static java.util.Locale.US;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import org.openide.util.NbBundle.Messages;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.contentviewers.osaccount.SectionData.RowData;
import org.sleuthkit.datamodel.BlackboardAttribute;
import org.sleuthkit.datamodel.DataSource;
import org.sleuthkit.datamodel.Host;
import org.sleuthkit.datamodel.OsAccount;
import org.sleuthkit.datamodel.OsAccountAttribute;
import org.sleuthkit.datamodel.OsAccountInstance;
import org.sleuthkit.datamodel.OsAccountManager;
import org.sleuthkit.datamodel.OsAccountRealm;
import org.sleuthkit.datamodel.SleuthkitCase;

/**
 * Panel for displaying the properties of an OsAccount.
 */
public class OsAccountDataPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    final private static Logger logger = Logger.getLogger(OsAccountDataPanel.class.getName());

    private static final int KEY_COLUMN = 0;
    private static final int VALUE_COLUMN = 1;

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM dd yyyy", US);

    private PanelDataFetcher dataFetcher = null;

    // Panel constructor.
    OsAccountDataPanel() {
        initialize();
    }

    /**
     * Initializes the panel layout.
     */
    private void initialize() {
        this.setLayout(new GridBagLayout());
    }

    /**
     * Set the OsAccount to display in this panel.
     *
     * @param account OsAccount to display, if null is passed the panel will
     *                appear blank.
     */
    void setOsAccountId(Long osAccountId) {
        removeAll();
        revalidate();

        if (osAccountId != null) {
            setLayout(new BorderLayout());
            add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);

            if (dataFetcher != null && !dataFetcher.isDone()) {
                dataFetcher.cancel(true);
            }

            dataFetcher = new PanelDataFetcher(osAccountId);
            dataFetcher.execute();
        }
    }
    
    void setOsAccount(OsAccount account) {
        removeAll();
        revalidate();

        if (account != null) {
            setLayout(new BorderLayout());
            add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);

            if (dataFetcher != null && !dataFetcher.isDone()) {
                dataFetcher.cancel(true);
            }

            dataFetcher = new PanelDataFetcher(account);
            dataFetcher.execute();
        }
    }

    /**
     * Give all of the data to display, create the swing components and add to
     * the panel.
     *
     * @param panelData Data to be displayed.
     */
    private void addDataComponents(List<SectionData> panelData) {
        int rowCnt = 0;
        for (SectionData section : panelData) {
            addTitle(section.getTitle(), rowCnt++);

            for (RowData<String, String> rowData : section) {
                String key = rowData.getKey();
                String value = rowData.getValue();

                addPropertyName(key, rowCnt);
                addPropertyValue(value, rowCnt++);
            }
        }

        // Generate the constraints for a Vertical Glue to fill the space, if
        // any at the bottom of the panel.
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = rowCnt;
        constraints.gridwidth = 2;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;
        add(Box.createVerticalGlue(), constraints);
    }

    @Messages({
        "OsAccountDataPanel_basic_title=Basic Properties",
        "OsAccountDataPanel_basic_login=Login",
        "OsAccountDataPanel_basic_fullname=Full Name",
        "OsAccountDataPanel_basic_address=Address",
        "OsAccountDataPanel_basic_admin=Administrator",
        "OsAccountDataPanel_basic_type=Type",
        "OsAccountDataPanel_basic_creationDate=Creation Date",})

    /**
     * Returns the data for the Basic Properties section of the panel.
     *
     * @param account Selected account
     *
     * @return The basic properties data for the given account.
     */
    private SectionData buildBasicProperties(OsAccount account) {
        SectionData data = new SectionData(Bundle.OsAccountDataPanel_basic_title());

        Optional<String> optional = account.getLoginName();
        data.addData(Bundle.OsAccountDataPanel_basic_login(),
                optional.isPresent() ? optional.get() : "");

        optional = account.getFullName();
        data.addData(Bundle.OsAccountDataPanel_basic_fullname(),
                optional.isPresent() ? optional.get() : "");

        data.addData(Bundle.OsAccountDataPanel_basic_address(),
                account.getName() == null || account.getName().isEmpty() ? "" : account.getName());

        data.addData(Bundle.OsAccountDataPanel_basic_type(), account.getOsAccountType().getName());

        Optional<Long> crTime = account.getCreationTime();
        if (crTime.isPresent()) {
            data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), DATE_FORMAT.format(new Date(crTime.get() * 1000)));
        } else {
            data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), "");
        }

        return data;
    }
<commit_msg>4262: Account type and status getters should return optional.<commit_after>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2021 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.contentviewers.osaccount;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import static java.util.Locale.US;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import org.openide.util.NbBundle.Messages;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.contentviewers.osaccount.SectionData.RowData;
import org.sleuthkit.datamodel.BlackboardAttribute;
import org.sleuthkit.datamodel.DataSource;
import org.sleuthkit.datamodel.Host;
import org.sleuthkit.datamodel.OsAccount;
import org.sleuthkit.datamodel.OsAccountAttribute;
import org.sleuthkit.datamodel.OsAccountInstance;
import org.sleuthkit.datamodel.OsAccountManager;
import org.sleuthkit.datamodel.OsAccountRealm;
import org.sleuthkit.datamodel.SleuthkitCase;

/**
 * Panel for displaying the properties of an OsAccount.
 */
public class OsAccountDataPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    final private static Logger logger = Logger.getLogger(OsAccountDataPanel.class.getName());

    private static final int KEY_COLUMN = 0;
    private static final int VALUE_COLUMN = 1;

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM dd yyyy", US);

    private PanelDataFetcher dataFetcher = null;

    // Panel constructor.
    OsAccountDataPanel() {
        initialize();
    }

    /**
     * Initializes the panel layout.
     */
    private void initialize() {
        this.setLayout(new GridBagLayout());
    }

    /**
     * Set the OsAccount to display in this panel.
     *
     * @param account OsAccount to display, if null is passed the panel will
     *                appear blank.
     */
    void setOsAccountId(Long osAccountId) {
        removeAll();
        revalidate();

        if (osAccountId != null) {
            setLayout(new BorderLayout());
            add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);

            if (dataFetcher != null && !dataFetcher.isDone()) {
                dataFetcher.cancel(true);
            }

            dataFetcher = new PanelDataFetcher(osAccountId);
            dataFetcher.execute();
        }
    }
    
    void setOsAccount(OsAccount account) {
        removeAll();
        revalidate();

        if (account != null) {
            setLayout(new BorderLayout());
            add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);

            if (dataFetcher != null && !dataFetcher.isDone()) {
                dataFetcher.cancel(true);
            }

            dataFetcher = new PanelDataFetcher(account);
            dataFetcher.execute();
        }
    }

    /**
     * Give all of the data to display, create the swing components and add to
     * the panel.
     *
     * @param panelData Data to be displayed.
     */
    private void addDataComponents(List<SectionData> panelData) {
        int rowCnt = 0;
        for (SectionData section : panelData) {
            addTitle(section.getTitle(), rowCnt++);

            for (RowData<String, String> rowData : section) {
                String key = rowData.getKey();
                String value = rowData.getValue();

                addPropertyName(key, rowCnt);
                addPropertyValue(value, rowCnt++);
            }
        }

        // Generate the constraints for a Vertical Glue to fill the space, if
        // any at the bottom of the panel.
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = rowCnt;
        constraints.gridwidth = 2;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weightx = 1;
        constraints.weighty = 1;
        add(Box.createVerticalGlue(), constraints);
    }

    @Messages({
        "OsAccountDataPanel_basic_title=Basic Properties",
        "OsAccountDataPanel_basic_login=Login",
        "OsAccountDataPanel_basic_fullname=Full Name",
        "OsAccountDataPanel_basic_address=Address",
        "OsAccountDataPanel_basic_admin=Administrator",
        "OsAccountDataPanel_basic_type=Type",
        "OsAccountDataPanel_basic_creationDate=Creation Date",})

    /**
     * Returns the data for the Basic Properties section of the panel.
     *
     * @param account Selected account
     *
     * @return The basic properties data for the given account.
     */
    private SectionData buildBasicProperties(OsAccount account) {
        SectionData data = new SectionData(Bundle.OsAccountDataPanel_basic_title());

        Optional<String> optional = account.getLoginName();
        data.addData(Bundle.OsAccountDataPanel_basic_login(),
                optional.isPresent() ? optional.get() : "");

        optional = account.getFullName();
        data.addData(Bundle.OsAccountDataPanel_basic_fullname(),
                optional.isPresent() ? optional.get() : "");

        data.addData(Bundle.OsAccountDataPanel_basic_address(),
                account.getName() == null || account.getName().isEmpty() ? "" : account.getName());

        
        data.addData(Bundle.OsAccountDataPanel_basic_type(), 
            account.getOsAccountType().isPresent() ? account.getOsAccountType().get().getName() : "");

        Optional<Long> crTime = account.getCreationTime();
        if (crTime.isPresent()) {
            data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), DATE_FORMAT.format(new Date(crTime.get() * 1000)));
        } else {
            data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), "");
        }

        return data;
    }

{'fraction_non_alphanumeric': 0.09860944976076555, 'fraction_numerical': 0.003289473684210526, 'mean_word_length': 2.889793544635068, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5220341', 'n_tokens_mistral': 3804, 'n_tokens_neox': 3687, 'n_words': 919}