forked from community/device-mgt-core
parent
8813619929
commit
6399afe64c
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AllowedDeviceStatus {
|
||||
private String type;
|
||||
|
||||
private List<String> status;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public List<String> getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(List<String> status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.common.metadata.mgt;
|
||||
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
|
||||
|
||||
public interface DeviceStatusManagementService {
|
||||
|
||||
/**
|
||||
* This method is useful to create & persist default device status filter for provided tenant if
|
||||
* it doesn't exist already
|
||||
*
|
||||
* @throws MetadataManagementException if error while adding default device status
|
||||
*/
|
||||
void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to reset existing device status to default values in xml
|
||||
*
|
||||
* @throws MetadataManagementException if error while resetting default device status
|
||||
*/
|
||||
void resetToDefaultDeviceStausFilter() throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to update existing allowed device status
|
||||
*
|
||||
* @throws MetadataManagementException if error while updating existing device status
|
||||
*/
|
||||
void updateDefaultDeviceStatusFilter(int tenantId)
|
||||
throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to get existing device status filters
|
||||
*
|
||||
* @throws MetadataManagementException if error while getting existing device status
|
||||
*/
|
||||
AllowedDeviceStatus getDeviceStatusFilters(int tenantId) throws MetadataManagementException;
|
||||
|
||||
/**
|
||||
* This method is useful to get existing device status filters
|
||||
*
|
||||
* @throws MetadataManagementException if error while getting existing device status check
|
||||
*/
|
||||
boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException;
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.core.config.ui;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import java.util.List;
|
||||
|
||||
public class DeviceStatusConfigurations {
|
||||
|
||||
private List<DeviceStatusItem> deviceStatusItems;
|
||||
private boolean deviceStatusCheck;
|
||||
|
||||
@XmlElement(name = "DeviceStatusItem")
|
||||
public List<DeviceStatusItem> getDeviceStatusItems() {
|
||||
return deviceStatusItems;
|
||||
}
|
||||
|
||||
@XmlElement(name = "EnableDeviceStatusCheck")
|
||||
public boolean isDeviceStatusCheck() {
|
||||
return deviceStatusCheck;
|
||||
}
|
||||
|
||||
public void setDeviceStatusCheck(boolean deviceStatusCheck) {
|
||||
this.deviceStatusCheck = deviceStatusCheck;
|
||||
}
|
||||
|
||||
public void setDeviceStatusItems(List<DeviceStatusItem> deviceStatusItems) {
|
||||
this.deviceStatusItems = deviceStatusItems;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.core.config.ui;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import java.util.List;
|
||||
|
||||
public class DeviceStatusItem {
|
||||
|
||||
private List<String> status;
|
||||
private String type;
|
||||
|
||||
|
||||
@XmlElement(name = "Type")
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name = "AllowedStatus")
|
||||
@XmlElement(name = "Status")
|
||||
public List<String> getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setStatus(List<String> status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.core.metadata.mgt;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.AllowedDeviceStatus;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
|
||||
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusConfigurations;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.config.ui.DeviceStatusItem;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.config.ui.UIConfigurationManager;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataDAO;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOException;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
|
||||
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.dao.util.MetadataConstants;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class);
|
||||
|
||||
private final MetadataDAO metadataDAO;
|
||||
|
||||
public DeviceStatusManagementServiceImpl() {
|
||||
this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException {
|
||||
try {
|
||||
MetadataManagementDAOFactory.beginTransaction();
|
||||
if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY) && !metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) {
|
||||
Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus());
|
||||
Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck());
|
||||
// Add default device status and device status check metadata entries
|
||||
addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
|
||||
addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
|
||||
}
|
||||
MetadataManagementDAOFactory.commitTransaction();
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
MetadataManagementDAOFactory.rollbackTransaction();
|
||||
String msg = "Error occurred while inserting default device status metadata entry.";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetToDefaultDeviceStausFilter() throws MetadataManagementException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateDefaultDeviceStatusFilter(int tenantId) throws MetadataManagementException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public AllowedDeviceStatus getDeviceStatusFilters(int tenantId) throws MetadataManagementException {
|
||||
try {
|
||||
MetadataManagementDAOFactory.openConnection();
|
||||
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
|
||||
return new Gson().fromJson(metadata.getMetaValue(), AllowedDeviceStatus.class);
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDeviceStatusCheck(int tenantId) throws MetadataManagementException {
|
||||
try {
|
||||
MetadataManagementDAOFactory.openConnection();
|
||||
Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
|
||||
String metaValue = metadata.getMetaValue();
|
||||
|
||||
Gson gson = new Gson();
|
||||
JsonObject jsonObject = gson.fromJson(metaValue, JsonObject.class);
|
||||
|
||||
if (jsonObject.has("deviceStatusCheck")) {
|
||||
String deviceStatusCheckValue = jsonObject.get("deviceStatusCheck").getAsString();
|
||||
return Boolean.parseBoolean(deviceStatusCheckValue);
|
||||
} else {
|
||||
// Handle the case where "deviceStatusCheck" key is not found
|
||||
return false;
|
||||
}
|
||||
} catch (MetadataManagementDAOException e) {
|
||||
String msg = "Error occurred while retrieving device status check meta data for tenant:" + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while opening a connection to the data source";
|
||||
log.error(msg, e);
|
||||
throw new MetadataManagementException(msg, e);
|
||||
} finally {
|
||||
MetadataManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException {
|
||||
metadataDAO.addMetadata(tenantId, metadata);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(key + " metadata entry has been inserted successfully");
|
||||
}
|
||||
}
|
||||
|
||||
private Metadata constructDeviceStatusMetadata(List<DeviceStatusItem> deviceStatusItems) {
|
||||
Gson gson = new Gson();
|
||||
String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems);
|
||||
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setMetaKey(MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY);
|
||||
metadata.setMetaValue(deviceStatusItemsJsonString);
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private Metadata constructDeviceStatusCheckMetadata(boolean deviceStatusCheck) {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("deviceStatusCheck", String.valueOf(deviceStatusCheck));
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setMetaKey(MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY);
|
||||
metadata.setMetaValue(String.valueOf(deviceStatusCheck));
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private List<DeviceStatusItem> getDefaultDeviceStatus() {
|
||||
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
|
||||
List<DeviceStatusItem> deviceStatusItems = new ArrayList<>();
|
||||
|
||||
if (deviceStatusConfigurations != null) {
|
||||
// Access the list of DeviceStatusItem objects
|
||||
deviceStatusItems = deviceStatusConfigurations.getDeviceStatusItems();
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("DeviceStatusConfigurations is null.");
|
||||
}
|
||||
}
|
||||
|
||||
return deviceStatusItems;
|
||||
}
|
||||
|
||||
private boolean getDefaultDeviceStatusCheck() {
|
||||
DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations();
|
||||
boolean deviceStatusCheck = false;
|
||||
|
||||
if (deviceStatusConfigurations != null) {
|
||||
// Access the deviceStatusCheck
|
||||
deviceStatusCheck = deviceStatusConfigurations.isDeviceStatusCheck();
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("DeviceStatusConfigurations is null.");
|
||||
}
|
||||
}
|
||||
return deviceStatusCheck;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue