Refactor operations

revert-70aa11f8
manoj 10 years ago
parent 3fc11654e7
commit 932cb003da

@ -119,5 +119,17 @@ public class Operation implements Serializable {
this.payLoad = payLoad; this.payLoad = payLoad;
} }
@Override
public String toString() {
return "Operation{" +
"code='" + code + '\'' +
", type=" + type +
", id=" + id +
", status=" + status +
", receivedTimeStamp='" + receivedTimeStamp + '\'' +
", createdTimeStamp='" + createdTimeStamp + '\'' +
", isEnabled=" + isEnabled +
'}';
}
} }

@ -18,6 +18,8 @@
package org.wso2.carbon.device.mgt.common.operation.mgt; package org.wso2.carbon.device.mgt.common.operation.mgt;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import java.util.List; import java.util.List;
/** /**
@ -34,8 +36,8 @@ public interface OperationManager {
* @throws OperationManagementException If some unusual behaviour is observed while adding the * @throws OperationManagementException If some unusual behaviour is observed while adding the
* operation * operation
*/ */
public boolean addOperation(Operation operation, public boolean addOperation(Operation operation, List<DeviceIdentifier> devices) throws
List<DeviceIdentifier> devices) throws OperationManagementException; OperationManagementException;
/** /**
* Method to retrieve the list of all operations to a device. * Method to retrieve the list of all operations to a device.
@ -60,4 +62,15 @@ public interface OperationManager {
public void updateOperation(int operationId, Operation.Status operationStatus) throws OperationManagementException; public void updateOperation(int operationId, Operation.Status operationStatus) throws OperationManagementException;
public void deleteOperation(int operationId) throws OperationManagementException;
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException;
public List<? extends Operation> getOperationsByDeviceAndStatus(DeviceIdentifier identifier,
Operation.Status status) throws OperationManagementException, DeviceManagementException;
public Operation getOperation(int operationId) throws OperationManagementException;
public List<? extends Operation> getOperationsForStatus(Operation.Status status) throws OperationManagementException;
} }

@ -363,6 +363,34 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
@Override @Override
public void updateOperation(int operationId, Operation.Status operationStatus) public void updateOperation(int operationId, Operation.Status operationStatus)
throws OperationManagementException { throws OperationManagementException {
operationManager.updateOperation(operationId,operationStatus); operationManager.updateOperation(operationId, operationStatus);
}
@Override
public void deleteOperation(int operationId) throws OperationManagementException {
operationManager.deleteOperation(operationId);
}
@Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException {
return operationManager.getOperationByDeviceAndOperationId(deviceId, operationId);
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(DeviceIdentifier identifier,
Operation.Status status) throws OperationManagementException, DeviceManagementException {
return operationManager.getOperationsByDeviceAndStatus(identifier, status);
}
@Override
public Operation getOperation(int operationId) throws OperationManagementException {
return operationManager.getOperation(operationId);
}
@Override
public List<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
return operationManager.getOperationsForStatus(status);
} }
} }

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
@ -65,6 +66,13 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public boolean addOperation(Operation operation, public boolean addOperation(Operation operation,
List<DeviceIdentifier> devices) throws OperationManagementException { List<DeviceIdentifier> devices) throws OperationManagementException {
if (log.isDebugEnabled()){
log.debug("operation:["+operation.toString()+"]");
for(DeviceIdentifier deviceIdentifier:devices){
log.debug("device identifier id:["+deviceIdentifier.getId()+"] type:["+deviceIdentifier.getType()+"]");
}
}
try { try {
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
@ -79,6 +87,7 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
return true; return true;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
log.error("Error occurred while adding operation: ",e);
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) { } catch (OperationManagementDAOException e1) {
@ -86,6 +95,7 @@ public class OperationManagerImpl implements OperationManager {
} }
throw new OperationManagementException("Error occurred while adding operation", e); throw new OperationManagementException("Error occurred while adding operation", e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
log.error("Error occurred while adding operation device mapping: ",e);
try { try {
OperationManagementDAOFactory.rollbackTransaction(); OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) { } catch (OperationManagementDAOException e1) {
@ -123,19 +133,31 @@ public class OperationManagerImpl implements OperationManager {
} }
@Override @Override
public List<? extends Operation> getPendingOperations( public List<? extends Operation> getPendingOperations(DeviceIdentifier deviceId)
DeviceIdentifier deviceId) throws OperationManagementException { throws OperationManagementException {
if (log.isDebugEnabled()){
log.debug("device identifier id:["+deviceId.getId()+"] type:["+deviceId.getType()+"]");
}
try { try {
Device device;
device = deviceDAO.getDevice(deviceId);
List<Operation> operations = new ArrayList<Operation>(); List<Operation> operations = new ArrayList<Operation>();
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = operationDAO List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
.getOperationsForStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING); operationDAO.getOperationsByDeviceAndStatus(device.getId(), org.wso2.carbon.device.mgt.core.dto
.operation.mgt.Operation.Status.PENDING);
Operation operation; Operation operation;
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation); operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation); operations.add(operation);
} }
return operations; return operations;
} catch (DeviceManagementDAOException deviceDAOException) {
String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId();
log.error(errorMsg, deviceDAOException);
throw new OperationManagementException(errorMsg, deviceDAOException);
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " + throw new OperationManagementException("Error occurred while retrieving the list of " +
"pending operations assigned for '" + deviceId.getType() + "' device '" + "pending operations assigned for '" + deviceId.getType() + "' device '" +
@ -145,10 +167,17 @@ public class OperationManagerImpl implements OperationManager {
@Override @Override
public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException {
if (log.isDebugEnabled()){
log.debug("device identifier id:["+deviceId.getId()+"] type:["+deviceId.getType()+"]");
}
try { try {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getNextOperation(deviceId); .getNextOperation(deviceId);
Operation operation = OperationDAOUtil.convertOperation(dtoOperation); Operation operation = null;
if (dtoOperation != null) {
operation = OperationDAOUtil.convertOperation(dtoOperation);
}
return operation; return operation;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving next pending operation", e); throw new OperationManagementException("Error occurred while retrieving next pending operation", e);
@ -159,9 +188,17 @@ public class OperationManagerImpl implements OperationManager {
public void updateOperation(int operationId, Operation.Status operationStatus) public void updateOperation(int operationId, Operation.Status operationStatus)
throws OperationManagementException { throws OperationManagementException {
if (log.isDebugEnabled()){
log.debug("operation Id:"+operationId+" status:"+operationStatus);
}
try { try {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation
(operationId); (operationId);
if (dtoOperation == null){
throw new OperationManagementException("Operation not found for operation id:"+operationId);
}
dtoOperation.setStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf dtoOperation.setStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf
(operationStatus.toString())); (operationStatus.toString()));
OperationManagementDAOFactory.beginTransaction(); OperationManagementDAOFactory.beginTransaction();
@ -173,10 +210,153 @@ public class OperationManagerImpl implements OperationManager {
} catch (OperationManagementDAOException e1) { } catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the update operation transaction", e1); log.warn("Error occurred while roll-backing the update operation transaction", e1);
} }
log.error("Error occurred while updating the operation: " + operationId); log.error("Error occurred while updating the operation: " + operationId + " status:"+operationStatus,ex);
throw new OperationManagementException("Error occurred while update operation", ex); throw new OperationManagementException("Error occurred while update operation", ex);
} }
}
@Override
public void deleteOperation(int operationId) throws OperationManagementException {
try {
OperationManagementDAOFactory.beginTransaction();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operation = operationDAO.getOperation
(operationId);
if (operation == null){
throw new OperationManagementException("Operation not found for operation id:"+ operationId);
}
if (operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) {
commandOperationDAO.deleteOperation(operationId);
} else if (operation.getType().equals(
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) {
configOperationDAO.deleteOperation(operationId);
} else if (operation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type
.PROFILE)) {
profileOperationDAO.deleteOperation(operationId);
}
OperationManagementDAOFactory.commitTransaction();
} catch (OperationManagementDAOException ex) {
try {
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the delete operation transaction", e1);
}
log.error("Error occurred while deleting the operation: " + operationId, ex);
throw new OperationManagementException("Error occurred while delete operation", ex);
}
}
@Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException {
Device device;
Operation operation;
if (log.isDebugEnabled()){
log.debug("Device Type:"+deviceId.getType()+" Id:"+deviceId.getId());
}
try {
device = deviceDAO.getDevice(deviceId);
if (device == null){
throw new OperationManagementException("Device not found for given device identifier:"+deviceId.getId
()+" type:"+device.getDeviceType());
}
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO
.getOperationByDeviceAndId(device.getId(), operationId);
if (dtoOperation == null){
throw new OperationManagementException("Operation not found for operation Id:"+ operationId +" device" +
" Id:"+device.getId());
}
operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (DeviceManagementDAOException deviceDAOException) {
String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId();
log.error(errorMsg, deviceDAOException);
throw new OperationManagementException(errorMsg, deviceDAOException);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e);
}
return operation;
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(DeviceIdentifier identifier,
Operation.Status status) throws OperationManagementException, DeviceManagementException {
try {
List<Operation> operations = new ArrayList<Operation>();
Device device = deviceDAO.getDevice(identifier);
if (device == null){
throw new DeviceManagementException("Device not found for device id:"+identifier.getId()+" " +
"type:"+identifier.getType());
}
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList = operationDAO
.getOperationsByDeviceAndStatus(device.getId(),
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status
.valueOf(status.toString()));
Operation operation;
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation);
}
return operations;
} catch (DeviceManagementDAOException deviceDAOException) {
String errorMsg = "Error occurred while retrieving the device " +
"for device Identifier type -'" + identifier.getType() + "' and device Id '" + identifier.getId();
log.error(errorMsg, deviceDAOException);
throw new OperationManagementException(errorMsg, deviceDAOException);
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations assigned for '" + identifier.getType() + "' device '" +
identifier.getId() + "' and status:" + status.toString(), e);
}
}
@Override
public Operation getOperation(int operationId) throws OperationManagementException {
Operation operation;
try {
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation
(operationId);
if (dtoOperation == null){
throw new OperationManagementException("Operation not found for given Id:"+operationId);
}
operation = OperationDAOUtil.convertOperation(dtoOperation);
} catch (OperationManagementDAOException e) {
String errorMsg = "Error occurred while retrieving the operation with operation Id '" + operationId;
log.error(errorMsg, e);
throw new OperationManagementException(errorMsg, e);
}
return operation;
}
@Override
public List<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
try {
List<Operation> operations = new ArrayList<Operation>();
List<? extends org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation> dtoOperationList =
operationDAO.getOperationsForStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status
.valueOf(status.toString()));
Operation operation;
for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) {
operation = OperationDAOUtil.convertOperation(dtoOperation);
operations.add(operation);
}
return operations;
} catch (OperationManagementDAOException e) {
throw new OperationManagementException("Error occurred while retrieving the list of " +
"operations for status:'" + status.toString(), e);
}
} }
private OperationDAO lookupOperationDAO(Operation operation) { private OperationDAO lookupOperationDAO(Operation operation) {

@ -33,11 +33,10 @@ public interface OperationDAO {
Operation getOperation(int operationId) throws OperationManagementDAOException; Operation getOperation(int operationId) throws OperationManagementDAOException;
Operation getOperationByDeviceAndId(int deviceId, Operation getOperationByDeviceAndId(int deviceId, int operationId) throws OperationManagementDAOException;
int operationId) throws OperationManagementDAOException;
List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, Operation.Status status) throws List<? extends Operation> getOperationsByDeviceAndStatus(int deviceId, Operation.Status status)
OperationManagementDAOException; throws OperationManagementDAOException;
List<? extends Operation> getOperationsForDevice(int deviceId) throws OperationManagementDAOException; List<? extends Operation> getOperationsForDevice(int deviceId) throws OperationManagementDAOException;

@ -160,8 +160,8 @@ public class OperationDAOImpl implements OperationDAO {
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL Error occurred while retrieving the operation object " + String errorMsg = "SQL Error occurred while retrieving the operation object " + "available for the id '"
"available for the id '" + id; + id;
log.error(errorMsg, e); log.error(errorMsg, e);
throw new OperationManagementDAOException(errorMsg, e); throw new OperationManagementDAOException(errorMsg, e);
} finally { } finally {
@ -248,7 +248,7 @@ public class OperationDAOImpl implements OperationDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
Operation operation = null; Operation operation;
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ois; ObjectInputStream ois;
@ -393,7 +393,7 @@ public class OperationDAOImpl implements OperationDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
Operation operation = null; Operation operation;
ByteArrayInputStream bais; ByteArrayInputStream bais;
ObjectInputStream ois; ObjectInputStream ois;
@ -459,6 +459,7 @@ public class OperationDAOImpl implements OperationDAO {
@Override @Override
public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException { public Operation getNextOperation(DeviceIdentifier deviceId) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
@ -478,8 +479,8 @@ public class OperationDAOImpl implements OperationDAO {
rs = stmt.executeQuery(); rs = stmt.executeQuery();
Operation operation = null; Operation operation = null;
if (rs.next()) {
if (rs.next()) {
operation = new Operation(); operation = new Operation();
operation.setType(this.getType(rs.getString("TYPE"))); operation.setType(this.getType(rs.getString("TYPE")));
operation.setStatus(this.getStatus(rs.getString("STATUS"))); operation.setStatus(this.getStatus(rs.getString("STATUS")));

@ -23,10 +23,10 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import java.util.List; import java.util.List;
public class DeviceManagementServiceImpl implements DeviceManagementService { public class DeviceManagementServiceImpl implements DeviceManagementService{
@Override @Override
public String getProviderType() { public String getProviderType() {
@ -142,6 +142,36 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
operationStatus); operationStatus);
} }
@Override
public void deleteOperation(int operationId) throws OperationManagementException {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().deleteOperation(operationId);
}
@Override
public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId,
int operationId) throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.getOperationByDeviceAndOperationId(deviceId, operationId);
}
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(DeviceIdentifier identifier,
Operation.Status status) throws OperationManagementException, DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperationsByDeviceAndStatus
(identifier, status);
}
@Override
public Operation getOperation(int operationId) throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperation(operationId);
}
@Override
public List<? extends Operation> getOperationsForStatus(Operation.Status status)
throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperationsForStatus(status);
}
@Override @Override
public void sendEnrolmentInvitation( public void sendEnrolmentInvitation(
EmailMessageProperties emailMessageProperties) throws DeviceManagementException { EmailMessageProperties emailMessageProperties) throws DeviceManagementException {

Loading…
Cancel
Save