forked from community/device-mgt-core
parent
0f915a2961
commit
bd8cf5237d
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
public class EventAction {
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "actionType",
|
||||
value = "Type of the event action to be triggered in the device level")
|
||||
private String actionType;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "payload",
|
||||
value = "Payload of the event action")
|
||||
private Object payload;
|
||||
|
||||
public String getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(String actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void setPayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.jaxrs.beans;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventConfig {
|
||||
@ApiModelProperty(
|
||||
name = "eventLogic",
|
||||
value = "Logic of the event should be handled at the device level")
|
||||
private String eventLogic;
|
||||
|
||||
@ApiModelProperty(
|
||||
name = "actions",
|
||||
value = "List of actions to be triggered according to the logic")
|
||||
private List<EventAction> actions;
|
||||
|
||||
public String getEventLogic() {
|
||||
return eventLogic;
|
||||
}
|
||||
|
||||
public void setEventLogic(String eventLogic) {
|
||||
this.eventLogic = eventLogic;
|
||||
}
|
||||
|
||||
public List<EventAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(List<EventAction> actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public class EventAction {
|
||||
private String actionType;
|
||||
private Object payload;
|
||||
|
||||
public String getActionType() {
|
||||
return actionType;
|
||||
}
|
||||
|
||||
public void setActionType(String actionType) {
|
||||
this.actionType = actionType;
|
||||
}
|
||||
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public void setPayload(Object payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventConfig {
|
||||
private int eventId;
|
||||
private String eventSource;
|
||||
private String eventLogic;
|
||||
private String actions;
|
||||
|
||||
public String getEventLogic() {
|
||||
return eventLogic;
|
||||
}
|
||||
|
||||
public void setEventLogic(String eventLogic) {
|
||||
this.eventLogic = eventLogic;
|
||||
}
|
||||
|
||||
public String getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(String actions) {
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
public int getEventId() {
|
||||
return eventId;
|
||||
}
|
||||
|
||||
public void setEventId(int eventId) {
|
||||
this.eventId = eventId;
|
||||
}
|
||||
|
||||
public String getEventSource() {
|
||||
return eventSource;
|
||||
}
|
||||
|
||||
public void setEventSource(String eventSource) {
|
||||
this.eventSource = eventSource;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
public class EventConfigurationException extends Exception {
|
||||
public EventConfigurationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventConfigurationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public EventConfigurationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public EventConfigurationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.common.event.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventConfigurationProviderService {
|
||||
boolean createEventOfDeviceGroup(List<EventConfig> eventConfigList, List<Integer> groupIds, int tenantId)
|
||||
throws EventConfigurationException;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EventConfigDAO {
|
||||
int[] storeEventRecords(List<EventConfig> eventConfigList, int tenantId) throws EventManagementDAOException;
|
||||
|
||||
boolean addEventGroupMappingRecords(int[] generatedEventIds, List<Integer> groupIds) throws EventManagementDAOException;
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.dao;
|
||||
|
||||
public class EventManagementDAOException extends Exception{
|
||||
public EventManagementDAOException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventManagementDAOException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public EventManagementDAOException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public EventManagementDAOException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class EventConfigDAOImpl implements EventConfigDAO {
|
||||
private static final Log log = LogFactory.getLog(EventConfigDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public int[] storeEventRecords(List<EventConfig> eventConfigList, int tenantId) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_EVENT(" +
|
||||
"EVENT_SOURCE, " +
|
||||
"EVENT_LOGIC, " +
|
||||
"ACTIONS, "+
|
||||
"CREATED_TIMESTAMP, " +
|
||||
"TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
|
||||
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
for (EventConfig eventConfig : eventConfigList) {
|
||||
stmt.setString(1, eventConfig.getEventSource());
|
||||
stmt.setString(2, eventConfig.getEventLogic());
|
||||
stmt.setString(3, eventConfig.getActions());
|
||||
stmt.setTimestamp(4, new Timestamp(new Date().getTime()));
|
||||
stmt.setInt(5, tenantId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
int[] createdRowCount = stmt.executeBatch();
|
||||
int[] generatedIds = new int[createdRowCount.length];
|
||||
ResultSet generatedKeys = stmt.getGeneratedKeys();
|
||||
for (int i = 0; i < createdRowCount.length; i++) {
|
||||
if (generatedKeys.next()) {
|
||||
generatedIds[i] = generatedKeys.getInt(1);
|
||||
}
|
||||
}
|
||||
return generatedIds;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event configurations for the tenant id " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addEventGroupMappingRecords(int[] generatedEventIds, List<Integer> groupIds) throws EventManagementDAOException {
|
||||
try {
|
||||
Connection conn = this.getConnection();
|
||||
String sql = "INSERT INTO DM_DEVICE_EVENT_GROUP_MAPPING(" +
|
||||
"EVENT_ID ," +
|
||||
"GROUP_ID) " +
|
||||
"VALUES (?, ?)";
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
for (Integer groupId : groupIds) {
|
||||
for (int i = 0; i < generatedEventIds.length; i++) {
|
||||
stmt.setInt(1, generatedEventIds[i]);
|
||||
stmt.setInt(2, groupId);
|
||||
stmt.addBatch();
|
||||
}
|
||||
}
|
||||
return stmt.executeBatch().length > 0;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while creating event group mapping records";
|
||||
log.error(msg, e);
|
||||
throw new EventManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return DeviceManagementDAOFactory.getConnection();
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.event.config;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventAction;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfig;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
|
||||
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationProviderService;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventConfigDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.EventManagementDAOException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventConfigurationProviderServiceImpl implements EventConfigurationProviderService {
|
||||
private static final Log log = LogFactory.getLog(EventConfigurationProviderServiceImpl.class);
|
||||
private final EventConfigDAO eventConfigDAO;
|
||||
|
||||
public EventConfigurationProviderServiceImpl() {
|
||||
eventConfigDAO = DeviceManagementDAOFactory.getEventConfigDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createEventOfDeviceGroup(List<EventConfig> eventConfigList, List<Integer> groupIds, int tenantId)
|
||||
throws EventConfigurationException {
|
||||
try {
|
||||
DeviceManagementDAOFactory.beginTransaction();
|
||||
int[] generatedEventIds = eventConfigDAO.storeEventRecords(eventConfigList, tenantId);
|
||||
boolean isRecordsCreated = eventConfigDAO.addEventGroupMappingRecords(generatedEventIds, groupIds);
|
||||
DeviceManagementDAOFactory.commitTransaction();
|
||||
return isRecordsCreated;
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Failed to start/open transaction to store device event configurations";
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} catch (EventManagementDAOException e) {
|
||||
String msg = "Error occurred while saving event records";
|
||||
log.error(msg, e);
|
||||
DeviceManagementDAOFactory.rollbackTransaction();
|
||||
throw new EventConfigurationException(msg, e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue