org.marat.workflow.demo.app.App.java Source code

Java tutorial

Introduction

Here is the source code for org.marat.workflow.demo.app.App.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.app;

import java.math.BigDecimal;

import org.marat.workflow.demo.config.DemoConfiguration;
import org.marat.workflow.demo.model.Product;
import org.marat.workflow.demo.model.ProductBundle;
import org.marat.workflow.demo.model.ProductComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class App {

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

    @Autowired
    private WorkflowBean workflowBean;

    public void run() {
        final ProductContext context = new ProductContext(createProductHierarchy());

        workflowBean.getWorkflow().runWorkflow(context);

        logger.info("Total price: {}", context.getTotalPrice());
    }

    private Product createProductHierarchy() {
        return createProductBundle("rootProduct").addChildProduct(createProductComponent("childProductOne", 13))
                .addChildProduct(createProductComponent("childProductTwo", 9));
    }

    private ProductBundle createProductBundle(final String productId) {
        return new ProductBundle(productId);
    }

    private ProductComponent createProductComponent(final String productId, final long price) {
        final ProductComponent productComponent = new ProductComponent(productId);
        productComponent.setPrice(new BigDecimal(price));
        return productComponent;
    }

    public static void main(final String[] args) {
        try (final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
                DemoConfiguration.class)) {
            applicationContext.getBean(App.class).run();
        }
    }

}