/**
 * Copyright © 2016-2022 The Thingsboard Authors
 *
 * 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 org.thingsboard.server.common.data;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.ApiModelProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.server.common.data.id.DashboardId;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

@EqualsAndHashCode(callSuper = true)
public class Dashboard extends DashboardInfo implements ExportableEntity<DashboardId> {

    private static final long serialVersionUID = 872682138346187503L;

    private transient JsonNode configuration;

    @Getter
    @Setter
    private DashboardId externalId;

    public Dashboard() {
        super();
    }

    public Dashboard(DashboardId id) {
        super(id);
    }

    public Dashboard(DashboardInfo dashboardInfo) {
        super(dashboardInfo);
    }

    public Dashboard(Dashboard dashboard) {
        super(dashboard);
        this.configuration = dashboard.getConfiguration();
        this.externalId = dashboard.getExternalId();
    }

    @ApiModelProperty(position = 9, value = "JSON object with main configuration of the dashboard: layouts, widgets, aliases, etc. " +
            "The JSON structure of the dashboard configuration is quite complex. " +
            "The easiest way to learn it is to export existing dashboard to JSON."
            , dataType = "com.fasterxml.jackson.databind.JsonNode")
    public JsonNode getConfiguration() {
        return configuration;
    }

    public void setConfiguration(JsonNode configuration) {
        this.configuration = configuration;
    }

    @JsonIgnore
    public List<ObjectNode> getEntityAliasesConfig() {
        return getChildObjects("entityAliases");
    }

    @JsonIgnore
    public List<ObjectNode> getWidgetsConfig() {
        return getChildObjects("widgets");
    }

    @JsonIgnore
    private List<ObjectNode> getChildObjects(String propertyName) {
        return Optional.ofNullable(configuration)
                .map(config -> config.get(propertyName))
                .filter(node -> !node.isEmpty())
                .map(node -> (ObjectNode) node)
                .map(object -> {
                    List<ObjectNode> widgets = new ArrayList<>(object.size());
                    object.forEach(child -> {
                        if (child.isObject()) {
                            widgets.add((ObjectNode) child);
                        }
                    });
                    return widgets;
                })
                .orElse(Collections.emptyList());
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Dashboard [tenantId=");
        builder.append(getTenantId());
        builder.append(", title=");
        builder.append(getTitle());
        builder.append(", configuration=");
        builder.append(configuration);
        builder.append("]");
        return builder.toString();
    }
}

@@ -796,21 +796,22 @@
 nd.node.
-Array
+Object
 Node;%0Aim
@@ -823,50 +823,37 @@
 com.
-fasterxml.jackson.databind.node.ObjectNode
+google.common.collect.Streams
 ;%0Aim
@@ -1038,36 +1038,8 @@
 d;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -1142,21 +1142,18 @@
 eam.
-StreamSupport
+Collectors
 ;%0A%0A@
@@ -2891,169 +2891,109 @@
 ty()
-)%0A                .map(node -%3E (ObjectNode) node)%0A                .map(object -%3E %7B%0A                    List%3CObjectNode%3E widgets = new ArrayList%3C%3E(object.size
+ && (node.isObject() %7C%7C node.isArray()))%0A                .map(node -%3E Streams.stream(node.elements
 ())
-;
 %0A   
@@ -3013,76 +3013,38 @@
     
-object.forEach(child -%3E %7B%0A                        if (child.
+    .filter(JsonNode::
 isObject
 ()) 
@@ -3043,13 +3043,9 @@
 ject
-()) %7B
+)
 %0A   
@@ -3069,24 +3069,25 @@
     
-    widgets.add(
+.map(jsonNode -%3E 
 (Obj
@@ -3099,18 +3099,18 @@
 de) 
-child);%0A  
+jsonNode)%0A
     
@@ -3131,87 +3131,39 @@
     
-%7D%0A                    %7D);%0A                    return widgets;%0A                %7D
+  .collect(Collectors.toList())
 )%0A  

Fix export/import of deprecated dashboards