]> git.basschouten.com Git - openhab-addons.git/blob
caa6296f3b3007075598340253378063d7a0dd64
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lametrictime.internal.api.common.impl.typeadapters;
14
15 import java.util.Map.Entry;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lametrictime.internal.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
20 import org.openhab.binding.lametrictime.internal.api.local.dto.Application;
21
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24
25 /**
26  * Adapter factory for applications.
27  *
28  * @author Gregory Moyer - Initial contribution
29  */
30 @NonNullByDefault
31 public class ApplicationTypeAdapterFactory extends CustomizedTypeAdapterFactory<Application> {
32     private static final String PROPERTY_ID = "id";
33     private static final String PROPERTY_WIDGETS = "widgets";
34     private static final String PROPERTY_ACTIONS = "actions";
35
36     public ApplicationTypeAdapterFactory() {
37         super(Application.class);
38     }
39
40     @Override
41     protected void beforeWrite(Application source, JsonElement toSerialize) {
42         if (toSerialize == null || toSerialize.isJsonNull()) {
43             return;
44         }
45
46         JsonObject appObj = toSerialize.getAsJsonObject();
47         if (appObj == null || appObj.isJsonNull()) {
48             return;
49         }
50
51         // remove widget IDs
52         JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
53         if (widgetsElem != null && !widgetsElem.isJsonNull()) {
54             for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
55                 JsonElement widgetElem = entry.getValue();
56                 if (widgetElem == null || widgetElem.isJsonNull()) {
57                     continue;
58                 }
59                 widgetElem.getAsJsonObject().remove(PROPERTY_ID);
60             }
61         }
62
63         // remove action IDs
64         JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
65         if (actionsElem != null && !actionsElem.isJsonNull()) {
66             for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
67                 JsonElement actionElem = entry.getValue();
68                 if (actionElem == null || actionElem.isJsonNull()) {
69                     continue;
70                 }
71                 actionElem.getAsJsonObject().remove(PROPERTY_ID);
72             }
73         }
74     }
75
76     @Override
77     protected void afterRead(@Nullable JsonElement deserialized) {
78         if (deserialized == null || deserialized.isJsonNull()) {
79             return;
80         }
81
82         JsonObject appObj = deserialized.getAsJsonObject();
83         if (appObj == null || appObj.isJsonNull()) {
84             return;
85         }
86
87         // inject widget IDs
88         JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
89         if (widgetsElem != null && !widgetsElem.isJsonNull()) {
90             for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet()) {
91                 JsonElement widgetElem = entry.getValue();
92                 if (widgetElem == null || widgetElem.isJsonNull()) {
93                     continue;
94                 }
95                 widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
96             }
97         }
98
99         // inject action IDs
100         JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
101         if (actionsElem != null && !actionsElem.isJsonNull()) {
102             for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet()) {
103                 JsonElement actionElem = entry.getValue();
104                 if (actionElem == null || actionElem.isJsonNull()) {
105                     continue;
106                 }
107                 actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
108             }
109         }
110     }
111 }