Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Device Birth Extended Properties #3273

Merged
merged 5 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public static <E extends KapuaEntity> E delete(@NotNull EntityManager em, @NotNu
/**
* Find by fields {@link KapuaEntity} utility method
*
* @param em The {@link EntityManager} that holds the transaction.
* @param clazz The {@link KapuaEntity} class. This must be the implementing {@code class}.
* @param name The {@link KapuaEntity} name of the field from which to search.
* @param value The value of the field from which to search.
* @param em The {@link EntityManager} that holds the transaction.
* @param clazz The {@link KapuaEntity} class. This must be the implementing {@code class}.
* @param name The {@link KapuaEntity} name of the field from which to search.
* @param value The value of the field from which to search.
* @return The {@link KapuaEntity} found, or {@code null} if not found.
* @throws NonUniqueResultException When more than one result is returned
* @since 1.0.0
Expand Down Expand Up @@ -392,9 +392,11 @@ public static <I extends KapuaEntity, E extends I, L extends KapuaListResult<I>>
criteriaSelectQuery.select(entityRoot).distinct(true);

// Fetch LAZY attributes if necessary
if (kapuaQuery.getFetchAttributes() != null) {
for (String fetchAttribute : kapuaQuery.getFetchAttributes()) {
for (String fetchAttribute : kapuaQuery.getFetchAttributes()) {
if (entityType.getAttribute(fetchAttribute).isAssociation()) {
entityRoot.fetch(entityType.getSingularAttribute(fetchAttribute), JoinType.LEFT);
} else {
entityRoot.fetch(fetchAttribute);
}
}

Expand Down Expand Up @@ -809,8 +811,7 @@ protected static void handleKapuaQueryGroupPredicate(@NotNull KapuaQuery query,
if (kapuaSession != null && !kapuaSession.isTrustedMode()) {
handleKapuaQueryGroupPredicate(kapuaSession, query, domain, groupPredicateName);
}
}
else {
} else {
LOG.warn("'Access Group Permission' feature is disabled");
}
}
Expand All @@ -822,9 +823,9 @@ private static void handleKapuaQueryGroupPredicate(KapuaSession kapuaSession, Ka
AccessInfo accessInfo = KapuaSecurityUtils.doPrivileged(() -> ACCESS_INFO_SERVICE.findByUserId(kapuaSession.getScopeId(), userId));

List<Permission> groupPermissions = new ArrayList<>();
if (accessInfo!=null) {
if (accessInfo != null) {

AccessPermissionListResult accessPermissions = KapuaSecurityUtils.doPrivileged(() -> ACCESS_PERMISSION_SERVICE.findByAccessInfoId(accessInfo.getScopeId(), accessInfo.getId()));
AccessPermissionListResult accessPermissions = KapuaSecurityUtils.doPrivileged(() -> ACCESS_PERMISSION_SERVICE.findByAccessInfoId(accessInfo.getScopeId(), accessInfo.getId()));

for (AccessPermission ap : accessPermissions.getItems()) {
if (checkGroupPermission(domain, groupPermissions, ap.getPermission())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,38 @@ protected void onRender(Element parent, int index) {
RpcProxy<ListLoadResult<GwtGroupedNVPair>> proxy = getDataProxy();
descriptionValuesLoader = new BaseListLoader<ListLoadResult<GwtGroupedNVPair>>(proxy);
descriptionValuesLoader.addLoadListener(new DescriptionLoadListener());

descriptionValuesStore = new GroupingStore<GwtGroupedNVPair>(descriptionValuesLoader);
descriptionValuesStore.groupBy("groupLoc");

//
// Columns
List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
ColumnConfig name = new ColumnConfig("nameLoc", MSGS.entityTabDescriptionName(), 50);
ColumnConfig value = new ColumnConfig("value", MSGS.devicePropValue(), 50);
// Name column
GridCellRenderer<GwtGroupedNVPair> renderer = new GridCellRenderer<GwtGroupedNVPair>() {

// Name
ColumnConfig nameColumn = new ColumnConfig();
nameColumn.setId("nameLoc");
nameColumn.setHeader(MSGS.entityTabDescriptionName());
nameColumn.setWidth(50);
columns.add(nameColumn);

// Value
GridCellRenderer<GwtGroupedNVPair> valueColumnRenderer = new GridCellRenderer<GwtGroupedNVPair>() {

@Override
public Object render(GwtGroupedNVPair model, String property, ColumnData config,
int rowIndex, int colIndex, ListStore<GwtGroupedNVPair> store,
Grid<GwtGroupedNVPair> grid) {
return renderValueCell(model, property, config, rowIndex, colIndex, store, grid);
int rowIndex, int colIndex, ListStore<GwtGroupedNVPair> store,
Grid<GwtGroupedNVPair> grid) {
return renderValueCell(model, property, config, rowIndex, colIndex, store, grid);
}
};

value.setRenderer(renderer);
columns.add(name);
columns.add(value);

ColumnModel cm = new ColumnModel(columns);
ColumnConfig valueColumn = new ColumnConfig();
valueColumn.setId("value");
valueColumn.setHeader(MSGS.devicePropValue());
valueColumn.setWidth(50);
valueColumn.setRenderer(valueColumnRenderer);
columns.add(valueColumn);

//
// Grid
Expand All @@ -109,6 +117,8 @@ public Object render(GwtGroupedNVPair model, String property, ColumnData config,
groupingView.setShowGroupedColumn(false);
groupingView.setEnableNoGroups(false);
groupingView.setEnableGroupingMenu(false);

ColumnModel cm = new ColumnModel(columns);
descriptionGrid = new KapuaGrid<GwtGroupedNVPair>(descriptionValuesStore, cm);
descriptionGrid.setView(groupingView);
descriptionGrid.setBorders(false);
Expand Down Expand Up @@ -147,9 +157,14 @@ protected Object renderNameCell(GwtGroupedNVPair model, String property, ColumnD

protected Object renderValueCell(GwtGroupedNVPair model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<GwtGroupedNVPair> store, Grid<GwtGroupedNVPair> grid) {
Object value = model.getValue();
if (value != null && value instanceof Date) {

if (value instanceof Date) {
Date dateValue = (Date) value;
return DateUtils.formatDateTime(dateValue);
} else if (value instanceof String) {
return ((String) value)
.replace("\n", "<br>")
.replace(" ", "&nbsp;");
}
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,39 @@
*******************************************************************************/
package org.eclipse.kapua.app.console.module.api.shared.model;

import java.io.Serializable;


import com.google.gwt.core.client.GWT;
import org.eclipse.kapua.app.console.module.api.client.messages.ValidationMessages;

import java.io.Serializable;

public class GwtGroupedNVPair extends KapuaBaseModel implements Serializable {

private static final long serialVersionUID = 6017065568183482351L;

@Override
@SuppressWarnings("unchecked")
public <X> X get(String property) {
ValidationMessages msgs = GWT.create(ValidationMessages.class);
if ("groupLoc".equals(property)) {
ValidationMessages msgs = GWT.create(ValidationMessages.class);
return (X) msgs.getString(getGroup());
try {
return (X) msgs.getString(getGroup());
} catch (Exception e) {
return (X) getGroup();
}
} else if ("nameLoc".equals(property)) {
ValidationMessages msgs = GWT.create(ValidationMessages.class);
return (X) msgs.getString(getName());
try {
return (X) msgs.getString(getName());
} catch (Exception e) {
String[] nameSplitted = getName().split("_");

StringBuilder sb = new StringBuilder();
for (String split : nameSplitted) {
sb.append(split.substring(0, 1).toUpperCase())
.append(split.substring(1))
.append(" ");
}

return (X) sb.deleteCharAt(sb.length()).toString();
}
} else {
X value = (X) super.get(property);
if (value == null || (value instanceof String && ((String) value).isEmpty())) {
Expand All @@ -45,6 +59,7 @@ public GwtGroupedNVPair() {

public GwtGroupedNVPair(String group, String name, Object value) {
this();

setGroup(group);
setName(name);
setValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.eclipse.kapua.service.device.registry.DeviceAttributes;
import org.eclipse.kapua.service.device.registry.DeviceCreator;
import org.eclipse.kapua.service.device.registry.DeviceDomains;
import org.eclipse.kapua.service.device.registry.DeviceExtendedProperty;
import org.eclipse.kapua.service.device.registry.DeviceFactory;
import org.eclipse.kapua.service.device.registry.DeviceQuery;
import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
Expand Down Expand Up @@ -293,6 +294,18 @@ public User call() throws Exception {
pairs.add(new GwtGroupedNVPair(MODEM_INFO, "modemImei", device.getImei()));
pairs.add(new GwtGroupedNVPair(MODEM_INFO, "modemImsi", device.getImsi()));
pairs.add(new GwtGroupedNVPair(MODEM_INFO, "modemIccid", device.getIccid()));

//
// Extended Properties
for (DeviceExtendedProperty deviceExtendedProperty : device.getExtendedProperties()) {
pairs.add(
new GwtGroupedNVPair(
deviceExtendedProperty.getGroupName(),
deviceExtendedProperty.getName(),
deviceExtendedProperty.getValue()
)
);
}
}
} catch (Throwable t) {
KapuaExceptionHandler.handle(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,19 @@ public interface KapuaBirthPayload extends KapuaLifecyclePayload {
*/
void setModemIccid(String modemIccid);

/**
* Gets the extended properties.
*
* @return The extended properties.
* @since 1.5.0
*/
String getExtendedProperties();

/**
* Sets the extended properties.
*
* @param extendedProperties The extended properties.
* @since 1.5.0
*/
void setExtendedProperties(String extendedProperties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,9 @@ private KapuaBirthPayloadAttibutes() {
* @since 1.0.0
*/
public static final String MODEM_ICCID = "modemIccid";

/**
* @since 1.5.0
*/
public static final String EXTENDED_PROPERTIES = "extendedProperties";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
public class KapuaAppsPayloadImpl extends KapuaBirthPayloadImpl implements KapuaAppsPayload {

private static final long serialVersionUID = -918081814625264739L;

/**
* Constructor.
*
Expand Down Expand Up @@ -63,7 +65,8 @@ public KapuaAppsPayloadImpl(String uptime,
String osArch,
String modemImei,
String modemImsi,
String modemIccid) {
String modemIccid,
String extendedProperties) {
super(uptime,
displayName,
modelName,
Expand Down Expand Up @@ -92,6 +95,7 @@ public KapuaAppsPayloadImpl(String uptime,
osArch,
modemImei,
modemImsi,
modemIccid);
modemIccid,
extendedProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
public class KapuaBirthPayloadImpl extends AbstractLifecyclePayloadImpl implements KapuaBirthPayload {

private static final long serialVersionUID = 304433271740125817L;

/**
* Constructor.
*
Expand Down Expand Up @@ -66,6 +68,7 @@ public KapuaBirthPayloadImpl() {
* @param modemImei The {@link KapuaBirthPayloadAttibutes#MODEM_IMEI} of the {@link KapuaBirthMessage}
* @param modemImsi The {@link KapuaBirthPayloadAttibutes#MODEM_IMSI} of the {@link KapuaBirthMessage}
* @param modemIccid The {@link KapuaBirthPayloadAttibutes#MODEM_ICCID} of the {@link KapuaBirthMessage}
* @param extendedProperties The {@link KapuaBirthPayloadAttibutes#EXTENDED_PROPERTIES} of the {@link KapuaBirthMessage}
* @since 1.0.0
*/
public KapuaBirthPayloadImpl(String uptime,
Expand Down Expand Up @@ -96,7 +99,8 @@ public KapuaBirthPayloadImpl(String uptime,
String osArch,
String modemImei,
String modemImsi,
String modemIccid) {
String modemIccid,
String extendedProperties) {

setUptime(uptime);
setDisplayName(displayName);
Expand Down Expand Up @@ -127,6 +131,7 @@ public KapuaBirthPayloadImpl(String uptime,
setModemImei(modemImei);
setModemImsi(modemImsi);
setModemIccid(modemIccid);
setExtendedProperties(extendedProperties);
}

@Override
Expand Down Expand Up @@ -419,4 +424,13 @@ public void setModemIccid(String modemIccid) {
getMetrics().put(KapuaBirthPayloadAttibutes.MODEM_ICCID, modemIccid);
}

@Override
public String getExtendedProperties() {
return (String) getMetrics().get(KapuaBirthPayloadAttibutes.EXTENDED_PROPERTIES);
}

@Override
public void setExtendedProperties(String extendedProperties) {
getMetrics().put(KapuaBirthPayloadAttibutes.EXTENDED_PROPERTIES, extendedProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 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.message.internal.device.lifecycle.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import java.util.HashMap;
import java.util.Map;

/**
* Birth extended property definition.
*
* @since 1.5.0
*/
@JsonRootName("deviceExtendedProperties")
public class BirthExtendedProperties {

@JsonProperty("version")
private String version;

@JsonProperty("properties")
@JsonDeserialize(keyAs = String.class, contentAs = BirthExtendedProperty.class)
private Map<String, BirthExtendedProperty> extendedProperties;

/**
* Gets the version.
*
* @return The version.
* @since 1.5.0
*/
public String getVersion() {
return version;
}

/**
* Sets the version.
*
* @param version The version.
* @since 1.5.0
*/
public void setVersion(String version) {
this.version = version;
}

/**
* Gets the {@link Map} of {@link BirthExtendedProperty}es.
*
* @return The {@link Map} of {@link BirthExtendedProperty}es.
* @since 1.5.0
*/
public Map<String, BirthExtendedProperty> getExtendedProperties() {
if (extendedProperties == null) {
extendedProperties = new HashMap<>();
}

return extendedProperties;
}

/**
* Sets the {@link Map} of {@link BirthExtendedProperty}es.
*
* @param extendedProperties The {@link Map} of {@link BirthExtendedProperty}es.
* @since 1.5.0
*/
public void setExtendedProperties(Map<String, BirthExtendedProperty> extendedProperties) {
this.extendedProperties = extendedProperties;
}
}