com.redhat.rhtracking.core.services.DeliveryMatrixServiceHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.redhat.rhtracking.core.services.DeliveryMatrixServiceHandler.java

Source

/*
 * Copyright (C) 2015 Nuuptech
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.redhat.rhtracking.core.services;

import com.redhat.rhtracking.events.TreeNode;
import com.redhat.rhtracking.core.domain.DeliveryProbability;
import com.redhat.rhtracking.core.domain.DeliveryStatus;
import com.redhat.rhtracking.core.domain.DeliverySubStatus;
import com.redhat.rhtracking.core.domain.HourType;
import com.redhat.rhtracking.core.domain.OpportunityType;
import com.redhat.rhtracking.events.EventStatus;
import com.redhat.rhtracking.persistance.services.DeliveryMatrixPersistanceService;
import com.redhat.rhtracking.persistance.services.OpportunityPersistanceService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author marco-g8
 */
public class DeliveryMatrixServiceHandler implements DeliveryMatrixService {

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

    @Autowired
    private DeliveryMatrixPersistanceService deliveryMatrixPersistanceService;
    @Autowired
    private OpportunityPersistanceService opportunityPersistanceService;

    @Override
    public Map<String, Object> getExecutiveSummary(Map<String, Object> event) {
        Map<String, Object> r1 = opportunityPersistanceService.listAllOpportunities(new HashMap<String, Object>());
        List<Map<String, Object>> opportunities = new ArrayList<>();
        for (Map<String, Object> o : (List<Map<String, Object>>) r1.get("list")) {
            if (!o.containsKey("deliveryMatrix")) {
                continue;
            }
            opportunities.add(o);
        }

        Map<String, Object> response0 = new HashMap<>();
        Map<String, Object> resume = new HashMap<>();

        for (Map<String, Object> o : opportunities) {
            Map<String, Object> dm = (Map<String, Object>) o.get("deliveryMatrix");

            DeliveryStatus status = DeliveryStatus.valueOf((String) dm.get("deliveryStatus"));
            int totalHours = (int) o.get("totalHours");
            BigDecimal amount = (BigDecimal) o.get("billingAmount");

            Map<String, Object> row = getExecutiveSumaryMap(resume, status.toString());
            Map<String, Object> total = getExecutiveSumaryMap(resume, "TOTAL");

            updateMap(row, totalHours, amount);
            updateMap(total, totalHours, amount);
        }
        response0.put("executiveSummary", resume);
        return response0;
    }

    private Map<String, Object> getExecutiveSumaryMap(Map<String, Object> map, String key) {
        if (map.containsKey(key)) {
            return (Map<String, Object>) map.get(key);
        } else {
            Map<String, Object> newmap = initMap(key);
            map.put(key, newmap);
            return newmap;
        }
    }

    private Map<String, Object> initMap(String status) {
        Map<String, Object> row = new HashMap<>();
        row.put("status", status);
        row.put("opportunities", 0);
        row.put("totalHours", 0);
        row.put("amount", BigDecimal.ZERO);
        return row;
    }

    private void updateMap(Map<String, Object> data, int hours, BigDecimal amount) {
        data.put("opportunities", (int) data.get("opportunities") + 1);
        data.put("totalHours", (int) data.get("totalHours") + hours);
        data.put("amount", ((BigDecimal) data.get("amount")).add(amount));
    }

    @Override
    public Map<String, Object> getDeliveryIndicators(Map<String, Object> event) {
        Map<String, Object> r1 = opportunityPersistanceService.listAllOpportunities(new HashMap<String, Object>());
        List<Map<String, Object>> opportunities = new ArrayList<>();
        for (Map<String, Object> o : (List<Map<String, Object>>) r1.get("list")) {
            if (!o.containsKey("deliveryMatrix")) {
                continue;
            }
            opportunities.add(o);
        }

        // ~~~ indicadores delivery ~~~ 
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> rootmap = new HashMap<>();
        TreeNode<Map<String, Object>> root = new TreeNode<>(rootmap);
        response.put("deliveryIndicators", root);

        // level 0 root
        // level 1 delivery status
        // level 2 delivery sub status
        // level 3 probabilidad  de entrega
        // level 4 tipo de oportunidad
        for (Map<String, Object> o : opportunities) {
            Map<String, Object> dm = (Map<String, Object>) o.get("deliveryMatrix");

            DeliveryStatus ds = DeliveryStatus.valueOf((String) dm.get("deliveryStatus"));
            DeliverySubStatus dss = DeliverySubStatus.valueOf((String) dm.get("deliverySubStatus"));
            DeliveryProbability dp = DeliveryProbability.valueOf((String) dm.get("deliveryProbability"));
            OpportunityType ot = OpportunityType.valueOf((String) o.get("type"));

            int totalHours = (int) o.get("totalHours");
            BigDecimal billingAmount = (BigDecimal) o.get("billingAmount");

            // level 1
            TreeNode<Map<String, Object>> leaf = getDeliveryIndicatorsLeaf(root, ds.toString());
            updateMap(leaf.getData(), totalHours, billingAmount);

            // level 2
            leaf = getDeliveryIndicatorsLeaf(leaf, dss.toString());
            updateMap(leaf.getData(), totalHours, billingAmount);

            // level 3
            leaf = getDeliveryIndicatorsLeaf(leaf, dp.toString());
            updateMap(leaf.getData(), totalHours, billingAmount);

            // level 4
            leaf = getDeliveryIndicatorsLeaf(leaf, ot.toString());
            updateMap(leaf.getData(), totalHours, billingAmount);
        }
        return response;
    }

    private TreeNode<Map<String, Object>> getDeliveryIndicatorsLeaf(TreeNode<Map<String, Object>> node,
            String key) {
        if (node.containsKey(key)) {
            return node.getChild(key);
        } else {
            Map<String, Object> data = initMap(key);
            return node.addChild(key, data);
        }
    }

    @Override
    public Map<String, Object> findDeliveryMatrixBy(Map<String, Object> event) {
        return deliveryMatrixPersistanceService.findBy(event);
    }

    @Override
    public Map<String, Object> findOpportunityBy(Map<String, Object> event) {
        return opportunityPersistanceService.findBy(event);
    }

    @Override
    public Map<String, Object> findDeliveryMatrixOfOpportunity(Map<String, Object> event) {
        logger.debug("findDeliveryMatrixOfOpportunity " + event);
        Map<String, Object> request = new HashMap<>();
        request.put("opportunityId", event.get("id"));
        return deliveryMatrixPersistanceService.findBy(request);
    }

    @Override
    public Map<String, Object> findDeliveryMatrixById(Map<String, Object> event) {
        logger.debug("findDeliveryMatrixById " + event);
        return deliveryMatrixPersistanceService.findDeliveryMatrixById((long) event.get("id"));
    }

    @Override
    public Map<String, Object> listAllDeliveryMatrixPaged(Map<String, Object> event) {
        logger.debug("listAllDeliveryMatrixPaged " + event);
        return deliveryMatrixPersistanceService.listAllDeliveryMatrixPaged(event);
    }

    @Override
    public Map<String, Object> saveOpportunity(Map<String, Object> event) {
        Map<String, Object> opportunity = new HashMap<>();
        opportunity.put("identifier", (String) event.get("identifier"));
        opportunity.put("project", (String) event.get("project"));
        opportunity.put("opportunityType", (String) event.get("opportunityType"));
        opportunity.put("customerId", (long) event.get("customer"));
        opportunity.put("customerBillingId", (long) event.get("customerBilling"));
        opportunity.put("currency", (String) event.get("currency"));
        opportunity.put("exchangeRate", event.get("exchangeRate"));

        int totalHours = (int) event.get("platformHours") + (int) event.get("middlewareHours")
                + (int) event.get("workshopHours");
        opportunity.put("totalHours", totalHours);
        opportunity.put("hoursToBill", 0);

        BigDecimal billingAmount = ((BigDecimal) event.get("platformPrice"))
                .multiply(new BigDecimal((int) event.get("platformHours")));
        billingAmount = billingAmount.add(((BigDecimal) event.get("middlewarePrice"))
                .multiply(new BigDecimal((int) event.get("middlewareHours"))));
        billingAmount = billingAmount.add(((BigDecimal) event.get("workshopPrice"))
                .multiply(new BigDecimal((int) event.get("workshopHours"))));
        opportunity.put("billingAmount", billingAmount);
        opportunity.put("costAmount", BigDecimal.ZERO);

        Map<String, Object> addOpportunityResponse = opportunityPersistanceService.saveOpportunity(opportunity);
        if (addOpportunityResponse.get("status") != EventStatus.SUCCESS) {
            return addOpportunityResponse;
        }

        for (HourType type : HourType.values()) {
            if ((int) event.get(type.toString().toLowerCase() + "Hours") < 1) {
                continue;
            }
            Map<String, Object> newHours = new HashMap<>();
            newHours.put("opportunityId", (long) addOpportunityResponse.get("id"));
            newHours.put("type", type.toString());
            newHours.put("quantity", (int) event.get(type.toString().toLowerCase() + "Hours"));
            newHours.put("hoursToBill", 0);
            newHours.put("unitPrice", event.get(type.toString().toLowerCase() + "Price"));

            Map<String, Object> hourResponse = opportunityPersistanceService.saveOpportunityHours(newHours);
            if (hourResponse.get("status") != com.redhat.rhtracking.events.EventStatus.SUCCESS) {
                throw new UnsupportedOperationException("Exception when creating the opportunity");
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("status", com.redhat.rhtracking.events.EventStatus.SUCCESS);
        result.put("id", addOpportunityResponse.get("id"));
        return result;
    }

    @Override
    public Map<String, Object> saveDeliveryMatrix(Map<String, Object> event) {
        return deliveryMatrixPersistanceService.save(event);
    }

    @Override
    public Map<String, Object> updateOpportunity(Map<String, Object> event) {
        return opportunityPersistanceService.updateOpportunity(event);
    }

    @Override
    public Map<String, Object> updateDeliveryMatrix(Map<String, Object> event) {
        return deliveryMatrixPersistanceService.update(event);
    }

    @Override
    public Map<String, Object> deleteOpportunity(Map<String, Object> event) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Map<String, Object> listAllOpportunitiesPaged(Map<String, Object> event) {
        return opportunityPersistanceService.listAllOpportunitiesPaged(event);
    }

    @Override
    public Map<String, Object> listAllOpportunities(Map<String, Object> event) {
        return opportunityPersistanceService.listAllOpportunities(event);
    }

    @Override
    public Map<String, Object> findOpportunityByIdentifier(Map<String, Object> event) {
        return opportunityPersistanceService.findByIdentifier((String) event.get("identifier"));
    }

    @Override
    public Map<String, Object> findOpportunityHours(Map<String, Object> event) {
        return opportunityPersistanceService.listOpportunityHours(event);
    }

}