2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bosesoundtouch.internal;
15 import java.util.HashMap;
17 import java.util.Objects;
19 import org.apache.commons.lang3.StringEscapeUtils;
20 import org.openhab.core.types.StateOption;
22 import com.google.gson.annotations.Expose;
25 * The {@link ContentItem} class manages a ContentItem
27 * @author Christian Niessner - Initial contribution
28 * @author Thomas Traunbauer - Initial contribution
30 public class ContentItem {
32 private String source;
33 private String sourceAccount;
34 private String location;
35 private boolean presetable;
36 private String itemName;
38 private String containerArt;
40 private final Map<String, String> additionalAttributes;
43 * Creates a new instance of this class
45 public ContentItem() {
53 additionalAttributes = new HashMap<>();
57 * Returns true if this ContentItem is defined as Preset
59 * @return true if this ContentItem is defined as Preset
61 public boolean isPreset() {
70 * Returns true if all necessary stats are set
72 * @return true if all necessary stats are set
74 public boolean isValid() {
75 if (getOperationMode() == OperationModeType.STANDBY) {
78 if (itemName == null || source == null || itemName.isEmpty() || source.isEmpty()) {
86 * Returns true if source, sourceAccount, location, itemName, and presetable are equal
88 * @return true if source, sourceAccount, location, itemName, and presetable are equal
91 public boolean equals(Object obj) {
92 if (obj instanceof ContentItem) {
93 ContentItem other = (ContentItem) obj;
94 if (!Objects.equals(other.source, this.source)) {
97 if (!Objects.equals(other.sourceAccount, this.sourceAccount)) {
100 if (other.presetable != this.presetable) {
103 if (!Objects.equals(other.location, this.location)) {
106 if (!Objects.equals(other.itemName, this.itemName)) {
111 return super.equals(obj);
115 * Returns the operation Mode, depending on the stats that are set
117 * @return the operation Mode, depending on the stats that are set
119 public OperationModeType getOperationMode() {
120 OperationModeType operationMode = OperationModeType.OTHER;
121 if (source == null || source.equals("")) {
122 return OperationModeType.OTHER;
124 if (source.contains("PRODUCT")) {
125 if (sourceAccount.contains("TV")) {
126 operationMode = OperationModeType.TV;
128 if (sourceAccount.contains("HDMI")) {
129 operationMode = OperationModeType.HDMI1;
131 return operationMode;
134 operationMode = OperationModeType.valueOf(source);
135 return operationMode;
136 } catch (IllegalArgumentException iae) {
137 return OperationModeType.OTHER;
141 public void setSource(String source) {
142 this.source = source;
145 public void setSourceAccount(String sourceAccount) {
146 this.sourceAccount = sourceAccount;
149 public void setLocation(String location) {
150 this.location = location;
153 public void setItemName(String itemName) {
154 this.itemName = itemName;
157 public void setAdditionalAttribute(String name, String value) {
158 this.additionalAttributes.put(name, value);
161 public void setPresetable(boolean presetable) {
162 this.presetable = presetable;
165 public void setPresetID(int presetID) {
166 this.presetID = presetID;
169 public void setContainerArt(String containerArt) {
170 this.containerArt = containerArt;
173 public String getSource() {
177 public String getSourceAccount() {
178 return sourceAccount;
181 public String getLocation() {
185 public String getItemName() {
189 public boolean isPresetable() {
193 public int getPresetID() {
197 public String getContainerArt() {
202 * Returns the XML Code that is needed to switch to this ContentItem
204 * @return the XML Code that is needed to switch to this ContentItem
206 public String generateXML() {
208 switch (getOperationMode()) {
210 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
216 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
219 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
222 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
225 StringBuilder sbXml = new StringBuilder("<ContentItem");
226 if (source != null) {
227 sbXml.append(" source=\"").append(StringEscapeUtils.escapeXml(source)).append("\"");
229 if (location != null) {
230 sbXml.append(" location=\"").append(StringEscapeUtils.escapeXml(location)).append("\"");
232 if (sourceAccount != null) {
233 sbXml.append(" sourceAccount=\"").append(StringEscapeUtils.escapeXml(sourceAccount)).append("\"");
235 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
236 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
237 sbXml.append(" ").append(aae.getKey()).append("=\"")
238 .append(StringEscapeUtils.escapeXml(aae.getValue())).append("\"");
241 if (itemName != null) {
242 sbXml.append("<itemName>").append(itemName).append("</itemName>");
244 if (containerArt != null) {
245 sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
247 sbXml.append("</ContentItem>");
248 xml = sbXml.toString();
254 public StateOption toStateOption() {
255 String stateOptionLabel = String.valueOf(presetID) + ": " + itemName;
256 return new StateOption(String.valueOf(presetID), stateOptionLabel);
260 public String toString() {
261 // if (presetID >= 1 && presetID <= 6) {
262 // StringBuilder buffer = new StringBuilder();
263 // buffer.append("PRESET_");
264 // buffer.append(presetID);
265 // return buffer.toString();