25 changed files with 584 additions and 66 deletions
@ -0,0 +1,186 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.service.entitiy.user; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.common.data.HasTitle; |
||||
|
import org.thingsboard.server.common.data.StringUtils; |
||||
|
import org.thingsboard.server.common.data.id.DashboardId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.id.UserId; |
||||
|
import org.thingsboard.server.common.data.settings.AbstractUserDashboardInfo; |
||||
|
import org.thingsboard.server.common.data.settings.LastVisitedDashboardInfo; |
||||
|
import org.thingsboard.server.common.data.settings.StarredDashboardInfo; |
||||
|
import org.thingsboard.server.common.data.settings.UserDashboardAction; |
||||
|
import org.thingsboard.server.common.data.settings.UserDashboardsInfo; |
||||
|
import org.thingsboard.server.common.data.settings.UserSettings; |
||||
|
import org.thingsboard.server.dao.dashboard.DashboardService; |
||||
|
import org.thingsboard.server.dao.user.UserSettingsService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.UUID; |
||||
|
import java.util.function.Function; |
||||
|
import java.util.function.Predicate; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Service |
||||
|
@TbCoreComponent |
||||
|
@AllArgsConstructor |
||||
|
@Slf4j |
||||
|
public class DefaultTbUserSettingsService implements TbUserSettingsService { |
||||
|
|
||||
|
private static final int MAX_DASHBOARD_INFO_LIST_SIZE = 10; |
||||
|
private static final Predicate<HasTitle> EMPTY_TITLE = i -> StringUtils.isNotEmpty(i.getTitle()); |
||||
|
|
||||
|
private final UserSettingsService settingsService; |
||||
|
private final DashboardService dashboardService; |
||||
|
|
||||
|
@Override |
||||
|
public UserSettings saveUserSettings(TenantId tenantId, UserSettings userSettings) { |
||||
|
return settingsService.saveUserSettings(tenantId, userSettings); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void updateUserSettings(TenantId tenantId, UserId userId, JsonNode settings) { |
||||
|
updateUserSettings(tenantId, userId, UserSettings.GENERAL, settings); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void updateUserSettings(TenantId tenantId, UserId userId, String type, JsonNode settings) { |
||||
|
settingsService.updateUserSettings(tenantId, userId, type, settings); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserSettings findUserSettings(TenantId tenantId, UserId userId) { |
||||
|
return findUserSettings(tenantId, userId, UserSettings.GENERAL); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserSettings findUserSettings(TenantId tenantId, UserId userId, String type) { |
||||
|
return settingsService.findUserSettings(tenantId, userId, type); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteUserSettings(TenantId tenantId, UserId userId, List<String> jsonPaths) { |
||||
|
deleteUserSettings(tenantId, userId, UserSettings.GENERAL, jsonPaths); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteUserSettings(TenantId tenantId, UserId userId, String type, List<String> jsonPaths) { |
||||
|
settingsService.deleteUserSettings(tenantId, userId, type, jsonPaths); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserDashboardsInfo findUserDashboardsInfo(TenantId tenantId, UserId id) { |
||||
|
UserSettings us = findUserSettings(tenantId, id, UserSettings.STARRED_DASHBOARDS); |
||||
|
if (us == null) { |
||||
|
return UserDashboardsInfo.EMPTY; |
||||
|
} |
||||
|
UserDashboardsInfo stored = JacksonUtil.convertValue(us.getSettings(), UserDashboardsInfo.class); |
||||
|
if (stored == null) { |
||||
|
return UserDashboardsInfo.EMPTY; |
||||
|
} |
||||
|
|
||||
|
if (!stored.getLast().isEmpty()) { |
||||
|
stored.getLast().forEach(i -> setTitleIfEmpty(tenantId, i)); |
||||
|
stored.getLast().removeIf(EMPTY_TITLE); |
||||
|
} |
||||
|
if (!stored.getStarred().isEmpty()) { |
||||
|
Map<UUID, LastVisitedDashboardInfo> lastMap = stored.getLast().stream().collect(Collectors.toMap(LastVisitedDashboardInfo::getId, Function.identity())); |
||||
|
stored.getStarred().forEach(i -> { |
||||
|
var last = lastMap.get(i.getId()); |
||||
|
i.setTitle(last != null ? last.getTitle() : null); |
||||
|
}); |
||||
|
stored.getStarred().forEach(i -> setTitleIfEmpty(tenantId, i)); |
||||
|
stored.getStarred().removeIf(EMPTY_TITLE); |
||||
|
} |
||||
|
return stored; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UserDashboardsInfo reportUserDashboardAction(TenantId tenantId, UserId id, DashboardId dashboardId, UserDashboardAction action) { |
||||
|
UserSettings us = findUserSettings(tenantId, id, UserSettings.STARRED_DASHBOARDS); |
||||
|
UserDashboardsInfo stored = null; |
||||
|
if (us != null) { |
||||
|
stored = JacksonUtil.convertValue(us.getSettings(), UserDashboardsInfo.class); |
||||
|
} |
||||
|
if (stored == null) { |
||||
|
stored = new UserDashboardsInfo(); |
||||
|
} |
||||
|
|
||||
|
switch (action) { |
||||
|
case STAR: |
||||
|
addToStarred(stored, dashboardId); |
||||
|
break; |
||||
|
case UNSTAR: |
||||
|
removeFromStarred(stored, dashboardId); |
||||
|
break; |
||||
|
case VISIT: |
||||
|
addToVisited(stored, dashboardId); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
us = new UserSettings(); |
||||
|
us.setUserId(id); |
||||
|
us.setType(UserSettings.STARRED_DASHBOARDS); |
||||
|
us.setSettings(JacksonUtil.valueToTree(stored)); |
||||
|
saveUserSettings(tenantId, us); |
||||
|
return stored; |
||||
|
} |
||||
|
|
||||
|
private void addToVisited(UserDashboardsInfo stored, DashboardId dashboardId) { |
||||
|
UUID id = dashboardId.getId(); |
||||
|
stored.getStarred().removeIf(d -> id.equals(d.getId())); |
||||
|
} |
||||
|
|
||||
|
private void removeFromStarred(UserDashboardsInfo stored, DashboardId dashboardId) { |
||||
|
UUID id = dashboardId.getId(); |
||||
|
stored.getStarred().removeIf(d -> id.equals(d.getId())); |
||||
|
stored.getLast().stream().filter(d -> id.equals(d.getId())).findFirst().ifPresent(d -> d.setStarred(false)); |
||||
|
} |
||||
|
|
||||
|
private void addToStarred(UserDashboardsInfo stored, DashboardId dashboardId) { |
||||
|
UUID id = dashboardId.getId(); |
||||
|
long ts = System.currentTimeMillis(); |
||||
|
var opt = stored.getStarred().stream().filter(d -> id.equals(d.getId())).findFirst(); |
||||
|
if (opt.isPresent()) { |
||||
|
opt.get().setStarredAt(ts); |
||||
|
} else { |
||||
|
var newInfo = new StarredDashboardInfo(); |
||||
|
newInfo.setId(id); |
||||
|
newInfo.setStarredAt(System.currentTimeMillis()); |
||||
|
stored.getStarred().add(newInfo); |
||||
|
} |
||||
|
stored.getLast().stream().filter(d -> id.equals(d.getId())).forEach(d -> d.setStarred(true)); |
||||
|
//TODO: self-heal if some of the dashboards were deleted.
|
||||
|
//TODO: limit by size.
|
||||
|
} |
||||
|
|
||||
|
private void setTitleIfEmpty(TenantId tenantId, AbstractUserDashboardInfo i) { |
||||
|
if (StringUtils.isEmpty(i.getTitle())) { |
||||
|
var dashboardInfo = dashboardService.findDashboardInfoById(tenantId, new DashboardId(i.getId())); |
||||
|
i.setTitle(dashboardInfo != null ? dashboardInfo.getTitle() : null); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.service.entitiy.user; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import org.thingsboard.server.common.data.id.DashboardId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.id.UserId; |
||||
|
import org.thingsboard.server.common.data.settings.UserDashboardAction; |
||||
|
import org.thingsboard.server.common.data.settings.UserDashboardsInfo; |
||||
|
import org.thingsboard.server.common.data.settings.UserSettings; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface TbUserSettingsService { |
||||
|
|
||||
|
void updateUserSettings(TenantId tenantId, UserId userId, JsonNode settings); |
||||
|
|
||||
|
void updateUserSettings(TenantId tenantId, UserId userId, String type, JsonNode settings); |
||||
|
|
||||
|
UserSettings saveUserSettings(TenantId tenantId, UserSettings userSettings); |
||||
|
|
||||
|
UserSettings findUserSettings(TenantId tenantId, UserId userId); |
||||
|
|
||||
|
UserSettings findUserSettings(TenantId tenantId, UserId userId, String type); |
||||
|
|
||||
|
void deleteUserSettings(TenantId tenantId, UserId userId, List<String> jsonPaths); |
||||
|
|
||||
|
void deleteUserSettings(TenantId tenantId, UserId userId, String type, List<String> jsonPaths); |
||||
|
|
||||
|
UserDashboardsInfo findUserDashboardsInfo(TenantId tenantId, UserId id); |
||||
|
|
||||
|
UserDashboardsInfo reportUserDashboardAction(TenantId tenantId, UserId id, DashboardId dashboardId, UserDashboardAction action); |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.HasTitle; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@ApiModel |
||||
|
@Data |
||||
|
public abstract class AbstractUserDashboardInfo implements HasTitle, Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = -6461562426034242608L; |
||||
|
|
||||
|
@ApiModelProperty(position = 1, value = "JSON object with Dashboard id.", accessMode = ApiModelProperty.AccessMode.READ_ONLY) |
||||
|
private UUID id; |
||||
|
@ApiModelProperty(position = 2, value = "Title of the dashboard.") |
||||
|
private String title; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ApiModel |
||||
|
@Data |
||||
|
public class LastVisitedDashboardInfo extends AbstractUserDashboardInfo implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = -6461562426034242608L; |
||||
|
|
||||
|
@ApiModelProperty(position = 3, value = "Starred flag") |
||||
|
private boolean starred; |
||||
|
@ApiModelProperty(position = 4, value = "Last visit timestamp") |
||||
|
private long lastVisited; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,36 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.HasTitle; |
||||
|
import org.thingsboard.server.common.data.id.DashboardId; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
|
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ApiModel |
||||
|
@Data |
||||
|
public class StarredDashboardInfo extends AbstractUserDashboardInfo implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = -7830828696329673361L; |
||||
|
@ApiModelProperty(position = 4, value = "Starred timestamp") |
||||
|
private long starredAt; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
public enum UserDashboardAction { |
||||
|
|
||||
|
VISIT, STAR, UNSTAR |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
import io.swagger.annotations.ApiModel; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@ApiModel |
||||
|
@Data |
||||
|
@AllArgsConstructor |
||||
|
public class UserDashboardsInfo implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = 2628320657987010348L; |
||||
|
public static final UserDashboardsInfo EMPTY = new UserDashboardsInfo(Collections.emptyList(), Collections.emptyList()); |
||||
|
|
||||
|
@ApiModelProperty(position = 1, value = "List of last visited dashboards.", accessMode = ApiModelProperty.AccessMode.READ_ONLY) |
||||
|
private List<LastVisitedDashboardInfo> last; |
||||
|
|
||||
|
@ApiModelProperty(position = 2, value = "List of starred dashboards.", accessMode = ApiModelProperty.AccessMode.READ_ONLY) |
||||
|
private List<StarredDashboardInfo> starred; |
||||
|
|
||||
|
public UserDashboardsInfo() { |
||||
|
this(new ArrayList<>(), new ArrayList<>()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2023 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.settings; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@NoArgsConstructor |
||||
|
@AllArgsConstructor |
||||
|
@Data |
||||
|
public class UserSettingsCompositeKey implements Serializable { |
||||
|
|
||||
|
private static final long serialVersionUID = -7883642552545291489L; |
||||
|
|
||||
|
private UUID userId; |
||||
|
private String type; |
||||
|
|
||||
|
public UserSettingsCompositeKey(UserSettings userSettings) { |
||||
|
this.userId = userSettings.getUserId().getId(); |
||||
|
this.type = userSettings.getType(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
return userId.toString() + "_" + type; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue