Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / drydock / query / DrydockResourceQuery.php
blobbcbff0366344e62044f7377c940225c62c25b3f0
1 <?php
3 final class DrydockResourceQuery extends DrydockQuery {
5 private $ids;
6 private $phids;
7 private $statuses;
8 private $types;
9 private $blueprintPHIDs;
10 private $datasourceQuery;
11 private $needUnconsumedCommands;
13 public function withIDs(array $ids) {
14 $this->ids = $ids;
15 return $this;
18 public function withPHIDs(array $phids) {
19 $this->phids = $phids;
20 return $this;
23 public function withTypes(array $types) {
24 $this->types = $types;
25 return $this;
28 public function withStatuses(array $statuses) {
29 $this->statuses = $statuses;
30 return $this;
33 public function withBlueprintPHIDs(array $blueprint_phids) {
34 $this->blueprintPHIDs = $blueprint_phids;
35 return $this;
38 public function withDatasourceQuery($query) {
39 $this->datasourceQuery = $query;
40 return $this;
43 public function needUnconsumedCommands($need) {
44 $this->needUnconsumedCommands = $need;
45 return $this;
48 public function newResultObject() {
49 return new DrydockResource();
52 protected function loadPage() {
53 return $this->loadStandardPage($this->newResultObject());
56 protected function willFilterPage(array $resources) {
57 $blueprint_phids = mpull($resources, 'getBlueprintPHID');
59 $blueprints = id(new DrydockBlueprintQuery())
60 ->setViewer($this->getViewer())
61 ->withPHIDs($blueprint_phids)
62 ->execute();
63 $blueprints = mpull($blueprints, null, 'getPHID');
65 foreach ($resources as $key => $resource) {
66 $blueprint = idx($blueprints, $resource->getBlueprintPHID());
67 if (!$blueprint) {
68 $this->didRejectResult($resource);
69 unset($resources[$key]);
70 continue;
72 $resource->attachBlueprint($blueprint);
75 return $resources;
78 protected function didFilterPage(array $resources) {
79 if ($this->needUnconsumedCommands) {
80 $commands = id(new DrydockCommandQuery())
81 ->setViewer($this->getViewer())
82 ->setParentQuery($this)
83 ->withTargetPHIDs(mpull($resources, 'getPHID'))
84 ->withConsumed(false)
85 ->execute();
86 $commands = mgroup($commands, 'getTargetPHID');
88 foreach ($resources as $resource) {
89 $list = idx($commands, $resource->getPHID(), array());
90 $resource->attachUnconsumedCommands($list);
94 return $resources;
97 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
98 $where = parent::buildWhereClauseParts($conn);
100 if ($this->ids !== null) {
101 $where[] = qsprintf(
102 $conn,
103 'resource.id IN (%Ld)',
104 $this->ids);
107 if ($this->phids !== null) {
108 $where[] = qsprintf(
109 $conn,
110 'resource.phid IN (%Ls)',
111 $this->phids);
114 if ($this->types !== null) {
115 $where[] = qsprintf(
116 $conn,
117 'resource.type IN (%Ls)',
118 $this->types);
121 if ($this->statuses !== null) {
122 $where[] = qsprintf(
123 $conn,
124 'resource.status IN (%Ls)',
125 $this->statuses);
128 if ($this->blueprintPHIDs !== null) {
129 $where[] = qsprintf(
130 $conn,
131 'resource.blueprintPHID IN (%Ls)',
132 $this->blueprintPHIDs);
135 if ($this->datasourceQuery !== null) {
136 $where[] = qsprintf(
137 $conn,
138 'resource.name LIKE %>',
139 $this->datasourceQuery);
142 return $where;
145 protected function getPrimaryTableAlias() {
146 return 'resource';