function write_structure_to_db(conn,patient_id)
%function write_structure_to_db(conn,patient_id)
%
%Input: Writes all structures from global planC to database
%
%APA, 02/27/2011

global planC

indexS = planC{end};

columnNames = {'structure_id','patient_id','scan_id','structure_name','structure_uid','structure_color'};

%Loop over all scans, find structures belonging to scan and add to DB.
numStructs = length(planC{indexS.structures});
allStructsV = 1:numStructs;

for scanNum = 1:length(planC{indexS.scan})

    %Find matching scan in DB
    scanUID = planC{indexS.scan}(scanNum).scanUID;
    sqlq_find_scan = ['Select scan_id from scan where scan_uid = ''', scanUID,''''];
    scan_raw = exec(conn, sqlq_find_scan);
    scan = fetch(scan_raw);
    scan = scan.Data;
    if ~isstruct(scan)
        warning('Scan does not exist in DB. Cannnot write structures. Proceed to next scan')
    else
        scan_id = scan.scan_id{1};
    end

    %Loop over structures belonging to this scan
    assocScansV = getStructureAssociatedScan(allStructsV, planC);

    matchingScansV = find(assocScansV == scanNum);

    indMatchStructsV = allStructsV(matchingScansV);

    for structNum = indMatchStructsV

        structS = planC{indexS.structures}(structNum);

        %Find matching structure in DB
        whereclause = {['where structure_uid = ''', structS.strUID,'''']};
        sqlq_find_str = ['Select structure_id from structure where structure_uid = ''', structS.strUID,''''];
        str_raw = exec(conn, sqlq_find_str);
        str = fetch(str_raw);
        str = str.Data;
        if ~isstruct(str)
            structure_id = char(java.util.UUID.randomUUID);
            isNewRecord = 1;
        else
            structure_id = str.structure_id{1};
            isNewRecord = 0;
        end

        %structure_id
        recC{1} = structure_id;

        %patient_id
        recC{2} = patient_id;

        %scan_id
        recC{3} = scan_id;

        %structureName
        recC{4} = structS.structureName;

        %structure_uid
        recC{5} = structS.strUID;

        %structure_color
        recC{6} = mat2str(structS.structureColor);

        if isNewRecord
            insert(conn,'structure',columnNames,recC);
        else
            update(conn,'structure',columnNames,recC,whereclause);
        end

        pause(0.05)

    end

end


function varargout = panel_guidelines( varargin )
% PANEL_GUIDELINES: Load a scenario in the guidelines tab.
%
% USAGE:  [bstPanel] = panel_guidelines('CreatePanel', ScenarioName)

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2017

eval(macro_method);
end

%% ===== CREATE PANEL =====
function [bstPanelNew, panelName] = CreatePanel(ScenarioName) %#ok<DEFNU>
    % Java initializations
    import java.awt.*;
    import javax.swing.*;
    panelName = 'Guidelines';

    % Load all the panels from the target scenario
    switch lower(ScenarioName)
        case 'epileptogenicity'
            ctrl = scenario_epilepto('CreatePanels');
        otherwise
            error(['Unknow scenario: ' ScenarioName]);
    end
    % Create main panel
    ctrl.jPanelContainer = gui_component('Panel');
    ctrl.ScenarioName = ScenarioName;

    % Create control panel
    jPanelControl = gui_river([1,1], [1,3,1,3], ' ');
    ctrl.jPanelContainer.add(jPanelControl, BorderLayout.EAST);
    % Add buttons
    buttonFormat = {Insets(0,0,0,0), Dimension(java_scaled('value', 47), java_scaled('value', 22))};
    ctrl.jButtonReset = gui_component('button', jPanelControl, 'br', 'Reset', buttonFormat, [], @(h,ev)ResetPanel());
    gui_component('label', jPanelControl, 'br', ' ');
    ctrl.jButtonPrev = gui_component('button', jPanelControl, 'br', '<<', buttonFormat, [], @(h,ev)SwitchPanel('prev'));
    ctrl.jButtonNext = gui_component('button', jPanelControl, 'br', '>>', buttonFormat, [], @(h,ev)SwitchPanel('next'));
    ctrl.jLabelStep  = gui_component('label', jPanelControl, 'br hfill', sprintf('0 / %d', length(ctrl.jPanels)));
    ctrl.jLabelStep.setHorizontalAlignment(JTextField.CENTER);
    gui_component('label', jPanelControl, 'br', ' ');
    ctrl.jButtonSkip = gui_component('button', jPanelControl, 'br', 'Skip', buttonFormat, [], @(h,ev)SwitchPanel('skip'));

    % Create the BstPanel object that is returned by the function
    bstPanelNew = BstPanel(panelName, ...
                           ctrl.jPanelContainer, ...
                           ctrl);
end

%% =================================================================================
%  === EXTERNAL CALLBACKS  =========================================================
%  =================================================================================
%% ===== GET PANEL CONTENTS =====
% GET Panel contents in a structure
function s = GetPanelContents() %#ok<DEFNU>
    s = [];
end

%% ===== CLOSE PANEL =====
function ClosePanel(varargin) %#ok<DEFNU>
    panelName = 'Guidelines';
    % Hide panel
    gui_hide(panelName);
    % Release mutex
    bst_mutex('release', panelName);
end

%% ===== GET CURRENT PANEL =====
function iPanel = GetCurrentPanel()
    % Get panel controls handles
    ctrl = bst_get('PanelControls', 'Guidelines');
    if isempty(ctrl)
        iPanel = 0;
        return;
    end
    % Read panel index
    strIndex = char(ctrl.jLabelStep.getText());
    iPanel = str2num(strIndex(1:2));
end

%% ===== SWITCH PANEL =====
function SwitchPanel(command)
    import java.awt.*;
    % Get panel controls handles
    ctrl = bst_get('PanelControls', 'Guidelines');
    if isempty(ctrl)
        return;
    end
    % Get current panel
    iPanel = GetCurrentPanel();

    % === VALIDATE CURRENT PANEL ===
    % If there is a validation callback for this panel
    if (iPanel >= 1) && ~isempty(ctrl.fcnValidate{iPanel}) && strcmpi(command, 'next')
        [isValidated, errMsg] = ctrl.fcnValidate{iPanel}();
        if ~isempty(errMsg)
            bst_error(errMsg, sprintf('Step #%d', iPanel), 0);
            return;
        end
    end
    % If this is the last panel and moving forward: stop
    if (iPanel == length(ctrl.jPanels)) && strcmpi(command, 'next')
        return;
    end

    % === MOVE TO NEXT PANEL ===
    % Remove existing panel
    if (iPanel >= 1) && (iPanel <= length(ctrl.jPanels))
        ctrl.jPanelContainer.remove(ctrl.jPanels(iPanel));
    end
    % Switch according to command
    switch (command)
        case 'next',  iPanel = iPanel + 1;
        case 'skip',  iPanel = iPanel + 1;
        case 'prev',  iPanel = iPanel - 1;
    end
    % If invalid panel: stop
    if (iPanel < 1) || (iPanel > length(ctrl.jPanels))
        return;
    end
    % Add new panel
    ctrl.jPanelContainer.add(ctrl.jPanels(iPanel), BorderLayout.CENTER);
    % Update step number
    ctrl.jLabelStep.setText(sprintf('%d / %d', iPanel, length(ctrl.jPanels)));
    % Repaint
    ctrl.jPanelContainer.invalidate();
    ctrl.jPanelContainer.repaint();
    % First panel: Disable previous button
    if (iPanel == 1)
        ctrl.jButtonPrev.setEnabled(0);
    else
        ctrl.jButtonPrev.setEnabled(1);
    end
    % Show/Hide Skip button
    ctrl.jButtonSkip.setVisible(ctrl.isSkip(iPanel));
%     % Last panel: Disable next button
%     if (iPanel == length(ctrl.jPanels))
%         ctrl.jButtonNext.setEnabled(0);
%     else
%         ctrl.jButtonNext.setEnabled(1);
%     end

    % === UPDATE NEW PANEL ===
    % If there is an update callback for this panel
    if (iPanel >= 1) && ~isempty(ctrl.fcnUpdate{iPanel})
        ctrl.fcnUpdate{iPanel}();
    end
end

%% ===== RESET PANEL =====
function ResetPanel()
    % Get panel controls handles
    ctrl = bst_get('PanelControls', 'Guidelines');
    if isempty(ctrl)
        return;
    end
    % Get current panel
    iPanel = GetCurrentPanel();
    if (iPanel >= 1) && ~isempty(ctrl.fcnReset{iPanel})
        ctrl.fcnReset{iPanel}();
    end
end

%% ===== OPTIONS: PICK FILE CALLBACK =====
function [OutputFiles, FileFormat] = PickFile(jControl, DefaultDir, SelectionMode, FilesOrDir, Filters, DefaultFormat) %#ok<DEFNU>
    % Parse inputs
    if (nargin < 6) || isempty(DefaultFormat)
        DefaultFormat = [];
    end
    % Get default import directory and formats
    LastUsedDirs = bst_get('LastUsedDirs');
    DefaultFormats = bst_get('DefaultFormats');
    % Default dir type
    DefaultFile = LastUsedDirs.(DefaultDir);
    % Default filter
    if ~isempty(DefaultFormat) && isfield(DefaultFormats, DefaultFormat)
        defaultFilter = DefaultFormats.(DefaultFormat);
    else
        defaultFilter = [];
        DefaultFormat = [];
    end
    % Pick a file
    [OutputFiles, FileFormat, FileFilter] = java_getfile('open', 'Select file', DefaultFile, SelectionMode, FilesOrDir, Filters, defaultFilter);
    % If nothing selected
    if isempty(OutputFiles)
        return
    end
    % If only one file selected
    if ~iscell(OutputFiles)
        OutputFiles = {OutputFiles};
    end
    % Get the files
    OutputFiles = file_expand_selection(FileFilter, OutputFiles);
    if isempty(OutputFiles)
        error(['No ' FileFormat ' file in the selected directories.']);
    end
    % Save default import directory
    if ischar(OutputFiles)
        newDir = OutputFiles;
    elseif iscell(OutputFiles)
        newDir = OutputFiles{1};
    end
    % Get parent folder if needed
    if ~isdir(newDir)
        newDir = bst_fileparts(newDir);
    end
    LastUsedDirs.(DefaultDir) = newDir;
    bst_set('LastUsedDirs', LastUsedDirs);
    % Save default import format
    if ~isempty(DefaultFormat)
        DefaultFormats.(DefaultFormat) = FileFormat;
        bst_set('DefaultFormats',  DefaultFormats);
    end

    % Get file descriptions (one/many)
    if ischar(OutputFiles)
        strFiles = OutputFiles;
    else
        if (length(OutputFiles) == 1)
            strFiles = OutputFiles{1};
        elseif isa(jControl, 'javax.swing.JLabel')
            strFiles = sprintf('%s
', OutputFiles{:});
        else
            strFiles = sprintf('[%d files]', length(OutputFiles));
        end
    end

    % Update the attached control
    if isempty(jControl)
        %disp(strFiles);
    elseif isa(jControl, 'javax.swing.JTextField')
        jControl.setText(strFiles);
    elseif isa(jControl, 'javax.swing.JLabel')
        jControl.setText(['' strFiles]);
    end
end


function about

%Tools for maintaining and programming in the GISMO environment.
% The admin package provides a series of tools to assist with programming
% in GISMO.

help admin;


function [dateNum, dateStr] = gitLastRevDate(repo, file)
dateNum = 0;
dateStr = '';

if ~exist('repo','var') || isempty(repo)
    repo = pwd;
end
if ~exist('file','var') || isempty(file)
    file = '';
end
repoFull = filesepStandard_startup(repo, 'full');
currdir = pwd;

if ismac()
    cmdprefix = 'TERM=ansi; ';
    e = system(cmdprefix);
    if e ~= 0
        return;
    end
else
    cmdprefix = '';
end

ii = 1;
cmds{ii,1} = sprintf('cd %s', repoFull); ii = ii+1;
cmds{ii,1} = sprintf('%sgit log -1 --format=%%ci --date=short %s', cmdprefix, file);
[errs, msgs] = exeShellCmds(cmds, false, true);
if all(errs==0)
    if ~isempty(msgs{2})
        ds = msgs{2};
    else
        ds = '';
        %ds = char(datetime('now','TimeZone','local','Format','yyyy-MM-dd HH:mm:ss'));
    end
    [dateNum, dateStr] = datestr2datenum(ds);
end
cd(currdir)


if(exist('E:\datasets\FERA_2015\semaine/SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'E:\datasets\FERA_2015\semaine/SEMAINE-Sessions/';
elseif(exist('I:\datasets\FERA_2015\Semaine\SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'I:\datasets\FERA_2015\Semaine\SEMAINE-Sessions/';
elseif(exist('C:\tadas\face_datasets\fera_2015\semaine/SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'C:\tadas\face_datasets\fera_2015\semaine/SEMAINE-Sessions/';
elseif(exist('D:\datasets\face_datasets\fera_2015\semaine\SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'D:\datasets\face_datasets\fera_2015\semaine\SEMAINE-Sessions/';
elseif(exist('D:\Datasets\FERA_2015\semaine\SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'D:\Datasets\FERA_2015\semaine\SEMAINE-Sessions/';
elseif(exist('D:/fera_2015/semaine/SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = 'D:/fera_2015/semaine/SEMAINE-Sessions/';
elseif(exist('/multicomp/datasets/face_datasets/FERA_2015/Semaine/SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = '/multicomp/datasets/face_datasets/FERA_2015/Semaine/SEMAINE-Sessions/';
elseif(exist('/media/tadas/2EBEA130BEA0F20F/datasets/FERA_2015/semaine/SEMAINE-Sessions/', 'file'))
    SEMAINE_dir = '/media/tadas/2EBEA130BEA0F20F/datasets/FERA_2015/semaine/SEMAINE-Sessions/';
else
    fprintf('SEMAINE location not found (or not defined)\n');
end

if(exist('SEMAINE_dir', 'var'))
    hog_data_dir = [SEMAINE_dir, '../processed_data/'];
end

train_recs = {'rec1', 'rec12', 'rec14', 'rec19', 'rec23', 'rec25', 'rec37', 'rec39', 'rec43', 'rec45', 'rec48', 'rec50', 'rec52', 'rec54', 'rec56', 'rec60'};
devel_recs = {'rec9', 'rec13', 'rec15', 'rec20', 'rec24', 'rec26', 'rec38', 'rec42', 'rec44', 'rec46', 'rec49', 'rec51', 'rec53', 'rec55', 'rec58'};

aus_SEMAINE = [2 12 17 25 28 45];


function varargout = fname(varargin)
% Format
% For getting the value
% dat = fname(obj)
%
% For setting the value
% obj = fname(obj,dat)
% _______________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

%
% $Id$

if nargin==2,
    varargout{1} = asgn(varargin{:});
elseif nargin==1,
    varargout{1} = ref(varargin{:});
else
    error('Wrong number of arguments.');
end;
return;

function dat = ref(obj)
dat = obj.fname;
return;

function obj = asgn(obj,dat)
if ischar(dat)
    obj.fname = deblank(dat(:)');
else
    error('"fname" must be a character string.');
end;
return;