]> git.basschouten.com Git - openhab-addons.git/blob
fed1281598f1131a82dc8ef98909b225c8f6a519
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.lametrictime.api.common.impl.typeadapters;
17
18 import java.util.Map.Entry;
19
20 import org.openhab.binding.lametrictime.api.common.impl.typeadapters.imported.CustomizedTypeAdapterFactory;
21 import org.openhab.binding.lametrictime.api.local.model.Application;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25
26 public class ApplicationTypeAdapterFactory extends CustomizedTypeAdapterFactory<Application>
27 {
28     private static final String PROPERTY_ID = "id";
29     private static final String PROPERTY_WIDGETS = "widgets";
30     private static final String PROPERTY_ACTIONS = "actions";
31
32     public ApplicationTypeAdapterFactory()
33     {
34         super(Application.class);
35     }
36
37     @Override
38     protected void beforeWrite(Application source, JsonElement toSerialize)
39     {
40         if (toSerialize == null || toSerialize.isJsonNull())
41         {
42             return;
43         }
44
45         JsonObject appObj = toSerialize.getAsJsonObject();
46         if (appObj == null || appObj.isJsonNull())
47         {
48             return;
49         }
50
51         // remove widget IDs
52         JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
53         if (widgetsElem != null && !widgetsElem.isJsonNull())
54         {
55             for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet())
56             {
57                 JsonElement widgetElem = entry.getValue();
58                 if (widgetElem == null || widgetElem.isJsonNull())
59                 {
60                     continue;
61                 }
62                 widgetElem.getAsJsonObject().remove(PROPERTY_ID);
63             }
64         }
65
66         // remove action IDs
67         JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
68         if (actionsElem != null && !actionsElem.isJsonNull())
69         {
70             for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet())
71             {
72                 JsonElement actionElem = entry.getValue();
73                 if (actionElem == null || actionElem.isJsonNull())
74                 {
75                     continue;
76                 }
77                 actionElem.getAsJsonObject().remove(PROPERTY_ID);
78             }
79         }
80     }
81
82     @Override
83     protected void afterRead(JsonElement deserialized)
84     {
85         if (deserialized == null || deserialized.isJsonNull())
86         {
87             return;
88         }
89
90         JsonObject appObj = deserialized.getAsJsonObject();
91         if (appObj == null || appObj.isJsonNull())
92         {
93             return;
94         }
95
96         // inject widget IDs
97         JsonElement widgetsElem = appObj.get(PROPERTY_WIDGETS);
98         if (widgetsElem != null && !widgetsElem.isJsonNull())
99         {
100             for (Entry<String, JsonElement> entry : widgetsElem.getAsJsonObject().entrySet())
101             {
102                 JsonElement widgetElem = entry.getValue();
103                 if (widgetElem == null || widgetElem.isJsonNull())
104                 {
105                     continue;
106                 }
107                 widgetElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
108             }
109         }
110
111         // inject action IDs
112         JsonElement actionsElem = appObj.get(PROPERTY_ACTIONS);
113         if (actionsElem != null && !actionsElem.isJsonNull())
114         {
115             for (Entry<String, JsonElement> entry : actionsElem.getAsJsonObject().entrySet())
116             {
117                 JsonElement actionElem = entry.getValue();
118                 if (actionElem == null || actionElem.isJsonNull())
119                 {
120                     continue;
121                 }
122                 actionElem.getAsJsonObject().addProperty(PROPERTY_ID, entry.getKey());
123             }
124         }
125     }
126 }