3 final class AlmanacServiceQuery
14 private $needBindings;
15 private $needActiveBindings;
17 public function withIDs(array $ids) {
22 public function withPHIDs(array $phids) {
23 $this->phids
= $phids;
27 public function withNames(array $names) {
28 $this->names
= $names;
32 public function withServiceTypes(array $types) {
33 $this->serviceTypes
= $types;
37 public function withDevicePHIDs(array $phids) {
38 $this->devicePHIDs
= $phids;
42 public function withNamePrefix($prefix) {
43 $this->namePrefix
= $prefix;
47 public function withNameSuffix($suffix) {
48 $this->nameSuffix
= $suffix;
52 public function withNameNgrams($ngrams) {
53 return $this->withNgramsConstraint(
54 new AlmanacServiceNameNgrams(),
58 public function needBindings($need_bindings) {
59 $this->needBindings
= $need_bindings;
63 public function needActiveBindings($need_active) {
64 $this->needActiveBindings
= $need_active;
68 public function newResultObject() {
69 return new AlmanacService();
72 protected function buildJoinClauseParts(AphrontDatabaseConnection
$conn) {
73 $joins = parent
::buildJoinClauseParts($conn);
75 if ($this->shouldJoinBindingTable()) {
78 'JOIN %T binding ON service.phid = binding.servicePHID',
79 id(new AlmanacBinding())->getTableName());
85 protected function buildWhereClauseParts(AphrontDatabaseConnection
$conn) {
86 $where = parent
::buildWhereClauseParts($conn);
88 if ($this->ids
!== null) {
91 'service.id IN (%Ld)',
95 if ($this->phids
!== null) {
98 'service.phid IN (%Ls)',
102 if ($this->names
!== null) {
104 foreach ($this->names
as $name) {
105 $hashes[] = PhabricatorHash
::digestForIndex($name);
110 'service.nameIndex IN (%Ls)',
114 if ($this->serviceTypes
!== null) {
117 'service.serviceType IN (%Ls)',
118 $this->serviceTypes
);
121 if ($this->devicePHIDs
!== null) {
124 'binding.devicePHID IN (%Ls)',
128 if ($this->namePrefix
!== null) {
131 'service.name LIKE %>',
135 if ($this->nameSuffix
!== null) {
138 'service.name LIKE %<',
145 protected function willFilterPage(array $services) {
146 $service_map = AlmanacServiceType
::getAllServiceTypes();
148 foreach ($services as $key => $service) {
149 $implementation = idx($service_map, $service->getServiceType());
151 if (!$implementation) {
152 $this->didRejectResult($service);
153 unset($services[$key]);
157 $implementation = clone $implementation;
158 $service->attachServiceImplementation($implementation);
164 protected function didFilterPage(array $services) {
165 $need_all = $this->needBindings
;
166 $need_active = $this->needActiveBindings
;
168 $need_any = ($need_all ||
$need_active);
169 $only_active = ($need_active && !$need_all);
172 $service_phids = mpull($services, 'getPHID');
174 $bindings_query = id(new AlmanacBindingQuery())
175 ->setViewer($this->getViewer())
176 ->withServicePHIDs($service_phids)
177 ->needProperties($this->getNeedProperties());
180 $bindings_query->withIsActive(true);
183 $bindings = $bindings_query->execute();
184 $bindings = mgroup($bindings, 'getServicePHID');
186 foreach ($services as $service) {
187 $service_bindings = idx($bindings, $service->getPHID(), array());
190 $service->attachActiveBindings($service_bindings);
192 $service->attachBindings($service_bindings);
197 return parent
::didFilterPage($services);
200 private function shouldJoinBindingTable() {
201 return ($this->devicePHIDs
!== null);
204 protected function shouldGroupQueryResultRows() {
205 if ($this->shouldJoinBindingTable()) {
209 return parent
::shouldGroupQueryResultRows();
212 protected function getPrimaryTableAlias() {
216 public function getOrderableColumns() {
217 return parent
::getOrderableColumns() +
array(
219 'table' => $this->getPrimaryTableAlias(),
228 protected function newPagingMapFromPartialObject($object) {
230 'id' => (int)$object->getID(),
231 'name' => $object->getName(),
235 public function getBuiltinOrders() {
238 'vector' => array('name'),
239 'name' => pht('Service Name'),
241 ) + parent
::getBuiltinOrders();