/* * Copyright 2000-2016 Vaadin Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.vaadin.data; import com.vaadin.server.SerializableConsumer; import com.vaadin.server.SerializableFunction; import java.util.Objects; import java.util.Optional; /** * An internal implementation of {@code Result}. * * @param * the result value type */ class SimpleResult implements Result { private final R value; private final String message; /** * Creates a new {@link Result} instance using {@code value} for a non error * {@link Result} and {@code message} for an error {@link Result}. *

* If {@code message} is null then {@code value} is ignored and result is an * error. * * @param value * the value of the result, may be {@code null} * @param message * the error message of the result, may be {@code null} */ SimpleResult(R value, String message) { // value != null => message == null assert value == null || message == null : "Message must be null if value is provided"; this.value = value; this.message = message; } @Override @SuppressWarnings("unchecked") public Result flatMap(SerializableFunction> mapper) { Objects.requireNonNull(mapper, "mapper cannot be null"); if (isError()) { // Safe cast; valueless return (Result) this; } else { return mapper.apply(value); } } @Override public void handle(SerializableConsumer ifOk, SerializableConsumer ifError) { Objects.requireNonNull(ifOk, "ifOk cannot be null"); Objects.requireNonNull(ifError, "ifError cannot be null"); if (isError()) { ifError.accept(message); } else { ifOk.accept(value); } } @Override public Optional getMessage() { return Optional.ofNullable(message); } @Override public boolean isError() { return message != null; } @Override public String toString() { if (isError()) { return "error(" + message + ")"; } else { return "ok(" + value + ")"; } } @Override public R getOrThrow( SerializableFunction exceptionSupplier) throws X { Objects.requireNonNull(exceptionSupplier, "Exception supplier cannot be null"); if (isError()) { throw exceptionSupplier.apply(message); } else { return value; } } } option> Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats

blob: 915a0434cc8f42472a8f514285d12829bb9d5a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php

declare(strict_types=1);

/**
 * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Contacts\ContactsMenu;

use OCP\Contacts\ContactsMenu\IAction;
use OCP\Contacts\ContactsMenu\IEntry;

class Entry implements IEntry {

	/** @var string|int|null */
	private $id = null;

	/** @var string */
	private $fullName = '';

	/** @var string[] */
	private $emailAddresses = [];

	/** @var string|null */
	private $avatar;

	/** @var string|null */
	private $profileTitle;

	/** @var string|null */
	private $profileUrl;

	/** @var IAction[] */
	private $actions = [];

	/** @var array */
	private $properties = [];

	/**
	 * @param string $id
	 */
	public function setId(string $id): void {
		$this->id = $id;
	}

	/**
	 * @param string $displayName
	 */
	public function setFullName(string $displayName): void {
		$this->fullName = $displayName;
	}

	/**
	 * @return string
	 */
	public function getFullName(): string {
		return $this->fullName;
	}

	/**
	 * @param string $address
	 */
	public function addEMailAddress(string $address): void {
		$this->emailAddresses[] = $address;
	}

	/**
	 * @return string[]
	 */
	public function getEMailAddresses(): array {
		return $this->emailAddresses;
	}

	/**
	 * @param string $avatar
	 */
	public function setAvatar(string $avatar): void {
		$this->avatar = $avatar;
	}

	/**
	 * @return string
	 */
	public function getAvatar(): ?string {
		return $this->avatar;
	}

	/**
	 * @param string $profileTitle
	 */
	public function setProfileTitle(string $profileTitle): void {
		$this->profileTitle = $profileTitle;
	}

	/**
	 * @return string
	 */
	public function getProfileTitle(): ?string {
		return $this->profileTitle;
	}

	/**
	 * @param string $profileUrl
	 */
	public function setProfileUrl(string $profileUrl): void {
		$this->profileUrl = $profileUrl;
	}

	/**
	 * @return string
	 */
	public function getProfileUrl(): ?string {
		return $this->profileUrl;
	}

	/**
	 * @param IAction $action
	 */
	public function addAction(IAction $action): void {
		$this->actions[] = $action;
		$this->sortActions();
	}

	/**
	 * @return IAction[]
	 */
	public function getActions(): array {
		return $this->actions;
	}

	/**
	 * sort the actions by priority and name
	 */
	private function sortActions(): void {
		usort($this->actions, function (IAction $action1, IAction $action2) {
			$prio1 = $action1->getPriority();
			$prio2 = $action2->getPriority();

			if ($prio1 === $prio2) {
				// Ascending order for same priority
				return strcasecmp($action1->getName(), $action2->getName());
			}

			// Descending order when priority differs
			return $prio2 - $prio1;
		});
	}

	/**
	 * @param array $contact key-value array containing additional properties
	 */
	public function setProperties(array $contact): void {
		$this->properties = $contact;
	}

	/**
	 * @param string $key
	 * @return mixed
	 */
	public function getProperty(string $key) {
		if (!isset($this->properties[$key])) {
			return null;
		}
		return $this->properties[$key];
	}

	/**
	 * @return array
	 */
	public function jsonSerialize(): array {
		$topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null;
		$otherActions = array_map(function (IAction $action) {
			return $action->jsonSerialize();
		}, array_slice($this->actions, 1));

		return [
			'id' => $this->id,
			'fullName' => $this->fullName,
			'avatar' => $this->getAvatar(),
			'topAction' => $topAction,
			'actions' => $otherActions,
			'lastMessage' => '',
			'emailAddresses' => $this->getEMailAddresses(),
			'profileTitle' => $this->profileTitle,
			'profileUrl' => $this->profileUrl,
		];
	}
}