/
GwtKapuaCommonsModelConverter.java
128 lines (116 loc) · 5.29 KB
/
GwtKapuaCommonsModelConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
* Copyright (c) 2017, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.api.shared.util;
import com.extjs.gxt.ui.client.data.BaseModel;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigComponent;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtConfigParameter;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtEntityModel;
import org.eclipse.kapua.app.console.module.api.shared.model.GwtUpdatableEntityModel;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.KapuaUpdatableEntity;
import org.eclipse.kapua.model.id.KapuaId;
import java.util.HashMap;
import java.util.Map;
/**
* Utility class for convertKapuaId {@link BaseModel}s to {@link KapuaEntity}ies and other Kapua models
*/
public class GwtKapuaCommonsModelConverter {
private GwtKapuaCommonsModelConverter() {
}
/**
* Utility method to convertKapuaId commons properties of {@link GwtUpdatableEntityModel} object to the matching {@link KapuaUpdatableEntity} object
*
* @param gwtEntity
* The {@link GwtUpdatableEntityModel} from which copy values
* @param kapuaEntity
* The {@link KapuaUpdatableEntity} into which to copy values
* @since 1.0.0
*/
public static void convertUpdatableEntity(GwtUpdatableEntityModel gwtEntity, KapuaUpdatableEntity kapuaEntity) {
if (kapuaEntity == null || gwtEntity == null) {
return;
}
convertEntity(gwtEntity, kapuaEntity);
kapuaEntity.setOptlock(gwtEntity.getOptlock());
}
/**
* Utility method to convertKapuaId commons properties of {@link GwtEntityModel} object to the matching {@link KapuaEntity} object
*
* @param gwtEntity
* The {@link GwtEntityModel} from which copy values
* @param kapuaEntity
* The {@link KapuaEntity} into which to copy values
* @since 1.0.0
*/
public static void convertEntity(GwtEntityModel gwtEntity, KapuaEntity kapuaEntity) {
if (kapuaEntity == null || gwtEntity == null) {
return;
}
kapuaEntity.setId(convertKapuaId(gwtEntity.getId()));
}
/**
* Converts a {@link KapuaId} form the short form to the actual object.
* <p>
* Example: AQ => 1
* </p>
*
* @param shortKapuaId
* the {@link KapuaId} in the short form
* @return The converted {@link KapuaId}
* @since 1.0.0
*/
public static KapuaId convertKapuaId(String shortKapuaId) {
if (shortKapuaId == null) {
return null;
}
return KapuaEid.parseCompactId(shortKapuaId);
}
public static Map<String, Object> convertConfigComponent(GwtConfigComponent configComponent) {
Map<String, Object> parameters = new HashMap<String, Object>();
for (GwtConfigParameter gwtConfigParameter : configComponent.getParameters()) {
switch (gwtConfigParameter.getType()) {
case BOOLEAN:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Boolean.parseBoolean(gwtConfigParameter.getValue()) : null);
break;
case BYTE:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Byte.parseByte(gwtConfigParameter.getValue()) : null);
break;
case CHAR:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? gwtConfigParameter.getValue().toCharArray() : null);
break;
case DOUBLE:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Double.parseDouble(gwtConfigParameter.getValue()) : null);
break;
case FLOAT:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Float.parseFloat(gwtConfigParameter.getValue()) : null);
break;
case INTEGER:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Integer.parseInt(gwtConfigParameter.getValue()) : null);
break;
case LONG:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Long.parseLong(gwtConfigParameter.getValue()) : null);
break;
case PASSWORD:
case STRING:
default:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue());
break;
case SHORT:
parameters.put(gwtConfigParameter.getId(), gwtConfigParameter.getValue() != null ? Short.parseShort(gwtConfigParameter.getValue()) : null);
break;
}
}
return parameters;
}
}