2012-12-17 23:47:21 +01:00
|
|
|
<?php
|
|
|
|
|
2013-12-27 22:15:30 +01:00
|
|
|
final class DrydockResourceQuery extends DrydockQuery {
|
2012-12-17 23:47:21 +01:00
|
|
|
|
|
|
|
private $ids;
|
2013-12-26 21:29:58 +01:00
|
|
|
private $phids;
|
2013-11-27 22:32:51 +01:00
|
|
|
private $statuses;
|
|
|
|
private $types;
|
2013-12-03 01:09:07 +01:00
|
|
|
private $blueprintPHIDs;
|
2015-08-24 13:13:20 +02:00
|
|
|
private $datasourceQuery;
|
2015-09-28 18:35:26 +02:00
|
|
|
private $needUnconsumedCommands;
|
2012-12-17 23:47:21 +01:00
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-26 21:29:58 +01:00
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-11-27 22:32:51 +01:00
|
|
|
public function withTypes(array $types) {
|
|
|
|
$this->types = $types;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withStatuses(array $statuses) {
|
|
|
|
$this->statuses = $statuses;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:09:07 +01:00
|
|
|
public function withBlueprintPHIDs(array $blueprint_phids) {
|
|
|
|
$this->blueprintPHIDs = $blueprint_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:13:20 +02:00
|
|
|
public function withDatasourceQuery($query) {
|
|
|
|
$this->datasourceQuery = $query;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:35:26 +02:00
|
|
|
public function needUnconsumedCommands($need) {
|
|
|
|
$this->needUnconsumedCommands = $need;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-09-21 13:42:04 +02:00
|
|
|
public function newResultObject() {
|
|
|
|
return new DrydockResource();
|
|
|
|
}
|
2012-12-17 23:47:21 +01:00
|
|
|
|
2015-09-21 13:42:04 +02:00
|
|
|
protected function loadPage() {
|
|
|
|
return $this->loadStandardPage($this->newResultObject());
|
|
|
|
}
|
2012-12-17 23:47:21 +01:00
|
|
|
|
2015-09-21 13:42:04 +02:00
|
|
|
protected function willFilterPage(array $resources) {
|
|
|
|
$blueprint_phids = mpull($resources, 'getBlueprintPHID');
|
|
|
|
|
|
|
|
$blueprints = id(new DrydockBlueprintQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withPHIDs($blueprint_phids)
|
|
|
|
->execute();
|
|
|
|
$blueprints = mpull($blueprints, null, 'getPHID');
|
|
|
|
|
|
|
|
foreach ($resources as $key => $resource) {
|
|
|
|
$blueprint = idx($blueprints, $resource->getBlueprintPHID());
|
|
|
|
if (!$blueprint) {
|
|
|
|
$this->didRejectResult($resource);
|
|
|
|
unset($resources[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$resource->attachBlueprint($blueprint);
|
|
|
|
}
|
2012-12-17 23:47:21 +01:00
|
|
|
|
|
|
|
return $resources;
|
|
|
|
}
|
|
|
|
|
2015-09-28 18:35:26 +02:00
|
|
|
protected function didFilterPage(array $resources) {
|
|
|
|
if ($this->needUnconsumedCommands) {
|
|
|
|
$commands = id(new DrydockCommandQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->setParentQuery($this)
|
|
|
|
->withTargetPHIDs(mpull($resources, 'getPHID'))
|
|
|
|
->withConsumed(false)
|
|
|
|
->execute();
|
|
|
|
$commands = mgroup($commands, 'getTargetPHID');
|
|
|
|
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
$list = idx($commands, $resource->getPHID(), array());
|
|
|
|
$resource->attachUnconsumedCommands($list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $resources;
|
|
|
|
}
|
|
|
|
|
2015-09-21 13:42:04 +02:00
|
|
|
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
|
|
|
|
$where = parent::buildWhereClauseParts($conn);
|
2012-12-17 23:47:21 +01:00
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->ids !== null) {
|
2012-12-17 23:47:21 +01:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2012-12-17 23:47:21 +01:00
|
|
|
'id IN (%Ld)',
|
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->phids !== null) {
|
2013-12-26 21:29:58 +01:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2013-12-26 21:29:58 +01:00
|
|
|
'phid IN (%Ls)',
|
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->types !== null) {
|
2013-11-27 22:32:51 +01:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2013-11-27 22:32:51 +01:00
|
|
|
'type IN (%Ls)',
|
|
|
|
$this->types);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->statuses !== null) {
|
2013-11-27 22:32:51 +01:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2013-11-27 22:32:51 +01:00
|
|
|
'status IN (%Ls)',
|
|
|
|
$this->statuses);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->blueprintPHIDs !== null) {
|
2013-12-03 01:09:07 +01:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2013-12-03 01:09:07 +01:00
|
|
|
'blueprintPHID IN (%Ls)',
|
|
|
|
$this->blueprintPHIDs);
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:23:04 +02:00
|
|
|
if ($this->datasourceQuery !== null) {
|
2015-08-24 13:13:20 +02:00
|
|
|
$where[] = qsprintf(
|
2015-09-21 13:42:04 +02:00
|
|
|
$conn,
|
2015-08-24 13:13:20 +02:00
|
|
|
'name LIKE %>',
|
|
|
|
$this->datasourceQuery);
|
|
|
|
}
|
|
|
|
|
2015-09-21 13:42:04 +02:00
|
|
|
return $where;
|
2012-12-17 23:47:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|