<commit_before><?php
App::uses('AppModel', 'Model');
App::uses('TmpFileTool', 'Tools');

/**
 * @property Tag $Tag
 * @property GalaxyClusterRelation $GalaxyClusterRelation
 * @property GalaxyElement $GalaxyElement
 */
class GalaxyCluster extends AppModel
{
    public $useTable = 'galaxy_clusters';

    public $recursive = -1;

    public $actsAs = array(
        'AuditLog',
        'SysLogLogable.SysLogLogable' => array( // TODO Audit, logable
            'userModel' => 'User',
            'userKey' => 'user_id',
            'change' => 'full'),
        'Containable',
    );

    private $__assetCache = array();

    public $validate = array(
        'value' => array(
            'stringNotEmpty' => array(
                'rule' => array('stringNotEmpty')
            )
        ),
        'uuid' => array(
            'uuid' => array(
                'rule' => 'uuid',
                'message' => 'Please provide a valid RFC 4122 UUID'
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'The UUID provided is not unique',
                'required' => 'create'
            )
        ),
        'distribution' => array(
            'rule' => array('inList', array('0', '1', '2', '3', '4')),
            'message' => 'Options: Your organisation only, This community only, Connected communities, All communities, Sharing group',
            'required' => true
        ),
        'published' => array(
            'boolean' => array(
                'rule' => array('boolean'),
            ),
        ),
    );

    public $belongsTo = array(
        'Galaxy' => array(
            'className' => 'Galaxy',
            'foreignKey' => 'galaxy_id',
        ),
        'Tag' => array(
            'foreignKey' => false,
            'conditions' => array('GalaxyCluster.tag_name = Tag.name')
        ),
        'Org' => array(
            'className' => 'Organisation',
            'foreignKey' => 'org_id'
        ),
        'Orgc' => array(
            'className' => 'Organisation',
            'foreignKey' => 'orgc_id'
        ),
        'SharingGroup' => array(
                'className' => 'SharingGroup',
                'foreignKey' => 'sharing_group_id'
        )
    );

    private $__clusterCache = array();
    private $deletedClusterUUID;
    public $bulkEntry = false;

    public $hasMany = array(
        'GalaxyElement' => array('dependent' => true),
        'GalaxyClusterRelation' => array(
            'className' => 'GalaxyClusterRelation',
            'foreignKey' => 'galaxy_cluster_id',
            'dependent' => true,
        ),
        'TargetingClusterRelation' => array(
            'className' => 'GalaxyClusterRelation',
            'foreignKey' => 'referenced_galaxy_cluster_id',
        ),
    );

    public $validFormats = array(
        'json' => array('json', 'JsonExport', 'json'),
    );

    public function beforeValidate($options = array())
    {
        $cluster = &$this->data['GalaxyCluster'];
        if (!isset($cluster['description'])) {
            $cluster['description'] = '';
        }
        if (isset($cluster['distribution']) && $cluster['distribution'] != 4) {
            $cluster['sharing_group_id'] = null;
        }
        if (!isset($cluster['published'])) {
            $cluster['published'] = false;
        }
        if (!isset($cluster['authors']) || $cluster['authors'] === null) {
            $cluster['authors'] = '';
        } elseif (is_array($cluster['authors'])) {
            $cluster['authors'] = json_encode($cluster['authors']);
        }
        return true;
    }

    public function afterFind($results, $primary = false)
    {
        foreach ($results as $k => $result) {
            if (isset($result[$this->alias]['authors'])) {
                $results[$k][$this->alias]['authors'] = json_decode($results[$k][$this->alias]['authors'], true);
            }
            if (isset($result[$this->alias]['distribution']) && $results[$k][$this->alias]['distribution'] != 4) {
                unset($results[$k]['SharingGroup']);
            }
            if (isset($result[$this->alias]['org_id']) && $results[$k][$this->alias]['org_id'] == 0) {
                if (isset($results[$k]['Org'])) {
                    $results[$k]['Org'] = Organisation::GENERIC_MISP_ORGANISATION;
                }
            }
            if (isset($result[$this->alias]['orgc_id']) && $results[$k][$this->alias]['orgc_id'] == 0) {
                if (isset($results[$k]['Orgc'])) {
                    $results[$k]['Orgc'] = Organisation::GENERIC_MISP_ORGANISATION;
                }
            }

            if (!empty($result['GalaxyClusterRelation'])) {
                foreach ($results[$k]['GalaxyClusterRelation'] as $i => $relation) {
                    if (isset($relation['distribution']) && $relation['distribution'] != 4) {
                        unset($results[$k]['GalaxyClusterRelation'][$i]['SharingGroup']);
                    }
                }
            }
        }
        return $results;
    }<commit_msg>chg: [galaxy] Simplify code for fetching galaxy cluster<commit_after><?php
App::uses('AppModel', 'Model');
App::uses('TmpFileTool', 'Tools');

/**
 * @property Tag $Tag
 * @property GalaxyClusterRelation $GalaxyClusterRelation
 * @property GalaxyElement $GalaxyElement
 */
class GalaxyCluster extends AppModel
{
    public $useTable = 'galaxy_clusters';

    public $recursive = -1;

    public $actsAs = array(
        'AuditLog',
        'SysLogLogable.SysLogLogable' => array( // TODO Audit, logable
            'userModel' => 'User',
            'userKey' => 'user_id',
            'change' => 'full'),
        'Containable',
    );

    private $__assetCache = array();

    public $validate = array(
        'value' => array(
            'stringNotEmpty' => array(
                'rule' => array('stringNotEmpty')
            )
        ),
        'uuid' => array(
            'uuid' => array(
                'rule' => 'uuid',
                'message' => 'Please provide a valid RFC 4122 UUID'
            ),
            'unique' => array(
                'rule' => 'isUnique',
                'message' => 'The UUID provided is not unique',
                'required' => 'create'
            )
        ),
        'distribution' => array(
            'rule' => array('inList', array('0', '1', '2', '3', '4')),
            'message' => 'Options: Your organisation only, This community only, Connected communities, All communities, Sharing group',
            'required' => true
        ),
        'published' => array(
            'boolean' => array(
                'rule' => array('boolean'),
            ),
        ),
    );

    public $belongsTo = array(
        'Galaxy' => array(
            'className' => 'Galaxy',
            'foreignKey' => 'galaxy_id',
        ),
        'Tag' => array(
            'foreignKey' => false,
            'conditions' => array('GalaxyCluster.tag_name = Tag.name')
        ),
        'Org' => array(
            'className' => 'Organisation',
            'foreignKey' => 'org_id'
        ),
        'Orgc' => array(
            'className' => 'Organisation',
            'foreignKey' => 'orgc_id'
        ),
        'SharingGroup' => array(
                'className' => 'SharingGroup',
                'foreignKey' => 'sharing_group_id'
        )
    );

    private $__clusterCache = array();
    private $deletedClusterUUID;
    public $bulkEntry = false;

    public $hasMany = array(
        'GalaxyElement' => array('dependent' => true),
        'GalaxyClusterRelation' => array(
            'className' => 'GalaxyClusterRelation',
            'foreignKey' => 'galaxy_cluster_id',
            'dependent' => true,
        ),
        'TargetingClusterRelation' => array(
            'className' => 'GalaxyClusterRelation',
            'foreignKey' => 'referenced_galaxy_cluster_id',
        ),
    );

    public $validFormats = array(
        'json' => array('json', 'JsonExport', 'json'),
    );

    public function beforeValidate($options = array())
    {
        $cluster = &$this->data['GalaxyCluster'];
        if (!isset($cluster['description'])) {
            $cluster['description'] = '';
        }
        if (isset($cluster['distribution']) && $cluster['distribution'] != 4) {
            $cluster['sharing_group_id'] = null;
        }
        if (!isset($cluster['published'])) {
            $cluster['published'] = false;
        }
        if (!isset($cluster['authors'])) {
            $cluster['authors'] = '';
        } elseif (is_array($cluster['authors'])) {
            $cluster['authors'] = JsonTool::encode($cluster['authors']);
        }
        return true;
    }

    public function afterFind($results, $primary = false)
    {
        foreach ($results as $k => $result) {
            if (isset($result[$this->alias]['authors'])) {
                $results[$k][$this->alias]['authors'] = json_decode($result[$this->alias]['authors'], true);
            }
            if (isset($result[$this->alias]['distribution']) && $result[$this->alias]['distribution'] != 4) {
                unset($results[$k]['SharingGroup']);
            }
            if (isset($result[$this->alias]['org_id']) && $result[$this->alias]['org_id'] == 0) {
                if (isset($results[$k]['Org'])) {
                    $results[$k]['Org'] = Organisation::GENERIC_MISP_ORGANISATION;
                }
            }
            if (isset($result[$this->alias]['orgc_id']) && $result[$this->alias]['orgc_id'] == 0) {
                if (isset($results[$k]['Orgc'])) {
                    $results[$k]['Orgc'] = Organisation::GENERIC_MISP_ORGANISATION;
                }
            }

            if (!empty($result['GalaxyClusterRelation'])) {
                foreach ($result['GalaxyClusterRelation'] as $i => $relation) {
                    if (isset($relation['distribution']) && $relation['distribution'] != 4) {
                        unset($results[$k]['GalaxyClusterRelation'][$i]['SharingGroup']);
                    }
                }
            }
        }
        return $results;
    }
{'fraction_non_alphanumeric': 0.17272009474930913, 'fraction_numerical': 0.002960915909988156, 'mean_word_length': 1.9184907834101383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7290524', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2767, 'n_words': 523}