2020-05-06 07:59:57 +02:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
2021-07-27 21:01:52 +02:00
/**
* @ property GalaxyClusterRelationTag $GalaxyClusterRelationTag
2021-10-30 23:05:11 +02:00
* @ property GalaxyCluster $TargetCluster
2022-05-22 17:09:00 +02:00
* @ property SharingGroup $SharingGroup
2021-07-27 21:01:52 +02:00
*/
2020-05-06 07:59:57 +02:00
class GalaxyClusterRelation extends AppModel
{
public $useTable = 'galaxy_cluster_relations' ;
public $recursive = - 1 ;
public $actsAs = array (
2021-01-22 13:01:23 +01:00
'AuditLog' ,
2020-11-12 11:18:36 +01:00
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
'userModel' => 'User' ,
'userKey' => 'user_id' ,
'change' => 'full' ),
'Containable' ,
2020-05-06 07:59:57 +02:00
);
public $validate = array (
2020-05-07 11:03:18 +02:00
'referenced_galaxy_cluster_type' => array (
'stringNotEmpty' => array (
'rule' => array ( 'stringNotEmpty' )
)
),
2020-05-25 10:04:07 +02:00
'galaxy_cluster_uuid' => array (
'uuid' => array (
2021-07-27 21:01:52 +02:00
'rule' => 'uuid' ,
'message' => 'Please provide a valid RFC 4122 UUID'
2020-05-25 10:04:07 +02:00
),
2020-05-07 12:30:05 +02:00
),
2020-05-07 11:03:18 +02:00
'referenced_galaxy_cluster_uuid' => array (
'uuid' => array (
2021-07-27 21:01:52 +02:00
'rule' => 'uuid' ,
'message' => 'Please provide a valid RFC 4122 UUID'
2020-05-07 11:03:18 +02:00
),
),
'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
)
2020-05-06 07:59:57 +02:00
);
public $belongsTo = array (
2020-05-25 10:04:07 +02:00
'SourceCluster' => array (
2020-05-06 07:59:57 +02:00
'className' => 'GalaxyCluster' ,
'foreignKey' => 'galaxy_cluster_id' ,
2020-05-07 11:03:18 +02:00
),
2020-05-25 10:04:07 +02:00
'TargetCluster' => array (
2020-05-07 11:03:18 +02:00
'className' => 'GalaxyCluster' ,
'foreignKey' => 'referenced_galaxy_cluster_id' ,
),
'SharingGroup' => array (
'className' => 'SharingGroup' ,
'foreignKey' => 'sharing_group_id'
2020-05-25 10:04:07 +02:00
),
2020-05-06 07:59:57 +02:00
);
public $hasMany = array (
'GalaxyClusterRelationTag' => array ( 'dependent' => true ),
);
2020-06-08 11:10:59 +02:00
public function afterFind ( $results , $primary = false )
{
foreach ( $results as $k => $result ) {
2021-10-25 21:15:49 +02:00
if ( isset ( $result [ 'TargetCluster' ]) && key_exists ( 'id' , $result [ 'TargetCluster' ]) && is_null ( $result [ 'TargetCluster' ][ 'id' ])) {
2020-06-08 11:10:59 +02:00
$results [ $k ][ 'TargetCluster' ] = array ();
}
2021-10-25 21:15:49 +02:00
if ( isset ( $result [ 'GalaxyClusterRelation' ][ 'distribution' ]) && $result [ 'GalaxyClusterRelation' ][ 'distribution' ] != 4 ) {
2020-07-09 14:44:21 +02:00
unset ( $results [ $k ][ 'SharingGroup' ]);
}
2020-06-08 11:10:59 +02:00
}
return $results ;
}
2020-09-25 11:13:26 +02:00
public function buildConditions ( $user , $clusterConditions = true )
2020-05-07 11:03:18 +02:00
{
2020-09-23 17:34:13 +02:00
$conditions = [];
2020-05-07 11:03:18 +02:00
if ( ! $user [ 'Role' ][ 'perm_site_admin' ]) {
2020-06-12 15:08:01 +02:00
$alias = $this -> alias ;
2022-05-22 17:09:00 +02:00
$sgids = $this -> SharingGroup -> authorizedIds ( $user );
2020-09-25 11:56:44 +02:00
$gcOwnerIds = $this -> SourceCluster -> cacheGalaxyClusterOwnerIDs ( $user );
2020-09-23 17:34:13 +02:00
$conditionsRelations [ 'AND' ][ 'OR' ] = [
2020-09-25 11:56:44 +02:00
" ${ alias } .galaxy_cluster_id " => $gcOwnerIds ,
2020-09-23 17:34:13 +02:00
[
'AND' => [
2020-06-12 15:08:01 +02:00
" ${ alias } .distribution > " => 0 ,
" ${ alias } .distribution < " => 4
2020-09-23 17:34:13 +02:00
],
],
[
'AND' => [
2020-06-12 15:08:01 +02:00
" ${ alias } .sharing_group_id " => $sgids ,
" ${ alias } .distribution " => 4
2020-09-23 17:34:13 +02:00
]
]
];
2020-09-25 11:13:26 +02:00
$conditionsSourceCluster = $clusterConditions ? $this -> SourceCluster -> buildConditions ( $user ) : [];
2020-09-23 17:34:13 +02:00
$conditions = [
'AND' => [
$conditionsRelations ,
$conditionsSourceCluster
]
];
2020-05-07 11:03:18 +02:00
}
return $conditions ;
}
2020-05-11 16:55:18 +02:00
public function fetchRelations ( $user , $options , $full = false )
2020-05-07 12:30:05 +02:00
{
$params = array (
'conditions' => $this -> buildConditions ( $user ),
'recursive' => - 1
);
if ( ! empty ( $options [ 'contain' ])) {
$params [ 'contain' ] = $options [ 'contain' ];
2020-07-07 08:16:46 +02:00
} elseif ( $full ) {
2020-05-25 10:04:07 +02:00
$params [ 'contain' ] = array ( 'SharingGroup' , 'SourceCluster' , 'TargetCluster' );
2020-05-07 12:30:05 +02:00
}
2020-09-25 12:29:41 +02:00
if ( empty ( $params [ 'contain' ])) {
$params [ 'contain' ] = [ 'SourceCluster' ];
}
2020-09-25 12:16:54 +02:00
if ( ! in_array ( 'SourceCluster' , $params [ 'contain' ])) {
$params [ 'contain' ][] = 'SourceCluster' ;
}
2020-05-07 12:30:05 +02:00
if ( isset ( $options [ 'fields' ])) {
$params [ 'fields' ] = $options [ 'fields' ];
}
if ( isset ( $options [ 'conditions' ])) {
$params [ 'conditions' ][ 'AND' ][] = $options [ 'conditions' ];
}
if ( isset ( $options [ 'group' ])) {
$params [ 'group' ] = empty ( $options [ 'group' ]) ? $options [ 'group' ] : false ;
}
$relations = $this -> find ( 'all' , $params );
return $relations ;
}
2020-05-06 07:59:57 +02:00
public function getExistingRelationships ()
{
2021-02-19 12:25:54 +01:00
$existingRelationships = $this -> find ( 'column' , array (
2020-05-06 07:59:57 +02:00
'recursive' => - 1 ,
2021-02-19 12:25:54 +01:00
'fields' => array ( 'referenced_galaxy_cluster_type' ),
'unique' => true ,
));
$this -> ObjectRelationship = ClassRegistry :: init ( 'ObjectRelationship' );
$objectRelationships = $this -> ObjectRelationship -> find ( 'column' , array (
2020-09-25 16:33:42 +02:00
'recursive' => - 1 ,
2021-02-19 12:25:54 +01:00
'fields' => array ( 'name' ),
'unique' => true ,
2020-09-25 16:33:42 +02:00
));
2021-02-19 12:25:54 +01:00
return array_unique ( array_merge ( $existingRelationships , $objectRelationships ));
2020-05-06 07:59:57 +02:00
}
2020-07-09 14:44:21 +02:00
/**
* saveRelations
*
* @ see saveRelation
* @ return array List of errors if any
*/
public function saveRelations ( array $user , array $cluster , array $relations , $captureTag = false , $force = false )
2020-06-09 10:08:36 +02:00
{
$errors = array ();
2020-07-07 08:16:46 +02:00
foreach ( $relations as $k => $relation ) {
2020-07-09 14:44:21 +02:00
$saveResult = $this -> saveRelation ( $user , $cluster , $relation , $captureTag = $captureTag , $force = $force );
2020-06-09 10:08:36 +02:00
$errors = array_merge ( $errors , $saveResult );
}
return $errors ;
}
2020-11-23 19:38:19 +01:00
2020-07-09 14:44:21 +02:00
/**
* saveRelation Respecting ACL saves a relation and set correct fields where applicable .
* Contrary to its capture equivalent , trying to save a relation for a unknown target cluster will fail .
*
* @ param array $user
* @ param array $cluster The cluster from which the relation is originating
* @ param array $relation The relation to save
* @ param bool $captureTag Should the tag be captured if it doesn ' t exists
* @ param bool $force Should the relation be edited if it exists
* @ return array List errors if any
*/
public function saveRelation ( array $user , array $cluster , array $relation , $captureTag = false , $force = false )
2020-05-07 12:30:05 +02:00
{
2020-05-07 16:17:44 +02:00
$errors = array ();
2020-06-09 10:08:36 +02:00
if ( ! isset ( $relation [ 'GalaxyClusterRelation' ]) && ! empty ( $relation )) {
$relation = array ( 'GalaxyClusterRelation' => $relation );
}
2020-07-09 14:44:21 +02:00
$authorizationCheck = $this -> SourceCluster -> fetchIfAuthorized ( $user , $cluster , array ( 'edit' ), $throwErrors = false , $full = false );
if ( isset ( $authorizationCheck [ 'authorized' ]) && ! $authorizationCheck [ 'authorized' ]) {
$errors [] = $authorizationCheck [ 'error' ];
return $errors ;
}
2020-06-09 10:08:36 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ] = $cluster [ 'uuid' ];
2020-05-29 10:02:17 +02:00
2020-11-19 23:36:11 +01:00
$existingRelation = $this -> find ( 'first' , [
'conditions' => [
'galaxy_cluster_uuid' => $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ],
'referenced_galaxy_cluster_uuid' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ],
'referenced_galaxy_cluster_type' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_type' ],
],
'fields' => [ 'id' ],
'recursive' => - 1 ,
]);
2020-05-07 12:30:05 +02:00
if ( ! empty ( $existingRelation )) {
2020-05-29 10:02:17 +02:00
if ( ! $force ) {
$errors [] = __ ( 'Relation already exists' );
return $errors ;
} else {
$relation [ 'GalaxyClusterRelation' ][ 'id' ] = $existingRelation [ 'GalaxyClusterRelation' ][ 'id' ];
}
} else {
$this -> create ();
2020-05-07 12:30:05 +02:00
}
2020-05-07 16:17:44 +02:00
if ( empty ( $errors )) {
2020-07-09 14:44:21 +02:00
if ( ! isset ( $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ])) {
$errors [] = __ ( 'referenced_galaxy_cluster_uuid not provided' );
return $errors ;
}
2020-07-09 17:22:08 +02:00
if ( ! $force ) {
$targetCluster = $this -> TargetCluster -> fetchIfAuthorized ( $user , $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ], 'view' , $throwErrors = false , $full = false );
2022-11-08 16:13:14 +01:00
if ( isset ( $targetCluster [ 'authorized' ]) && ! $targetCluster [ 'authorized' ]) { // do not save the relation if referenced cluster is not accessible by the user (or does not exist)
2020-07-09 17:22:08 +02:00
$errors [] = array ( __ ( 'Invalid referenced galaxy cluster' ));
return $errors ;
}
2020-07-09 14:44:21 +02:00
}
2020-05-25 17:02:26 +02:00
$relation = $this -> syncUUIDsAndIDs ( $user , $relation );
2020-05-07 16:17:44 +02:00
$saveSuccess = $this -> save ( $relation );
if ( $saveSuccess ) {
$savedRelation = $this -> find ( 'first' , array (
'conditions' => array ( 'id' => $this -> id ),
'recursive' => - 1
));
2020-06-09 10:08:36 +02:00
$tags = array ();
if ( ! empty ( $relation [ 'GalaxyClusterRelation' ][ 'tags' ])) {
$tags = $relation [ 'GalaxyClusterRelation' ][ 'tags' ];
} elseif ( ! empty ( $relation [ 'GalaxyClusterRelation' ][ 'GalaxyClusterRelationTag' ])) {
$tags = $relation [ 'GalaxyClusterRelation' ][ 'GalaxyClusterRelationTag' ];
2020-06-09 12:57:19 +02:00
$tags = Hash :: extract ( $tags , '{n}.name' );
} elseif ( ! empty ( $relation [ 'GalaxyClusterRelation' ][ 'Tag' ])) {
$tags = $relation [ 'GalaxyClusterRelation' ][ 'Tag' ];
$tags = Hash :: extract ( $tags , '{n}.name' );
2020-06-09 10:08:36 +02:00
}
2020-06-09 12:57:19 +02:00
2020-06-09 10:08:36 +02:00
if ( ! empty ( $tags )) {
2020-07-09 14:44:21 +02:00
$tagSaveResults = $this -> GalaxyClusterRelationTag -> attachTags ( $user , $this -> id , $tags , $capture = $captureTag );
2020-06-09 10:08:36 +02:00
if ( ! $tagSaveResults ) {
2020-07-09 14:44:21 +02:00
$errors [] = __ ( 'Tags could not be saved for relation (%s)' , $this -> id );
2020-06-09 10:08:36 +02:00
}
2020-05-29 10:02:17 +02:00
}
2020-06-29 09:10:28 +02:00
} else {
2020-07-07 08:16:46 +02:00
foreach ( $this -> validationErrors as $validationError ) {
2020-06-29 09:10:28 +02:00
$errors [] = $validationError [ 0 ];
}
2020-05-07 16:17:44 +02:00
}
2020-05-07 12:30:05 +02:00
}
2020-05-07 16:17:44 +02:00
return $errors ;
2020-05-07 12:30:05 +02:00
}
2020-05-07 15:15:14 +02:00
2020-07-09 14:44:21 +02:00
/**
* editRelation Respecting ACL edits a relation and set correct fields where applicable .
* Contrary to its capture equivalent , trying to save a relation for a unknown target cluster will fail .
*
* @ param array $user
* @ param array $relation The relation to be saved
* @ param array $fieldList Only edit the fields provided
* @ param bool $captureTag Should the tag be captured if it doesn ' t exists
* @ return array List of errors if any
*/
public function editRelation ( array $user , array $relation , array $fieldList = array (), $captureTag = false )
2020-05-07 15:15:14 +02:00
{
$this -> SharingGroup = ClassRegistry :: init ( 'SharingGroup' );
$errors = array ();
2020-07-09 14:44:21 +02:00
if ( ! isset ( $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_id' ])) {
$errors [] = __ ( 'galaxy_cluster_id not provided' );
return $errors ;
2020-05-07 15:15:14 +02:00
}
2020-07-09 14:44:21 +02:00
$authorizationCheck = $this -> SourceCluster -> fetchIfAuthorized ( $user , $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_id' ], array ( 'edit' ), $throwErrors = false , $full = false );
if ( isset ( $authorizationCheck [ 'authorized' ]) && ! $authorizationCheck [ 'authorized' ]) {
$errors [] = $authorizationCheck [ 'error' ];
return $errors ;
}
2020-05-07 15:15:14 +02:00
if ( isset ( $relation [ 'GalaxyClusterRelation' ][ 'id' ])) {
$existingRelation = $this -> find ( 'first' , array ( 'conditions' => array ( 'GalaxyClusterRelation.id' => $relation [ 'GalaxyClusterRelation' ][ 'id' ])));
} else {
$errors [] = __ ( 'UUID not provided' );
}
if ( empty ( $existingRelation )) {
$errors [] = __ ( 'Unkown ID' );
} else {
2020-05-25 10:04:07 +02:00
$options = array ( 'conditions' => array (
2020-05-25 16:10:00 +02:00
'uuid' => $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ]
2020-05-25 10:04:07 +02:00
));
$cluster = $this -> SourceCluster -> fetchGalaxyClusters ( $user , $options );
if ( empty ( $cluster )) {
2020-05-25 16:10:00 +02:00
$errors [] = __ ( 'Invalid source galaxy cluster' );
2020-05-07 15:15:14 +02:00
}
2020-05-25 10:04:07 +02:00
$cluster = $cluster [ 0 ];
2020-05-07 15:15:14 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'id' ] = $existingRelation [ 'GalaxyClusterRelation' ][ 'id' ];
2020-05-25 16:10:00 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_id' ] = $cluster [ 'SourceCluster' ][ 'id' ];
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ] = $cluster [ 'SourceCluster' ][ 'uuid' ];
2020-05-25 10:04:07 +02:00
if ( isset ( $relation [ 'GalaxyClusterRelation' ][ 'distribution' ]) && $relation [ 'GalaxyClusterRelation' ][ 'distribution' ] == 4 && ! $this -> SharingGroup -> checkIfAuthorised ( $user , $relation [ 'GalaxyClusterRelation' ][ 'sharing_group_id' ])) {
$errors [] = array ( __ ( 'Galaxy Cluster Relation could not be saved: The user has to have access to the sharing group in order to be able to edit it.' ));
}
2020-05-25 16:10:00 +02:00
2020-05-07 15:15:14 +02:00
if ( empty ( $errors )) {
2020-07-09 14:44:21 +02:00
if ( ! isset ( $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ])) {
$errors [] = __ ( 'referenced_galaxy_cluster_uuid not provided' );
return $errors ;
}
$targetCluster = $this -> TargetCluster -> fetchIfAuthorized ( $user , $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ], 'view' , $throwErrors = false , $full = false );
2022-11-08 16:13:14 +01:00
if ( isset ( $targetCluster [ 'authorized' ]) && ! $targetCluster [ 'authorized' ]) { // do not save the relation if referenced cluster is not accessible by the user (or does not exist)
2020-05-25 16:10:00 +02:00
$errors [] = array ( __ ( 'Invalid referenced galaxy cluster' ));
2020-07-09 14:44:21 +02:00
return $errors ;
}
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_id' ] = $targetCluster [ 'TargetCluster' ][ 'id' ];
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ] = $targetCluster [ 'TargetCluster' ][ 'uuid' ];
$relation [ 'GalaxyClusterRelation' ][ 'default' ] = false ;
if ( empty ( $fieldList )) {
$fieldList = array ( 'galaxy_cluster_id' , 'galaxy_cluster_uuid' , 'referenced_galaxy_cluster_id' , 'referenced_galaxy_cluster_uuid' , 'referenced_galaxy_cluster_type' , 'distribution' , 'sharing_group_id' , 'default' );
}
2020-05-25 16:10:00 +02:00
2020-07-09 14:44:21 +02:00
$saveSuccess = $this -> save ( $relation , array ( 'fieldList' => $fieldList ));
if ( ! $saveSuccess ) {
foreach ( $this -> validationErrors as $validationError ) {
$errors [] = $validationError [ 0 ];
2020-05-07 15:15:14 +02:00
}
2020-07-09 14:44:21 +02:00
} else {
$this -> GalaxyClusterRelationTag -> deleteAll ( array ( 'GalaxyClusterRelationTag.galaxy_cluster_relation_id' => $relation [ 'GalaxyClusterRelation' ][ 'id' ]));
$this -> GalaxyClusterRelationTag -> attachTags ( $user , $relation [ 'GalaxyClusterRelation' ][ 'id' ], $relation [ 'GalaxyClusterRelation' ][ 'tags' ], $capture = $captureTag );
2020-05-07 15:15:14 +02:00
}
}
}
return $errors ;
}
2020-05-20 10:33:33 +02:00
2020-11-24 13:51:13 +01:00
public function bulkSaveRelations ( array $relations )
2020-11-23 19:38:19 +01:00
{
2021-10-30 23:05:11 +02:00
// Fetch existing tags Name => ID mapping
$tagNameToId = $this -> GalaxyClusterRelationTag -> Tag -> find ( 'list' , [
'fields' => [ 'Tag.name' , 'Tag.id' ],
'callbacks' => false ,
]);
// Fetch all cluster UUID => ID mapping
$galaxyClusterUuidToId = $this -> TargetCluster -> find ( 'list' , [
'fields' => [ 'uuid' , 'id' ],
'callbacks' => false ,
]);
2020-11-23 19:38:19 +01:00
$lookupSavedIds = [];
$relationTagsToSave = [];
2021-10-30 23:05:11 +02:00
foreach ( $relations as & $relation ) {
if ( isset ( $galaxyClusterUuidToId [ $relation [ 'referenced_galaxy_cluster_uuid' ]])) {
$relation [ 'referenced_galaxy_cluster_id' ] = $galaxyClusterUuidToId [ $relation [ 'referenced_galaxy_cluster_uuid' ]];
} else {
$relation [ 'referenced_galaxy_cluster_id' ] = 0 ; // referenced cluster doesn't exists
}
2020-11-23 19:38:19 +01:00
if ( ! empty ( $relation [ 'tags' ])) {
2021-10-30 19:31:52 +02:00
$lookupSavedIds [ $relation [ 'galaxy_cluster_id' ]] = true ;
2020-11-23 19:38:19 +01:00
foreach ( $relation [ 'tags' ] as $tag ) {
2021-10-30 23:05:11 +02:00
if ( ! isset ( $tagNameToId [ $tag ])) {
$tagNameToId [ $tag ] = $this -> GalaxyClusterRelationTag -> Tag -> quickAdd ( $tag );
2020-11-23 19:38:19 +01:00
}
2021-10-30 23:05:11 +02:00
$relationTagsToSave [ $relation [ 'galaxy_cluster_uuid' ]][ $relation [ 'referenced_galaxy_cluster_uuid' ]][] = $tagNameToId [ $tag ];
2020-11-23 19:38:19 +01:00
}
}
}
2021-10-30 23:05:11 +02:00
unset ( $galaxyClusterUuidToId , $tagNameToId );
$this -> saveMany ( $relations , [ 'validate' => false ]); // Some clusters uses invalid UUID :/
// Insert tags
2020-11-23 19:38:19 +01:00
$savedRelations = $this -> find ( 'all' , [
'recursive' => - 1 ,
'conditions' => [ 'galaxy_cluster_id' => array_keys ( $lookupSavedIds )],
'fields' => [ 'id' , 'galaxy_cluster_uuid' , 'referenced_galaxy_cluster_uuid' ]
]);
$relation_tags = [];
foreach ( $savedRelations as $savedRelation ) {
$uuid1 = $savedRelation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ];
$uuid2 = $savedRelation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ];
2021-10-30 19:31:52 +02:00
if ( isset ( $relationTagsToSave [ $uuid1 ][ $uuid2 ])) {
foreach ( $relationTagsToSave [ $uuid1 ][ $uuid2 ] as $tagId ) {
$relation_tags [] = [ $savedRelation [ 'GalaxyClusterRelation' ][ 'id' ], $tagId ];
2020-11-23 19:38:19 +01:00
}
}
}
if ( ! empty ( $relation_tags )) {
$db = $this -> getDataSource ();
$fields = array ( 'galaxy_cluster_relation_id' , 'tag_id' );
$db -> insertMulti ( 'galaxy_cluster_relation_tags' , $fields , $relation_tags );
}
}
2020-05-20 10:33:33 +02:00
/**
* Gets a relation then save it .
*
2020-07-09 14:44:21 +02:00
* @ param array $user
* @ param array $cluster The cluster for which the relation is being saved
* @ param array $relation The relation to be saved
* @ param bool $fromPull If the current capture is performed from a PULL sync . If set , it allows edition of existing relations
* @ return array The capture success results
2020-05-20 10:33:33 +02:00
*/
2020-07-09 14:44:21 +02:00
public function captureRelations ( array $user , array $cluster , array $relations , $fromPull = false )
2020-05-20 10:33:33 +02:00
{
$results = array ( 'success' => false , 'imported' => 0 , 'failed' => 0 );
$this -> Log = ClassRegistry :: init ( 'Log' );
2020-05-25 10:04:07 +02:00
$clusterUuid = $cluster [ 'GalaxyCluster' ][ 'uuid' ];
2020-05-20 10:33:33 +02:00
foreach ( $relations as $k => $relation ) {
if ( ! isset ( $relation [ 'GalaxyClusterRelation' ])) {
$relation = array ( 'GalaxyClusterRelation' => $relation );
}
2020-05-25 10:04:07 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ] = $clusterUuid ;
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_id' ] = $cluster [ 'GalaxyCluster' ][ 'id' ];
2020-11-23 19:38:19 +01:00
2020-05-25 10:04:07 +02:00
if ( empty ( $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ])) {
2020-06-08 11:10:59 +02:00
$this -> Log -> createLogEntry ( $user , 'captureRelations' , 'GalaxyClusterRelation' , 0 , __ ( 'No referenced cluster UUID provided' ), __ ( 'relation for cluster (%s)' , $clusterUuid ));
2020-05-20 10:33:33 +02:00
$results [ 'failed' ] ++ ;
continue ;
} else {
$options = array (
'conditions' => array (
2020-05-25 10:04:07 +02:00
'uuid' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ],
),
'fields' => array (
'id' , 'uuid' ,
2020-05-20 10:33:33 +02:00
)
);
2020-05-25 10:04:07 +02:00
$referencedCluster = $this -> SourceCluster -> fetchGalaxyClusters ( $user , $options );
2020-05-20 10:33:33 +02:00
if ( empty ( $referencedCluster )) {
2020-07-09 14:44:21 +02:00
if ( ! $fromPull ) {
$this -> Log -> createLogEntry ( $user , 'captureRelations' , 'GalaxyClusterRelation' , 0 , __ ( 'Referenced cluster not found' ), __ ( 'relation to (%s) for cluster (%s)' , $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ], $clusterUuid ));
}
2020-06-10 16:45:05 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_id' ] = 0 ;
2020-05-20 10:33:33 +02:00
} else {
$referencedCluster = $referencedCluster [ 0 ];
2020-05-25 10:04:07 +02:00
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_id' ] = $referencedCluster [ 'SourceCluster' ][ 'id' ];
2020-05-20 10:33:33 +02:00
}
}
2020-05-29 10:02:17 +02:00
$existingRelation = $this -> find ( 'first' , array ( 'conditions' => array (
'GalaxyClusterRelation.galaxy_cluster_uuid' => $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ],
'GalaxyClusterRelation.referenced_galaxy_cluster_uuid' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ],
'GalaxyClusterRelation.referenced_galaxy_cluster_type' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_type' ],
)));
if ( ! empty ( $existingRelation )) {
if ( ! $fromPull ) {
2020-06-08 11:10:59 +02:00
$this -> Log -> createLogEntry ( $user , 'captureRelations' , 'GalaxyClusterRelation' , 0 , __ ( 'Relation already exists' ), __ ( 'relation to (%s) for cluster (%s)' , $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ], $clusterUuid ));
2020-05-29 10:02:17 +02:00
$results [ 'failed' ] ++ ;
continue ;
} else {
$relation [ 'GalaxyClusterRelation' ][ 'id' ] = $existingRelation [ 'GalaxyClusterRelation' ][ 'id' ];
}
} else {
2020-07-10 10:57:40 +02:00
unset ( $relation [ 'GalaxyClusterRelation' ][ 'id' ]);
2020-05-29 10:02:17 +02:00
$this -> create ();
}
2020-05-25 10:04:07 +02:00
$this -> Event = ClassRegistry :: init ( 'Event' );
if ( isset ( $relation [ 'GalaxyClusterRelation' ][ 'distribution' ]) && $relation [ 'GalaxyClusterRelation' ][ 'distribution' ] == 4 ) {
2021-03-17 16:31:36 +01:00
$relation [ 'GalaxyClusterRelation' ] = $this -> Event -> captureSGForElement ( $relation [ 'GalaxyClusterRelation' ], $user );
2020-05-20 10:33:33 +02:00
}
$saveSuccess = $this -> save ( $relation );
if ( $saveSuccess ) {
$results [ 'imported' ] ++ ;
2020-06-08 12:14:56 +02:00
$modelKey = false ;
if ( ! empty ( $relation [ 'GalaxyClusterRelation' ][ 'GalaxyClusterRelationTag' ])) {
$modelKey = 'GalaxyClusterRelationTag' ;
} elseif ( ! empty ( $relation [ 'GalaxyClusterRelation' ][ 'Tag' ])) {
$modelKey = 'Tag' ;
}
if ( $modelKey !== false ) {
$tagNames = Hash :: extract ( $relation [ 'GalaxyClusterRelation' ][ $modelKey ], '{n}.name' );
2020-07-09 14:44:21 +02:00
// Similar behavior as for AttributeTags: Here we only attach tags. If they were removed at some point it's not taken into account.
// Since we don't have tag soft-deletion, tags added by users will be kept.
2020-05-29 08:56:29 +02:00
$this -> GalaxyClusterRelationTag -> attachTags ( $user , $this -> id , $tagNames , $capture = true );
2020-05-25 10:04:07 +02:00
}
2020-05-20 10:33:33 +02:00
} else {
$results [ 'failed' ] ++ ;
}
}
$results [ 'success' ] = $results [ 'imported' ] > 0 ;
return $results ;
}
2020-09-23 17:34:13 +02:00
public function removeNonAccessibleTargetCluster ( $user , $relations )
{
$availableTargetClusterIDs = $this -> TargetCluster -> cacheGalaxyClusterIDs ( $user );
$availableTargetClusterIDsKeyed = array_flip ( $availableTargetClusterIDs );
foreach ( $relations as $i => $relation ) {
if (
isset ( $relation [ 'TargetCluster' ][ 'id' ]) &&
! isset ( $availableTargetClusterIDsKeyed [ $relation [ 'TargetCluster' ][ 'id' ]])
) {
$relations [ $i ][ 'TargetCluster' ] = null ;
}
}
return $relations ;
}
2020-11-23 19:38:19 +01:00
2020-07-09 14:44:21 +02:00
/**
* syncUUIDsAndIDs Adapt IDs of source and target cluster inside the relation based on the provided two UUIDs
*
* @ param array $user
* @ param array $relation
* @ return array The adpated relation
*/
private function syncUUIDsAndIDs ( array $user , array $relation )
2020-05-25 17:02:26 +02:00
{
$options = array ( 'conditions' => array (
'uuid' => $relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ]
));
$sourceCluster = $this -> SourceCluster -> fetchGalaxyClusters ( $user , $options );
if ( ! empty ( $sourceCluster )) {
$sourceCluster = $sourceCluster [ 0 ];
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_id' ] = $sourceCluster [ 'SourceCluster' ][ 'id' ];
$relation [ 'GalaxyClusterRelation' ][ 'galaxy_cluster_uuid' ] = $sourceCluster [ 'SourceCluster' ][ 'uuid' ];
}
$options = array ( 'conditions' => array (
'uuid' => $relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ]
));
$targetCluster = $this -> TargetCluster -> fetchGalaxyClusters ( $user , $options );
if ( ! empty ( $targetCluster )) {
$targetCluster = $targetCluster [ 0 ];
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_id' ] = $targetCluster [ 'TargetCluster' ][ 'id' ];
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_uuid' ] = $targetCluster [ 'TargetCluster' ][ 'uuid' ];
2020-06-09 12:18:58 +02:00
} else {
$relation [ 'GalaxyClusterRelation' ][ 'referenced_galaxy_cluster_id' ] = 0 ;
2020-05-25 17:02:26 +02:00
}
return $relation ;
}
2020-05-06 07:59:57 +02:00
}