2 * Copyright (c) 2010-2020 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;
18 import org.apache.commons.lang.StringEscapeUtils;
19 import org.openhab.core.types.StateOption;
21 import com.google.gson.annotations.Expose;
24 * The {@link ContentItem} class manages a ContentItem
26 * @author Christian Niessner - Initial contribution
27 * @author Thomas Traunbauer - Initial contribution
29 public class ContentItem {
31 private String source;
32 private String sourceAccount;
33 private String location;
34 private boolean presetable;
35 private String itemName;
37 private String containerArt;
39 private final Map<String, String> additionalAttributes;
42 * Creates a new instance of this class
44 public ContentItem() {
52 additionalAttributes = new HashMap<>();
56 * Returns true if this ContentItem is defined as Preset
58 * @return true if this ContentItem is defined as Preset
60 public boolean isPreset() {
69 * Returns true if all necessary stats are set
71 * @return true if all necessary stats are set
73 public boolean isValid() {
74 if (getOperationMode() == OperationModeType.STANDBY) {
77 if (itemName == null || source == null || itemName.isEmpty() || source.isEmpty()) {
85 * Returns true if source, sourceAccount, location, itemName, and presetable are equal
87 * @return true if source, sourceAccount, location, itemName, and presetable are equal
90 public boolean equals(Object obj) {
91 if (obj instanceof ContentItem) {
92 ContentItem other = (ContentItem) obj;
93 if (!isEqual(other.source, this.source)) {
96 if (!isEqual(other.sourceAccount, this.sourceAccount)) {
99 if (other.presetable != this.presetable) {
102 if (!isEqual(other.location, this.location)) {
105 if (!isEqual(other.itemName, this.itemName)) {
110 return super.equals(obj);
114 * Returns the operation Mode, depending on the stats that are set
116 * @return the operation Mode, depending on the stats that are set
118 public OperationModeType getOperationMode() {
119 OperationModeType operationMode = OperationModeType.OTHER;
120 if (source == null || source.equals("")) {
121 return OperationModeType.OTHER;
123 if (source.contains("PRODUCT")) {
124 if (sourceAccount.contains("TV")) {
125 operationMode = OperationModeType.TV;
127 if (sourceAccount.contains("HDMI")) {
128 operationMode = OperationModeType.HDMI1;
130 return operationMode;
133 operationMode = OperationModeType.valueOf(source);
134 return operationMode;
135 } catch (IllegalArgumentException iae) {
136 return OperationModeType.OTHER;
140 public void setSource(String source) {
141 this.source = source;
144 public void setSourceAccount(String sourceAccount) {
145 this.sourceAccount = sourceAccount;
148 public void setLocation(String location) {
149 this.location = location;
152 public void setItemName(String itemName) {
153 this.itemName = itemName;
156 public void setAdditionalAttribute(String name, String value) {
157 this.additionalAttributes.put(name, value);
160 public void setPresetable(boolean presetable) {
161 this.presetable = presetable;
164 public void setPresetID(int presetID) {
165 this.presetID = presetID;
168 public void setContainerArt(String containerArt) {
169 this.containerArt = containerArt;
172 public String getSource() {
176 public String getSourceAccount() {
177 return sourceAccount;
180 public String getLocation() {
184 public String getItemName() {
188 public boolean isPresetable() {
192 public int getPresetID() {
196 public String getContainerArt() {
201 * Returns the XML Code that is needed to switch to this ContentItem
203 * @return the XML Code that is needed to switch to this ContentItem
205 public String generateXML() {
207 switch (getOperationMode()) {
209 xml = "<ContentItem source=\"BLUETOOTH\"></ContentItem>";
215 xml = "<ContentItem source=\"AUX\" sourceAccount=\"" + sourceAccount + "\"></ContentItem>";
218 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"TV\" isPresetable=\"false\" />";
221 xml = "<ContentItem source=\"PRODUCT\" sourceAccount=\"HDMI_1\" isPresetable=\"false\" />";
224 StringBuilder sbXml = new StringBuilder("<ContentItem");
225 if (source != null) {
226 sbXml.append(" source=\"").append(StringEscapeUtils.escapeXml(source)).append("\"");
228 if (location != null) {
229 sbXml.append(" location=\"").append(StringEscapeUtils.escapeXml(location)).append("\"");
231 if (sourceAccount != null) {
232 sbXml.append(" sourceAccount=\"").append(StringEscapeUtils.escapeXml(sourceAccount)).append("\"");
234 sbXml.append(" isPresetable=\"").append(presetable).append("\"");
235 for (Map.Entry<String, String> aae : additionalAttributes.entrySet()) {
236 sbXml.append(" ").append(aae.getKey()).append("=\"")
237 .append(StringEscapeUtils.escapeXml(aae.getValue())).append("\"");
240 if (itemName != null) {
241 sbXml.append("<itemName>").append(itemName).append("</itemName>");
243 if (containerArt != null) {
244 sbXml.append("<containerArt>").append(containerArt).append("</containerArt>");
246 sbXml.append("</ContentItem>");
247 xml = sbXml.toString();
253 public StateOption toStateOption() {
254 String stateOptionLabel = String.valueOf(presetID) + ": " + itemName;
255 return new StateOption(String.valueOf(presetID), stateOptionLabel);
259 public String toString() {
260 // if (presetID >= 1 && presetID <= 6) {
261 // StringBuilder buffer = new StringBuilder();
262 // buffer.append("PRESET_");
263 // buffer.append(presetID);
264 // return buffer.toString();
269 private boolean isEqual(String s1, String s2) {
273 if (s1 == null || s2 == null) {
276 return s1.equals(s2);