summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main
diff options
context:
space:
mode:
authorGary Wu <gary.i.wu@huawei.com>2017-04-13 14:47:14 -0700
committerMichael Lando <ml636r@att.com>2017-07-16 19:49:47 +0300
commitd83b84b14694faffc957540d97f423051a36e466 (patch)
tree40e6adbed5fce7b3b947c89b321e349b70fcce99 /catalog-be/src/main
parentcc31a0d5101787cf7556924b7d3e9bcd99278d96 (diff)
Simplify RollbackManager
Simplify RollbackManager using FunctionalJava and Java 8 features. Change-Id: Icfaa16926b90cb3cfd293c14026731671e007629 Signed-off-by: Gary Wu <gary.i.wu@huawei.com>
Diffstat (limited to 'catalog-be/src/main')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java60
1 files changed, 20 insertions, 40 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java b/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java
index 0641524038..99e0da0116 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/common/transaction/mngr/RollbackManager.java
@@ -20,68 +20,54 @@
package org.openecomp.sdc.common.transaction.mngr;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
import org.openecomp.sdc.common.transaction.api.RollbackHandler;
import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBActionCodeEnum;
import org.openecomp.sdc.common.transaction.api.TransactionUtils.DBTypeEnum;
import org.openecomp.sdc.common.util.MethodActivationStatusEnum;
+import fj.P;
import fj.data.Either;
+import fj.data.HashMap;
+import fj.data.List;
public class RollbackManager {
- private Map<DBTypeEnum, RollbackHandler> rollBackHandlersMap;
- private Integer transactionId;
- private String userId, actionType;
+ private final HashMap<DBTypeEnum, RollbackHandler> rollbackHandlersMap;
+ private final Integer transactionId;
+ private final String userId;
+ private final String actionType;
- RollbackManager(Integer transactionId, String userId, String actionType, List<RollbackHandler> roleBackHandlers) {
+ RollbackManager(Integer transactionId, String userId, String actionType, Iterable<RollbackHandler> rollbackHandlers) {
this.transactionId = transactionId;
this.userId = userId;
this.actionType = actionType;
- rollBackHandlersMap = new HashMap<>();
- for (RollbackHandler handler : roleBackHandlers) {
- rollBackHandlersMap.put(handler.getDBType(), handler);
- }
-
+ this.rollbackHandlersMap = HashMap.from(List.iterableList(rollbackHandlers).map(i -> P.p(i.getDBType(), i)));
}
public DBActionCodeEnum transactionRollback() {
- DBActionCodeEnum rollbackResult = DBActionCodeEnum.SUCCESS;
- Iterator<RollbackHandler> handlersItr = rollBackHandlersMap.values().iterator();
- while (handlersItr.hasNext()) {
- RollbackHandler handler = handlersItr.next();
- DBActionCodeEnum rollbackCode = handler.doRollback();
- if (rollbackCode == DBActionCodeEnum.FAIL_GENERAL) {
- rollbackResult = DBActionCodeEnum.FAIL_GENERAL;
- }
- }
-
- return rollbackResult;
+ List<DBActionCodeEnum> results = rollbackHandlersMap.values().map(RollbackHandler::doRollback);
+ boolean failure = results.exists(r -> r == DBActionCodeEnum.FAIL_GENERAL);
+ return failure ? DBActionCodeEnum.FAIL_GENERAL : DBActionCodeEnum.SUCCESS;
}
protected Either<RollbackHandler, MethodActivationStatusEnum> addRollbackHandler(RollbackHandler rollbackHandler) {
Either<RollbackHandler, MethodActivationStatusEnum> result;
- if (rollBackHandlersMap.containsKey(rollbackHandler.getDBType())) {
+ if (rollbackHandlersMap.contains(rollbackHandler.getDBType())) {
result = Either.right(MethodActivationStatusEnum.NOT_ALLOWED);
} else {
- rollBackHandlersMap.put(rollbackHandler.getDBType(), rollbackHandler);
+ rollbackHandlersMap.set(rollbackHandler.getDBType(), rollbackHandler);
result = Either.left(rollbackHandler);
}
return result;
}
- protected Either<RollbackHandler, MethodActivationStatusEnum> createRollbackHandler(DBTypeEnum dbType) {
+ protected Either<RollbackHandler, MethodActivationStatusEnum> createRollbackHandler(final DBTypeEnum dbType) {
- final DBTypeEnum dbTypeFinal = dbType;
RollbackHandler rollbackHandler = new RollbackHandler(transactionId, userId, actionType) {
@Override
public DBTypeEnum getDBType() {
- return dbTypeFinal;
+ return dbType;
}
@Override
@@ -89,19 +75,13 @@ public class RollbackManager {
return true;
}
};
- Either<RollbackHandler, MethodActivationStatusEnum> result = addRollbackHandler(rollbackHandler);
-
- return result;
+ return addRollbackHandler(rollbackHandler);
}
protected Either<RollbackHandler, MethodActivationStatusEnum> getRollbackHandler(DBTypeEnum dbType) {
- Either<RollbackHandler, MethodActivationStatusEnum> result;
- if (rollBackHandlersMap.containsKey(dbType)) {
- result = Either.left(rollBackHandlersMap.get(dbType));
- } else {
- result = Either.right(MethodActivationStatusEnum.NOT_FOUND);
- }
- return result;
+ // need to swap here because the uses of Either in SDC appears to be opposite of convention
+ // by convention left is failure; in SDC right is failure
+ return rollbackHandlersMap.get(dbType).toEither(MethodActivationStatusEnum.NOT_FOUND).swap();
}
}