org.marat.workflow.demo.task.CalculatePriceTask.java Source code

Java tutorial

Introduction

Here is the source code for org.marat.workflow.demo.task.CalculatePriceTask.java

Source

/*
 * Copyright 2015 Markus Ratzer
 *
 * 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.marat.workflow.demo.task;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.marat.workflow.ContextCreator;
import org.marat.workflow.Node;
import org.marat.workflow.NodeContext;
import org.marat.workflow.demo.app.ProductContext;
import org.marat.workflow.demo.model.Product;
import org.marat.workflow.demo.model.ProductBundle;
import org.marat.workflow.demo.model.ProductComponent;
import org.marat.workflow.demo.task.CalculatePriceTask.TaskContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class CalculatePriceTask implements Node<TaskContext>, ContextCreator<TaskContext> {

    private static final Logger logger = LoggerFactory.getLogger(CalculatePriceTask.class);

    @Override
    public TaskContext createContext() {
        logger.debug("Create task context");
        return new TaskContext();
    }

    @Override
    public void runNode(final NodeContext<TaskContext> nodeContext) {
        logger.debug("Run node");
        final ProductContext productContext = nodeContext.getParentContext();
        final TaskContext taskContext = nodeContext.getContext();

        collectPrices(taskContext, productContext.getRootProduct());

        productContext.setTotalPrice(calculateTotalPrice(taskContext));
    }

    private void collectPrices(final TaskContext taskContext, final Product product) {
        if (product instanceof ProductBundle) {
            for (final Product childProduct : ((ProductBundle) product).getChildProducts()) {
                collectPrices(taskContext, childProduct);
            }
        } else if (product instanceof ProductComponent) {
            taskContext.addPrice(((ProductComponent) product).getPrice());
        } else {
            throw new IllegalArgumentException();
        }
    }

    private BigDecimal calculateTotalPrice(final TaskContext taskContext) {
        BigDecimal totalPrice = BigDecimal.ZERO;

        for (final BigDecimal price : taskContext.getPrices()) {
            totalPrice = totalPrice.add(price);
        }

        return totalPrice;
    }

    static class TaskContext {

        private final List<BigDecimal> prices = new ArrayList<>();

        void addPrice(final BigDecimal price) {
            prices.add(price);
        }

        List<BigDecimal> getPrices() {
            return Collections.unmodifiableList(prices);
        }

    }

}